Skip to content

Commit 4405c35

Browse files
Ericson2314amaanq
authored andcommitted
Use designated initializers for derivations in more places
This is much nicer to read than imperatively mutating fields one by one.
1 parent a069710 commit 4405c35

10 files changed

Lines changed: 215 additions & 270 deletions

File tree

src/libexpr/primops.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1513,8 +1513,9 @@ static void derivationStrictInternal(EvalState & state, std::string_view drvName
15131513
"passed to builtins.derivationStrict");
15141514

15151515
/* Build the derivation expression by processing the attributes. */
1516-
Derivation drv;
1517-
drv.name = drvName;
1516+
Derivation drv{
1517+
.name = std::string{drvName},
1518+
};
15181519

15191520
NixStringContext context;
15201521

src/libstore-tests/derivation/external-formats.cc

Lines changed: 57 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,10 @@ INSTANTIATE_TEST_SUITE_P(
7474
std::pair{
7575
"caFixedNAR",
7676
DerivationOutput{DerivationOutput::CAFixed{
77-
.ca =
78-
{
79-
.method = ContentAddressMethod::Raw::NixArchive,
80-
.hash = Hash::parseAnyPrefixed("sha256-iUUXyRY8iW7DGirb0zwGgf1fRbLA7wimTJKgP7l/OQ8="),
81-
},
77+
.ca{
78+
.method = ContentAddressMethod::Raw::NixArchive,
79+
.hash = Hash::parseAnyPrefixed("sha256-iUUXyRY8iW7DGirb0zwGgf1fRbLA7wimTJKgP7l/OQ8="),
80+
},
8281
}},
8382
},
8483
std::pair{
@@ -185,41 +184,36 @@ struct DerivationJsonAtermTest : DerivationTest,
185184

186185
MAKE_TEST_P(DerivationJsonAtermTest);
187186

188-
INSTANTIATE_TEST_SUITE_P(DerivationJSONATerm, DerivationJsonAtermTest, ::testing::Values([]() {
189-
Derivation drv;
190-
drv.name = "simple-derivation";
191-
drv.inputs.srcs = {
192-
StorePath("c015dhfh5l0lp6wxyvdn7bmwhbbr6hr9-dep1"),
193-
};
194-
drv.inputs.drvs = {
195-
.map =
196-
{
197-
{
198-
StorePath("c015dhfh5l0lp6wxyvdn7bmwhbbr6hr9-dep2.drv"),
199-
{
200-
.value =
201-
{
202-
"cat",
203-
"dog",
204-
},
205-
},
206-
},
207-
},
208-
};
209-
drv.platform = "wasm-sel4";
210-
drv.builder = "foo";
211-
drv.args = {
212-
"bar",
213-
"baz",
214-
};
215-
drv.env = StringPairs{
216-
{
217-
"BIG_BAD",
218-
"WOLF",
219-
},
220-
};
221-
return drv;
222-
}()));
187+
INSTANTIATE_TEST_SUITE_P(
188+
DerivationJSONATerm,
189+
DerivationJsonAtermTest,
190+
::testing::Values(
191+
Derivation{
192+
.outputs = {},
193+
.inputs{
194+
.srcs{
195+
StorePath{"c015dhfh5l0lp6wxyvdn7bmwhbbr6hr9-dep1"},
196+
},
197+
.drvs{.map{
198+
{
199+
StorePath{"c015dhfh5l0lp6wxyvdn7bmwhbbr6hr9-dep2.drv"},
200+
{
201+
.value{
202+
"cat",
203+
"dog",
204+
},
205+
},
206+
},
207+
}},
208+
},
209+
.platform = "wasm-sel4",
210+
.builder = "foo",
211+
.args = {"bar", "baz"},
212+
.env{
213+
{"BIG_BAD", "WOLF"},
214+
},
215+
.name = "simple-derivation",
216+
}));
223217

224218
struct DynDerivationJsonAtermTest : DynDerivationTest,
225219
JsonCharacterizationTest<Derivation>,
@@ -230,60 +224,36 @@ MAKE_TEST_P(DynDerivationJsonAtermTest);
230224

231225
Derivation makeDynDepDerivation()
232226
{
233-
Derivation drv;
234-
drv.name = "dyn-dep-derivation";
235-
drv.inputs.srcs = {
236-
StorePath{"c015dhfh5l0lp6wxyvdn7bmwhbbr6hr9-dep1"},
237-
};
238-
drv.inputs.drvs = {
239-
.map =
240-
{
227+
return Derivation{
228+
.outputs = {},
229+
.inputs{
230+
.srcs{
231+
StorePath{"c015dhfh5l0lp6wxyvdn7bmwhbbr6hr9-dep1"},
232+
},
233+
.drvs{.map{
241234
{
242235
StorePath{"c015dhfh5l0lp6wxyvdn7bmwhbbr6hr9-dep2.drv"},
243236
DerivedPathMap<StringSet>::ChildNode{
244-
.value =
245-
{
246-
"cat",
247-
"dog",
248-
},
249-
.childMap =
250-
{
251-
{
252-
"cat",
253-
DerivedPathMap<StringSet>::ChildNode{
254-
.value =
255-
{
256-
"kitten",
257-
},
258-
},
259-
},
260-
{
261-
"goose",
262-
DerivedPathMap<StringSet>::ChildNode{
263-
.value =
264-
{
265-
"gosling",
266-
},
267-
},
268-
},
269-
},
237+
.value{
238+
"cat",
239+
"dog",
240+
},
241+
.childMap{
242+
{"cat", DerivedPathMap<StringSet>::ChildNode{.value = {"kitten"}}},
243+
{"goose", DerivedPathMap<StringSet>::ChildNode{.value = {"gosling"}}},
244+
},
270245
},
271246
},
272-
},
273-
};
274-
drv.platform = "wasm-sel4";
275-
drv.builder = "foo";
276-
drv.args = {
277-
"bar",
278-
"baz",
279-
};
280-
drv.env = StringPairs{
281-
{
282-
"BIG_BAD",
283-
"WOLF",
247+
}},
248+
},
249+
.platform = "wasm-sel4",
250+
.builder = "foo",
251+
.args = {"bar", "baz"},
252+
.env{
253+
{"BIG_BAD", "WOLF"},
284254
},
255+
.name = "dyn-dep-derivation",
285256
};
286-
return drv;
287257
}
288258

289259
INSTANTIATE_TEST_SUITE_P(DynDerivationJSONATerm, DynDerivationJsonAtermTest, ::testing::Values(makeDynDepDerivation()));

src/libstore-tests/derivation/invariants.cc

Lines changed: 55 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,18 @@ class FillInOutputPathsTest : public LibStoreTest, public JsonCharacterizationTe
2929
*/
3030
StorePath makeCAFloatingDependency(std::string_view name)
3131
{
32-
Derivation depDrv;
33-
depDrv.name = name;
34-
depDrv.platform = "x86_64-linux";
35-
depDrv.builder = "/bin/sh";
36-
depDrv.outputs = {
37-
{
38-
"out",
39-
// will ensure that downstream is deferred
40-
DerivationOutput{DerivationOutput::CAFloating{
41-
.method = ContentAddressMethod::Raw::NixArchive,
42-
.hashAlgo = HashAlgorithm::SHA256,
43-
}},
44-
},
32+
Derivation depDrv{
33+
.outputs =
34+
{{"out",
35+
DerivationOutput{DerivationOutput::CAFloating{
36+
.method = ContentAddressMethod::Raw::NixArchive,
37+
.hashAlgo = HashAlgorithm::SHA256,
38+
}}}},
39+
.platform = "x86_64-linux",
40+
.builder = "/bin/sh",
41+
.env = {{"out", ""}},
42+
.name = std::string{name},
4543
};
46-
depDrv.env = {{"out", ""}};
4744

4845
// Fill in the dependency derivation's output paths
4946
depDrv.fillInOutputPaths(*store);
@@ -64,14 +61,13 @@ TEST_F(FillInOutputPathsTest, fillsDeferredOutputs_emptyStringEnvVar)
6461
using nlohmann::json;
6562

6663
// Before: Derivation with deferred output
67-
Derivation drv;
68-
drv.name = "filled-in-deferred-empty-env-var";
69-
drv.platform = "x86_64-linux";
70-
drv.builder = "/bin/sh";
71-
drv.outputs = {
72-
{"out", DerivationOutput{DerivationOutput::Deferred{}}},
64+
Derivation drv{
65+
.outputs = {{"out", DerivationOutput{DerivationOutput::Deferred{}}}},
66+
.platform = "x86_64-linux",
67+
.builder = "/bin/sh",
68+
.env = {{"__doc", "Fill in deferred output with empty env var"}, {"out", ""}},
69+
.name = "filled-in-deferred-empty-env-var",
7370
};
74-
drv.env = {{"__doc", "Fill in deferred output with empty env var"}, {"out", ""}};
7571

7672
// Serialize before state
7773
checkpointJson("filled-in-deferred-empty-env-var-pre", drv);
@@ -95,15 +91,12 @@ TEST_F(FillInOutputPathsTest, fillsDeferredOutputs_empty_string_var)
9591
using nlohmann::json;
9692

9793
// Before: Derivation with deferred output
98-
Derivation drv;
99-
drv.name = "filled-in-deferred-no-env-var";
100-
drv.platform = "x86_64-linux";
101-
drv.builder = "/bin/sh";
102-
drv.outputs = {
103-
{"out", DerivationOutput{DerivationOutput::Deferred{}}},
104-
};
105-
drv.env = {
106-
{"__doc", "Fill in deferred with missing env var"},
94+
Derivation drv{
95+
.outputs = {{"out", DerivationOutput{DerivationOutput::Deferred{}}}},
96+
.platform = "x86_64-linux",
97+
.builder = "/bin/sh",
98+
.env = {{"__doc", "Fill in deferred with missing env var"}},
99+
.name = "filled-in-deferred-no-env-var",
107100
};
108101

109102
// Serialize before state
@@ -127,16 +120,12 @@ TEST_F(FillInOutputPathsTest, preservesInputAddressedOutputs)
127120
{
128121
auto expectedPath = StorePath{"w4bk7hpyxzgy2gx8fsa8f952435pll3i-filled-in-already"};
129122

130-
Derivation drv;
131-
drv.name = "filled-in-already";
132-
drv.platform = "x86_64-linux";
133-
drv.builder = "/bin/sh";
134-
drv.outputs = {
135-
{"out", DerivationOutput{DerivationOutput::InputAddressed{.path = expectedPath}}},
136-
};
137-
drv.env = {
138-
{"__doc", "Correct path stays unchanged"},
139-
{"out", store->printStorePath(expectedPath)},
123+
Derivation drv{
124+
.outputs = {{"out", DerivationOutput{DerivationOutput::InputAddressed{.path = expectedPath}}}},
125+
.platform = "x86_64-linux",
126+
.builder = "/bin/sh",
127+
.env = {{"__doc", "Correct path stays unchanged"}, {"out", store->printStorePath(expectedPath)}},
128+
.name = "filled-in-already",
140129
};
141130

142131
// Serialize before state
@@ -154,16 +143,12 @@ TEST_F(FillInOutputPathsTest, throwsOnIncorrectInputAddressedPath)
154143
{
155144
auto wrongPath = StorePath{"c015dhfh5l0lp6wxyvdn7bmwhbbr6hr9-wrong-name"};
156145

157-
Derivation drv;
158-
drv.name = "bad-path";
159-
drv.platform = "x86_64-linux";
160-
drv.builder = "/bin/sh";
161-
drv.outputs = {
162-
{"out", DerivationOutput{DerivationOutput::InputAddressed{.path = wrongPath}}},
163-
};
164-
drv.env = {
165-
{"__doc", "Wrong InputAddressed path throws error"},
166-
{"out", store->printStorePath(wrongPath)},
146+
Derivation drv{
147+
.outputs = {{"out", DerivationOutput{DerivationOutput::InputAddressed{.path = wrongPath}}}},
148+
.platform = "x86_64-linux",
149+
.builder = "/bin/sh",
150+
.env = {{"__doc", "Wrong InputAddressed path throws error"}, {"out", store->printStorePath(wrongPath)}},
151+
.name = "bad-path",
167152
};
168153

169154
// Serialize before state
@@ -177,16 +162,12 @@ TEST_F(FillInOutputPathsTest, throwsOnIncorrectEnvVar)
177162
{
178163
auto wrongPath = StorePath{"c015dhfh5l0lp6wxyvdn7bmwhbbr6hr9-wrong-name"};
179164

180-
Derivation drv;
181-
drv.name = "bad-env-var";
182-
drv.platform = "x86_64-linux";
183-
drv.builder = "/bin/sh";
184-
drv.outputs = {
185-
{"out", DerivationOutput{DerivationOutput::Deferred{}}},
186-
};
187-
drv.env = {
188-
{"__doc", "Wrong env var value throws error"},
189-
{"out", store->printStorePath(wrongPath)},
165+
Derivation drv{
166+
.outputs = {{"out", DerivationOutput{DerivationOutput::Deferred{}}}},
167+
.platform = "x86_64-linux",
168+
.builder = "/bin/sh",
169+
.env = {{"__doc", "Wrong env var value throws error"}, {"out", store->printStorePath(wrongPath)}},
170+
.name = "bad-env-var",
190171
};
191172

192173
// Serialize before state
@@ -204,19 +185,14 @@ TEST_F(FillInOutputPathsTest, preservesDeferredWithInputDrvs)
204185
auto depDrvPath = makeCAFloatingDependency("dependency");
205186

206187
// Create a derivation that depends on the dependency
207-
Derivation drv;
208-
drv.name = "depends-on-drv";
209-
drv.platform = "x86_64-linux";
210-
drv.builder = "/bin/sh";
211-
drv.outputs = {
212-
{"out", DerivationOutput{DerivationOutput::Deferred{}}},
213-
};
214-
drv.env = {
215-
{"__doc", "Deferred stays deferred with CA dependencies"},
216-
{"out", ""},
188+
Derivation drv{
189+
.outputs = {{"out", DerivationOutput{DerivationOutput::Deferred{}}}},
190+
.inputs = {.drvs = {.map = {{depDrvPath, {.value = {"out"}}}}}},
191+
.platform = "x86_64-linux",
192+
.builder = "/bin/sh",
193+
.env = {{"__doc", "Deferred stays deferred with CA dependencies"}, {"out", ""}},
194+
.name = "depends-on-drv",
217195
};
218-
// Add the real input derivation dependency
219-
drv.inputs.drvs = {.map = {{depDrvPath, {.value = {"out"}}}}};
220196

221197
// Serialize before state
222198
checkpointJson("depends-on-drv-pre", drv);
@@ -240,19 +216,14 @@ TEST_F(FillInOutputPathsTest, throwsOnPatWhenShouldBeDeffered)
240216
auto wrongPath = StorePath{"c015dhfh5l0lp6wxyvdn7bmwhbbr6hr9-wrong-name"};
241217

242218
// Create a derivation that depends on the dependency
243-
Derivation drv;
244-
drv.name = "depends-on-drv";
245-
drv.platform = "x86_64-linux";
246-
drv.builder = "/bin/sh";
247-
drv.outputs = {
248-
{"out", DerivationOutput{DerivationOutput::InputAddressed{.path = wrongPath}}},
249-
};
250-
drv.env = {
251-
{"__doc", "InputAddressed throws when should be deferred"},
252-
{"out", ""},
219+
Derivation drv{
220+
.outputs = {{"out", DerivationOutput{DerivationOutput::InputAddressed{.path = wrongPath}}}},
221+
.inputs = {.drvs = {.map = {{depDrvPath, {.value = {"out"}}}}}},
222+
.platform = "x86_64-linux",
223+
.builder = "/bin/sh",
224+
.env = {{"__doc", "InputAddressed throws when should be deferred"}, {"out", ""}},
225+
.name = "depends-on-drv",
253226
};
254-
// Add the real input derivation dependency
255-
drv.inputs.drvs = {.map = {{depDrvPath, {.value = {"out"}}}}};
256227

257228
// Serialize before state
258229
checkpointJson("bad-depends-on-drv-pre", drv);

0 commit comments

Comments
 (0)