-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
fix(tests): move more tests to use real npm #3463
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nothing in any of the cli code or its dependencies sets this, and the exit handler suppresses the callback error during any command that shells out anyways.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is clearly dead code.