stdenv: support multi-char separators in concatStringsSep (#360466)

This commit is contained in:
Philip Taron
2024-12-01 16:09:25 -08:00
committed by GitHub
3 changed files with 21 additions and 27 deletions
@@ -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
+12 -3
View File
@@ -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
}
+8
View File
@@ -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
'';
};