Skip to content

Commit 32b6c4a

Browse files
committed
add tests for --help
1 parent 081de2d commit 32b6c4a

File tree

4 files changed

+634
-0
lines changed

4 files changed

+634
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
import type { Task } from "@nomicfoundation/hardhat-core/types/tasks";
2+
3+
import assert from "node:assert/strict";
4+
import { describe, it } from "node:test";
5+
6+
import packageJson from "../../../../package.json";
7+
import { getGlobalHelpString } from "../../../../src/internal/cli/helpers/getGlobalHelpString.js";
8+
9+
describe("getGlobalHelpString", function () {
10+
describe("when there are no tasks", function () {
11+
it("should return the global help string", async function () {
12+
const tasks = new Map();
13+
const help = await getGlobalHelpString(tasks);
14+
15+
const expected = `Hardhat version ${packageJson.version}
16+
17+
Usage: hardhat [GLOBAL OPTIONS] <TASK> [SUBTASK] [TASK OPTIONS] [--] [TASK ARGUMENTS]
18+
19+
GLOBAL OPTIONS:
20+
21+
--config A Hardhat config file.
22+
--help Shows this message, or a task's help if its name is provided
23+
--show-stack-traces Show stack traces (always enabled on CI servers).
24+
--version Shows hardhat's version.
25+
26+
To get help for a specific task run: npx hardhat <TASK> [SUBTASK] --help`;
27+
28+
assert.equal(help, expected);
29+
});
30+
});
31+
32+
describe("when there are tasks", function () {
33+
it("should return the global help string with the tasks", async function () {
34+
const tasks: Map<string, Task> = new Map([
35+
[
36+
"task1",
37+
{
38+
id: ["task1"],
39+
description: "task1 description",
40+
actions: [{ pluginId: "task1-plugin-id", action: () => {} }],
41+
namedParameters: new Map(),
42+
positionalParameters: [],
43+
pluginId: "task1-plugin-id",
44+
subtasks: new Map(),
45+
isEmpty: false,
46+
run: async () => {},
47+
},
48+
],
49+
[
50+
"task2",
51+
{
52+
id: ["task2"],
53+
description: "task2 description",
54+
actions: [{ pluginId: "task2-plugin-id", action: () => {} }],
55+
namedParameters: new Map(),
56+
positionalParameters: [],
57+
pluginId: "task2-plugin-id",
58+
subtasks: new Map(),
59+
isEmpty: false,
60+
run: async () => {},
61+
},
62+
],
63+
]);
64+
65+
const help = await getGlobalHelpString(tasks);
66+
67+
const expected = `Hardhat version ${packageJson.version}
68+
69+
Usage: hardhat [GLOBAL OPTIONS] <TASK> [SUBTASK] [TASK OPTIONS] [--] [TASK ARGUMENTS]
70+
71+
AVAILABLE TASKS:
72+
73+
task1 task1 description
74+
task2 task2 description
75+
76+
GLOBAL OPTIONS:
77+
78+
--config A Hardhat config file.
79+
--help Shows this message, or a task's help if its name is provided
80+
--show-stack-traces Show stack traces (always enabled on CI servers).
81+
--version Shows hardhat's version.
82+
83+
To get help for a specific task run: npx hardhat <TASK> [SUBTASK] --help`;
84+
85+
assert.equal(help, expected);
86+
});
87+
});
88+
89+
describe("when there are subtasks", function () {
90+
it("should return the global help string with the tasks", async function () {
91+
const tasks: Map<string, Task> = new Map([
92+
[
93+
"task1",
94+
{
95+
id: ["task1"],
96+
description: "task1 description",
97+
actions: [{ pluginId: "task1-plugin-id", action: () => {} }],
98+
namedParameters: new Map(),
99+
positionalParameters: [],
100+
pluginId: "task1-plugin-id",
101+
subtasks: new Map().set("subtask1", {
102+
id: ["task1", "subtask1"],
103+
description: "subtask1 description",
104+
actions: [{ pluginId: "task1-plugin-id", action: () => {} }],
105+
namedParameters: new Map(),
106+
positionalParameters: [],
107+
pluginId: "task1-plugin-id",
108+
subtasks: new Map(),
109+
isEmpty: false,
110+
run: async () => {},
111+
}),
112+
isEmpty: false,
113+
run: async () => {},
114+
},
115+
],
116+
[
117+
"task2",
118+
{
119+
id: ["task2"],
120+
description: "task2 description",
121+
actions: [{ pluginId: "task2-plugin-id", action: () => {} }],
122+
namedParameters: new Map(),
123+
positionalParameters: [],
124+
pluginId: "task2-plugin-id",
125+
subtasks: new Map(),
126+
isEmpty: false,
127+
run: async () => {},
128+
},
129+
],
130+
]);
131+
132+
const help = await getGlobalHelpString(tasks);
133+
134+
const expected = `Hardhat version ${packageJson.version}
135+
136+
Usage: hardhat [GLOBAL OPTIONS] <TASK> [SUBTASK] [TASK OPTIONS] [--] [TASK ARGUMENTS]
137+
138+
AVAILABLE TASKS:
139+
140+
task1 task1 description
141+
task2 task2 description
142+
143+
AVAILABLE SUBTASKS:
144+
145+
task1 subtask1 subtask1 description
146+
147+
GLOBAL OPTIONS:
148+
149+
--config A Hardhat config file.
150+
--help Shows this message, or a task's help if its name is provided
151+
--show-stack-traces Show stack traces (always enabled on CI servers).
152+
--version Shows hardhat's version.
153+
154+
To get help for a specific task run: npx hardhat <TASK> [SUBTASK] --help`;
155+
156+
assert.equal(help, expected);
157+
});
158+
});
159+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
import type { Task } from "@nomicfoundation/hardhat-core/types/tasks";
2+
3+
import assert from "node:assert/strict";
4+
import { describe, it } from "node:test";
5+
6+
import { ParameterType } from "@nomicfoundation/hardhat-core/config";
7+
import chalk from "chalk";
8+
9+
import { getHelpString } from "../../../../src/internal/cli/helpers/getHelpString.js";
10+
11+
describe("getHelpString", function () {
12+
describe("when the task is empty", function () {
13+
it("should return the task's help string", async function () {
14+
const task: Task = {
15+
id: ["task"],
16+
description: "task description",
17+
actions: [{ pluginId: "task-plugin-id", action: () => {} }],
18+
namedParameters: new Map(),
19+
positionalParameters: [],
20+
pluginId: "task-plugin-id",
21+
subtasks: new Map().set("subtask", {
22+
id: ["task", "subtask"],
23+
description: "An example empty subtask task",
24+
isEmpty: false,
25+
run: async () => {},
26+
}),
27+
isEmpty: true,
28+
run: async () => {},
29+
};
30+
31+
const help = await getHelpString(task);
32+
33+
const expected = `${chalk.bold("task description")}
34+
35+
Usage: hardhat [GLOBAL OPTIONS] task <SUBTASK> [SUBTASK OPTIONS] [--] [SUBTASK POSITIONAL ARGUMENTS]
36+
37+
AVAILABLE SUBTASKS:
38+
39+
task subtask An example empty subtask task
40+
41+
To get help for a specific task run: npx hardhat task <SUBTASK> --help`;
42+
43+
assert.equal(help, expected);
44+
});
45+
});
46+
47+
describe("when the task is not empty", function () {
48+
describe("when there are options", function () {
49+
it("should return the task's help string", async function () {
50+
const task: Task = {
51+
id: ["task"],
52+
description: "task description",
53+
actions: [{ pluginId: "task-plugin-id", action: () => {} }],
54+
namedParameters: new Map()
55+
.set("option", {
56+
name: "option",
57+
description: "An example option",
58+
parameterType: "STRING",
59+
})
60+
.set("anotherOption", {
61+
name: "anotherOption",
62+
description: "Another example option",
63+
parameterType: "BOOLEAN",
64+
}),
65+
positionalParameters: [],
66+
pluginId: "task-plugin-id",
67+
subtasks: new Map(),
68+
isEmpty: false,
69+
run: async () => {},
70+
};
71+
72+
const help = await getHelpString(task);
73+
74+
const expected = `${chalk.bold("task description")}
75+
76+
Usage: hardhat [GLOBAL OPTIONS] task [--option <STRING>] [--another-option]
77+
78+
OPTIONS:
79+
80+
--option An example option
81+
--another-option Another example option
82+
83+
For global options help run: hardhat --help`;
84+
85+
assert.equal(help, expected);
86+
});
87+
});
88+
89+
describe("when there are positional arguments", function () {
90+
it("should return the task's help string", async function () {
91+
const task: Task = {
92+
id: ["task"],
93+
description: "task description",
94+
actions: [{ pluginId: "task-plugin-id", action: () => {} }],
95+
namedParameters: new Map()
96+
.set("option", {
97+
name: "option",
98+
description: "An example option",
99+
parameterType: "STRING",
100+
})
101+
.set("anotherOption", {
102+
name: "anotherOption",
103+
description: "Another example option",
104+
parameterType: "BOOLEAN",
105+
}),
106+
positionalParameters: [
107+
{
108+
name: "positionalArgument",
109+
description: "An example positional argument",
110+
parameterType: ParameterType.STRING,
111+
isVariadic: false,
112+
},
113+
],
114+
pluginId: "task-plugin-id",
115+
subtasks: new Map(),
116+
isEmpty: false,
117+
run: async () => {},
118+
};
119+
120+
const help = await getHelpString(task);
121+
122+
const expected = `${chalk.bold("task description")}
123+
124+
Usage: hardhat [GLOBAL OPTIONS] task [--option <STRING>] [--another-option] [--] positionalArgument
125+
126+
OPTIONS:
127+
128+
--option An example option
129+
--another-option Another example option
130+
131+
POSITIONAL ARGUMENTS:
132+
133+
positionalArgument An example positional argument
134+
135+
For global options help run: hardhat --help`;
136+
137+
assert.equal(help, expected);
138+
});
139+
});
140+
141+
describe("when there are subtasks", function () {
142+
it("should return the task's help string", async function () {
143+
const task: Task = {
144+
id: ["task"],
145+
description: "task description",
146+
actions: [{ pluginId: "task-plugin-id", action: () => {} }],
147+
namedParameters: new Map()
148+
.set("option", {
149+
name: "option",
150+
description: "An example option",
151+
parameterType: "STRING",
152+
})
153+
.set("anotherOption", {
154+
name: "anotherOption",
155+
description: "Another example option",
156+
parameterType: "BOOLEAN",
157+
}),
158+
positionalParameters: [
159+
{
160+
name: "positionalArgument",
161+
description: "An example positional argument",
162+
parameterType: ParameterType.STRING,
163+
isVariadic: false,
164+
},
165+
],
166+
pluginId: "task-plugin-id",
167+
subtasks: new Map().set("subtask", {
168+
id: ["task", "subtask"],
169+
description: "An example subtask",
170+
}),
171+
isEmpty: false,
172+
run: async () => {},
173+
};
174+
175+
const help = await getHelpString(task);
176+
177+
const expected = `${chalk.bold("task description")}
178+
179+
Usage: hardhat [GLOBAL OPTIONS] task [--option <STRING>] [--another-option] [--] positionalArgument
180+
181+
OPTIONS:
182+
183+
--option An example option
184+
--another-option Another example option
185+
186+
POSITIONAL ARGUMENTS:
187+
188+
positionalArgument An example positional argument
189+
190+
AVAILABLE SUBTASKS:
191+
192+
task subtask An example subtask
193+
194+
For global options help run: hardhat --help`;
195+
196+
assert.equal(help, expected);
197+
});
198+
});
199+
});
200+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import assert from "node:assert/strict";
2+
import { describe, it } from "node:test";
3+
4+
import packageJson from "../../../../package.json";
5+
import { getVersionString } from "../../../../src/internal/cli/helpers/getVersionString.js";
6+
7+
describe("getVersionString", function () {
8+
it("should return the version string", function () {
9+
const versionString = getVersionString();
10+
11+
assert.equal(versionString, `Hardhat version ${packageJson.version}`);
12+
});
13+
});

0 commit comments

Comments
 (0)