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
diff options
context:
space:
mode:
authorRuy Adorno <ruyadorno@hotmail.com>2020-11-20 01:50:07 +0300
committernlf <quitlahok@gmail.com>2020-11-20 21:57:15 +0300
commit7842b4d4dca1e076b0d26d554f9dce67484cd7be (patch)
treedcf72ff065ebfe1506a55c55af9b55e83ba98b71 /test/lib/version.js
parent5fc56b6dbcc7d7d1463a761abb67d2fc16ad3657 (diff)
fix: npm version usage
- Fix `npm version` unexpected arg count usage - Add tests for lib/version.js Fixes: https://github.com/npm/statusboard/issues/182 PR-URL: https://github.com/npm/cli/pull/2205 Credit: @ruyadorno Close: #2205 Reviewed-by: @nlf
Diffstat (limited to 'test/lib/version.js')
-rw-r--r--test/lib/version.js160
1 files changed, 160 insertions, 0 deletions
diff --git a/test/lib/version.js b/test/lib/version.js
new file mode 100644
index 000000000..f36132253
--- /dev/null
+++ b/test/lib/version.js
@@ -0,0 +1,160 @@
+const t = require('tap')
+const requireInject = require('require-inject')
+
+let result = []
+
+const noop = () => null
+const npm = {
+ flatOptions: {
+ json: false,
+ },
+ prefix: '',
+ version: '1.0.0',
+}
+const mocks = {
+ libnpmversion: noop,
+ '../../lib/npm.js': npm,
+ '../../lib/utils/output.js': (...msg) => {
+ for (const m of msg)
+ result.push(m)
+ },
+ '../../lib/utils/usage.js': () => 'usage instructions',
+}
+
+const version = requireInject('../../lib/version.js', mocks)
+
+const _processVersions = process.versions
+t.afterEach(cb => {
+ npm.flatOptions.json = false
+ npm.prefix = ''
+ process.versions = _processVersions
+ result = []
+ cb()
+})
+
+t.test('no args', t => {
+ const prefix = t.testdir({
+ 'package.json': JSON.stringify({
+ name: 'test-version-no-args',
+ version: '3.2.1',
+ }),
+ })
+ npm.prefix = prefix
+ Object.defineProperty(process, 'versions', { value: { node: '1.0.0' } })
+
+ version([], err => {
+ if (err)
+ throw err
+
+ t.deepEqual(
+ result,
+ [{
+ 'test-version-no-args': '3.2.1',
+ node: '1.0.0',
+ npm: '1.0.0',
+ }],
+ 'should output expected values for various versions in npm'
+ )
+
+ t.end()
+ })
+})
+
+t.test('too many args', t => {
+ version(['foo', 'bar'], err => {
+ t.match(
+ err,
+ 'usage instructions',
+ 'should throw usage instructions error'
+ )
+
+ t.end()
+ })
+})
+
+t.test('completion', t => {
+ const { completion } = version
+
+ const testComp = (argv, expect) => {
+ completion({ conf: { argv: { remain: argv } } }, (err, res) => {
+ t.ifError(err)
+ t.strictSame(res, expect, argv.join(' '))
+ })
+ }
+
+ testComp(['npm', 'version'], [
+ 'major',
+ 'minor',
+ 'patch',
+ 'premajor',
+ 'preminor',
+ 'prepatch',
+ 'prerelease',
+ 'from-git',
+ ])
+ testComp(['npm', 'version', 'major'], [])
+
+ t.end()
+})
+
+t.test('failure reading package.json', t => {
+ const prefix = t.testdir({})
+ npm.prefix = prefix
+
+ version([], err => {
+ if (err)
+ throw err
+
+ t.deepEqual(
+ result,
+ [{
+ npm: '1.0.0',
+ node: '1.0.0',
+ }],
+ 'should not have package name on returning object'
+ )
+
+ t.end()
+ })
+})
+
+t.test('--json option', t => {
+ const prefix = t.testdir({})
+ npm.flatOptions.json = true
+ npm.prefix = prefix
+ Object.defineProperty(process, 'versions', { value: {} })
+
+ version([], err => {
+ if (err)
+ throw err
+ t.deepEqual(
+ result,
+ ['{\n "npm": "1.0.0"\n}'],
+ 'should return json stringified result'
+ )
+ t.end()
+ })
+})
+
+t.test('with one arg', t => {
+ const version = requireInject('../../lib/version.js', {
+ ...mocks,
+ libnpmversion: (arg, opts) => {
+ t.equal(arg, 'major', 'should forward expected value')
+ t.deepEqual(
+ opts,
+ {
+ json: false,
+ path: '',
+ },
+ 'should forward expected options'
+ )
+ t.end()
+ },
+ })
+
+ version(['major'], err => {
+ if (err)
+ throw err
+ })
+})