-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib.test.ts
77 lines (71 loc) · 2.59 KB
/
lib.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { getCliArgs } from "./lib";
import { DEFAULT_COMMAND_NAME, COMMAND_NAME } from "./constants";
import type { RegisteredCommands, CommandOption, CommandOptions } from "./types";
import { describe, expect, it, mock } from "bun:test";
console.log = mock((...args: any[]) => {});
process.exit = mock((exitCode: number) => {}) as any;
describe("getCliArgs", () => {
const commandOptions: CommandOptions = {
option1: {
description: "Option 1",
type: "string",
required: true,
},
option2: {
description: "Option 2",
type: "boolean",
default: true,
},
};
const registeredCommands: RegisteredCommands = new Map();
registeredCommands.set("cmd1", {
name: "cmd1",
description: "Command 1",
options: commandOptions,
action: () => {},
registeredCommands: new Map(),
register: () => registeredCommands,
});
registeredCommands.set("cmd2", {
name: "cmd2",
description: "Command 2",
options: commandOptions,
action: () => {},
registeredCommands: new Map(),
register: () => registeredCommands,
});
it("should return an error when command is missing", () => {
expect(() => getCliArgs(registeredCommands, "test", ["cmd3"])).toThrow(/missing command/);
});
it("should return an error when required option is missing", () => {
expect(() => getCliArgs(registeredCommands, "test", ["cmd1"])).toThrow(/missing required/);
});
it("should return parsed args when all required options are provided", () => {
// `$ test command1 --option1 op`
const args = getCliArgs(registeredCommands, "test", ["cmd1", "--option1", "op"]);
expect(args).toEqual({
option1: "op",
option2: true,
[COMMAND_NAME]: "cmd1",
usingDefaultCommand: false,
});
});
it("should return parsed args for default command when no command is specified", () => {
registeredCommands.set(DEFAULT_COMMAND_NAME, {
name: "def",
description: "Default command",
options: commandOptions,
action: () => {},
registeredCommands: new Map(),
register: () => registeredCommands,
});
// `$ test --option1 op`
const args = getCliArgs(registeredCommands, "test", ["--option1", "op"]);
expect(args).toEqual({
option1: "op",
option2: true,
[COMMAND_NAME]: "def",
usingDefaultCommand: true,
});
});
});