Skip to content

Commit

Permalink
chore(tests): fix tests for @npmcli/run-script@4.1.0 update
Browse files Browse the repository at this point in the history
  • Loading branch information
nlf committed Jun 21, 2022
1 parent 7b9336f commit 2841706
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 16 deletions.
28 changes: 25 additions & 3 deletions test/lib/commands/edit.js
@@ -1,4 +1,5 @@
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 @@ -45,7 +46,14 @@ t.test('npm edit', async t => {
spawk.spawn('testeditor', [semverPath])
spawk.spawn(
scriptShell,
args => args.includes('testinstall'),
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 }
)
await npm.exec('edit', ['semver'])
Expand All @@ -62,7 +70,14 @@ t.test('rebuild failure', async t => {
spawk.spawn('testeditor', [semverPath])
spawk.spawn(
scriptShell,
args => args.includes('testinstall'),
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')
await t.rejects(
Expand Down Expand Up @@ -98,7 +113,14 @@ t.test('npm edit editor has flags', async t => {
spawk.spawn('testeditor', ['--flag', semverPath])
spawk.spawn(
scriptShell,
args => args.includes('testinstall'),
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 }
)
await npm.exec('edit', ['semver'])
Expand Down
10 changes: 8 additions & 2 deletions test/lib/commands/restart.js
@@ -1,3 +1,5 @@
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 Down Expand Up @@ -26,8 +28,12 @@ t.test('should run restart script from package.json', async t => {
})
const [scriptShell] = makeSpawnArgs({ path: npm.prefix })
const script = spawk.spawn(scriptShell, (args) => {
t.ok(args.includes('node ./test-restart.js "foo"'), 'ran restart script with extra args')
return true
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
})
await npm.exec('restart', ['foo'])
t.ok(script.called, 'script ran')
Expand Down
11 changes: 8 additions & 3 deletions test/lib/commands/start.js
@@ -1,3 +1,5 @@
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 @@ -10,7 +12,6 @@ const spawk = tspawk(t)
const makeSpawnArgs = require('@npmcli/run-script/lib/make-spawn-args.js')

t.test('should run start script from package.json', async t => {
t.plan(2)
const { npm } = await loadMockNpm(t, {
prefixDir: {
'package.json': JSON.stringify({
Expand All @@ -27,8 +28,12 @@ t.test('should run start script from package.json', async t => {
})
const [scriptShell] = makeSpawnArgs({ path: npm.prefix })
const script = spawk.spawn(scriptShell, (args) => {
t.ok(args.includes('node ./test-start.js "foo"'), 'ran start script with extra args')
return true
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
})
await npm.exec('start', ['foo'])
t.ok(script.called, 'script ran')
Expand Down
10 changes: 8 additions & 2 deletions test/lib/commands/stop.js
@@ -1,3 +1,5 @@
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 Down Expand Up @@ -26,8 +28,12 @@ t.test('should run stop script from package.json', async t => {
})
const [scriptShell] = makeSpawnArgs({ path: npm.prefix })
const script = spawk.spawn(scriptShell, (args) => {
t.ok(args.includes('node ./test-stop.js "foo"'), 'ran stop script with extra args')
return true
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
})
await npm.exec('stop', ['foo'])
t.ok(script.called, 'script ran')
Expand Down
10 changes: 8 additions & 2 deletions test/lib/commands/test.js
@@ -1,3 +1,5 @@
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 Down Expand Up @@ -26,8 +28,12 @@ t.test('should run test script from package.json', async t => {
})
const [scriptShell] = makeSpawnArgs({ path: npm.prefix })
const script = spawk.spawn(scriptShell, (args) => {
t.ok(args.includes('node ./test-test.js "foo"'), 'ran test script with extra args')
return true
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
})
await npm.exec('test', ['foo'])
t.ok(script.called, 'script ran')
Expand Down
Expand Up @@ -8,7 +8,7 @@
exports[`test/arborist/rebuild.js TAP verify dep flags in script environments > saved script results 1`] = `
Array [
Object {
"cmd": "node ../../env.js",
"cmd": "{TMP}/postinstall-{TIMESTAMP}",
"code": 0,
"event": "postinstall",
"pkg": Object {
Expand All @@ -30,7 +30,7 @@ Array [
"stdout": "npm_package_dev\\n",
},
Object {
"cmd": "node ../../env.js",
"cmd": "{TMP}/postinstall-{TIMESTAMP}",
"code": 0,
"event": "postinstall",
"pkg": Object {
Expand All @@ -46,7 +46,7 @@ Array [
"stdout": "npm_package_dev_optional\\n",
},
Object {
"cmd": "node ../../env.js",
"cmd": "{TMP}/postinstall-{TIMESTAMP}",
"code": 0,
"event": "postinstall",
"pkg": Object {
Expand All @@ -66,7 +66,7 @@ Array [
),
},
Object {
"cmd": "node ../../env.js",
"cmd": "{TMP}/postinstall-{TIMESTAMP}",
"code": 0,
"event": "postinstall",
"pkg": Object {
Expand Down
6 changes: 6 additions & 0 deletions workspaces/arborist/test/arborist/rebuild.js
Expand Up @@ -2,6 +2,7 @@ const t = require('tap')
const _trashList = Symbol.for('trashList')
const Arborist = require('../../lib/arborist/index.js')
const { resolve, dirname } = require('path')
const os = require('os')
const fs = require('fs')
const fixtures = resolve(__dirname, '../fixtures')
const relpath = require('../../lib/relpath.js')
Expand Down Expand Up @@ -185,6 +186,11 @@ t.test('verify dep flags in script environments', async t => {
localeCompare(patha, pathb) || localeCompare(eventa, eventb))
.map(({ pkg, event, cmd, code, signal, stdout, stderr }) =>
({ pkg, event, cmd, code, signal, stdout, stderr }))
t.cleanSnapshot = (input) => {
return input.replace(new RegExp(os.tmpdir().replace(/\\/g, '\\\\\\\\'), 'g'), '{TMP}')
.replace(/\\\\/g, '/')
.replace(/(\d+)\.(?:sh|cmd)/g, '{TIMESTAMP}')
}
t.matchSnapshot(saved, 'saved script results')

for (const [pkg, flags] of Object.entries(expect)) {
Expand Down

0 comments on commit 2841706

Please sign in to comment.