From 11c3127e38dafdf95ca71a85b1591a29b67e0c09 Mon Sep 17 00:00:00 2001 From: Artturin Date: Sat, 10 Dec 2022 01:09:40 +0200 Subject: stdenv: detect the type of variable in {prepend,append}ToVar stdenv: error if using {prepend,append}ToVar on associative array i don't know how to prepend to associative array --- pkgs/test/stdenv/default.nix | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'pkgs/test/stdenv') diff --git a/pkgs/test/stdenv/default.nix b/pkgs/test/stdenv/default.nix index c5af468655f..08e8eed118f 100644 --- a/pkgs/test/stdenv/default.nix +++ b/pkgs/test/stdenv/default.nix @@ -60,10 +60,6 @@ let }; passAsFile = [ "buildCommand" ] ++ lib.optionals (extraAttrs ? extraTest) [ "extraTest" ]; - # FIXME: with structuredAttrs string is converted to a indexed array - # should a/pToVar check if the passed variable is a array or a string? - # declare -x string="testing-string" - # declare -ax string=([0]="world" [1]="testing-string" [2]="hello") buildCommand = '' declare -p string appendToVar string hello @@ -71,8 +67,23 @@ let prependToVar string "world" declare -p string + declare -A associativeArray=(["X"]="Y") + [[ $(appendToVar associativeArray "fail" 2>&1) =~ "trying to use" ]] || (echo "prependToVar did not catch prepending associativeArray" && false) + [[ $(prependToVar associativeArray "fail" 2>&1) =~ "trying to use" ]] || (echo "prependToVar did not catch prepending associativeArray" && false) + [[ $string == "world testing-string hello" ]] || (echo "'\$string' was not 'world testing-string hello'" && false) + # test appending to a unset variable + appendToVar nonExistant created hello + typeset -p nonExistant + if [[ -n $__structuredAttrs ]]; then + [[ "''${nonExistant[@]}" == "created hello" ]] + else + # there's a extra " " in front here and a extra " " in the end of prependToVar + # shouldn't matter because these functions will mostly be used for $*Flags and the Flag variable will in most cases already exit + [[ "$nonExistant" == " created hello" ]] + fi + eval "$extraTest" touch $out -- cgit 1.4.1