-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathargs.test.ts
More file actions
49 lines (40 loc) · 1.82 KB
/
Copy pathargs.test.ts
File metadata and controls
49 lines (40 loc) · 1.82 KB
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
import { describe, expect, it } from "vitest";
import { extractFlag, hasHelpFlag } from "./args.js";
describe("hasHelpFlag", () => {
it.each(["-h", "--help"])("detects %s", (flag) => {
expect(hasHelpFlag(["sub", flag])).toBe(true);
});
it("returns false when no help flag is present", () => {
expect(hasHelpFlag(["set", "key", "value"])).toBe(false);
expect(hasHelpFlag([])).toBe(false);
});
it("treats a help flag after -- as an operand, not a request for help", () => {
expect(hasHelpFlag(["set", "key", "--", "-h"])).toBe(false);
});
});
describe("extractFlag", () => {
it("extracts a --flag value pair and removes both from rest", () => {
const result = extractFlag(["--name", "acme", "positional"], "name");
expect(result).toEqual({ value: "acme", rest: ["positional"] });
});
it("extracts a --flag=value form", () => {
const result = extractFlag(["--name=acme", "positional"], "name");
expect(result).toEqual({ value: "acme", rest: ["positional"] });
});
it("returns undefined value when the flag is missing", () => {
const result = extractFlag(["positional"], "name");
expect(result).toEqual({ value: undefined, rest: ["positional"] });
});
it("keeps the last occurrence when the flag is repeated", () => {
const result = extractFlag(["--name", "first", "--name", "second"], "name");
expect(result).toEqual({ value: "second", rest: [] });
});
it("does not confuse a similarly prefixed flag with the target flag", () => {
const result = extractFlag(["--nameOther", "x", "--name", "y"], "name");
expect(result).toEqual({ value: "y", rest: ["--nameOther", "x"] });
});
it("returns an empty rest and undefined value for an empty args list", () => {
const result = extractFlag([], "name");
expect(result).toEqual({ value: undefined, rest: [] });
});
});