|
| 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 | + |
| 8 | +import { |
| 9 | + parseTasks, |
| 10 | + parseSubtasks, |
| 11 | + parseOptions, |
| 12 | + formatOptionName, |
| 13 | + getLongestNameLength, |
| 14 | + getSection, |
| 15 | + getUsageString, |
| 16 | +} from "../../../../src/internal/cli/helpers/utils.js"; |
| 17 | + |
| 18 | +describe("utils", function () { |
| 19 | + describe("parseTasks", function () { |
| 20 | + it("should return tasks and subtasks", function () { |
| 21 | + const task: Task = { |
| 22 | + id: ["task"], |
| 23 | + description: "task description", |
| 24 | + actions: [{ pluginId: "task-plugin-id", action: () => {} }], |
| 25 | + options: new Map(), |
| 26 | + positionalParameters: [], |
| 27 | + pluginId: "task-plugin-id", |
| 28 | + subtasks: new Map().set("subtask", { |
| 29 | + id: ["task", "subtask"], |
| 30 | + description: "An example empty subtask task", |
| 31 | + isEmpty: false, |
| 32 | + run: async () => {}, |
| 33 | + }), |
| 34 | + isEmpty: false, |
| 35 | + run: async () => {}, |
| 36 | + }; |
| 37 | + |
| 38 | + const result = parseTasks(new Map().set("task", task)); |
| 39 | + |
| 40 | + assert.deepEqual(result, { |
| 41 | + tasks: [{ name: "task", description: "task description" }], |
| 42 | + subtasks: [ |
| 43 | + { |
| 44 | + name: "task subtask", |
| 45 | + description: "An example empty subtask task", |
| 46 | + }, |
| 47 | + ], |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + it("should not include empty tasks in the tasks list", function () { |
| 52 | + const task: Task = { |
| 53 | + id: ["task"], |
| 54 | + description: "task description", |
| 55 | + actions: [{ pluginId: "task-plugin-id", action: () => {} }], |
| 56 | + options: new Map(), |
| 57 | + positionalParameters: [], |
| 58 | + pluginId: "task-plugin-id", |
| 59 | + subtasks: new Map().set("subtask", { |
| 60 | + id: ["task", "subtask"], |
| 61 | + description: "An example empty subtask task", |
| 62 | + isEmpty: false, |
| 63 | + run: async () => {}, |
| 64 | + }), |
| 65 | + isEmpty: true, |
| 66 | + run: async () => {}, |
| 67 | + }; |
| 68 | + |
| 69 | + const result = parseTasks(new Map().set("task", task)); |
| 70 | + |
| 71 | + assert.deepEqual(result, { |
| 72 | + tasks: [], |
| 73 | + subtasks: [ |
| 74 | + { |
| 75 | + name: "task subtask", |
| 76 | + description: "An example empty subtask task", |
| 77 | + }, |
| 78 | + ], |
| 79 | + }); |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + describe("parseSubtasks", function () { |
| 84 | + it("should return subtasks", function () { |
| 85 | + const task: Task = { |
| 86 | + id: ["task"], |
| 87 | + description: "task description", |
| 88 | + actions: [{ pluginId: "task-plugin-id", action: () => {} }], |
| 89 | + options: new Map(), |
| 90 | + positionalParameters: [], |
| 91 | + pluginId: "task-plugin-id", |
| 92 | + subtasks: new Map().set("subtask", { |
| 93 | + id: ["task", "subtask"], |
| 94 | + description: "An example empty subtask task", |
| 95 | + isEmpty: false, |
| 96 | + run: async () => {}, |
| 97 | + }), |
| 98 | + isEmpty: false, |
| 99 | + run: async () => {}, |
| 100 | + }; |
| 101 | + |
| 102 | + const result = parseSubtasks(task); |
| 103 | + |
| 104 | + assert.deepEqual(result, [ |
| 105 | + { |
| 106 | + name: "task subtask", |
| 107 | + description: "An example empty subtask task", |
| 108 | + }, |
| 109 | + ]); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + describe("parseOptions", function () { |
| 114 | + it("should return options and positional arguments", function () { |
| 115 | + const task: Task = { |
| 116 | + id: ["task"], |
| 117 | + description: "task description", |
| 118 | + actions: [{ pluginId: "task-plugin-id", action: () => {} }], |
| 119 | + options: new Map() |
| 120 | + .set("option", { |
| 121 | + name: "option", |
| 122 | + description: "An example option", |
| 123 | + parameterType: ParameterType.STRING, |
| 124 | + }) |
| 125 | + .set("anotherOption", { |
| 126 | + name: "anotherOption", |
| 127 | + description: "Another example option", |
| 128 | + parameterType: ParameterType.BOOLEAN, |
| 129 | + }), |
| 130 | + positionalParameters: [ |
| 131 | + { |
| 132 | + name: "positionalArgument", |
| 133 | + description: "An example argument", |
| 134 | + parameterType: ParameterType.STRING, |
| 135 | + isVariadic: false, |
| 136 | + }, |
| 137 | + { |
| 138 | + name: "anotherPositionalArgument", |
| 139 | + description: "Another example argument", |
| 140 | + parameterType: ParameterType.STRING, |
| 141 | + isVariadic: false, |
| 142 | + defaultValue: "default", |
| 143 | + }, |
| 144 | + ], |
| 145 | + pluginId: "task-plugin-id", |
| 146 | + subtasks: new Map(), |
| 147 | + isEmpty: false, |
| 148 | + run: async () => {}, |
| 149 | + }; |
| 150 | + |
| 151 | + const result = parseOptions(task); |
| 152 | + |
| 153 | + assert.deepEqual(result, { |
| 154 | + options: [ |
| 155 | + { |
| 156 | + name: "--option", |
| 157 | + description: "An example option", |
| 158 | + type: "STRING", |
| 159 | + }, |
| 160 | + { |
| 161 | + name: "--another-option", |
| 162 | + description: "Another example option", |
| 163 | + type: "BOOLEAN", |
| 164 | + }, |
| 165 | + ], |
| 166 | + positionalArguments: [ |
| 167 | + { |
| 168 | + name: "positionalArgument", |
| 169 | + description: "An example argument", |
| 170 | + isRequired: true, |
| 171 | + }, |
| 172 | + { |
| 173 | + name: "anotherPositionalArgument", |
| 174 | + description: "Another example argument", |
| 175 | + isRequired: false, |
| 176 | + }, |
| 177 | + ], |
| 178 | + }); |
| 179 | + }); |
| 180 | + }); |
| 181 | + |
| 182 | + describe("formatOptionName", function () { |
| 183 | + it("should format option names", function () { |
| 184 | + assert.equal(formatOptionName("option"), "--option"); |
| 185 | + assert.equal(formatOptionName("anotherOption"), "--another-option"); |
| 186 | + }); |
| 187 | + }); |
| 188 | + |
| 189 | + describe("getLongestNameLength", function () { |
| 190 | + it("should return the length of the longest name", function () { |
| 191 | + assert.equal( |
| 192 | + getLongestNameLength([{ name: "name" }, { name: "anotherName" }]), |
| 193 | + 11, |
| 194 | + ); |
| 195 | + }); |
| 196 | + }); |
| 197 | + |
| 198 | + describe("getSection", function () { |
| 199 | + it("should return a section", function () { |
| 200 | + const section = getSection( |
| 201 | + "Section Title", |
| 202 | + [ |
| 203 | + { name: "content", description: "content description" }, |
| 204 | + { name: "content2", description: "content description2" }, |
| 205 | + { name: "content3", description: "content description3" }, |
| 206 | + ], |
| 207 | + 14, |
| 208 | + ); |
| 209 | + |
| 210 | + const expected = ` |
| 211 | +Section Title: |
| 212 | +
|
| 213 | + content content description |
| 214 | + content2 content description2 |
| 215 | + content3 content description3 |
| 216 | +`; |
| 217 | + |
| 218 | + assert.equal(section, expected); |
| 219 | + }); |
| 220 | + }); |
| 221 | + |
| 222 | + describe("getUsageString", function () { |
| 223 | + describe("with a required positional parameter", function () { |
| 224 | + it("should return a usage string", function () { |
| 225 | + const task: Task = { |
| 226 | + id: ["task"], |
| 227 | + description: "task description", |
| 228 | + actions: [{ pluginId: "task-plugin-id", action: () => {} }], |
| 229 | + options: new Map() |
| 230 | + .set("option", { |
| 231 | + name: "option", |
| 232 | + description: "An example option", |
| 233 | + parameterType: ParameterType.STRING, |
| 234 | + }) |
| 235 | + .set("anotherOption", { |
| 236 | + name: "anotherOption", |
| 237 | + description: "Another example option", |
| 238 | + parameterType: ParameterType.BOOLEAN, |
| 239 | + }), |
| 240 | + positionalParameters: [ |
| 241 | + { |
| 242 | + name: "positionalArgument", |
| 243 | + description: "An example argument", |
| 244 | + parameterType: ParameterType.STRING, |
| 245 | + isVariadic: false, |
| 246 | + }, |
| 247 | + ], |
| 248 | + pluginId: "task-plugin-id", |
| 249 | + subtasks: new Map(), |
| 250 | + isEmpty: false, |
| 251 | + run: async () => {}, |
| 252 | + }; |
| 253 | + |
| 254 | + const usageString = getUsageString( |
| 255 | + task, |
| 256 | + [ |
| 257 | + { |
| 258 | + name: "--option", |
| 259 | + description: "An example option", |
| 260 | + type: ParameterType.STRING, |
| 261 | + }, |
| 262 | + { |
| 263 | + name: "--another-option", |
| 264 | + description: "Another example option", |
| 265 | + type: ParameterType.BOOLEAN, |
| 266 | + }, |
| 267 | + ], |
| 268 | + [ |
| 269 | + { |
| 270 | + name: "positionalArgument", |
| 271 | + description: "An example argument", |
| 272 | + isRequired: true, |
| 273 | + }, |
| 274 | + ], |
| 275 | + ); |
| 276 | + |
| 277 | + const expected = `Usage: hardhat [GLOBAL OPTIONS] task [--option <STRING>] [--another-option] [--] positionalArgument`; |
| 278 | + |
| 279 | + assert.equal(usageString, expected); |
| 280 | + }); |
| 281 | + }); |
| 282 | + |
| 283 | + describe("with an optional positional parameter", function () { |
| 284 | + it("should return a usage string", function () { |
| 285 | + const task: Task = { |
| 286 | + id: ["task"], |
| 287 | + description: "task description", |
| 288 | + actions: [{ pluginId: "task-plugin-id", action: () => {} }], |
| 289 | + options: new Map() |
| 290 | + .set("option", { |
| 291 | + name: "option", |
| 292 | + description: "An example option", |
| 293 | + parameterType: ParameterType.STRING, |
| 294 | + }) |
| 295 | + .set("anotherOption", { |
| 296 | + name: "anotherOption", |
| 297 | + description: "Another example option", |
| 298 | + parameterType: ParameterType.BOOLEAN, |
| 299 | + }), |
| 300 | + positionalParameters: [ |
| 301 | + { |
| 302 | + name: "positionalArgument", |
| 303 | + description: "An example argument", |
| 304 | + parameterType: ParameterType.STRING, |
| 305 | + isVariadic: false, |
| 306 | + }, |
| 307 | + ], |
| 308 | + pluginId: "task-plugin-id", |
| 309 | + subtasks: new Map(), |
| 310 | + isEmpty: false, |
| 311 | + run: async () => {}, |
| 312 | + }; |
| 313 | + |
| 314 | + const usageString = getUsageString( |
| 315 | + task, |
| 316 | + [ |
| 317 | + { |
| 318 | + name: "--option", |
| 319 | + description: "An example option", |
| 320 | + type: ParameterType.STRING, |
| 321 | + }, |
| 322 | + { |
| 323 | + name: "--another-option", |
| 324 | + description: "Another example option", |
| 325 | + type: ParameterType.BOOLEAN, |
| 326 | + }, |
| 327 | + ], |
| 328 | + [ |
| 329 | + { |
| 330 | + name: "positionalArgument", |
| 331 | + description: "An example argument", |
| 332 | + isRequired: false, |
| 333 | + }, |
| 334 | + ], |
| 335 | + ); |
| 336 | + |
| 337 | + const expected = `Usage: hardhat [GLOBAL OPTIONS] task [--option <STRING>] [--another-option] [--] [positionalArgument]`; |
| 338 | + |
| 339 | + assert.equal(usageString, expected); |
| 340 | + }); |
| 341 | + }); |
| 342 | + }); |
| 343 | +}); |
0 commit comments