13 foofile = writeText "foofile" "foo";
14 barfile = writeText "barfile" "bar";
17 wrappedArgv0 = writeCBin "wrapped-argv0" ''
21 void main(int argc, char** argv) {
22 printf("argv0=%s", argv[0]);
26 wrappedBinaryVar = writeShellScript "wrapped-var" ''
29 wrappedBinaryArgs = writeShellScript "wrapped-args" ''
33 mkWrapperBinary = { name, args, wrapped ? wrappedBinaryVar }: runCommand name
35 nativeBuildInputs = [ makeWrapper ];
38 makeWrapper "${wrapped}" "$out/bin/${name}" ${lib.escapeShellArgs args}
41 mkTest = cmd: toExpect: ''
43 if [[ "$output" != '${toExpect}' ]]; then
44 echo "test failed: the output of ${cmd} was '$output', expected '${toExpect}'"
45 echo "the wrapper contents:"
47 if [[ $i =~ ^test- ]]; then
55 runCommand "make-wrapper-test"
59 (mkWrapperBinary { name = "test-argv0"; args = [ "--argv0" "foo" ]; wrapped = "${wrappedArgv0}/bin/wrapped-argv0"; })
60 (mkWrapperBinary { name = "test-set"; args = [ "--set" "VAR" "abc" ]; })
61 (mkWrapperBinary { name = "test-set-default"; args = [ "--set-default" "VAR" "abc" ]; })
62 (mkWrapperBinary { name = "test-unset"; args = [ "--unset" "VAR" ]; })
63 (mkWrapperBinary { name = "test-run"; args = [ "--run" "echo bar" ]; })
64 (mkWrapperBinary { name = "test-run-and-set"; args = [ "--run" "export VAR=foo" "--set" "VAR" "bar" ]; })
65 (mkWrapperBinary { name = "test-args"; args = [ "--add-flags" "abc" "--append-flags" "xyz" ]; wrapped = wrappedBinaryArgs; })
66 (mkWrapperBinary { name = "test-prefix"; args = [ "--prefix" "VAR" ":" "abc" ]; })
67 (mkWrapperBinary { name = "test-prefix-noglob"; args = [ "--prefix" "VAR" ":" "./*" ]; })
68 (mkWrapperBinary { name = "test-suffix"; args = [ "--suffix" "VAR" ":" "abc" ]; })
69 (mkWrapperBinary { name = "test-prefix-and-suffix"; args = [ "--prefix" "VAR" ":" "foo" "--suffix" "VAR" ":" "bar" ]; })
70 (mkWrapperBinary { name = "test-prefix-multi"; args = [ "--prefix" "VAR" ":" "abc:foo:foo" ]; })
71 (mkWrapperBinary { name = "test-suffix-each"; args = [ "--suffix-each" "VAR" ":" "foo bar:def" ]; })
72 (mkWrapperBinary { name = "test-prefix-each"; args = [ "--prefix-each" "VAR" ":" "foo bar:def" ]; })
73 (mkWrapperBinary { name = "test-suffix-contents"; args = [ "--suffix-contents" "VAR" ":" "${foofile} ${barfile}" ]; })
74 (mkWrapperBinary { name = "test-prefix-contents"; args = [ "--prefix-contents" "VAR" ":" "${foofile} ${barfile}" ]; })
79 mkTest "test-argv0" "argv0=foo"
82 + mkTest "test-set" "VAR=abc"
83 # --set overwrites the variable
84 + mkTest "VAR=foo test-set" "VAR=abc"
86 + mkTest "test-set-default" "VAR=abc"
87 # --set-default doesn"t overwrite the variable
88 + mkTest "VAR=foo test-set-default" "VAR=foo"
90 + mkTest "VAR=foo test-unset" "VAR="
92 # --add-flags and --append-flags work
93 + mkTest "test-args" "abc xyz"
94 # given flags are kept
95 + mkTest "test-args foo" "abc foo xyz"
98 + mkTest "test-run" "bar\nVAR="
100 + mkTest "test-run-and-set" "VAR=bar"
103 + mkTest "VAR=foo test-prefix" "VAR=abc:foo"
104 # sets variable if not set yet
105 + mkTest "test-prefix" "VAR=abc"
106 # prepends value only once
107 + mkTest "VAR=abc test-prefix" "VAR=abc"
108 # Moves value to the front if it already existed
109 + mkTest "VAR=foo:abc test-prefix" "VAR=abc:foo"
110 + mkTest "VAR=abc:foo:bar test-prefix-multi" "VAR=abc:foo:bar"
111 # Doesn't overwrite parts of the string
112 + mkTest "VAR=test:abcde:test test-prefix" "VAR=abc:test:abcde:test"
113 # Only append the value once when given multiple times in a parameter
115 + mkTest "test-prefix" "VAR=abc"
116 # --prefix doesn't expand globs
117 + mkTest "VAR=f?oo test-prefix-noglob" "VAR=./*:f?oo"
121 + mkTest "VAR=foo test-suffix" "VAR=foo:abc"
122 # sets variable if not set yet
123 + mkTest "test-suffix" "VAR=abc"
124 # adds the same value only once
125 + mkTest "VAR=abc test-suffix" "VAR=abc"
126 + mkTest "VAR=abc:foo test-suffix" "VAR=abc:foo"
127 # --prefix in combination with --suffix
128 + mkTest "VAR=abc test-prefix-and-suffix" "VAR=foo:abc:bar"
130 # --suffix-each works
131 + mkTest "VAR=abc test-suffix-each" "VAR=abc:foo:bar:def"
132 # --prefix-each works
133 + mkTest "VAR=abc test-prefix-each" "VAR=bar:def:foo:abc"
134 # --suffix-contents works
135 + mkTest "VAR=abc test-suffix-contents" "VAR=abc:foo:bar"
136 # --prefix-contents works
137 + mkTest "VAR=abc test-prefix-contents" "VAR=bar:foo:abc"