From d816c2efae41930cbdf4fff8657e0adc450d1dd4 Mon Sep 17 00:00:00 2001 From: nlf Date: Mon, 5 Oct 2020 09:46:34 -0700 Subject: [PATCH] tests: tests for bin command --- lib/bin.js | 2 +- test/lib/bin.js | 71 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 test/lib/bin.js diff --git a/lib/bin.js b/lib/bin.js index 9861a7ed5bfdc..85b3da7478a1e 100644 --- a/lib/bin.js +++ b/lib/bin.js @@ -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)') diff --git a/test/lib/bin.js b/test/lib/bin.js new file mode 100644 index 0000000000000..05fc1e21e05d4 --- /dev/null +++ b/test/lib/bin.js @@ -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') + }) +})