Skip to content

Commit 56b0962

Browse files
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.
1 parent eba4fcf commit 56b0962

3 files changed

Lines changed: 21 additions & 27 deletions

File tree

pkgs/development/interpreters/python/hooks/pytest-check-hook.sh

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,14 @@ echo "Sourcing pytest-check-hook"
44
declare -ar disabledTests
55
declare -a disabledTestPaths
66

7-
function _concatSep {
8-
local result
9-
local sep="$1"
10-
local -n arr=$2
11-
for index in ${!arr[*]}; do
12-
if [ $index -eq 0 ]; then
13-
result="${arr[index]}"
14-
else
15-
result+=" $sep ${arr[index]}"
16-
fi
17-
done
18-
echo "$result"
19-
}
20-
21-
function _pytestComputeDisabledTestsString() {
22-
declare -a tests
23-
local tests=($1)
24-
local prefix="not "
25-
prefixed=("${tests[@]/#/$prefix}")
26-
result=$(_concatSep "and" prefixed)
27-
echo "$result"
28-
}
29-
307
function pytestCheckPhase() {
318
echo "Executing pytestCheckPhase"
329
runHook preCheck
3310

3411
# Compose arguments
3512
args=" -m pytest"
3613
if [ -n "$disabledTests" ]; then
37-
disabledTestsString=$(_pytestComputeDisabledTestsString "${disabledTests[@]}")
14+
disabledTestsString="not $(concatStringsSep " and not " disabledTests)"
3815
args+=" -k \""$disabledTestsString"\""
3916
fi
4017

pkgs/stdenv/generic/setup.sh

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,11 @@ concatTo() {
432432
# $ flags=("lorem ipsum" "dolor" "sit amet")
433433
# $ concatStringsSep ";" flags
434434
# lorem ipsum;dolor;sit amet
435+
#
436+
# Also supports multi-character separators;
437+
# $ flags=("lorem ipsum" "dolor" "sit amet")
438+
# $ concatStringsSep " and " flags
439+
# lorem ipsum and dolor and sit amet
435440
concatStringsSep() {
436441
local sep="$1"
437442
local name="$2"
@@ -443,11 +448,15 @@ concatStringsSep() {
443448
echo "concatStringsSep(): ERROR: trying to use concatStringsSep on an associative array." >&2
444449
return 1 ;;
445450
-a*)
446-
local IFS="$sep"
447-
echo -n "${nameref[*]}" ;;
451+
# \036 is the "record separator" character. We assume that this will never need to be part of
452+
# an argument string we create here. If anyone ever hits this limitation: Feel free to refactor.
453+
local IFS=$'\036' ;;
448454
*)
449-
echo -n "${nameref// /"${sep}"}" ;;
455+
local IFS=" " ;;
456+
450457
esac
458+
local ifs_separated="${nameref[*]}"
459+
echo -n "${ifs_separated//"$IFS"/"$sep"}"
451460
fi
452461
}
453462

pkgs/test/stdenv/default.nix

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,14 @@ let
161161
arrayWithSep="$(concatStringsSep "&" array)"
162162
[[ "$arrayWithSep" == "lorem ipsum&dolor&sit amet" ]] || (echo "'\$arrayWithSep' was not 'lorem ipsum&dolor&sit amet'" && false)
163163
164+
array=("lorem ipsum" "dolor" "sit amet")
165+
arrayWithSep="$(concatStringsSep "++" array)"
166+
[[ "$arrayWithSep" == "lorem ipsum++dolor++sit amet" ]] || (echo "'\$arrayWithSep' was not 'lorem ipsum++dolor++sit amet'" && false)
167+
168+
array=("lorem ipsum" "dolor" "sit amet")
169+
arrayWithSep="$(concatStringsSep " and " array)"
170+
[[ "$arrayWithSep" == "lorem ipsum and dolor and sit amet" ]] || (echo "'\$arrayWithSep' was not 'lorem ipsum and dolor and sit amet'" && false)
171+
164172
touch $out
165173
'';
166174
};

0 commit comments

Comments
 (0)