diff options
author | Jacek Galowicz <jacek@galowicz.de> | 2021-12-07 17:39:38 +0000 |
---|---|---|
committer | Jacek Galowicz <jacek@galowicz.de> | 2021-12-09 11:32:28 +0000 |
commit | 2bc7345064f5fc454282f044589e9f4b288257d4 (patch) | |
tree | 632c70a081eca87970c9162c122c6010a9d72bce /pkgs/test | |
parent | f3b16a6b5d9d027f2a5d7f732378b90495c8bcdf (diff) |
Add golden effects test
Diffstat (limited to 'pkgs/test')
32 files changed, 145 insertions, 172 deletions
diff --git a/pkgs/test/make-binary-wrapper/add-flags.c b/pkgs/test/make-binary-wrapper/add-flags.c index 20d3b7508c05..7ce682c6be64 100644 --- a/pkgs/test/make-binary-wrapper/add-flags.c +++ b/pkgs/test/make-binary-wrapper/add-flags.c @@ -1,7 +1,3 @@ -// makeCWrapper /send/me/flags \ - --add-flags "-x -y -z" \ - --add-flags -abc - #include <unistd.h> #include <stdlib.h> #include <assert.h> @@ -22,4 +18,4 @@ int main(int argc, char **argv) { argv[0] = "/send/me/flags"; return execv("/send/me/flags", argv); -}
\ No newline at end of file +} diff --git a/pkgs/test/make-binary-wrapper/add-flags.cmdline b/pkgs/test/make-binary-wrapper/add-flags.cmdline new file mode 100644 index 000000000000..f840c772e349 --- /dev/null +++ b/pkgs/test/make-binary-wrapper/add-flags.cmdline @@ -0,0 +1,2 @@ + --add-flags "-x -y -z" \ + --add-flags -abc diff --git a/pkgs/test/make-binary-wrapper/add-flags.env b/pkgs/test/make-binary-wrapper/add-flags.env new file mode 100644 index 000000000000..9b8d1fb9f6a5 --- /dev/null +++ b/pkgs/test/make-binary-wrapper/add-flags.env @@ -0,0 +1,6 @@ +CWD=SUBST_CWD +SUBST_ARGV0 +-x +-y +-z +-abc diff --git a/pkgs/test/make-binary-wrapper/argv0.c b/pkgs/test/make-binary-wrapper/argv0.c index 8e3e1f2987b5..70c36889dc89 100644 --- a/pkgs/test/make-binary-wrapper/argv0.c +++ b/pkgs/test/make-binary-wrapper/argv0.c @@ -1,10 +1,7 @@ -// makeCWrapper /path/to/some/executable \ - --argv0 alternative-name - #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { argv[0] = "alternative-name"; - return execv("/path/to/some/executable", argv); + return execv("/send/me/flags", argv); } diff --git a/pkgs/test/make-binary-wrapper/argv0.cmdline b/pkgs/test/make-binary-wrapper/argv0.cmdline new file mode 100644 index 000000000000..1cadce8312a4 --- /dev/null +++ b/pkgs/test/make-binary-wrapper/argv0.cmdline @@ -0,0 +1 @@ + --argv0 alternative-name diff --git a/pkgs/test/make-binary-wrapper/argv0.env b/pkgs/test/make-binary-wrapper/argv0.env new file mode 100644 index 000000000000..04c13d32ee6d --- /dev/null +++ b/pkgs/test/make-binary-wrapper/argv0.env @@ -0,0 +1,2 @@ +CWD=SUBST_CWD +alternative-name diff --git a/pkgs/test/make-binary-wrapper/basic.c b/pkgs/test/make-binary-wrapper/basic.c index de366c519630..1c1266181377 100644 --- a/pkgs/test/make-binary-wrapper/basic.c +++ b/pkgs/test/make-binary-wrapper/basic.c @@ -1,9 +1,7 @@ -// makeCWrapper /path/to/executable - #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { - argv[0] = "/path/to/executable"; - return execv("/path/to/executable", argv); + argv[0] = "/send/me/flags"; + return execv("/send/me/flags", argv); } diff --git a/pkgs/test/make-binary-wrapper/basic.cmdline b/pkgs/test/make-binary-wrapper/basic.cmdline new file mode 100644 index 000000000000..e69de29bb2d1 --- /dev/null +++ b/pkgs/test/make-binary-wrapper/basic.cmdline diff --git a/pkgs/test/make-binary-wrapper/basic.env b/pkgs/test/make-binary-wrapper/basic.env new file mode 100644 index 000000000000..b0da31959447 --- /dev/null +++ b/pkgs/test/make-binary-wrapper/basic.env @@ -0,0 +1,2 @@ +CWD=SUBST_CWD +SUBST_ARGV0 diff --git a/pkgs/test/make-binary-wrapper/chdir.c b/pkgs/test/make-binary-wrapper/chdir.c index ff1f91a03bab..c67c695b1c3b 100644 --- a/pkgs/test/make-binary-wrapper/chdir.c +++ b/pkgs/test/make-binary-wrapper/chdir.c @@ -1,6 +1,3 @@ -// makeCWrapper /path/to/executable \ - --chdir /usr/local/bin - #include <unistd.h> #include <stdlib.h> #include <stdio.h> @@ -8,7 +5,7 @@ #define assert_success(e) do { if ((e) < 0) { perror(#e); abort(); } } while (0) int main(int argc, char **argv) { - assert_success(chdir("/usr/local/bin")); - argv[0] = "/path/to/executable"; - return execv("/path/to/executable", argv); -}
\ No newline at end of file + assert_success(chdir("/tmp/foo")); + argv[0] = "/send/me/flags"; + return execv("/send/me/flags", argv); +} diff --git a/pkgs/test/make-binary-wrapper/chdir.cmdline b/pkgs/test/make-binary-wrapper/chdir.cmdline new file mode 100644 index 000000000000..15235f20621c --- /dev/null +++ b/pkgs/test/make-binary-wrapper/chdir.cmdline @@ -0,0 +1 @@ + --chdir /tmp/foo diff --git a/pkgs/test/make-binary-wrapper/chdir.env b/pkgs/test/make-binary-wrapper/chdir.env new file mode 100644 index 000000000000..db129d68af74 --- /dev/null +++ b/pkgs/test/make-binary-wrapper/chdir.env @@ -0,0 +1,2 @@ +CWD=/tmp/foo +SUBST_ARGV0 diff --git a/pkgs/test/make-binary-wrapper/combination.c b/pkgs/test/make-binary-wrapper/combination.c index bb35d0d99f31..e9ce5f1d7244 100644 --- a/pkgs/test/make-binary-wrapper/combination.c +++ b/pkgs/test/make-binary-wrapper/combination.c @@ -1,11 +1,3 @@ -// makeCWrapper /path/to/executable \ - --argv0 my-wrapper \ - --set-default MESSAGE HELLO \ - --prefix PATH : /usr/bin/ \ - --suffix PATH : /usr/local/bin/ \ - --add-flags "-x -y -z" \ - --set MESSAGE2 WORLD - #define _GNU_SOURCE /* See feature_test_macros(7) */ #include <unistd.h> #include <stdlib.h> @@ -57,5 +49,5 @@ int main(int argc, char **argv) { argv = argv_tmp; argv[0] = "my-wrapper"; - return execv("/path/to/executable", argv); + return execv("/send/me/flags", argv); } diff --git a/pkgs/test/make-binary-wrapper/combination.cmdline b/pkgs/test/make-binary-wrapper/combination.cmdline new file mode 100644 index 000000000000..fb3861235c8b --- /dev/null +++ b/pkgs/test/make-binary-wrapper/combination.cmdline @@ -0,0 +1,6 @@ + --argv0 my-wrapper \ + --set-default MESSAGE HELLO \ + --prefix PATH : /usr/bin/ \ + --suffix PATH : /usr/local/bin/ \ + --add-flags "-x -y -z" \ + --set MESSAGE2 WORLD diff --git a/pkgs/test/make-binary-wrapper/combination.env b/pkgs/test/make-binary-wrapper/combination.env new file mode 100644 index 000000000000..886420c01d1e --- /dev/null +++ b/pkgs/test/make-binary-wrapper/combination.env @@ -0,0 +1,8 @@ +MESSAGE=HELLO +PATH=/usr/bin/:/usr/local/bin/ +MESSAGE2=WORLD +CWD=SUBST_CWD +my-wrapper +-x +-y +-z diff --git a/pkgs/test/make-binary-wrapper/default.nix b/pkgs/test/make-binary-wrapper/default.nix index eddab15fd31d..6f3b3ef4ae12 100644 --- a/pkgs/test/make-binary-wrapper/default.nix +++ b/pkgs/test/make-binary-wrapper/default.nix @@ -1,73 +1,54 @@ -{ lib, stdenv, runCommand, makeBinaryWrapper }: +{ lib, coreutils, python3, gcc, writeText, writeScript, runCommand, makeBinaryWrapper }: let - makeGoldenTest = { name, filename }: stdenv.mkDerivation { - name = name; - dontUnpack = true; - strictDeps = true; - nativeBuildInputs = [ makeBinaryWrapper ]; - phases = [ "installPhase" ]; - installPhase = '' - source ${./golden-test-utils.sh} - mkdir -p $out/bin - command=$(getInputCommand "${filename}") - eval "$command" > "$out/bin/result" - ''; - passthru = { - assertion = '' - source ${./golden-test-utils.sh} - contents=$(getOutputText "${filename}") - echo "$contents" | diff $out/bin/result - - ''; - }; - }; - tests = { - basic = makeGoldenTest { name = "basic"; filename = ./basic.c; }; - argv0 = makeGoldenTest { name = "argv0"; filename = ./argv0.c; }; - inherit_argv0 = makeGoldenTest { name = "inherit-argv0"; filename = ./inherit-argv0.c; }; - env = makeGoldenTest { name = "env"; filename = ./env.c; }; - invalid_env = makeGoldenTest { name = "invalid-env"; filename = ./invalid-env.c; }; - prefix = makeGoldenTest { name = "prefix"; filename = ./prefix.c; }; - suffix = makeGoldenTest { name = "suffix"; filename = ./suffix.c; }; - add_flags = makeGoldenTest { name = "add-flags"; filename = ./add-flags.c; }; - chdir = makeGoldenTest { name = "chdir"; filename = ./chdir.c; }; - combination = makeGoldenTest { name = "combination"; filename = ./combination.c; }; - }; -in runCommand "make-binary-wrapper-test" { - passthru = tests; - meta.platforms = lib.platforms.all; -} '' - validate() { - local name=$1 - local testout=$2 - local assertion=$3 - - echo -n "... $name: " >&2 - - local rc=0 - (out=$testout eval "$assertion") || rc=1 - - if [ "$rc" -eq 0 ]; then - echo "yes" >&2 + env = { nativeBuildInputs = [ makeBinaryWrapper gcc ]; }; + envCheck = runCommand "envcheck" env '' + cc -o $out ${./envcheck.c} + ''; + makeGoldenTest = testname: runCommand "test-wrapper_${testname}" env '' + mkdir -p /tmp/foo + + params=$(<"${./.}/${testname}.cmdline") + eval "makeCWrapper /send/me/flags $params" > wrapper.c + + diff wrapper.c "${./.}/${testname}.c" + + if [ -f "${./.}/${testname}.env" ]; then + eval "makeBinaryWrapper ${envCheck} wrapped $params" + env -i ./wrapped > env.txt + sed "s#SUBST_ARGV0#${envCheck}#;s#SUBST_CWD#$PWD#" \ + "${./.}/${testname}.env" > golden-env.txt + if ! diff env.txt golden-env.txt; then + echo "env/argv should be:" + cat golden-env.txt + echo "env/argv output is:" + cat env.txt + exit 1 + fi else - echo "no" >&2 + # without a golden env, we expect the wrapper compilation to fail + ! eval "makeBinaryWrapper ${envCheck} wrapped $params" &> error.txt fi - return "$rc" - } - - echo "checking whether makeCWrapper works properly... ">&2 - - fail= + cp wrapper.c $out + ''; + tests = let + names = [ + "add-flags" + "argv0" + "basic" + "chdir" + "combination" + "env" + "inherit-argv0" + "invalid-env" + "prefix" + "suffix" + ]; + f = name: lib.nameValuePair name (makeGoldenTest name); + in builtins.listToAttrs (builtins.map f names); +in writeText "make-binary-wrapper-test" '' ${lib.concatStringsSep "\n" (lib.mapAttrsToList (_: test: '' - validate "${test.name}" "${test}" ${lib.escapeShellArg test.assertion} || fail=1 + "${test.name}" "${test}" '') tests)} - - if [ "$fail" ]; then - echo "failed" - exit 1 - else - echo "succeeded" - touch $out - fi -'' +'' // tests diff --git a/pkgs/test/make-binary-wrapper/env.c b/pkgs/test/make-binary-wrapper/env.c index fa2e6fc4dd29..7e0422dee3bd 100644 --- a/pkgs/test/make-binary-wrapper/env.c +++ b/pkgs/test/make-binary-wrapper/env.c @@ -1,9 +1,3 @@ -// makeCWrapper /hello/world \ - --set PART1 HELLO \ - --set-default PART2 WORLD \ - --unset SOME_OTHER_VARIABLE \ - --set PART3 $'"!!\n"' - #include <unistd.h> #include <stdlib.h> #include <stdio.h> @@ -15,6 +9,6 @@ int main(int argc, char **argv) { assert_success(setenv("PART2", "WORLD", 0)); assert_success(unsetenv("SOME_OTHER_VARIABLE")); putenv("PART3=\"!!\n\""); - argv[0] = "/hello/world"; - return execv("/hello/world", argv); -}
\ No newline at end of file + argv[0] = "/send/me/flags"; + return execv("/send/me/flags", argv); +} diff --git a/pkgs/test/make-binary-wrapper/env.cmdline b/pkgs/test/make-binary-wrapper/env.cmdline new file mode 100644 index 000000000000..3c89f33e2dce --- /dev/null +++ b/pkgs/test/make-binary-wrapper/env.cmdline @@ -0,0 +1,4 @@ + --set PART1 HELLO \ + --set-default PART2 WORLD \ + --unset SOME_OTHER_VARIABLE \ + --set PART3 $'"!!\n"' diff --git a/pkgs/test/make-binary-wrapper/env.env b/pkgs/test/make-binary-wrapper/env.env new file mode 100644 index 000000000000..c7661e165e09 --- /dev/null +++ b/pkgs/test/make-binary-wrapper/env.env @@ -0,0 +1,6 @@ +PART1=HELLO +PART2=WORLD +PART3="!! +" +CWD=SUBST_CWD +SUBST_ARGV0 diff --git a/pkgs/test/make-binary-wrapper/envcheck.c b/pkgs/test/make-binary-wrapper/envcheck.c new file mode 100644 index 000000000000..848fbdaa80f2 --- /dev/null +++ b/pkgs/test/make-binary-wrapper/envcheck.c @@ -0,0 +1,22 @@ +#include <limits.h> +#include <stdio.h> +#include <unistd.h> + +int main(int argc, char **argv, char **envp) { + for (char **env = envp; *env != 0; ++env) { + puts(*env); + } + + char cwd[PATH_MAX]; + if (getcwd(cwd, sizeof(cwd))) { + printf("CWD=%s\n", cwd); + } else { + perror("getcwd() error"); + return 1; + } + + for (int i=0; i < argc; ++i) { + puts(argv[i]); + } + return 0; +} diff --git a/pkgs/test/make-binary-wrapper/golden-test-utils.sh b/pkgs/test/make-binary-wrapper/golden-test-utils.sh deleted file mode 100644 index 80e880e11e48..000000000000 --- a/pkgs/test/make-binary-wrapper/golden-test-utils.sh +++ /dev/null @@ -1,44 +0,0 @@ -#!/usr/bin/env bash -# Split a generated C-file into the command used to generate it, -# and the outputted code itself. - -# This is useful because it allows input and output to be inside the same file - -# How it works: -# - The first line needs to start with '//' (and becomes the command). -# - Whitespace/padding between the comment and the generated code is ignored -# - To write a command using multiple lines, end each line with backslash (\) - -# Count the number of lines before the output text starts -# commandLineCount FILE -commandLineCount() { - local n state - n=0 - state="init" - while IFS="" read -r p || [ -n "$p" ]; do - case $state in - init) - if [[ $p =~ ^//.*\\$ ]]; then state="comment" - elif [[ $p =~ ^//.* ]]; then state="padding" - else break - fi - ;; - comment) [[ ! $p =~ ^.*\\$ ]] && state="padding";; - padding) [ -n "${p// }" ] && break;; - esac - n=$((n+1)) - done < "$1" - printf '%s' "$n" -} - -# getInputCommand FILE -getInputCommand() { - n=$(commandLineCount "$1") - head -n "$n" "$1" | awk '{ if (NR == 1) print substr($0, 3); else print $0 }' -} - -# getOutputText FILE -getOutputText() { - n=$(commandLineCount "$1") - sed "1,${n}d" "$1" -} diff --git a/pkgs/test/make-binary-wrapper/inherit-argv0.c b/pkgs/test/make-binary-wrapper/inherit-argv0.c index 71e12d9b024a..e1c2bc926aa7 100644 --- a/pkgs/test/make-binary-wrapper/inherit-argv0.c +++ b/pkgs/test/make-binary-wrapper/inherit-argv0.c @@ -1,9 +1,6 @@ -// makeCWrapper /path/to/executable \ - --inherit-argv0 - #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { - return execv("/path/to/executable", argv); -}
\ No newline at end of file + return execv("/send/me/flags", argv); +} diff --git a/pkgs/test/make-binary-wrapper/inherit-argv0.cmdline b/pkgs/test/make-binary-wrapper/inherit-argv0.cmdline new file mode 100644 index 000000000000..088076799835 --- /dev/null +++ b/pkgs/test/make-binary-wrapper/inherit-argv0.cmdline @@ -0,0 +1 @@ + --inherit-argv0 diff --git a/pkgs/test/make-binary-wrapper/inherit-argv0.env b/pkgs/test/make-binary-wrapper/inherit-argv0.env new file mode 100644 index 000000000000..c46ca95eefbc --- /dev/null +++ b/pkgs/test/make-binary-wrapper/inherit-argv0.env @@ -0,0 +1,2 @@ +CWD=SUBST_CWD +./wrapped diff --git a/pkgs/test/make-binary-wrapper/invalid-env.c b/pkgs/test/make-binary-wrapper/invalid-env.c index bad647bd13a2..4dfd36fb68a0 100644 --- a/pkgs/test/make-binary-wrapper/invalid-env.c +++ b/pkgs/test/make-binary-wrapper/invalid-env.c @@ -1,7 +1,3 @@ -// makeCWrapper /bad/env/example \ - --set "=" "TEST1" \ - --set-default "" "TEST2" - #include <unistd.h> #include <stdlib.h> #include <stdio.h> @@ -13,6 +9,6 @@ int main(int argc, char **argv) { #error Illegal environment variable name `=` (cannot contain `=`) assert_success(setenv("", "TEST2", 0)); #error Environment variable name can't be empty. - argv[0] = "/bad/env/example"; - return execv("/bad/env/example", argv); -}
\ No newline at end of file + argv[0] = "/send/me/flags"; + return execv("/send/me/flags", argv); +} diff --git a/pkgs/test/make-binary-wrapper/invalid-env.cmdline b/pkgs/test/make-binary-wrapper/invalid-env.cmdline new file mode 100644 index 000000000000..a03b001e754e --- /dev/null +++ b/pkgs/test/make-binary-wrapper/invalid-env.cmdline @@ -0,0 +1,2 @@ + --set "=" "TEST1" \ + --set-default "" "TEST2" diff --git a/pkgs/test/make-binary-wrapper/prefix.c b/pkgs/test/make-binary-wrapper/prefix.c index de431168bffc..ea8fbdc64a84 100644 --- a/pkgs/test/make-binary-wrapper/prefix.c +++ b/pkgs/test/make-binary-wrapper/prefix.c @@ -1,7 +1,3 @@ -// makeCWrapper /path/to/executable \ - --prefix PATH : /usr/bin/ \ - --prefix PATH : /usr/local/bin/ - #define _GNU_SOURCE /* See feature_test_macros(7) */ #include <unistd.h> #include <stdlib.h> @@ -25,6 +21,6 @@ void set_env_prefix(char *env, char *sep, char *prefix) { int main(int argc, char **argv) { set_env_prefix("PATH", ":", "/usr/bin/"); set_env_prefix("PATH", ":", "/usr/local/bin/"); - argv[0] = "/path/to/executable"; - return execv("/path/to/executable", argv); + argv[0] = "/send/me/flags"; + return execv("/send/me/flags", argv); } diff --git a/pkgs/test/make-binary-wrapper/prefix.cmdline b/pkgs/test/make-binary-wrapper/prefix.cmdline new file mode 100644 index 000000000000..99cebf9503f4 --- /dev/null +++ b/pkgs/test/make-binary-wrapper/prefix.cmdline @@ -0,0 +1,2 @@ + --prefix PATH : /usr/bin/ \ + --prefix PATH : /usr/local/bin/ diff --git a/pkgs/test/make-binary-wrapper/prefix.env b/pkgs/test/make-binary-wrapper/prefix.env new file mode 100644 index 000000000000..033676457c57 --- /dev/null +++ b/pkgs/test/make-binary-wrapper/prefix.env @@ -0,0 +1,3 @@ +PATH=/usr/local/bin/:/usr/bin/ +CWD=SUBST_CWD +SUBST_ARGV0 diff --git a/pkgs/test/make-binary-wrapper/suffix.c b/pkgs/test/make-binary-wrapper/suffix.c index 3fcb338a6104..d33f86c070ca 100644 --- a/pkgs/test/make-binary-wrapper/suffix.c +++ b/pkgs/test/make-binary-wrapper/suffix.c @@ -1,7 +1,3 @@ -// makeCWrapper /path/to/executable \ - --suffix PATH : /usr/bin/ \ - --suffix PATH : /usr/local/bin/ - #define _GNU_SOURCE /* See feature_test_macros(7) */ #include <unistd.h> #include <stdlib.h> @@ -25,6 +21,6 @@ void set_env_suffix(char *env, char *sep, char *suffix) { int main(int argc, char **argv) { set_env_suffix("PATH", ":", "/usr/bin/"); set_env_suffix("PATH", ":", "/usr/local/bin/"); - argv[0] = "/path/to/executable"; - return execv("/path/to/executable", argv); + argv[0] = "/send/me/flags"; + return execv("/send/me/flags", argv); } diff --git a/pkgs/test/make-binary-wrapper/suffix.cmdline b/pkgs/test/make-binary-wrapper/suffix.cmdline new file mode 100644 index 000000000000..95d291f3c169 --- /dev/null +++ b/pkgs/test/make-binary-wrapper/suffix.cmdline @@ -0,0 +1,2 @@ + --suffix PATH : /usr/bin/ \ + --suffix PATH : /usr/local/bin/ diff --git a/pkgs/test/make-binary-wrapper/suffix.env b/pkgs/test/make-binary-wrapper/suffix.env new file mode 100644 index 000000000000..3ce4cc54de41 --- /dev/null +++ b/pkgs/test/make-binary-wrapper/suffix.env @@ -0,0 +1,3 @@ +PATH=/usr/bin/:/usr/local/bin/ +CWD=SUBST_CWD +SUBST_ARGV0 |