1{
2 replaceVars,
3 replaceVarsWith,
4 emptyDirectory,
5 emptyFile,
6 lib,
7 runCommand,
8 testers,
9}:
10let
11 inherit (testers) testEqualContents testBuildFailure;
12
13 mkTests =
14 callReplaceVars: mkExpectation:
15 lib.recurseIntoAttrs {
16 succeeds = testEqualContents {
17 assertion = "replaceVars-succeeds";
18 actual = callReplaceVars ./source.txt {
19 free = "free";
20 "equal in" = "are the same in";
21 brotherhood = "shared humanity";
22 };
23
24 expected = mkExpectation (
25 builtins.toFile "source.txt" ''
26 All human beings are born free and are the same in dignity and rights.
27 They are endowed with reason and conscience and should act towards
28 one another in a spirit of shared humanity.
29
30 -- eroosevelt@humanrights.un.org
31 ''
32 );
33 };
34
35 # There might eventually be a usecase for this, but it's not supported at the moment.
36 fails-on-directory =
37 runCommand "replaceVars-fails" { failed = testBuildFailure (callReplaceVars emptyDirectory { }); }
38 ''
39 grep -e "ERROR: file.*empty-directory.*does not exist" $failed/testBuildFailure.log
40 touch $out
41 '';
42
43 fails-in-build-phase =
44 runCommand "replaceVars-fails"
45 { failed = testBuildFailure (callReplaceVars emptyFile { not-found = "boo~"; }); }
46 ''
47 grep -e "ERROR: pattern @not-found@ doesn't match anything in file.*empty-file" $failed/testBuildFailure.log
48 touch $out
49 '';
50
51 fails-in-check-phase =
52 runCommand "replaceVars-fails"
53 {
54 failed =
55 let
56 src = builtins.toFile "source.txt" ''
57 Header.
58 before @whatIsThis@ middle @It'sOdd2Me@ after.
59 @cannot detect due to space@
60 Trailer.
61 '';
62 in
63 testBuildFailure (callReplaceVars src { });
64 }
65 ''
66 grep -e "unsubstituted Nix identifiers.*source.txt" $failed/testBuildFailure.log
67 grep -F "@whatIsThis@" $failed/testBuildFailure.log
68 grep -F "@It'sOdd2Me@" $failed/testBuildFailure.log
69 grep -F 'more precise `substitute` function' $failed/testBuildFailure.log
70
71 # Shouldn't see irrelevant details.
72 ! grep -q -E -e "Header|before|middle|after|Trailer" $failed/testBuildFailure.log
73
74 # Shouldn't see the "cannot detect" version.
75 ! grep -q -F "cannot detect due to space" $failed/testBuildFailure.log
76
77 touch $out
78 '';
79
80 succeeds-with-exemption = testEqualContents {
81 assertion = "replaceVars-succeeds";
82 actual = callReplaceVars ./source.txt {
83 free = "free";
84 "equal in" = "are the same in";
85 brotherhood = null;
86 };
87
88 expected = mkExpectation (
89 builtins.toFile "source.txt" ''
90 All human beings are born free and are the same in dignity and rights.
91 They are endowed with reason and conscience and should act towards
92 one another in a spirit of @brotherhood@.
93
94 -- eroosevelt@humanrights.un.org
95 ''
96 );
97 };
98
99 fails-in-check-phase-with-exemption =
100 runCommand "replaceVars-fails"
101 {
102 failed =
103 let
104 src = builtins.toFile "source.txt" ''
105 @a@
106 @b@
107 @c@
108 '';
109 in
110 testBuildFailure (
111 callReplaceVars src {
112 a = "a";
113 b = null;
114 }
115 );
116 }
117 ''
118 grep -e "unsubstituted Nix identifiers.*source.txt" $failed/testBuildFailure.log
119 grep -F "@c@" $failed/testBuildFailure.log
120 ! grep -F "@b@" $failed/testBuildFailure.log
121
122 touch $out
123 '';
124
125 fails-in-check-phase-with-bad-exemption =
126 runCommand "replaceVars-fails"
127 {
128 failed =
129 let
130 src = builtins.toFile "source.txt" ''
131 @a@
132 @b@
133 '';
134 in
135 testBuildFailure (
136 callReplaceVars src {
137 a = "a";
138 b = null;
139 c = null;
140 }
141 );
142 }
143 ''
144 grep -e "ERROR: pattern @c@ doesn't match anything in file.*source.txt" $failed/testBuildFailure.log
145
146 touch $out
147 '';
148 };
149in
150{
151 replaceVars = mkTests replaceVars lib.id;
152 replaceVarsWith =
153 mkTests
154 (
155 src: replacements:
156 replaceVarsWith {
157 inherit src replacements;
158 dir = "bin";
159 isExecutable = true;
160 }
161 )
162 (
163 file:
164 runCommand "expected" { inherit file; } ''
165 install -Dm755 "$file" "$out/bin/$(stripHash "$file")"
166 ''
167 );
168}