diff options
author | Silvan Mosberger <silvan.mosberger@tweag.io> | 2023-11-08 05:16:01 +0100 |
---|---|---|
committer | Silvan Mosberger <silvan.mosberger@tweag.io> | 2023-11-08 05:18:10 +0100 |
commit | f82a1686e6840c396a141ff122e2f39d64fb6d4b (patch) | |
tree | 98b7c667637d160db172932e6ab22ef280c74a01 /lib | |
parent | 72e453f6ce9dc95f089c47a40034b647c5097553 (diff) | |
download | nixpkgs-f82a1686e6840c396a141ff122e2f39d64fb6d4b.tar nixpkgs-f82a1686e6840c396a141ff122e2f39d64fb6d4b.tar.gz nixpkgs-f82a1686e6840c396a141ff122e2f39d64fb6d4b.tar.bz2 nixpkgs-f82a1686e6840c396a141ff122e2f39d64fb6d4b.tar.lz nixpkgs-f82a1686e6840c396a141ff122e2f39d64fb6d4b.tar.xz nixpkgs-f82a1686e6840c396a141ff122e2f39d64fb6d4b.tar.zst nixpkgs-f82a1686e6840c396a141ff122e2f39d64fb6d4b.zip |
lib.fileset: Split out internal test helper
Diffstat (limited to 'lib')
-rwxr-xr-x | lib/fileset/tests.sh | 68 |
1 files changed, 42 insertions, 26 deletions
diff --git a/lib/fileset/tests.sh b/lib/fileset/tests.sh index 529f23ae887..ead6d4c4956 100755 --- a/lib/fileset/tests.sh +++ b/lib/fileset/tests.sh @@ -224,6 +224,43 @@ withFileMonitor() { fi } + +# Create the tree structure declared in the tree variable, usage: +# +# tree=( +# [a/b] = # Declare that file a/b should exist +# [c/a] = # Declare that file c/a should exist +# [c/d/]= # Declare that directory c/d/ should exist +# ) +# createTree +declare -A tree +createTree() { + # Track which paths need to be created + local -a dirsToCreate=() + local -a filesToCreate=() + for p in "${!tree[@]}"; do + # If keys end with a `/` we treat them as directories, otherwise files + if [[ "$p" =~ /$ ]]; then + dirsToCreate+=("$p") + else + filesToCreate+=("$p") + fi + done + + # Create all the necessary paths. + # This is done with only a fixed number of processes, + # in order to not be too slow + # Though this does mean we're a bit limited with how many files can be created + if (( ${#dirsToCreate[@]} != 0 )); then + mkdir -p "${dirsToCreate[@]}" + fi + if (( ${#filesToCreate[@]} != 0 )); then + readarray -d '' -t parentsToCreate < <(dirname -z "${filesToCreate[@]}") + mkdir -p "${parentsToCreate[@]}" + touch "${filesToCreate[@]}" + fi +} + # Check whether a file set includes/excludes declared paths as expected, usage: # # tree=( @@ -232,34 +269,26 @@ withFileMonitor() { # [c/d/]= # Declare that directory c/d/ should exist and expect it to be excluded in the store path # ) # checkFileset './a' # Pass the fileset as the argument -declare -A tree checkFileset() { # New subshell so that we can have a separate trap handler, see `trap` below local fileset=$1 + # Create the tree + createTree + # Process the tree into separate arrays for included paths, excluded paths and excluded files. local -a included=() local -a excluded=() local -a excludedFiles=() - # Track which paths need to be created - local -a dirsToCreate=() - local -a filesToCreate=() for p in "${!tree[@]}"; do - # If keys end with a `/` we treat them as directories, otherwise files - if [[ "$p" =~ /$ ]]; then - dirsToCreate+=("$p") - isFile= - else - filesToCreate+=("$p") - isFile=1 - fi case "${tree[$p]}" in 1) included+=("$p") ;; 0) excluded+=("$p") - if [[ -n "$isFile" ]]; then + # If keys end with a `/` we treat them as directories, otherwise files + if [[ ! "$p" =~ /$ ]]; then excludedFiles+=("$p") fi ;; @@ -268,19 +297,6 @@ checkFileset() { esac done - # Create all the necessary paths. - # This is done with only a fixed number of processes, - # in order to not be too slow - # Though this does mean we're a bit limited with how many files can be created - if (( ${#dirsToCreate[@]} != 0 )); then - mkdir -p "${dirsToCreate[@]}" - fi - if (( ${#filesToCreate[@]} != 0 )); then - readarray -d '' -t parentsToCreate < <(dirname -z "${filesToCreate[@]}") - mkdir -p "${parentsToCreate[@]}" - touch "${filesToCreate[@]}" - fi - expression="toSource { root = ./.; fileset = $fileset; }" # We don't have lambda's in bash unfortunately, |