lol
1# Run with:
2# nix-build -A tests.trivial-builders.writeShellApplication
3{
4 writeShellApplication,
5 writeTextFile,
6 runCommand,
7 lib,
8 linkFarm,
9 diffutils,
10 hello,
11}:
12let
13 checkShellApplication =
14 args@{ name, expected, ... }:
15 let
16 writeShellApplicationArgs = builtins.removeAttrs args [ "expected" ];
17 script = writeShellApplication writeShellApplicationArgs;
18 executable = lib.getExe script;
19 expected' = writeTextFile {
20 name = "${name}-expected";
21 text = expected;
22 };
23 actual = "${name}-actual";
24 in
25 runCommand name { } ''
26 echo "Running test executable ${name}"
27 ${executable} > ${actual}
28 echo "Got output from test executable:"
29 cat ${actual}
30 echo "Checking test output against expected output:"
31 ${diffutils}/bin/diff --color --unified ${expected'} ${actual}
32 touch $out
33 '';
34in
35linkFarm "writeShellApplication-tests" {
36 test-meta =
37 let
38 script = writeShellApplication {
39 name = "test-meta";
40 text = "";
41 meta.description = "Test for the `writeShellApplication` `meta` argument";
42 };
43 in
44 assert script.meta.mainProgram == "test-meta";
45 assert script.meta.description == "A test for the `writeShellApplication` `meta` argument";
46 script;
47
48 test-runtime-inputs = checkShellApplication {
49 name = "test-runtime-inputs";
50 text = ''
51 hello
52 '';
53 runtimeInputs = [ hello ];
54 expected = "Hello, world!\n";
55 };
56
57 test-runtime-env = checkShellApplication {
58 name = "test-runtime-env";
59 runtimeEnv = {
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:
63 BOOL = true;
64 INT = 1;
65 LIST = [
66 1
67 2
68 3
69 ];
70 MAP = {
71 a = "a";
72 b = "b";
73 };
74 };
75 text = ''
76 echo "$MY_COOL_ENV_VAR"
77 echo "$MY_OTHER_COOL_ENV_VAR"
78 '';
79 expected = ''
80 my-cool-env-value
81 my-other-cool-env-value
82 '';
83 };
84
85 test-check-phase = checkShellApplication {
86 name = "test-check-phase";
87 text = "";
88 checkPhase = ''
89 echo "echo -n hello" > $target
90 '';
91 expected = "hello";
92 };
93
94 test-argument-forwarding = checkShellApplication {
95 name = "test-argument-forwarding";
96 text = "";
97 derivationArgs.MY_BUILD_TIME_VARIABLE = "puppy";
98 derivationArgs.postCheck = ''
99 if [[ "$MY_BUILD_TIME_VARIABLE" != puppy ]]; then
100 echo "\$MY_BUILD_TIME_VARIABLE is not set to 'puppy'!"
101 exit 1
102 fi
103 '';
104 meta.description = "Test checking that `writeShellApplication` forwards extra arguments to `stdenv.mkDerivation`";
105 expected = "";
106 };
107
108 test-exclude-shell-checks = writeShellApplication {
109 name = "test-exclude-shell-checks";
110 excludeShellChecks = [ "SC2016" ];
111 text = ''
112 # Triggers SC2016: Expressions don't expand in single quotes, use double
113 # quotes for that.
114 echo '$SHELL'
115 '';
116 };
117
118 test-bash-options-pipefail = checkShellApplication {
119 name = "test-bash-options-pipefail";
120 text = ''
121 touch my-test-file
122 echo puppy | grep doggy | sed 's/doggy/puppy/g'
123 # ^^^^^^^^^^ This will fail.
124 true
125 '';
126 # Don't use `pipefail`:
127 bashOptions = [
128 "errexit"
129 "nounset"
130 ];
131 expected = "";
132 };
133
134 test-bash-options-nounset = checkShellApplication {
135 name = "test-bash-options-nounset";
136 text = ''
137 echo -n "$someUndefinedVariable"
138 '';
139 # Don't use `nounset`:
140 bashOptions = [ ];
141 # Don't warn about the undefined variable at build time:
142 excludeShellChecks = [ "SC2154" ];
143 expected = "";
144 };
145
146}