From 222f4fe985f58bf62882cdbb1ab0de3d872e9fd8 Mon Sep 17 00:00:00 2001 From: nlf Date: Wed, 10 Aug 2022 09:28:41 -0700 Subject: chore: fix tests for @npmcli/run-script@4.2.1 --- test/lib/commands/edit.js | 46 ++++++-------------------------------------- test/lib/commands/restart.js | 17 +++++++--------- test/lib/commands/start.js | 17 ++++++---------- test/lib/commands/stop.js | 16 +++++---------- test/lib/commands/test.js | 16 +++++---------- 5 files changed, 29 insertions(+), 83 deletions(-) (limited to 'test') diff --git a/test/lib/commands/edit.js b/test/lib/commands/edit.js index b2a10be13..22543f8e3 100644 --- a/test/lib/commands/edit.js +++ b/test/lib/commands/edit.js @@ -1,5 +1,4 @@ const t = require('tap') -const fs = require('fs') const path = require('path') const tspawk = require('../../fixtures/tspawk') const { load: loadMockNpm } = require('../../fixtures/mock-npm') @@ -39,24 +38,13 @@ t.test('npm edit', async t => { const { npm, joinedOutput } = await loadMockNpm(t, npmConfig) const semverPath = path.resolve(npm.prefix, 'node_modules', 'semver') - const [scriptShell] = makeSpawnArgs({ + const [scriptShell, scriptArgs] = makeSpawnArgs({ event: 'install', path: npm.prefix, cmd: 'testinstall', }) spawk.spawn('testeditor', [semverPath]) - spawk.spawn( - scriptShell, - args => { - const lastArg = args[args.length - 1] - const rightExtension = lastArg.endsWith('.cmd') || lastArg.endsWith('.sh') - const rightFilename = path.basename(lastArg).startsWith('install') - const rightContents = fs.readFileSync(lastArg, { encoding: 'utf8' }) - .trim().endsWith('testinstall') - return rightExtension && rightFilename && rightContents - }, - { cwd: semverPath } - ) + spawk.spawn(scriptShell, scriptArgs, { cwd: semverPath }) await npm.exec('edit', ['semver']) t.match(joinedOutput(), 'rebuilt dependencies successfully') }) @@ -64,24 +52,13 @@ t.test('npm edit', async t => { t.test('rebuild failure', async t => { const { npm } = await loadMockNpm(t, npmConfig) const semverPath = path.resolve(npm.prefix, 'node_modules', 'semver') - const [scriptShell] = makeSpawnArgs({ + const [scriptShell, scriptArgs] = makeSpawnArgs({ event: 'install', path: npm.prefix, cmd: 'testinstall', }) spawk.spawn('testeditor', [semverPath]) - spawk.spawn( - scriptShell, - args => { - const lastArg = args[args.length - 1] - const rightExtension = lastArg.endsWith('.cmd') || lastArg.endsWith('.sh') - const rightFilename = path.basename(lastArg).startsWith('install') - const rightContents = fs.readFileSync(lastArg, { encoding: 'utf8' }) - .trim().endsWith('testinstall') - return rightExtension && rightFilename && rightContents - }, - { cwd: semverPath } - ).exit(1).stdout('test error') + spawk.spawn(scriptShell, scriptArgs, { cwd: semverPath }).exit(1).stdout('test error') await t.rejects( npm.exec('edit', ['semver']), { message: 'command failed' } @@ -108,24 +85,13 @@ t.test('npm edit editor has flags', async t => { }) const semverPath = path.resolve(npm.prefix, 'node_modules', 'semver') - const [scriptShell] = makeSpawnArgs({ + const [scriptShell, scriptArgs] = makeSpawnArgs({ event: 'install', path: npm.prefix, cmd: 'testinstall', }) spawk.spawn('testeditor', ['--flag', semverPath]) - spawk.spawn( - scriptShell, - args => { - const lastArg = args[args.length - 1] - const rightExtension = lastArg.endsWith('.cmd') || lastArg.endsWith('.sh') - const rightFilename = path.basename(lastArg).startsWith('install') - const rightContents = fs.readFileSync(lastArg, { encoding: 'utf8' }) - .trim().endsWith('testinstall') - return rightExtension && rightFilename && rightContents - }, - { cwd: semverPath } - ) + spawk.spawn(scriptShell, scriptArgs, { cwd: semverPath }) await npm.exec('edit', ['semver']) }) diff --git a/test/lib/commands/restart.js b/test/lib/commands/restart.js index bfbe715e8..c2257b8ce 100644 --- a/test/lib/commands/restart.js +++ b/test/lib/commands/restart.js @@ -1,5 +1,3 @@ -const fs = require('fs') -const path = require('path') const t = require('tap') const tspawk = require('../../fixtures/tspawk') const { load: loadMockNpm } = require('../../fixtures/mock-npm') @@ -26,15 +24,14 @@ t.test('should run restart script from package.json', async t => { loglevel: 'silent', }, }) - const [scriptShell] = makeSpawnArgs({ path: npm.prefix, cmd: 'node ./test-restart.js' }) - const script = spawk.spawn(scriptShell, (args) => { - const lastArg = args[args.length - 1] - const rightExtension = lastArg.endsWith('.cmd') || lastArg.endsWith('.sh') - const rightFilename = path.basename(lastArg).startsWith('restart') - const rightContents = fs.readFileSync(lastArg, { encoding: 'utf8' }) - .trim().endsWith('foo') - return rightExtension && rightFilename && rightContents + const [scriptShell, scriptArgs] = makeSpawnArgs({ + path: npm.prefix, + cmd: 'node ./test-restart.js', }) + let scriptContent = scriptArgs.pop() + scriptContent += ' foo' + scriptArgs.push(scriptContent) + const script = spawk.spawn(scriptShell, scriptArgs) await npm.exec('restart', ['foo']) t.ok(script.called, 'script ran') }) diff --git a/test/lib/commands/start.js b/test/lib/commands/start.js index 79c2133bc..3caaa478f 100644 --- a/test/lib/commands/start.js +++ b/test/lib/commands/start.js @@ -1,5 +1,3 @@ -const fs = require('fs') -const path = require('path') const t = require('tap') const tspawk = require('../../fixtures/tspawk') const { load: loadMockNpm } = require('../../fixtures/mock-npm') @@ -26,15 +24,12 @@ t.test('should run start script from package.json', async t => { loglevel: 'silent', }, }) - const [scriptShell] = makeSpawnArgs({ path: npm.prefix, cmd: 'node ./test-start.js' }) - const script = spawk.spawn(scriptShell, (args) => { - const lastArg = args[args.length - 1] - const rightExtension = lastArg.endsWith('.cmd') || lastArg.endsWith('.sh') - const rightFilename = path.basename(lastArg).startsWith('start') - const rightContents = fs.readFileSync(lastArg, { encoding: 'utf8' }) - .trim().endsWith('foo') - return rightExtension && rightFilename && rightContents - }) + const [scriptShell, scriptArgs] = makeSpawnArgs({ path: npm.prefix, cmd: 'node ./test-start.js' }) + // we're calling the script with 'foo' as an argument, so add that to the script + let scriptContent = scriptArgs.pop() + scriptContent += ' foo' + scriptArgs.push(scriptContent) + const script = spawk.spawn(scriptShell, scriptArgs) await npm.exec('start', ['foo']) t.ok(script.called, 'script ran') }) diff --git a/test/lib/commands/stop.js b/test/lib/commands/stop.js index 1a4abd0b3..72c2843ad 100644 --- a/test/lib/commands/stop.js +++ b/test/lib/commands/stop.js @@ -1,5 +1,3 @@ -const fs = require('fs') -const path = require('path') const t = require('tap') const tspawk = require('../../fixtures/tspawk') const { load: loadMockNpm } = require('../../fixtures/mock-npm') @@ -26,15 +24,11 @@ t.test('should run stop script from package.json', async t => { loglevel: 'silent', }, }) - const [scriptShell] = makeSpawnArgs({ path: npm.prefix, cmd: 'node ./test-stop.js' }) - const script = spawk.spawn(scriptShell, (args) => { - const lastArg = args[args.length - 1] - const rightExtension = lastArg.endsWith('.cmd') || lastArg.endsWith('.sh') - const rightFilename = path.basename(lastArg).startsWith('stop') - const rightContents = fs.readFileSync(lastArg, { encoding: 'utf8' }) - .trim().endsWith('foo') - return rightExtension && rightFilename && rightContents - }) + const [scriptShell, scriptArgs] = makeSpawnArgs({ path: npm.prefix, cmd: 'node ./test-stop.js' }) + let scriptContent = scriptArgs.pop() + scriptContent += ' foo' + scriptArgs.push(scriptContent) + const script = spawk.spawn(scriptShell, scriptArgs) await npm.exec('stop', ['foo']) t.ok(script.called, 'script ran') }) diff --git a/test/lib/commands/test.js b/test/lib/commands/test.js index c6d3f530b..a6a6f723a 100644 --- a/test/lib/commands/test.js +++ b/test/lib/commands/test.js @@ -1,5 +1,3 @@ -const fs = require('fs') -const path = require('path') const t = require('tap') const tspawk = require('../../fixtures/tspawk') const { load: loadMockNpm } = require('../../fixtures/mock-npm') @@ -26,15 +24,11 @@ t.test('should run test script from package.json', async t => { loglevel: 'silent', }, }) - const [scriptShell] = makeSpawnArgs({ path: npm.prefix, cmd: 'node ./test-test.js' }) - const script = spawk.spawn(scriptShell, (args) => { - const lastArg = args[args.length - 1] - const rightExtension = lastArg.endsWith('.cmd') || lastArg.endsWith('.sh') - const rightFilename = path.basename(lastArg).startsWith('test') - const rightContents = fs.readFileSync(lastArg, { encoding: 'utf8' }) - .trim().endsWith('foo') - return rightExtension && rightFilename && rightContents - }) + const [scriptShell, scriptArgs] = makeSpawnArgs({ path: npm.prefix, cmd: 'node ./test-test.js' }) + let scriptContent = scriptArgs.pop() + scriptContent += ' foo' + scriptArgs.push(scriptContent) + const script = spawk.spawn(scriptShell, scriptArgs) await npm.exec('test', ['foo']) t.ok(script.called, 'script ran') }) -- cgit v1.2.3