summaryrefslogtreecommitdiff
path: root/lib/fileset/tests.sh
diff options
context:
space:
mode:
authorSilvan Mosberger <silvan.mosberger@tweag.io>2023-10-18 00:16:50 +0200
committerSilvan Mosberger <silvan.mosberger@tweag.io>2023-11-08 21:21:30 +0100
commit73493584a74cddb22d785942a2215c3dab12c29e (patch)
tree4ca00ef53322cd9033c7eb6a76f54bcb5578625c /lib/fileset/tests.sh
parentlib.fileset: Split out internal test helper (diff)
downloadnixpkgs-73493584a74cddb22d785942a2215c3dab12c29e.tar.gz
lib.fileset.fromSource: init
Diffstat (limited to 'lib/fileset/tests.sh')
-rwxr-xr-xlib/fileset/tests.sh213
1 files changed, 213 insertions, 0 deletions
diff --git a/lib/fileset/tests.sh b/lib/fileset/tests.sh
index ead6d4c49569..13f02bb656b9 100755
--- a/lib/fileset/tests.sh
+++ b/lib/fileset/tests.sh
@@ -1,5 +1,7 @@
#!/usr/bin/env bash
# shellcheck disable=SC2016
+# shellcheck disable=SC2317
+# shellcheck disable=SC2192
# Tests lib.fileset
# Run:
@@ -839,6 +841,217 @@ touch 0 "${filesToCreate[@]}"
expectTrace 'unions (mapAttrsToList (n: _: ./. + "/${n}") (removeAttrs (builtins.readDir ./.) [ "0" ]))' "$expectedTrace"
rm -rf -- *
+## lib.fileset.fromSource
+
+# Check error messages
+expectFailure 'fromSource null' 'lib.fileset.fromSource: The source origin of the argument is of type null, but it should be a path instead.'
+
+expectFailure 'fromSource (lib.cleanSource "")' 'lib.fileset.fromSource: The source origin of the argument is a string-like value \(""\), but it should be a path instead.
+\s*Sources created from paths in strings cannot be turned into file sets, use `lib.sources` or derivations instead.'
+
+expectFailure 'fromSource (lib.cleanSource null)' 'lib.fileset.fromSource: The source origin of the argument is of type null, but it should be a path instead.'
+
+# fromSource on a path works and is the same as coercing that path
+mkdir a
+touch a/b c
+expectEqual 'trace (fromSource ./.) null' 'trace ./. null'
+rm -rf -- *
+
+# Check that converting to a file set doesn't read the included files
+mkdir a
+touch a/b
+run() {
+ expectEqual "trace (fromSource (lib.cleanSourceWith { src = ./a; })) null" "builtins.trace \"$work/a (all files in directory)\" null"
+ rm a/b
+}
+withFileMonitor run a/b
+rm -rf -- *
+
+# Check that converting to a file set doesn't read entries for directories that are filtered out
+mkdir -p a/b
+touch a/b/c
+run() {
+ expectEqual "trace (fromSource (lib.cleanSourceWith {
+ src = ./a;
+ filter = pathString: type: false;
+ })) null" "builtins.trace \"(empty)\" null"
+ rm a/b/c
+ rmdir a/b
+}
+withFileMonitor run a/b
+rm -rf -- *
+
+# The filter is not needed on empty directories
+expectEqual 'trace (fromSource (lib.cleanSourceWith {
+ src = ./.;
+ filter = abort "filter should not be needed";
+})) null' 'trace _emptyWithoutBase null'
+
+# Single files also work
+touch a b
+expectEqual 'trace (fromSource (cleanSourceWith { src = ./a; })) null' 'trace ./a null'
+rm -rf -- *
+
+# For a tree assigning each subpath true/false,
+# check whether a source filter with those results includes the same files
+# as a file set created using fromSource. Usage:
+#
+# tree=(
+# [a]=1 # ./a is a file and the filter should return true for it
+# [b/]=0 # ./b is a directory and the filter should return false for it
+# )
+# checkSource
+checkSource() {
+ createTree
+
+ # Serialise the tree as JSON (there's only minimal savings with jq,
+ # and we don't need to handle escapes)
+ {
+ echo "{"
+ first=1
+ for p in "${!tree[@]}"; do
+ if [[ -z "$first" ]]; then
+ echo ","
+ else
+ first=
+ fi
+ echo "\"$p\":"
+ case "${tree[$p]}" in
+ 1)
+ echo "true"
+ ;;
+ 0)
+ echo "false"
+ ;;
+ *)
+ die "Unsupported tree value: ${tree[$p]}"
+ esac
+ done
+ echo "}"
+ } > "$tmp/tree.json"
+
+ # An expression to create a source value with a filter matching the tree
+ sourceExpr='
+ let
+ tree = importJSON '"$tmp"'/tree.json;
+ in
+ cleanSourceWith {
+ src = ./.;
+ filter =
+ pathString: type:
+ let
+ stripped = removePrefix (toString ./. + "/") pathString;
+ key = stripped + optionalString (type == "directory") "/";
+ in
+ tree.${key} or
+ (throw "tree key ${key} missing");
+ }
+ '
+
+ filesetExpr='
+ toSource {
+ root = ./.;
+ fileset = fromSource ('"$sourceExpr"');
+ }
+ '
+
+ # Turn both into store paths
+ sourceStorePath=$(expectStorePath "$sourceExpr")
+ filesetStorePath=$(expectStorePath "$filesetExpr")
+
+ # Loop through each path in the tree
+ while IFS= read -r -d $'\0' subpath; do
+ if [[ ! -e "$sourceStorePath"/"$subpath" ]]; then
+ # If it's not in the source store path, it's also not in the file set store path
+ if [[ -e "$filesetStorePath"/"$subpath" ]]; then
+ die "The store path $sourceStorePath created by $expr doesn't contain $subpath, but the corresponding store path $filesetStorePath created via fromSource does contain $subpath"
+ fi
+ elif [[ -z "$(find "$sourceStorePath"/"$subpath" -type f)" ]]; then
+ # If it's an empty directory in the source store path, it shouldn't be in the file set store path
+ if [[ -e "$filesetStorePath"/"$subpath" ]]; then
+ die "The store path $sourceStorePath created by $expr contains the path $subpath without any files, but the corresponding store path $filesetStorePath created via fromSource didn't omit it"
+ fi
+ else
+ # If it's non-empty directory or a file, it should be in the file set store path
+ if [[ ! -e "$filesetStorePath"/"$subpath" ]]; then
+ die "The store path $sourceStorePath created by $expr contains the non-empty path $subpath, but the corresponding store path $filesetStorePath created via fromSource doesn't include it"
+ fi
+ fi
+ done < <(find . -mindepth 1 -print0)
+
+ rm -rf -- *
+}
+
+# Check whether the filter is evaluated correctly
+tree=(
+ [a]=
+ [b/]=
+ [b/c]=
+ [b/d]=
+ [e/]=
+ [e/e/]=
+)
+# We fill out the above tree values with all possible combinations of 0 and 1
+# Then check whether a filter based on those return values gets turned into the corresponding file set
+for i in $(seq 0 $((2 ** ${#tree[@]} - 1 ))); do
+ for p in "${!tree[@]}"; do
+ tree[$p]=$(( i % 2 ))
+ (( i /= 2 )) || true
+ done
+ checkSource
+done
+
+# The filter is called with the same arguments in the same order
+mkdir a e
+touch a/b a/c d e
+expectEqual '
+ trace (fromSource (cleanSourceWith {
+ src = ./.;
+ filter = pathString: type: builtins.trace "${pathString} ${toString type}" true;
+ })) null
+' '
+ builtins.seq (cleanSourceWith {
+ src = ./.;
+ filter = pathString: type: builtins.trace "${pathString} ${toString type}" true;
+ }).outPath
+ builtins.trace "'"$work"' (all files in directory)"
+ null
+'
+rm -rf -- *
+
+# Test that if a directory is not included, the filter isn't called on its contents
+mkdir a b
+touch a/c b/d
+expectEqual 'trace (fromSource (cleanSourceWith {
+ src = ./.;
+ filter = pathString: type:
+ if pathString == toString ./a then
+ false
+ else if pathString == toString ./b then
+ true
+ else if pathString == toString ./b/d then
+ true
+ else
+ abort "This filter should not be called with path ${pathString}";
+})) null' 'trace (_create ./. { b = "directory"; }) null'
+rm -rf -- *
+
+# The filter is called lazily:
+# If a later say intersection removes a part of the tree, the filter won't run on it
+mkdir a d
+touch a/{b,c} d/e
+expectEqual 'trace (intersection ./a (fromSource (lib.cleanSourceWith {
+ src = ./.;
+ filter = pathString: type:
+ if pathString == toString ./a || pathString == toString ./a/b then
+ true
+ else if pathString == toString ./a/c then
+ false
+ else
+ abort "filter should not be called on ${pathString}";
+}))) null' 'trace ./a/b null'
+rm -rf -- *
+
# TODO: Once we have combinators and a property testing library, derive property tests from https://en.wikipedia.org/wiki/Algebra_of_sets
echo >&2 tests ok