|
| 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 | +}); |
0 commit comments