-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(tests): move more tests to use real npm
This moves a handful of the smaller tests to using the new npm mock that uses the real actual npm object. It also extends the testing surface area of a few tests back down into the actual `process.spawn` that results, instead of anything internal to the code. Some dead code in `lib/test.js` was found during this, as well as an instance of a module throwing a string instead of an error object. PR-URL: #3463 Credit: @wraithgar Close: #3463 Reviewed-by: @nlf
- Loading branch information
Showing
18 changed files
with
272 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,26 @@ | ||
const t = require('tap') | ||
|
||
const FindDupes = require('../../lib/find-dupes.js') | ||
const { real: mockNpm } = require('../fixtures/mock-npm') | ||
|
||
t.test('should run dedupe in dryRun mode', (t) => { | ||
t.plan(3) | ||
const findDupesTest = new FindDupes({ | ||
config: { | ||
set: (k, v) => { | ||
t.match(k, 'dry-run') | ||
t.match(v, true) | ||
}, | ||
t.test('should run dedupe in dryRun mode', async (t) => { | ||
t.plan(5) | ||
const { npm, command } = mockNpm(t, { | ||
'@npmcli/arborist': function (args) { | ||
t.ok(args, 'gets options object') | ||
t.ok(args.path, 'gets path option') | ||
t.ok(args.dryRun, 'is called in dryRun mode') | ||
this.dedupe = () => { | ||
t.ok(true, 'dedupe is called') | ||
} | ||
}, | ||
commands: { | ||
dedupe: (args, cb) => { | ||
t.match(args, []) | ||
cb() | ||
}, | ||
'../../lib/utils/reify-finish.js': (npm, arb) => { | ||
t.ok(arb, 'gets arborist tree') | ||
}, | ||
}) | ||
findDupesTest.exec({}, () => { | ||
t.end() | ||
}) | ||
await npm.load() | ||
// explicitly set to false so we can be 100% sure it's always true when it | ||
// hits arborist | ||
npm.config.set('dry-run', false) | ||
npm.config.set('prefix', 'foo') | ||
await command('find-dupes') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
const t = require('tap') | ||
const { real: mockNpm } = require('../fixtures/mock-npm') | ||
|
||
t.test('should retrieve values from npm.commands.config', (t) => { | ||
const Get = t.mock('../../lib/get.js') | ||
const get = new Get({ | ||
commands: { | ||
config: ([action, arg]) => { | ||
t.equal(action, 'get', 'should use config get action') | ||
t.equal(arg, 'foo', 'should use expected key') | ||
t.end() | ||
}, | ||
}, | ||
}) | ||
|
||
get.exec(['foo']) | ||
t.test('should retrieve values from config', async t => { | ||
const { joinedOutput, command, npm } = mockNpm(t) | ||
const name = 'editor' | ||
const value = 'vigor' | ||
await npm.load() | ||
npm.config.set(name, value) | ||
await command('get', [name]) | ||
t.equal( | ||
joinedOutput(), | ||
value, | ||
'outputs config item' | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,13 @@ | ||
const t = require('tap') | ||
const { real: mockNpm } = require('../fixtures/mock-npm') | ||
|
||
t.test('prefix', (t) => { | ||
t.plan(3) | ||
const dir = '/prefix/dir' | ||
|
||
const Prefix = require('../../lib/prefix.js') | ||
const prefix = new Prefix({ | ||
prefix: dir, | ||
output: (output) => { | ||
t.equal(output, dir, 'prints the correct directory') | ||
}, | ||
}) | ||
|
||
prefix.exec([], (err) => { | ||
t.error(err, 'npm prefix') | ||
t.ok('should have printed directory') | ||
}) | ||
t.test('prefix', async (t) => { | ||
const { joinedOutput, command, npm } = mockNpm(t) | ||
await npm.load() | ||
await command('prefix') | ||
t.equal( | ||
joinedOutput(), | ||
npm.prefix, | ||
'outputs npm.prefix' | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,36 @@ | ||
const t = require('tap') | ||
let runArgs | ||
const npm = { | ||
commands: { | ||
'run-script': (args, cb) => { | ||
runArgs = args | ||
cb() | ||
}, | ||
}, | ||
} | ||
const Restart = require('../../lib/restart.js') | ||
const restart = new Restart(npm) | ||
restart.exec(['foo'], () => { | ||
t.match(runArgs, ['restart', 'foo']) | ||
t.end() | ||
const spawk = require('spawk') | ||
const { real: mockNpm } = require('../fixtures/mock-npm') | ||
|
||
spawk.preventUnmatched() | ||
t.teardown(() => { | ||
spawk.unload() | ||
}) | ||
|
||
// TODO this ... smells. npm "script-shell" config mentions defaults but those | ||
// are handled by run-script, not npm. So for now we have to tie tests to some | ||
// pretty specific internals of runScript | ||
const makeSpawnArgs = require('@npmcli/run-script/lib/make-spawn-args.js') | ||
|
||
t.test('should run stop script from package.json', async t => { | ||
const prefix = t.testdir({ | ||
'package.json': JSON.stringify({ | ||
name: 'x', | ||
version: '1.2.3', | ||
scripts: { | ||
restart: 'node ./test-restart.js', | ||
}, | ||
}), | ||
}) | ||
const { command, npm } = mockNpm(t) | ||
await npm.load() | ||
npm.log.level = 'silent' | ||
npm.localPrefix = prefix | ||
const [scriptShell] = makeSpawnArgs({ path: prefix }) | ||
const script = spawk.spawn(scriptShell, (args) => { | ||
t.ok(args.includes('node ./test-restart.js "foo"'), 'ran stop script with extra args') | ||
return true | ||
}) | ||
await command('restart', ['foo']) | ||
t.ok(script.called, 'script ran') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,13 @@ | ||
const t = require('tap') | ||
const { real: mockNpm } = require('../fixtures/mock-npm') | ||
|
||
t.test('root', (t) => { | ||
t.plan(3) | ||
const dir = '/root/dir' | ||
|
||
const Root = require('../../lib/root.js') | ||
const root = new Root({ | ||
dir, | ||
output: (output) => { | ||
t.equal(output, dir, 'prints the correct directory') | ||
}, | ||
}) | ||
|
||
root.exec([], (err) => { | ||
t.error(err, 'npm root') | ||
t.ok('should have printed directory') | ||
}) | ||
t.test('prefix', async (t) => { | ||
const { joinedOutput, command, npm } = mockNpm(t) | ||
await npm.load() | ||
await command('root') | ||
t.equal( | ||
joinedOutput(), | ||
npm.dir, | ||
'outputs npm.dir' | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.