Skip to content

Commit e16155d

Browse files
Create an integration test to combine all the parsing steps (global parameters, named parameters, positional, etc.) (#5380)
1 parent a72e883 commit e16155d

File tree

8 files changed

+342
-18
lines changed

8 files changed

+342
-18
lines changed

v-next/hardhat/src/internal/cli/main.ts

+10-16
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import {
3333
} from "../helpers/config-loading.js";
3434
import { getHardhatRuntimeEnvironmentSingleton } from "../hre-singleton.js";
3535

36-
export async function main(cliArguments: string[]) {
36+
export async function main(cliArguments: string[], print = console.log) {
3737
const hreInitStart = performance.now();
3838

3939
const usedCliArguments: boolean[] = new Array(cliArguments.length).fill(
@@ -46,7 +46,7 @@ export async function main(cliArguments: string[]) {
4646
);
4747

4848
if (hardhatSpecialArgs.version) {
49-
console.log("3.0.0");
49+
print("3.0.0");
5050
return;
5151
}
5252

@@ -83,7 +83,7 @@ export async function main(cliArguments: string[]) {
8383
);
8484

8585
const hreInitEnd = performance.now();
86-
console.log("Time to initialize the HRE (ms):", hreInitEnd - hreInitStart);
86+
print("Time to initialize the HRE (ms):", hreInitEnd - hreInitStart);
8787

8888
const taskParsingStart = performance.now();
8989

@@ -92,7 +92,7 @@ export async function main(cliArguments: string[]) {
9292
if (Array.isArray(result)) {
9393
if (result.length === 0) {
9494
// TODO: Print the global help
95-
console.log("Global help");
95+
print("Global help");
9696
return;
9797
}
9898

@@ -104,32 +104,26 @@ export async function main(cliArguments: string[]) {
104104
if (hardhatSpecialArgs.help) {
105105
if (task.isEmpty) {
106106
// TODO: Print information about its subtasks
107-
console.log("Info about subtasks");
107+
print("Info about subtasks");
108108
return;
109109
}
110110

111111
// TODO: Print the help message for this task
112-
console.log("Help message of the task");
112+
print("Help message of the task");
113113
return;
114114
}
115115

116116
const taskParsingEnd = performance.now();
117117

118-
console.log(
119-
"Time to parse the task (ms):",
120-
taskParsingEnd - taskParsingStart,
121-
);
118+
print("Time to parse the task (ms):", taskParsingEnd - taskParsingStart);
122119

123120
const taskRunningStart = performance.now();
124121

125122
await task.run(taskArguments);
126123

127124
const taskRunningEnd = performance.now();
128125

129-
console.log(
130-
"Time to run the task (ms):",
131-
taskRunningEnd - taskRunningStart,
132-
);
126+
print("Time to run the task (ms):", taskRunningEnd - taskRunningStart);
133127
} catch (error) {
134128
process.exitCode = 1;
135129

@@ -140,10 +134,10 @@ export async function main(cliArguments: string[]) {
140134

141135
// TODO: Print the errors nicely, especially `HardhatError`s.
142136

143-
console.log("Error running the task:", error.message);
137+
print("Error running the task:", error.message);
144138

145139
if (hardhatSpecialArgs.showStackTraces) {
146-
console.log("");
140+
print("");
147141
console.error(error);
148142
}
149143
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import type { HardhatUserConfig } from "@nomicfoundation/hardhat-core/config";
2+
3+
import { task } from "@nomicfoundation/hardhat-core/config";
4+
5+
export const tasksResults = {
6+
wasParam1Used: false,
7+
};
8+
9+
const customTask = task("task")
10+
.setAction(() => {
11+
tasksResults.wasParam1Used = true;
12+
})
13+
.build();
14+
15+
export default {
16+
tasks: [customTask],
17+
} satisfies HardhatUserConfig;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import type { HardhatUserConfig } from "@nomicfoundation/hardhat-core/config";
2+
3+
import { emptyTask } from "@nomicfoundation/hardhat-core/config";
4+
5+
const customTask = emptyTask("empty-task", "empty task description").build();
6+
7+
export default {
8+
tasks: [customTask],
9+
} satisfies HardhatUserConfig;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import type { HardhatUserConfig } from "@nomicfoundation/hardhat-core/config";
2+
3+
import { task } from "@nomicfoundation/hardhat-core/config";
4+
5+
export const tasksResults = {
6+
wasParam1Used: false,
7+
wasParam2Used: false,
8+
wasParam3Used: false,
9+
};
10+
11+
function resetResults() {
12+
tasksResults.wasParam1Used = false;
13+
tasksResults.wasParam2Used = false;
14+
tasksResults.wasParam3Used = false;
15+
}
16+
17+
const customTask = task("task")
18+
.addNamedParameter({ name: "param1" })
19+
.addPositionalParameter({ name: "param2" })
20+
.addVariadicParameter({ name: "param3" })
21+
.setAction((taskArguments) => {
22+
resetResults();
23+
24+
const { param1, param2, param3 } = taskArguments;
25+
26+
tasksResults.wasParam1Used = param1 === "<value1>";
27+
tasksResults.wasParam2Used = param2 === "<value2>";
28+
if (Array.isArray(param3)) {
29+
tasksResults.wasParam3Used = param3[0] === "<value3>"; // Variadic parameters are always in an array
30+
}
31+
})
32+
.build();
33+
34+
const customTask2 = task("task-default")
35+
.addNamedParameter({ name: "param1", defaultValue: "<default-value1>" })
36+
.setAction((taskArguments) => {
37+
resetResults();
38+
39+
const { param1 } = taskArguments;
40+
tasksResults.wasParam1Used = param1 === "<default-value1>";
41+
})
42+
.build();
43+
44+
const customSubtask = task(["task", "subtask"])
45+
.addNamedParameter({ name: "param1" })
46+
.addPositionalParameter({ name: "param2" })
47+
.addVariadicParameter({ name: "param3" })
48+
.setAction((taskArguments) => {
49+
resetResults();
50+
51+
const { param1, param2, param3 } = taskArguments;
52+
53+
tasksResults.wasParam1Used = param1 === "<value1>";
54+
tasksResults.wasParam2Used = param2 === "<value2>";
55+
if (Array.isArray(param3)) {
56+
tasksResults.wasParam3Used = param3[0] === "<value3>"; // Variadic parameters are always in an array
57+
}
58+
})
59+
.build();
60+
61+
const customSubtask2 = task(["task-default", "subtask-default"])
62+
.addNamedParameter({ name: "param1", defaultValue: "<default-value1>" })
63+
.setAction((taskArguments) => {
64+
resetResults();
65+
66+
const { param1 } = taskArguments;
67+
tasksResults.wasParam1Used = param1 === "<default-value1>";
68+
})
69+
.build();
70+
71+
export default {
72+
tasks: [customTask, customTask2, customSubtask, customSubtask2],
73+
} satisfies HardhatUserConfig;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import type { HardhatUserConfig } from "@nomicfoundation/hardhat-core/config";
2+
3+
export default {
4+
tasks: [],
5+
} satisfies HardhatUserConfig;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import type { HardhatUserConfig } from "@nomicfoundation/hardhat-core/config";
2+
3+
import { task } from "@nomicfoundation/hardhat-core/config";
4+
5+
export const tasksResults = {
6+
wasParam1Used: false,
7+
};
8+
9+
const customTask = task("user-task")
10+
.setAction(() => {
11+
tasksResults.wasParam1Used = true;
12+
})
13+
.build();
14+
15+
export default {
16+
tasks: [customTask],
17+
} satisfies HardhatUserConfig;

v-next/hardhat/test/fixture-projects/cli/type-validation/test.txt

-1
This file was deleted.

0 commit comments

Comments
 (0)