Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorisaacs <i@izs.me>2021-02-01 23:34:05 +0300
committerisaacs <i@izs.me>2021-02-01 23:42:06 +0300
commit8c5ca2f516f5ac87f3bbd7f1fd95c0b283a21f14 (patch)
tree762fcc67e7958778f7d3a4a705c44bc2bab595f7 /test
parent13a5e31781cdaa37d3f007e1c8583c7cb591c62a (diff)
test: Add test for npm-usage.js, and fix 'npm --long' output
Diffstat (limited to 'test')
-rw-r--r--test/lib/utils/npm-usage.js126
1 files changed, 126 insertions, 0 deletions
diff --git a/test/lib/utils/npm-usage.js b/test/lib/utils/npm-usage.js
new file mode 100644
index 000000000..723750111
--- /dev/null
+++ b/test/lib/utils/npm-usage.js
@@ -0,0 +1,126 @@
+const t = require('tap')
+
+const deref = require('../../../lib/utils/deref-command.js')
+const npm = {
+ argv: [],
+ deref,
+ config: {
+ _options: {
+ viewer: null,
+ long: false,
+ userconfig: '/some/config/file/.npmrc',
+ },
+ get: k => {
+ if (npm.config._options[k] === undefined)
+ throw new Error('unknown config')
+ return npm.config._options[k]
+ },
+ set: (k, v) => {
+ npm.config._options[k] = v
+ },
+ },
+ log: {},
+ version: '7.99.9999',
+}
+
+const OUTPUT = []
+const output = (...msg) => OUTPUT.push(msg)
+
+const { dirname } = require('path')
+const basedir = dirname(dirname(dirname(__dirname)))
+t.cleanSnapshot = str => str.split(basedir).join('{BASEDIR}')
+
+const requireInject = require('require-inject')
+const usage = requireInject('../../../lib/utils/npm-usage.js', {
+ '../../../lib/npm.js': npm,
+ '../../../lib/utils/output.js': output,
+})
+
+t.test('basic usage', t => {
+ usage()
+ t.equal(OUTPUT.length, 1)
+ t.equal(OUTPUT[0].length, 1)
+ t.matchSnapshot(OUTPUT[0][0])
+ OUTPUT.length = 0
+ t.end()
+})
+
+t.test('with browser', t => {
+ npm.config.set('viewer', 'browser')
+ usage()
+ t.equal(OUTPUT.length, 1)
+ t.equal(OUTPUT[0].length, 1)
+ t.matchSnapshot(OUTPUT[0][0])
+ OUTPUT.length = 0
+ npm.config.set('viewer', null)
+ t.end()
+})
+
+t.test('with long', t => {
+ npm.config.set('long', true)
+ usage()
+ t.equal(OUTPUT.length, 1)
+ t.equal(OUTPUT[0].length, 1)
+ t.matchSnapshot(OUTPUT[0][0])
+ OUTPUT.length = 0
+ npm.config.set('long', false)
+ t.end()
+})
+
+t.test('did you mean?', t => {
+ npm.argv.push('unistnall')
+ usage()
+ t.equal(OUTPUT.length, 2)
+ t.equal(OUTPUT[0].length, 1)
+ t.equal(OUTPUT[1].length, 1)
+ t.matchSnapshot(OUTPUT[0][0])
+ t.matchSnapshot(OUTPUT[1][0])
+ OUTPUT.length = 0
+ npm.argv.length = 0
+ t.end()
+})
+
+t.test('did you mean?', t => {
+ npm.argv.push('unistnall')
+ const { exitCode } = process
+ t.teardown(() => {
+ if (t.passing())
+ process.exitCode = exitCode
+ })
+ // make sure it fails when invalid
+ usage(false)
+ t.equal(process.exitCode, 1)
+ OUTPUT.length = 0
+ npm.argv.length = 0
+ t.end()
+})
+
+t.test('set process.stdout.columns', t => {
+ const { columns } = process.stdout
+ t.teardown(() => {
+ Object.defineProperty(process.stdout, 'columns', {
+ value: columns,
+ enumerable: true,
+ configurable: true,
+ writable: true,
+ })
+ })
+ const cases = [0, 90]
+ for (const cols of cases) {
+ t.test(`columns=${cols}`, t => {
+ Object.defineProperty(process.stdout, 'columns', {
+ value: cols,
+ enumerable: true,
+ configurable: true,
+ writable: true,
+ })
+ usage()
+ t.equal(OUTPUT.length, 1)
+ t.equal(OUTPUT[0].length, 1)
+ t.matchSnapshot(OUTPUT[0][0])
+ OUTPUT.length = 0
+ t.end()
+ })
+ }
+ t.end()
+})