From d816c2efae41930cbdf4fff8657e0adc450d1dd4 Mon Sep 17 00:00:00 2001 From: nlf Date: Mon, 5 Oct 2020 09:46:34 -0700 Subject: [PATCH 1/4] 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') + }) +}) From c8f0d5457dd913b425987ae30a611d4eb9e84b7d Mon Sep 17 00:00:00 2001 From: nlf Date: Mon, 5 Oct 2020 09:46:44 -0700 Subject: [PATCH 2/4] tests: tests for prefix command --- test/lib/prefix.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 test/lib/prefix.js diff --git a/test/lib/prefix.js b/test/lib/prefix.js new file mode 100644 index 0000000000000..a6e4d731ab187 --- /dev/null +++ b/test/lib/prefix.js @@ -0,0 +1,19 @@ +const { test } = require('tap') +const requireInject = require('require-inject') + +test('prefix', (t) => { + t.plan(3) + const dir = '/prefix/dir' + + const prefix = requireInject('../../lib/prefix.js', { + '../../lib/npm.js': { prefix: dir }, + '../../lib/utils/output.js': (output) => { + t.equal(output, dir, 'prints the correct directory') + } + }) + + prefix([], (err) => { + t.ifError(err, 'npm prefix') + t.ok('should have printed directory') + }) +}) From d48086d0d3e006e76f364fb2c62b406a97ce8f68 Mon Sep 17 00:00:00 2001 From: nlf Date: Mon, 5 Oct 2020 09:46:55 -0700 Subject: [PATCH 3/4] tests: tests for root command --- test/lib/root.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 test/lib/root.js diff --git a/test/lib/root.js b/test/lib/root.js new file mode 100644 index 0000000000000..210e9b029121c --- /dev/null +++ b/test/lib/root.js @@ -0,0 +1,19 @@ +const { test } = require('tap') +const requireInject = require('require-inject') + +test('root', (t) => { + t.plan(3) + const dir = '/root/dir' + + const root = requireInject('../../lib/root.js', { + '../../lib/npm.js': { dir }, + '../../lib/utils/output.js': (output) => { + t.equal(output, dir, 'prints the correct directory') + } + }) + + root([], (err) => { + t.ifError(err, 'npm root') + t.ok('should have printed directory') + }) +}) From 2db6f1d2ece9af692d57727d5a25f64d30ffc092 Mon Sep 17 00:00:00 2001 From: nlf Date: Mon, 5 Oct 2020 09:47:15 -0700 Subject: [PATCH 4/4] tests: tests for birthday command --- test/lib/birthday.js | 84 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 test/lib/birthday.js diff --git a/test/lib/birthday.js b/test/lib/birthday.js new file mode 100644 index 0000000000000..35255f97aa34b --- /dev/null +++ b/test/lib/birthday.js @@ -0,0 +1,84 @@ +const { test } = require('tap') +const requireInject = require('require-inject') + +test('birthday (nope)', (t) => { + t.plan(1) + const B = global[Buffer.from([66, 117, 102, 102, 101, 114])] + const f = B.from([102, 114, 111, 109]) + const D = global[B[f]([68, 97, 116, 101])] + const _6 = B[f]([98, 97, 115, 101, 54, 52]) + '' + const l = B[f]('dG9TdHJpbmc=', _6) + class FD extends D { + [B[f]('Z2V0VVRDTW9udGg=', _6)[l]()] () { + return 7 + } + } + global[B[f]([68, 97, 116, 101])] = FD + const consoleLog = console.log + console.log = () => undefined + t.tearDown(() => { + global[B[f]([68, 97, 116, 101])] = D + console.log = consoleLog + }) + const birthday = requireInject('../../lib/birthday', {}) + birthday([], (err) => { + t.match(err, 'try again', 'not telling you the secret that easily are we?') + }) +}) + +test('birthday (nope again)', (t) => { + t.plan(1) + const B = global[Buffer.from([66, 117, 102, 102, 101, 114])] + const f = B.from([102, 114, 111, 109]) + const D = global[B[f]([68, 97, 116, 101])] + const _6 = B[f]([98, 97, 115, 101, 54, 52]) + '' + const l = B[f]('dG9TdHJpbmc=', _6) + class FD extends D { + [B[f]('Z2V0RnVsbFllYXI=', _6)[l]()] () { + const d = new D() + return d[B[f]('Z2V0RnVsbFllYXI=', _6)[l]()]() + 1 + } + [B[f]('Z2V0VVRDTW9udGg=', _6)[l]()] () { + return 9 + } + } + global[B[f]([68, 97, 116, 101])] = FD + const consoleLog = console.log + console.log = () => undefined + t.tearDown(() => { + global[B[f]([68, 97, 116, 101])] = D + console.log = consoleLog + }) + const birthday = requireInject('../../lib/birthday', {}) + birthday([], (err) => { + t.match(err, 'try again', 'not telling you the secret that easily are we?') + }) +}) + +test('birthday (yup)', (t) => { + t.plan(1) + const B = global[Buffer.from([66, 117, 102, 102, 101, 114])] + const f = B.from([102, 114, 111, 109]) + const D = global[B[f]([68, 97, 116, 101])] + const _6 = B[f]([98, 97, 115, 101, 54, 52]) + '' + const l = B[f]('dG9TdHJpbmc=', _6) + class FD extends D { + [B[f]('Z2V0VVRDTW9udGg=', _6)[l]()] () { + return 8 + } + [B[f]('Z2V0VVRDRGF0ZQ==', _6)[l]()] () { + return 29 + } + } + global[B[f]([68, 97, 116, 101])] = FD + const consoleLog = console.log + console.log = () => undefined + t.tearDown(() => { + global[B[f]([68, 97, 116, 101])] = D + console.log = consoleLog + }) + const birthday = requireInject('../../lib/birthday', {}) + birthday([], (err) => { + t.ifError(err, 'npm birthday') + }) +})