-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcli.spec.js
125 lines (92 loc) · 3.81 KB
/
cli.spec.js
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"use strict";
const myLibrary = require("../utils/my-cli");
const manifest = require("../../package.json");
const { expect } = require("chai");
describe("my-cli", () => {
it("should run without any arguments", () => {
// Run the CLI without any arguments.
let cli = myLibrary("");
// It should have printed the default greeting
expect(cli).to.have.stdout("Hello, world.\n");
});
it("should error if an invalid argument is used", () => {
let cli = myLibrary("--fizzbuzz");
expect(cli).to.have.exitCode(9);
expect(cli).to.have.stdout("");
expect(cli).to.have.stderr.that.matches(/^Unknown option: --fizzbuzz\n\nUsage: my-cli \[options\] \[files...\]\n/);
});
it("should error if an invalid shorthand argument is used", () => {
let cli = myLibrary("-qhzt");
expect(cli).to.have.exitCode(9);
expect(cli).to.have.stdout("");
expect(cli).to.have.stderr.that.matches(/^Unknown option: -z\n\nUsage: my-cli \[options\] \[files...\]\n/);
});
it("should error if an argument is missing its value", () => {
let cli = myLibrary("--subject");
expect(cli).to.have.exitCode(9);
expect(cli).to.have.stdout("");
expect(cli).to.have.stderr.that.matches(/^The --subject option requires a value\.\n\nUsage: my-cli \[options\] \[files...\]\n/);
});
it("should print a more detailed error if DEBUG is set", () => {
let cli = myLibrary("--greeting Goodbye", { env: { DEBUG: "true" }});
expect(cli).to.have.stdout("");
expect(cli).to.have.exitCode(1);
expect(cli).to.have.stderr.that.matches(/^Error: Cannot say goodbye\n\s+at \w+/);
});
});
describe("my-cli --help", () => {
it("should show usage text", () => {
let cli = myLibrary("--help");
expect(cli).to.have.exitCode(0);
expect(cli).to.have.stderr("");
expect(cli).to.have.stdout.that.contains(manifest.description);
expect(cli).to.have.stdout.that.matches(/\nUsage: my-cli \[options\] \[files...\]\n/);
});
it("should support -h shorthand", () => {
let cli = myLibrary("-h");
expect(cli).to.have.exitCode(0);
expect(cli).to.have.stderr("");
expect(cli).to.have.stdout.that.contains(manifest.description);
expect(cli).to.have.stdout.that.matches(/\nUsage: my-cli \[options\] \[files...\]\n/);
});
it("should ignore other arguments", () => {
let cli = myLibrary("--quiet --help --version");
expect(cli).to.have.exitCode(0);
expect(cli).to.have.stderr("");
expect(cli).to.have.stdout.that.contains(manifest.description);
expect(cli).to.have.stdout.that.matches(/\nUsage: my-cli \[options\] \[files...\]\n/);
});
it("should ignore other shorthand arguments", () => {
let cli = myLibrary("-qhv");
expect(cli).to.have.exitCode(0);
expect(cli).to.have.stderr("");
expect(cli).to.have.stdout.that.contains(manifest.description);
expect(cli).to.have.stdout.that.matches(/\nUsage: my-cli \[options\] \[files...\]\n/);
});
});
describe("my-cli --version", () => {
it("should show the version number", () => {
let cli = myLibrary("--version");
expect(cli).to.have.exitCode(0);
expect(cli).to.have.stderr("");
expect(cli).to.have.stdout(manifest.version + "\n");
});
it("should support -v shorthand", () => {
let cli = myLibrary("-v");
expect(cli).to.have.exitCode(0);
expect(cli).to.have.stderr("");
expect(cli).to.have.stdout(manifest.version + "\n");
});
it("should ignore other arguments", () => {
let cli = myLibrary("--quiet --version");
expect(cli).to.have.exitCode(0);
expect(cli).to.have.stderr("");
expect(cli).to.have.stdout(manifest.version + "\n");
});
it("should ignore other shorthand arguments", () => {
let cli = myLibrary("-qv");
expect(cli).to.have.exitCode(0);
expect(cli).to.have.stderr("");
expect(cli).to.have.stdout(manifest.version + "\n");
});
});