nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1# NOTE: We must use `pkgs.runCommand` instead of `testers.runCommand` for negative tests -- those wrapped with
2# `testers.testBuildFailure`. This is due to the fact that `testers.testBuildFailure` modifies the derivation such that
3# it produces an output containing the exit code, logs, and other things. Since `testers.runCommand` expects the empty
4# derivation, it produces a hash mismatch.
5{ lib, testers }:
6let
7 inherit (lib.attrsets) recurseIntoAttrs;
8 inherit (testers) testBuildFailure' testEqualArrayOrMap;
9 concatValuesArrayToActualArray = ''
10 nixLog "appending all values in valuesArray to actualArray"
11 for value in "''${valuesArray[@]}"; do
12 actualArray+=( "$value" )
13 done
14 '';
15 concatValuesMapToActualMap = ''
16 nixLog "adding all values in valuesMap to actualMap"
17 for key in "''${!valuesMap[@]}"; do
18 actualMap["$key"]="''${valuesMap["$key"]}"
19 done
20 '';
21in
22recurseIntoAttrs {
23 # NOTE: This particular test is used in the docs:
24 # See https://nixos.org/manual/nixpkgs/unstable/#tester-testEqualArrayOrMap
25 # or doc/build-helpers/testers.chapter.md
26 docs-test-function-add-cowbell = testEqualArrayOrMap {
27 name = "test-function-add-cowbell";
28 valuesArray = [
29 "cowbell"
30 "cowbell"
31 ];
32 expectedArray = [
33 "cowbell"
34 "cowbell"
35 "cowbell"
36 ];
37 script = ''
38 addCowbell() {
39 local -rn arrayNameRef="$1"
40 arrayNameRef+=( "cowbell" )
41 }
42
43 nixLog "appending all values in valuesArray to actualArray"
44 for value in "''${valuesArray[@]}"; do
45 actualArray+=( "$value" )
46 done
47
48 nixLog "applying addCowbell"
49 addCowbell actualArray
50 '';
51 };
52 array-append = testEqualArrayOrMap {
53 name = "testEqualArrayOrMap-array-append";
54 valuesArray = [
55 "apple"
56 "bee"
57 "cat"
58 ];
59 expectedArray = [
60 "apple"
61 "bee"
62 "cat"
63 "dog"
64 ];
65 script = ''
66 ${concatValuesArrayToActualArray}
67 actualArray+=( "dog" )
68 '';
69 };
70 array-prepend = testEqualArrayOrMap {
71 name = "testEqualArrayOrMap-array-prepend";
72 valuesArray = [
73 "apple"
74 "bee"
75 "cat"
76 ];
77 expectedArray = [
78 "dog"
79 "apple"
80 "bee"
81 "cat"
82 ];
83 script = ''
84 actualArray+=( "dog" )
85 ${concatValuesArrayToActualArray}
86 '';
87 };
88 array-empty = testEqualArrayOrMap {
89 name = "testEqualArrayOrMap-array-empty";
90 valuesArray = [
91 "apple"
92 "bee"
93 "cat"
94 ];
95 expectedArray = [ ];
96 script = ''
97 # doing nothing
98 '';
99 };
100 array-missing-value = testBuildFailure' {
101 drv = testEqualArrayOrMap {
102 name = "testEqualArrayOrMap-array-missing-value";
103 valuesArray = [ "apple" ];
104 expectedArray = [ ];
105 script = concatValuesArrayToActualArray;
106 };
107 expectedBuilderLogEntries = [
108 "ERROR: assertEqualArray: arrays differ in length: expectedArray has length 0 but actualArray has length 1"
109 "ERROR: assertEqualArray: arrays differ at index 0: expectedArray has no such index but actualArray has value 'apple'"
110 ];
111 };
112 map-insert = testEqualArrayOrMap {
113 name = "testEqualArrayOrMap-map-insert";
114 valuesMap = {
115 apple = "0";
116 bee = "1";
117 cat = "2";
118 };
119 expectedMap = {
120 apple = "0";
121 bee = "1";
122 cat = "2";
123 dog = "3";
124 };
125 script = ''
126 ${concatValuesMapToActualMap}
127 actualMap["dog"]="3"
128 '';
129 };
130 map-remove = testEqualArrayOrMap {
131 name = "testEqualArrayOrMap-map-remove";
132 valuesMap = {
133 apple = "0";
134 bee = "1";
135 cat = "2";
136 dog = "3";
137 };
138 expectedMap = {
139 apple = "0";
140 cat = "2";
141 dog = "3";
142 };
143 script = ''
144 ${concatValuesMapToActualMap}
145 unset 'actualMap[bee]'
146 '';
147 };
148 map-missing-key = testBuildFailure' {
149 drv = testEqualArrayOrMap {
150 name = "testEqualArrayOrMap-map-missing-key";
151 valuesMap = {
152 bee = "1";
153 cat = "2";
154 dog = "3";
155 };
156 expectedMap = {
157 apple = "0";
158 bee = "1";
159 cat = "2";
160 dog = "3";
161 };
162 script = concatValuesMapToActualMap;
163 };
164 expectedBuilderLogEntries = [
165 "ERROR: assertEqualMap: maps differ in length: expectedMap has length 4 but actualMap has length 3"
166 "ERROR: assertEqualMap: maps differ at key 'apple': expectedMap has value '0' but actualMap has no such key"
167 ];
168 };
169 map-missing-key-with-empty = testBuildFailure' {
170 drv = testEqualArrayOrMap {
171 name = "testEqualArrayOrMap-map-missing-key-with-empty";
172 valuesArray = [ ];
173 expectedMap.apple = 1;
174 script = "";
175 };
176 expectedBuilderLogEntries = [
177 "ERROR: assertEqualMap: maps differ in length: expectedMap has length 1 but actualMap has length 0"
178 "ERROR: assertEqualMap: maps differ at key 'apple': expectedMap has value '1' but actualMap has no such key"
179 ];
180 };
181 map-extra-key = testBuildFailure' {
182 drv = testEqualArrayOrMap {
183 name = "testEqualArrayOrMap-map-extra-key";
184 valuesMap = {
185 apple = "0";
186 bee = "1";
187 cat = "2";
188 dog = "3";
189 };
190 expectedMap = {
191 apple = "0";
192 bee = "1";
193 dog = "3";
194 };
195 script = concatValuesMapToActualMap;
196 };
197 expectedBuilderLogEntries = [
198 "ERROR: assertEqualMap: maps differ in length: expectedMap has length 3 but actualMap has length 4"
199 "ERROR: assertEqualMap: maps differ at key 'cat': expectedMap has no such key but actualMap has value '2'"
200 ];
201 };
202}