diff --git a/lib/cli/entry.js b/lib/cli/entry.js index fc196b8e234fc..5d676c3f0a8a1 100644 --- a/lib/cli/entry.js +++ b/lib/cli/entry.js @@ -39,15 +39,12 @@ module.exports = async (process, validateEngines) => { // Now actually fire up npm and run the command. // This is how to use npm programmatically: try { - const { exec } = await npm.load() + const { exec, command, args } = await npm.load() if (!exec) { return exitHandler() } - const command = npm.argv.shift() - const args = npm.argv - if (!command) { output.standard(npm.usage) process.exitCode = 1 diff --git a/lib/npm.js b/lib/npm.js index 892cf4b96c8a0..eb715fa803508 100644 --- a/lib/npm.js +++ b/lib/npm.js @@ -77,7 +77,8 @@ class Npm { return this.constructor.version } - setCmd (cmd) { + // Call an npm command + async exec (cmd, args = this.argv) { const Command = Npm.cmd(cmd) const command = new Command(this) @@ -88,12 +89,6 @@ class Npm { process.env.npm_command = this.command } - return command - } - - // Call an npm command - async exec (cmd, args = this.argv) { - const command = this.setCmd(cmd) return time.start(`command:${cmd}`, () => command.cmdExec(args)) } @@ -102,6 +97,8 @@ class Npm { const { exec = true } = await this.#load().then(r => r ?? {}) return { exec, + command: this.argv.shift(), + args: this.argv, } }) } diff --git a/test/fixtures/mock-npm.js b/test/fixtures/mock-npm.js index 5618a12566003..d8a4834a9abff 100644 --- a/test/fixtures/mock-npm.js +++ b/test/fixtures/mock-npm.js @@ -120,7 +120,6 @@ const setupMockNpm = async (t, { // preload a command command = null, // string name of the command exec = null, // optionally exec the command before returning - setCmd = false, // test dirs prefixDir = {}, homeDir = {}, @@ -242,7 +241,11 @@ const setupMockNpm = async (t, { init, load, mocks: withDirs(mocks), - npm: { argv, excludeNpmCwd: true, ...withDirs(npmOpts) }, + npm: { + argv: command ? [command, ...argv] : argv, + excludeNpmCwd: true, + ...withDirs(npmOpts), + }, }) if (config.omit?.includes('prod')) { @@ -267,16 +270,6 @@ const setupMockNpm = async (t, { const mockCommand = {} if (command) { const Cmd = mockNpm.Npm.cmd(command) - if (setCmd) { - // XXX(hack): This is a hack to allow fake-ish tests to set the currently - // running npm command without running exec. Generally, we should rely on - // actually exec-ing the command to asserting the state of the world - // through what is printed/on disk/etc. This is a stop-gap to allow tests - // that are time intensive to convert to continue setting the npm command - // this way. TODO: remove setCmd from all tests and remove the setCmd - // method from `lib/npm.js` - npm.setCmd(command) - } mockCommand.cmd = new Cmd(npm) mockCommand[command] = { usage: Cmd.describeUsage, diff --git a/test/lib/utils/reify-output.js b/test/lib/utils/reify-output.js index fd15e25a74984..205b7baf421f7 100644 --- a/test/lib/utils/reify-output.js +++ b/test/lib/utils/reify-output.js @@ -8,7 +8,18 @@ const mockReify = async (t, reify, { command, ...config } = {}) => { const mock = await mockNpm(t, { command, config, - setCmd: true, + }) + + // Hack to adapt existing fake test. Make npm.command + // return whatever was passed in to this function. + // What it should be doing is npm.exec(command) but that + // breaks most of these tests because they dont expect + // a command to actually run. + Object.defineProperty(mock.npm, 'command', { + get () { + return command + }, + enumerable: true, }) reifyOutput(mock.npm, reify)