mirror of
https://github.com/numtide/nix-filter.git
synced 2024-09-11 14:16:38 +03:00
parent
08602032e4
commit
46af6efca5
@ -1,66 +1,64 @@
|
||||
let
|
||||
nix-filter = import ../.;
|
||||
testCases = import ./tests.nix;
|
||||
|
||||
# Convert a path to a string with the root stripped
|
||||
toRelativeString = root: path:
|
||||
let
|
||||
r = toString root;
|
||||
p = toString path;
|
||||
in
|
||||
builtins.substring
|
||||
(builtins.stringLength r + 1)
|
||||
(builtins.stringLength p)
|
||||
p;
|
||||
|
||||
# Traverse a directory, returning all children in a list
|
||||
listDir =
|
||||
root:
|
||||
let
|
||||
files = builtins.readDir root;
|
||||
filesAsList = map (fileName:
|
||||
{
|
||||
path = root + ("/" + fileName);
|
||||
type = builtins.getAttr fileName files;
|
||||
}
|
||||
) (builtins.attrNames files);
|
||||
pathsInDir = map (file: file.path) filesAsList;
|
||||
nestedPaths = builtins.concatMap (file: listDir file.path)
|
||||
(builtins.filter (file: file.type == "directory") filesAsList);
|
||||
in
|
||||
pathsInDir ++ nestedPaths;
|
||||
|
||||
# Run a test, returning a list of failures
|
||||
runTest = testDef:
|
||||
let
|
||||
missing = builtins.filter (file:
|
||||
! builtins.pathExists (testDef.actual + ("/" + file))
|
||||
) testDef.expected;
|
||||
included = map (toRelativeString testDef.actual) (listDir testDef.actual);
|
||||
extra = builtins.filter (path:
|
||||
! builtins.elem path testDef.expected
|
||||
) included;
|
||||
in
|
||||
(map (x: { path = x; status = "missing"; }) missing)
|
||||
++
|
||||
(map (x: { path = x; status = "extra"; }) extra);
|
||||
|
||||
# Take a set of test results and filter out every key that
|
||||
# is not failing.
|
||||
onlyFailures = results:
|
||||
let
|
||||
names = builtins.attrNames results;
|
||||
in
|
||||
builtins.foldl' (finalFailures: testName:
|
||||
let
|
||||
failures = builtins.getAttr testName results;
|
||||
in
|
||||
if builtins.length failures == 0
|
||||
then finalFailures
|
||||
else finalFailures // { ${testName} = failures; }
|
||||
) {} names;
|
||||
|
||||
testResults = builtins.mapAttrs (_: testDef: runTest testDef) testCases;
|
||||
in
|
||||
{
|
||||
all = nix-filter {
|
||||
root = ./fixture1;
|
||||
};
|
||||
|
||||
without-readme = nix-filter {
|
||||
root = ./fixture1;
|
||||
exclude = [
|
||||
"README.md"
|
||||
];
|
||||
};
|
||||
|
||||
with-matchExt = nix-filter {
|
||||
root = ./fixture1;
|
||||
include = [
|
||||
"package.json"
|
||||
"src"
|
||||
(nix-filter.matchExt "js")
|
||||
];
|
||||
};
|
||||
|
||||
with-matchExt2 = nix-filter {
|
||||
root = ./fixture1;
|
||||
include = [
|
||||
"package.json"
|
||||
"src/innerdir"
|
||||
(nix-filter.matchExt "js")
|
||||
];
|
||||
};
|
||||
|
||||
with-inDirectory = nix-filter {
|
||||
root = ./fixture1;
|
||||
include = [
|
||||
(nix-filter.inDirectory "src") # should match everything under ./fixture1/src/
|
||||
(nix-filter.inDirectory "READ") # should not match README.md
|
||||
];
|
||||
};
|
||||
|
||||
# should match everything under ./fixture1/src/ but not in ./fixture1/src/innerdir/
|
||||
with-inDirectory2 = nix-filter {
|
||||
root = ./fixture1;
|
||||
include = [
|
||||
(nix-filter.inDirectory "src")
|
||||
];
|
||||
exclude = [
|
||||
(nix-filter.inDirectory ./fixture1/src/innerdir)
|
||||
];
|
||||
};
|
||||
|
||||
combiners = nix-filter {
|
||||
root = ./fixture1;
|
||||
include = with nix-filter; [
|
||||
(and isDirectory (inDirectory "src"))
|
||||
];
|
||||
};
|
||||
|
||||
trace = nix-filter {
|
||||
root = ./fixture1;
|
||||
include = [
|
||||
nix-filter.traceUnmatched
|
||||
];
|
||||
};
|
||||
}
|
||||
testResults // { "@onlyFailures" = onlyFailures testResults; }
|
||||
|
15
tests/run-tests.sh
Executable file
15
tests/run-tests.sh
Executable file
@ -0,0 +1,15 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -e
|
||||
|
||||
extra_flags=""
|
||||
|
||||
if [[ "$#" -eq 1 ]]; then
|
||||
extra_flags="-A $1"
|
||||
fi
|
||||
|
||||
|
||||
# Need to build first or the store paths don't exist
|
||||
# for default.nix to traverse
|
||||
nix-build &>/dev/null
|
||||
nix-instantiate --eval --strict --json $extra_flags
|
141
tests/tests.nix
Normal file
141
tests/tests.nix
Normal file
@ -0,0 +1,141 @@
|
||||
let
|
||||
nix-filter = import ../.;
|
||||
in
|
||||
{
|
||||
all = rec {
|
||||
root = ./fixture1;
|
||||
actual = nix-filter {
|
||||
inherit root;
|
||||
};
|
||||
expected = [
|
||||
"README.md"
|
||||
"package.json"
|
||||
"src"
|
||||
"src/main.js"
|
||||
"src/components"
|
||||
"src/components/widget.jsx"
|
||||
"src/innerdir"
|
||||
"src/innerdir/inner.js"
|
||||
];
|
||||
};
|
||||
|
||||
without-readme = rec {
|
||||
root = ./fixture1;
|
||||
actual = nix-filter {
|
||||
inherit root;
|
||||
exclude = [
|
||||
"README.md"
|
||||
];
|
||||
};
|
||||
expected = [
|
||||
"package.json"
|
||||
"src"
|
||||
"src/main.js"
|
||||
"src/components"
|
||||
"src/components/widget.jsx"
|
||||
"src/innerdir"
|
||||
"src/innerdir/inner.js"
|
||||
];
|
||||
};
|
||||
|
||||
with-matchExt = rec {
|
||||
root = ./fixture1;
|
||||
actual = nix-filter {
|
||||
inherit root;
|
||||
include = [
|
||||
"package.json"
|
||||
"src"
|
||||
(nix-filter.matchExt "js")
|
||||
];
|
||||
};
|
||||
expected = [
|
||||
"package.json"
|
||||
"src"
|
||||
"src/main.js"
|
||||
];
|
||||
};
|
||||
|
||||
with-matchExt2 = rec {
|
||||
root = ./fixture1;
|
||||
actual = nix-filter {
|
||||
inherit root;
|
||||
include = [
|
||||
"package.json"
|
||||
"src/innerdir"
|
||||
(nix-filter.matchExt "js")
|
||||
];
|
||||
};
|
||||
expected = [
|
||||
"package.json"
|
||||
"src"
|
||||
"src/main.js"
|
||||
"src/innerdir"
|
||||
"src/innerdir/inner.js"
|
||||
];
|
||||
};
|
||||
|
||||
with-inDirectory = rec {
|
||||
root = ./fixture1;
|
||||
actual = nix-filter {
|
||||
inherit root;
|
||||
include = [
|
||||
(nix-filter.inDirectory "src")
|
||||
(nix-filter.inDirectory "READ")
|
||||
];
|
||||
};
|
||||
expected = [
|
||||
"src"
|
||||
"src/main.js"
|
||||
"src/components"
|
||||
"src/components/widget.jsx"
|
||||
"src/innerdir"
|
||||
"src/innerdir/inner.js"
|
||||
];
|
||||
};
|
||||
|
||||
with-inDirectory2 = rec {
|
||||
root = ./fixture1;
|
||||
actual = nix-filter {
|
||||
inherit root;
|
||||
include = [
|
||||
(nix-filter.inDirectory "src")
|
||||
(nix-filter.inDirectory "READ")
|
||||
];
|
||||
exclude = [
|
||||
(nix-filter.inDirectory ./fixture1/src/innerdir)
|
||||
];
|
||||
};
|
||||
expected = [
|
||||
"src"
|
||||
"src/main.js"
|
||||
"src/components"
|
||||
"src/components/widget.jsx"
|
||||
];
|
||||
};
|
||||
|
||||
combiners = rec {
|
||||
root = ./fixture1;
|
||||
actual = nix-filter {
|
||||
inherit root;
|
||||
include = with nix-filter; [
|
||||
(and isDirectory (inDirectory "src"))
|
||||
];
|
||||
};
|
||||
expected = [
|
||||
"src"
|
||||
"src/components"
|
||||
"src/innerdir"
|
||||
];
|
||||
};
|
||||
|
||||
trace = rec {
|
||||
root = ./fixture1;
|
||||
actual = nix-filter {
|
||||
inherit root;
|
||||
include = [
|
||||
nix-filter.traceUnmatched
|
||||
];
|
||||
};
|
||||
expected = [];
|
||||
};
|
||||
}
|
Loading…
Reference in New Issue
Block a user