Skip to content

Commit 3b61ad2

Browse files
Added tests for the CLI
1 parent 8fe7fad commit 3b61ad2

File tree

5 files changed

+557
-14
lines changed

5 files changed

+557
-14
lines changed

src/options.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export interface Options {
3232
checkVersion?: boolean;
3333

3434
/**
35-
* Suppress console output from NPM.
35+
* Suppress console output from NPM and npm-publish.
3636
*
3737
* Defaults to `false`
3838
*/

test/specs/cli/args.spec.js

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
"use strict";
2+
3+
const npm = require("../../utils/npm");
4+
const files = require("../../utils/files");
5+
const exec = require("../../utils/exec");
6+
const { expect } = require("chai");
7+
const manifest = require("../../../package.json");
8+
9+
describe("CLI - argument tests", () => {
10+
11+
it("should error if an invalid argument is used", () => {
12+
let cli = exec.cli("--debug", "--help", "--fizzbuzz", "--quiet");
13+
14+
expect(cli).to.have.exitCode(9);
15+
expect(cli).to.have.stdout("");
16+
expect(cli.stderr).to.match(/^Unknown option: --fizzbuzz\n\nUsage: npm-publish \[options\] \[package_path\]\n/);
17+
});
18+
19+
it("should error if an invalid shorthand argument is used", () => {
20+
let cli = exec.cli("-dqzv");
21+
22+
expect(cli).to.have.exitCode(9);
23+
expect(cli).to.have.stdout("");
24+
expect(cli.stderr).to.match(/^Unknown option: -z\n\nUsage: npm-publish \[options\] \[package_path\]\n/);
25+
});
26+
27+
it("should print a more detailed error if DEBUG is set", () => {
28+
process.env.DEBUG = "true";
29+
let cli = exec.cli();
30+
31+
expect(cli).to.have.stdout("");
32+
expect(cli).to.have.exitCode(1);
33+
34+
expect(cli).to.have.stderr.that.matches(/^Error: Unable to read package.json \nENOENT: no such file or directory/);
35+
expect(cli).to.have.stderr.that.matches(/\n at /);
36+
37+
process.env.DEBUG = "";
38+
});
39+
40+
describe("npm-publish --help", () => {
41+
it("should show usage text", () => {
42+
let cli = exec.cli("--help");
43+
44+
expect(cli).to.have.exitCode(0);
45+
expect(cli).to.have.stderr("");
46+
expect(cli.stdout).to.match(/^\nUsage: npm-publish \[options\] \[package_path\]\n/);
47+
});
48+
49+
it("should support -h shorthand", () => {
50+
let cli = exec.cli("-h");
51+
52+
expect(cli).to.have.exitCode(0);
53+
expect(cli).to.have.stderr("");
54+
expect(cli.stdout).to.match(/^\nUsage: npm-publish \[options\] \[package_path\]\n/);
55+
});
56+
57+
it("should ignore other arguments", () => {
58+
let cli = exec.cli("--quiet", "--help", "--version");
59+
60+
expect(cli).to.have.exitCode(0);
61+
expect(cli).to.have.stderr("");
62+
expect(cli.stdout).to.match(/^\nUsage: npm-publish \[options\] \[package_path\]\n/);
63+
});
64+
65+
it("should ignore other shorthand arguments", () => {
66+
let cli = exec.cli("-dhv");
67+
68+
expect(cli).to.have.exitCode(0);
69+
expect(cli).to.have.stderr("");
70+
expect(cli.stdout).to.match(/^\nUsage: npm-publish \[options\] \[package_path\]\n/);
71+
});
72+
});
73+
74+
describe("npm-publish --version", () => {
75+
it("should show the version number", () => {
76+
let cli = exec.cli("--version");
77+
78+
expect(cli).to.have.exitCode(0);
79+
expect(cli).to.have.stderr("");
80+
expect(cli).to.have.stdout(manifest.version + "\n");
81+
});
82+
83+
it("should support -v shorthand", () => {
84+
let cli = exec.cli("-v");
85+
86+
expect(cli).to.have.exitCode(0);
87+
expect(cli).to.have.stderr("");
88+
expect(cli).to.have.stdout(manifest.version + "\n");
89+
});
90+
91+
it("should ignore other arguments", () => {
92+
let cli = exec.cli("--quiet", "--version", "--debug");
93+
94+
expect(cli).to.have.exitCode(0);
95+
expect(cli).to.have.stderr("");
96+
expect(cli).to.have.stdout(manifest.version + "\n");
97+
});
98+
99+
it("should ignore other shorthand arguments", () => {
100+
let cli = exec.cli("-dvq");
101+
102+
expect(cli).to.have.exitCode(0);
103+
expect(cli).to.have.stderr("");
104+
expect(cli).to.have.stdout(manifest.version + "\n");
105+
});
106+
});
107+
108+
describe("npm-publish --token", () => {
109+
it("should error if the --token is missing its value", () => {
110+
let cli = exec.cli("--quiet", "--token", "--version");
111+
112+
expect(cli).to.have.exitCode(9);
113+
expect(cli).to.have.stdout("");
114+
expect(cli.stderr).to.match(/^The --token argument requires a value\n\nUsage: npm-publish \[options\] \[package_path\]\n/);
115+
});
116+
});
117+
118+
describe("npm-publish --registry", () => {
119+
it("should error if the --registry is missing its value", () => {
120+
let cli = exec.cli("--quiet", "--registry", "--version");
121+
122+
expect(cli).to.have.exitCode(9);
123+
expect(cli).to.have.stdout("");
124+
expect(cli.stderr).to.match(/^The --registry argument requires a value\n\nUsage: npm-publish \[options\] \[package_path\]\n/);
125+
});
126+
127+
it("should fail if the NPM registry URL is invalid", () => {
128+
files.create([
129+
{ path: "workspace/package.json", contents: { name: "my-lib", version: "1.2.3" }},
130+
]);
131+
132+
let cli = exec.cli("--registry", "example.com");
133+
134+
expect(cli).to.have.stdout("");
135+
expect(cli).stderr.to.include("Invalid URL: example.com");
136+
expect(cli).to.have.exitCode(1);
137+
138+
files.assert.doesNotExist("home/.npmrc");
139+
npm.assert.ran(0);
140+
});
141+
});
142+
});

test/specs/cli/failure.spec.js

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
"use strict";
2+
3+
const npm = require("../../utils/npm");
4+
const files = require("../../utils/files");
5+
const paths = require("../../utils/paths");
6+
const exec = require("../../utils/exec");
7+
const { expect } = require("chai");
8+
const { EOL } = require("os");
9+
10+
describe("CLI - failure tests", () => {
11+
12+
it("should fail if the package.json file does not exist", () => {
13+
let cli = exec.cli();
14+
15+
expect(cli).to.have.stdout("");
16+
expect(cli).stderr.to.include("Unable to read package.json \nENOENT: no such file or directory");
17+
expect(cli).to.have.exitCode(1);
18+
19+
files.assert.doesNotExist("home/.npmrc");
20+
files.assert.doesNotExist("workspace/package.json");
21+
npm.assert.didNotRun();
22+
});
23+
24+
it("should fail if the package.json file is invalid", () => {
25+
files.create([
26+
{ path: "workspace/package.json", contents: "hello, world" },
27+
]);
28+
29+
let cli = exec.cli();
30+
31+
expect(cli).to.have.stdout("");
32+
expect(cli).to.have.stderr("Unable to parse package.json \nUnexpected token h in JSON at position 0\n");
33+
expect(cli).to.have.exitCode(1);
34+
35+
files.assert.doesNotExist("home/.npmrc");
36+
npm.assert.didNotRun();
37+
});
38+
39+
it("should fail if the package name is invalid", () => {
40+
files.create([
41+
{ path: "workspace/package.json", contents: { name: "\n \t" }},
42+
]);
43+
44+
let cli = exec.cli();
45+
46+
expect(cli).to.have.stdout("");
47+
expect(cli).to.have.stderr("Unable to parse package.json \nInvalid package name\n");
48+
expect(cli).to.have.exitCode(1);
49+
50+
files.assert.doesNotExist("home/.npmrc");
51+
npm.assert.didNotRun();
52+
});
53+
54+
it("should fail if the package version is invalid", () => {
55+
files.create([
56+
{ path: "workspace/package.json", contents: { name: "my-lib", version: "hello, world" }},
57+
]);
58+
59+
let cli = exec.cli();
60+
61+
expect(cli).to.have.stdout("");
62+
expect(cli).to.have.stderr("Unable to parse package.json \nInvalid Version: hello, world\n");
63+
expect(cli).to.have.exitCode(1);
64+
65+
files.assert.doesNotExist("home/.npmrc");
66+
npm.assert.didNotRun();
67+
});
68+
69+
it('should fail if the "npm view" command errors', () => {
70+
files.create([
71+
{ path: "workspace/package.json", contents: { name: "my-lib", version: "2.0.0" }},
72+
]);
73+
74+
npm.mock({
75+
args: ["view", "my-lib", "version"],
76+
stderr: "BOOM!",
77+
exitCode: 1,
78+
});
79+
80+
let cli = exec.cli();
81+
82+
expect(cli).to.have.stdout("");
83+
expect(cli).to.have.stderr(
84+
"Unable to determine the current version of my-lib on NPM. \n" +
85+
"npm view my-lib version exited with a status of 1.\n\n" +
86+
"BOOM!\n"
87+
);
88+
expect(cli).to.have.exitCode(1);
89+
90+
npm.assert.ran(1);
91+
});
92+
93+
it("should fail if the .npmrc file is invalid", () => {
94+
files.create([
95+
{ path: "workspace/package.json", contents: { name: "my-lib", version: "2.0.0" }},
96+
{ path: "home/.npmrc/file.txt", contents: "~/.npmrc is a directory, not a file" },
97+
]);
98+
99+
npm.mock({
100+
args: ["view", "my-lib", "version"],
101+
stdout: `1.0.0${EOL}`,
102+
});
103+
104+
npm.mock({
105+
args: ["config", "get", "userconfig"],
106+
stdout: `${paths.npmrc}${EOL}`,
107+
});
108+
109+
let cli = exec.cli();
110+
111+
expect(cli).to.have.stdout("");
112+
expect(cli).stderr.to.include("Unable to read the NPM config file: ");
113+
expect(cli).stderr.to.include("EISDIR: illegal operation on a directory, read");
114+
expect(cli).to.have.exitCode(1);
115+
116+
npm.assert.ran(2);
117+
});
118+
119+
it('should fail if the "npm config" command errors', () => {
120+
files.create([
121+
{ path: "workspace/package.json", contents: { name: "my-lib", version: "2.0.0" }},
122+
]);
123+
124+
npm.mock({
125+
args: ["view", "my-lib", "version"],
126+
stdout: `1.0.0${EOL}`,
127+
});
128+
129+
npm.mock({
130+
args: ["config", "get", "userconfig"],
131+
stderr: "BOOM!",
132+
exitCode: 1,
133+
});
134+
135+
let cli = exec.cli();
136+
137+
expect(cli).to.have.stdout("");
138+
expect(cli).stderr.to.include("Unable to determine the NPM config file path.");
139+
expect(cli).stderr.to.include("npm config get userconfig exited with a status of 1");
140+
expect(cli).stderr.to.include("BOOM!");
141+
expect(cli).to.have.exitCode(1);
142+
143+
npm.assert.ran(2);
144+
});
145+
146+
it('should fail if the "npm publish" command errors', () => {
147+
files.create([
148+
{ path: "workspace/package.json", contents: { name: "my-lib", version: "2.0.0" }},
149+
]);
150+
151+
npm.mock({
152+
args: ["view", "my-lib", "version"],
153+
stdout: `1.0.0${EOL}`,
154+
});
155+
156+
npm.mock({
157+
args: ["config", "get", "userconfig"],
158+
stdout: `${paths.npmrc}${EOL}`,
159+
});
160+
161+
npm.mock({
162+
args: ["publish"],
163+
stderr: "BOOM!",
164+
exitCode: 1,
165+
});
166+
167+
let cli = exec.cli();
168+
169+
expect(cli).to.have.stdout("");
170+
expect(cli).stderr.to.include("Unable to publish my-lib v2.0.0 to NPM.");
171+
expect(cli).stderr.to.include("npm publish exited with a status of 1");
172+
expect(cli).stderr.to.include("BOOM!");
173+
expect(cli).to.have.exitCode(1);
174+
175+
npm.assert.ran(3);
176+
});
177+
178+
});

0 commit comments

Comments
 (0)