Skip to content

Commit 3b674d1

Browse files
committed
Add tests to resolveGlobalArguments
1 parent d84d424 commit 3b674d1

File tree

2 files changed

+229
-2
lines changed

2 files changed

+229
-2
lines changed

v-next/core/test/internal/global-parameters.ts

+199-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import { ParameterType } from "../../src/config.js";
77
import {
88
buildGlobalParametersMap,
99
buildGlobalParameterDefinition,
10+
resolveGlobalArguments,
1011
} from "../../src/internal/global-parameters.js";
1112
import { RESERVED_PARAMETER_NAMES } from "../../src/internal/parameters.js";
13+
import { createTestEnvManager } from "../utils.js";
1214

1315
describe("Global Parameters", () => {
1416
describe("buildGlobalParametersMap", () => {
@@ -240,7 +242,202 @@ describe("Global Parameters", () => {
240242
});
241243
});
242244

243-
describe.todo("resolveGlobalArguments", () => {
244-
// TODO: Implement tests.
245+
describe("resolveGlobalArguments", () => {
246+
const { setEnvVar } = createTestEnvManager();
247+
248+
it("should resolve to the default values when no arguments are provided", () => {
249+
const globalParametersMap = buildGlobalParametersMap([
250+
{
251+
id: "plugin1",
252+
globalParameters: [
253+
buildGlobalParameterDefinition({
254+
name: "param1",
255+
description: "param1 description",
256+
parameterType: ParameterType.BOOLEAN,
257+
defaultValue: true,
258+
}),
259+
buildGlobalParameterDefinition({
260+
name: "param2",
261+
description: "param2 description",
262+
defaultValue: "default",
263+
}),
264+
],
265+
},
266+
]);
267+
268+
const globalArguments = resolveGlobalArguments({}, globalParametersMap);
269+
270+
assert.deepEqual(globalArguments, {
271+
param1: true,
272+
param2: "default",
273+
});
274+
});
275+
276+
it("should resolve to the user provided arguments and env variables", () => {
277+
const globalParametersMap = buildGlobalParametersMap([
278+
{
279+
id: "plugin1",
280+
globalParameters: [
281+
buildGlobalParameterDefinition({
282+
name: "param1",
283+
description: "param1 description",
284+
parameterType: ParameterType.BOOLEAN,
285+
defaultValue: true,
286+
}),
287+
buildGlobalParameterDefinition({
288+
name: "param2",
289+
description: "param2 description",
290+
defaultValue: "default",
291+
}),
292+
buildGlobalParameterDefinition({
293+
name: "param3",
294+
description: "param3 description",
295+
parameterType: ParameterType.BIGINT,
296+
defaultValue: 0n,
297+
}),
298+
],
299+
},
300+
]);
301+
302+
setEnvVar("HARDHAT_PARAM3", "5n");
303+
304+
const globalArguments = resolveGlobalArguments(
305+
{
306+
param1: "false",
307+
param2: "user",
308+
},
309+
globalParametersMap,
310+
);
311+
312+
assert.deepEqual(globalArguments, {
313+
param1: false,
314+
param2: "user",
315+
param3: 5n,
316+
});
317+
});
318+
319+
it("should resolve to the user provided arguments over the environment variables", () => {
320+
const globalParametersMap = buildGlobalParametersMap([
321+
{
322+
id: "plugin1",
323+
globalParameters: [
324+
buildGlobalParameterDefinition({
325+
name: "param1",
326+
description: "param1 description",
327+
parameterType: ParameterType.BOOLEAN,
328+
defaultValue: true,
329+
}),
330+
buildGlobalParameterDefinition({
331+
name: "param2",
332+
description: "param2 description",
333+
defaultValue: "default",
334+
}),
335+
],
336+
},
337+
]);
338+
339+
setEnvVar("HARDHAT_PARAM2", "env");
340+
341+
const globalArguments = resolveGlobalArguments(
342+
{
343+
param1: "false",
344+
param2: "user",
345+
},
346+
globalParametersMap,
347+
);
348+
349+
assert.deepEqual(globalArguments, {
350+
param1: false,
351+
param2: "user",
352+
});
353+
});
354+
355+
it("should ignore arguments that are not defined in the global parameters map", () => {
356+
const globalParametersMap = buildGlobalParametersMap([
357+
{
358+
id: "plugin1",
359+
globalParameters: [
360+
buildGlobalParameterDefinition({
361+
name: "param1",
362+
description: "param1 description",
363+
parameterType: ParameterType.BOOLEAN,
364+
defaultValue: true,
365+
}),
366+
],
367+
},
368+
]);
369+
370+
setEnvVar("HARDHAT_PARAM3", "env");
371+
372+
const globalArguments = resolveGlobalArguments(
373+
{
374+
param1: "false",
375+
param2: "user",
376+
},
377+
globalParametersMap,
378+
);
379+
380+
assert.deepEqual(globalArguments, {
381+
param1: false,
382+
});
383+
});
384+
385+
it("should throw if the provided argument is not valid", () => {
386+
const globalParametersMap = buildGlobalParametersMap([
387+
{
388+
id: "plugin1",
389+
globalParameters: [
390+
buildGlobalParameterDefinition({
391+
name: "param1",
392+
description: "param1 description",
393+
parameterType: ParameterType.BOOLEAN,
394+
defaultValue: true,
395+
}),
396+
],
397+
},
398+
]);
399+
400+
assert.throws(
401+
() =>
402+
resolveGlobalArguments(
403+
{
404+
param1: "not a boolean",
405+
},
406+
globalParametersMap,
407+
),
408+
new HardhatError(HardhatError.ERRORS.ARGUMENTS.INVALID_VALUE_FOR_TYPE, {
409+
value: "not a boolean",
410+
name: "param1",
411+
type: ParameterType.BOOLEAN,
412+
}),
413+
);
414+
});
415+
416+
it("should throw if the environment variable is not valid", () => {
417+
const globalParametersMap = buildGlobalParametersMap([
418+
{
419+
id: "plugin1",
420+
globalParameters: [
421+
buildGlobalParameterDefinition({
422+
name: "param1",
423+
description: "param1 description",
424+
parameterType: ParameterType.BOOLEAN,
425+
defaultValue: true,
426+
}),
427+
],
428+
},
429+
]);
430+
431+
setEnvVar("HARDHAT_PARAM1", "not a boolean");
432+
433+
assert.throws(
434+
() => resolveGlobalArguments({}, globalParametersMap),
435+
new HardhatError(HardhatError.ERRORS.ARGUMENTS.INVALID_VALUE_FOR_TYPE, {
436+
value: "not a boolean",
437+
name: "param1",
438+
type: ParameterType.BOOLEAN,
439+
}),
440+
);
441+
});
245442
});
246443
});

v-next/core/test/utils.ts

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { afterEach } from "node:test";
2+
3+
export function createTestEnvManager() {
4+
const changes = new Set<string>();
5+
const originalValues = new Map<string, string | undefined>();
6+
7+
afterEach(() => {
8+
// Revert changes to process.env based on the originalValues Map entries
9+
changes.forEach((key) => {
10+
const originalValue = originalValues.get(key);
11+
if (originalValue === undefined) {
12+
delete process.env[key];
13+
} else {
14+
process.env[key] = originalValue;
15+
}
16+
});
17+
changes.clear();
18+
});
19+
20+
return {
21+
setEnvVar(name: string, value: string) {
22+
// Before setting a new value, save the original value if it hasn't been saved yet
23+
if (!changes.has(name)) {
24+
originalValues.set(name, process.env[name]);
25+
changes.add(name);
26+
}
27+
process.env[name] = value;
28+
},
29+
};
30+
}

0 commit comments

Comments
 (0)