Skip to content

fix: npm version usage #2205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/version.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict'

const libversion = require('libnpmversion')
const npm = require('./npm.js')
const output = require('./utils/output.js')
Expand Down Expand Up @@ -42,7 +44,7 @@ const version = async args => {
path: npm.prefix,
}))
default:
throw version.usage
throw usage
}
}

Expand Down
160 changes: 160 additions & 0 deletions test/lib/version.js
Original file line number Diff line number Diff line change
@@ -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
})
})