2 # nix-build -A tests.trivial-builders.writeShellApplication
3 { writeShellApplication
12 checkShellApplication = args@{name, expected, ...}:
14 writeShellApplicationArgs = builtins.removeAttrs args ["expected"];
15 script = writeShellApplication writeShellApplicationArgs;
16 executable = lib.getExe script;
17 expected' = writeTextFile {
18 name = "${name}-expected";
21 actual = "${name}-actual";
23 runCommand name { } ''
24 echo "Running test executable ${name}"
25 ${executable} > ${actual}
26 echo "Got output from test executable:"
28 echo "Checking test output against expected output:"
29 ${diffutils}/bin/diff --color --unified ${expected'} ${actual}
33 linkFarm "writeShellApplication-tests" {
36 script = writeShellApplication {
39 meta.description = "A test for the `writeShellApplication` `meta` argument.";
42 assert script.meta.mainProgram == "test-meta";
43 assert script.meta.description == "A test for the `writeShellApplication` `meta` argument.";
47 checkShellApplication {
48 name = "test-runtime-inputs";
52 runtimeInputs = [ hello ];
53 expected = "Hello, world!\n";
57 checkShellApplication {
58 name = "test-runtime-env";
60 MY_COOL_ENV_VAR = "my-cool-env-value";
61 MY_OTHER_COOL_ENV_VAR = "my-other-cool-env-value";
62 # Check that we can serialize a bunch of different types:
72 echo "$MY_COOL_ENV_VAR"
73 echo "$MY_OTHER_COOL_ENV_VAR"
77 my-other-cool-env-value
82 checkShellApplication {
83 name = "test-check-phase";
86 echo "echo -n hello" > $target
91 test-argument-forwarding =
92 checkShellApplication {
93 name = "test-argument-forwarding";
95 derivationArgs.MY_BUILD_TIME_VARIABLE = "puppy";
96 derivationArgs.postCheck = ''
97 if [[ "$MY_BUILD_TIME_VARIABLE" != puppy ]]; then
98 echo "\$MY_BUILD_TIME_VARIABLE is not set to 'puppy'!"
102 meta.description = "A test checking that `writeShellApplication` forwards extra arguments to `stdenv.mkDerivation`.";
106 test-exclude-shell-checks = writeShellApplication {
107 name = "test-exclude-shell-checks";
108 excludeShellChecks = [ "SC2016" ];
110 # Triggers SC2016: Expressions don't expand in single quotes, use double
116 test-bash-options-pipefail = checkShellApplication {
117 name = "test-bash-options-pipefail";
120 echo puppy | grep doggy | sed 's/doggy/puppy/g'
121 # ^^^^^^^^^^ This will fail.
124 # Don't use `pipefail`:
125 bashOptions = ["errexit" "nounset"];
129 test-bash-options-nounset = checkShellApplication {
130 name = "test-bash-options-nounset";
132 echo -n "$someUndefinedVariable"
134 # Don't use `nounset`:
136 # Don't warn about the undefined variable at build time:
137 excludeShellChecks = [ "SC2154" ];