From 56b09628e33333c9a9a9463f004fe24ce01e5f98 Mon Sep 17 00:00:00 2001 From: Wolfgang Walther Date: Sat, 30 Nov 2024 16:38:02 +0100 Subject: [PATCH] stdenv: support multi-char separators in concatStringsSep One prominent use-case for this is pytestCheckHook. This will help making it work with structuredAttrs in the future. --- .../python/hooks/pytest-check-hook.sh | 25 +------------------ pkgs/stdenv/generic/setup.sh | 15 ++++++++--- pkgs/test/stdenv/default.nix | 8 ++++++ 3 files changed, 21 insertions(+), 27 deletions(-) diff --git a/pkgs/development/interpreters/python/hooks/pytest-check-hook.sh b/pkgs/development/interpreters/python/hooks/pytest-check-hook.sh index fd61fd14043d..b542398a92b0 100644 --- a/pkgs/development/interpreters/python/hooks/pytest-check-hook.sh +++ b/pkgs/development/interpreters/python/hooks/pytest-check-hook.sh @@ -4,29 +4,6 @@ echo "Sourcing pytest-check-hook" declare -ar disabledTests declare -a disabledTestPaths -function _concatSep { - local result - local sep="$1" - local -n arr=$2 - for index in ${!arr[*]}; do - if [ $index -eq 0 ]; then - result="${arr[index]}" - else - result+=" $sep ${arr[index]}" - fi - done - echo "$result" -} - -function _pytestComputeDisabledTestsString() { - declare -a tests - local tests=($1) - local prefix="not " - prefixed=("${tests[@]/#/$prefix}") - result=$(_concatSep "and" prefixed) - echo "$result" -} - function pytestCheckPhase() { echo "Executing pytestCheckPhase" runHook preCheck @@ -34,7 +11,7 @@ function pytestCheckPhase() { # Compose arguments args=" -m pytest" if [ -n "$disabledTests" ]; then - disabledTestsString=$(_pytestComputeDisabledTestsString "${disabledTests[@]}") + disabledTestsString="not $(concatStringsSep " and not " disabledTests)" args+=" -k \""$disabledTestsString"\"" fi diff --git a/pkgs/stdenv/generic/setup.sh b/pkgs/stdenv/generic/setup.sh index 0660c6b642db..79997237482f 100644 --- a/pkgs/stdenv/generic/setup.sh +++ b/pkgs/stdenv/generic/setup.sh @@ -432,6 +432,11 @@ concatTo() { # $ flags=("lorem ipsum" "dolor" "sit amet") # $ concatStringsSep ";" flags # lorem ipsum;dolor;sit amet +# +# Also supports multi-character separators; +# $ flags=("lorem ipsum" "dolor" "sit amet") +# $ concatStringsSep " and " flags +# lorem ipsum and dolor and sit amet concatStringsSep() { local sep="$1" local name="$2" @@ -443,11 +448,15 @@ concatStringsSep() { echo "concatStringsSep(): ERROR: trying to use concatStringsSep on an associative array." >&2 return 1 ;; -a*) - local IFS="$sep" - echo -n "${nameref[*]}" ;; + # \036 is the "record separator" character. We assume that this will never need to be part of + # an argument string we create here. If anyone ever hits this limitation: Feel free to refactor. + local IFS=$'\036' ;; *) - echo -n "${nameref// /"${sep}"}" ;; + local IFS=" " ;; + esac + local ifs_separated="${nameref[*]}" + echo -n "${ifs_separated//"$IFS"/"$sep"}" fi } diff --git a/pkgs/test/stdenv/default.nix b/pkgs/test/stdenv/default.nix index d8f11f98e9b8..cd333ccf04be 100644 --- a/pkgs/test/stdenv/default.nix +++ b/pkgs/test/stdenv/default.nix @@ -161,6 +161,14 @@ let arrayWithSep="$(concatStringsSep "&" array)" [[ "$arrayWithSep" == "lorem ipsum&dolor&sit amet" ]] || (echo "'\$arrayWithSep' was not 'lorem ipsum&dolor&sit amet'" && false) + array=("lorem ipsum" "dolor" "sit amet") + arrayWithSep="$(concatStringsSep "++" array)" + [[ "$arrayWithSep" == "lorem ipsum++dolor++sit amet" ]] || (echo "'\$arrayWithSep' was not 'lorem ipsum++dolor++sit amet'" && false) + + array=("lorem ipsum" "dolor" "sit amet") + arrayWithSep="$(concatStringsSep " and " array)" + [[ "$arrayWithSep" == "lorem ipsum and dolor and sit amet" ]] || (echo "'\$arrayWithSep' was not 'lorem ipsum and dolor and sit amet'" && false) + touch $out ''; };