|
| 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 | +}); |
0 commit comments