Skip to content

Commit

Permalink
chore: fix tests for @npmcli/run-script@4.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
nlf authored and wraithgar committed Aug 10, 2022
1 parent 76e477c commit 222f4fe
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 83 deletions.
46 changes: 6 additions & 40 deletions 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')
Expand Down Expand Up @@ -39,49 +38,27 @@ 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')
})

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' }
Expand All @@ -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'])
})

Expand Down
17 changes: 7 additions & 10 deletions 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')
Expand All @@ -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')
})
17 changes: 6 additions & 11 deletions 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')
Expand All @@ -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')
})
16 changes: 5 additions & 11 deletions 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')
Expand All @@ -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')
})
16 changes: 5 additions & 11 deletions 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')
Expand All @@ -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')
})

0 comments on commit 222f4fe

Please sign in to comment.