Skip to content

Commit

Permalink
tests: tests for bin command
Browse files Browse the repository at this point in the history
  • Loading branch information
nlf committed Oct 5, 2020
1 parent ab2c934 commit d816c2e
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ const npm = require('./npm.js')
const output = require('./utils/output.js')
const usageUtil = require('./utils/usage.js')
const completion = require('./utils/completion/none.js')
const PATH = require('./utils/path.js')
const cmd = (args, cb) => bin(args).then(() => cb()).catch(cb)
const usage = usageUtil('bin', 'npm bin [-g]')
const bin = async (args, cb) => {
const b = npm.bin
const PATH = require('./utils/path.js')
output(b)
if (npm.flatOptions.global && !PATH.includes(b)) {
console.error('(not in PATH env variable)')
Expand Down
71 changes: 71 additions & 0 deletions test/lib/bin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const { test } = require('tap')
const requireInject = require('require-inject')

test('bin', (t) => {
t.plan(3)
const dir = '/bin/dir'

const bin = requireInject('../../lib/bin.js', {
'../../lib/npm.js': { bin: dir, flatOptions: { global: false } },
'../../lib/utils/output.js': (output) => {
t.equal(output, dir, 'prints the correct directory')
}
})

bin([], (err) => {
t.ifError(err, 'npm bin')
t.ok('should have printed directory')
})
})

test('bin -g', (t) => {
t.plan(3)
const consoleError = console.error
t.tearDown(() => {
console.error = consoleError
})

console.error = (output) => {
t.fail('should not have printed to console.error')
}
const dir = '/bin/dir'

const bin = requireInject('../../lib/bin.js', {
'../../lib/npm.js': { bin: dir, flatOptions: { global: true } },
'../../lib/utils/path.js': [dir],
'../../lib/utils/output.js': (output) => {
t.equal(output, dir, 'prints the correct directory')
}
})

bin([], (err) => {
t.ifError(err, 'npm bin')
t.ok('should have printed directory')
})
})

test('bin -g (not in path)', (t) => {
t.plan(4)
const consoleError = console.error
t.tearDown(() => {
console.error = consoleError
})

console.error = (output) => {
t.equal(output, '(not in PATH env variable)', 'prints env warning')
}
const dir = '/bin/dir'

const bin = requireInject('../../lib/bin.js', {
'../../lib/npm.js': { bin: dir, flatOptions: { global: true } },
'../../lib/utils/path.js': ['/not/my/dir'],
'../../lib/utils/output.js': (output) => {
t.equal(output, dir, 'prints the correct directory')
}
})

bin([], (err) => {
t.ifError(err, 'npm bin')
t.ok('should have printed directory')
})
})

0 comments on commit d816c2e

Please sign in to comment.