From f82a1686e6840c396a141ff122e2f39d64fb6d4b Mon Sep 17 00:00:00 2001 From: Silvan Mosberger Date: Wed, 8 Nov 2023 05:16:01 +0100 Subject: lib.fileset: Split out internal test helper --- lib/fileset/tests.sh | 68 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 42 insertions(+), 26 deletions(-) (limited to 'lib/fileset') 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, -- cgit 1.4.1