1{
2 devShellTools,
3 emptyFile,
4 lib,
5 stdenv,
6 hello,
7 writeText,
8 zlib,
9 nixosTests,
10}:
11let
12 inherit (lib)
13 concatLines
14 escapeShellArg
15 isString
16 mapAttrsToList
17 ;
18in
19lib.recurseIntoAttrs {
20
21 # nix-build -A tests.devShellTools.nixos
22 nixos = nixosTests.docker-tools-nix-shell;
23
24 # nix-build -A tests.devShellTools.valueToString
25 valueToString =
26 let
27 inherit (devShellTools) valueToString;
28 in
29
30 stdenv.mkDerivation {
31 name = "devShellTools-valueToString-built-tests";
32
33 # Test inputs
34 inherit emptyFile hello;
35 one = 1;
36 boolTrue = true;
37 boolFalse = false;
38 foo = "foo";
39 list = [
40 1
41 2
42 3
43 ];
44 pathDefaultNix = ./default.nix;
45 packages = [
46 hello
47 emptyFile
48 ];
49 # TODO: nested lists
50
51 buildCommand = ''
52 touch $out
53 ( set -x
54 [[ "$one" = ${escapeShellArg (valueToString 1)} ]]
55 [[ "$boolTrue" = ${escapeShellArg (valueToString true)} ]]
56 [[ "$boolFalse" = ${escapeShellArg (valueToString false)} ]]
57 [[ "$foo" = ${escapeShellArg (valueToString "foo")} ]]
58 [[ "$hello" = ${escapeShellArg (valueToString hello)} ]]
59 [[ "$list" = ${
60 escapeShellArg (valueToString [
61 1
62 2
63 3
64 ])
65 } ]]
66 [[ "$packages" = ${
67 escapeShellArg (valueToString [
68 hello
69 emptyFile
70 ])
71 } ]]
72 [[ "$pathDefaultNix" = ${escapeShellArg (valueToString ./default.nix)} ]]
73 [[ "$emptyFile" = ${escapeShellArg (valueToString emptyFile)} ]]
74 ) >log 2>&1 || { cat log; exit 1; }
75 '';
76 };
77
78 # nix-build -A tests.devShellTools.valueToString
79 unstructuredDerivationInputEnv =
80 let
81 inherit (devShellTools) unstructuredDerivationInputEnv;
82
83 drvAttrs = {
84 one = 1;
85 boolTrue = true;
86 boolFalse = false;
87 foo = "foo";
88 list = [
89 1
90 2
91 3
92 ];
93 pathDefaultNix = ./default.nix;
94 stringWithDep = "Exe: ${hello}/bin/hello";
95 aPackageAttrSet = hello;
96 anOutPath = hello.outPath;
97 anAnAlternateOutput = zlib.dev;
98 args = [
99 "args must not be added to the environment"
100 "Nix doesn't do it."
101 ];
102
103 passAsFile = [ "bar" ];
104 bar = ''
105 bar
106 ${writeText "qux" "yea"}
107 '';
108 };
109 result = unstructuredDerivationInputEnv { inherit drvAttrs; };
110 in
111 assert
112 result // { barPath = "<check later>"; } == {
113 one = "1";
114 boolTrue = "1";
115 boolFalse = "";
116 foo = "foo";
117 list = "1 2 3";
118 pathDefaultNix = "${./default.nix}";
119 stringWithDep = "Exe: ${hello}/bin/hello";
120 aPackageAttrSet = "${hello}";
121 anOutPath = "${hello.outPath}";
122 anAnAlternateOutput = "${zlib.dev}";
123
124 passAsFile = "bar";
125 barPath = "<check later>";
126 };
127
128 # Not runCommand, because it alters `passAsFile`
129 stdenv.mkDerivation (
130 {
131 name = "devShellTools-unstructuredDerivationInputEnv-built-tests";
132
133 exampleBarPathString =
134 assert isString result.barPath;
135 result.barPath;
136
137 dontUnpack = true;
138 dontBuild = true;
139 dontFixup = true;
140 doCheck = true;
141
142 installPhase = "touch $out";
143
144 checkPhase = ''
145 fail() {
146 echo "$@" >&2
147 exit 1
148 }
149 checkAttr() {
150 echo checking attribute $1...
151 if [[ "$2" != "$3" ]]; then
152 echo "expected: $3"
153 echo "actual: $2"
154 exit 1
155 fi
156 }
157 ${concatLines (
158 mapAttrsToList (name: value: "checkAttr ${name} \"\$${name}\" ${escapeShellArg value}") (
159 removeAttrs result [
160 "args"
161
162 # Nix puts it in workdir, which is not a concept for
163 # unstructuredDerivationInputEnv, so we have to put it in the
164 # store instead. This means the full path won't match.
165 "barPath"
166 ]
167 )
168 )}
169 (
170 set -x
171
172 diff $exampleBarPathString $barPath
173
174 ${lib.optionalString (builtins ? convertHash) ''
175 [[ "$(basename $exampleBarPathString)" = "$(basename $barPath)" ]]
176 ''}
177 )
178
179 ''${args:+fail "args should not be set by Nix. We don't expect it to and unstructuredDerivationInputEnv removes it."}
180 if [[ "''${builder:-x}" == x ]]; then
181 fail "builder should be set by Nix. We don't remove it in unstructuredDerivationInputEnv."
182 fi
183 '';
184 }
185 // removeAttrs drvAttrs [
186 # This would break the derivation. Instead, we have a check in the derivation to make sure Nix doesn't set it.
187 "args"
188 ]
189 );
190}