Skip to content

Commit 3e28f3c

Browse files
nlffritzy
authored andcommittedJun 22, 2022
chore(tests): fix tests for @npmcli/run-script@4.1.0 update
1 parent 2c06cee commit 3e28f3c

File tree

7 files changed

+67
-16
lines changed

7 files changed

+67
-16
lines changed
 

‎test/lib/commands/edit.js

+25-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const t = require('tap')
2+
const fs = require('fs')
23
const path = require('path')
34
const tspawk = require('../../fixtures/tspawk')
45
const { load: loadMockNpm } = require('../../fixtures/mock-npm')
@@ -45,7 +46,14 @@ t.test('npm edit', async t => {
4546
spawk.spawn('testeditor', [semverPath])
4647
spawk.spawn(
4748
scriptShell,
48-
args => args.includes('testinstall'),
49+
args => {
50+
const lastArg = args[args.length - 1]
51+
const rightExtension = lastArg.endsWith('.cmd') || lastArg.endsWith('.sh')
52+
const rightFilename = path.basename(lastArg).startsWith('install')
53+
const rightContents = fs.readFileSync(lastArg, { encoding: 'utf8' })
54+
.trim().endsWith('testinstall')
55+
return rightExtension && rightFilename && rightContents
56+
},
4957
{ cwd: semverPath }
5058
)
5159
await npm.exec('edit', ['semver'])
@@ -62,7 +70,14 @@ t.test('rebuild failure', async t => {
6270
spawk.spawn('testeditor', [semverPath])
6371
spawk.spawn(
6472
scriptShell,
65-
args => args.includes('testinstall'),
73+
args => {
74+
const lastArg = args[args.length - 1]
75+
const rightExtension = lastArg.endsWith('.cmd') || lastArg.endsWith('.sh')
76+
const rightFilename = path.basename(lastArg).startsWith('install')
77+
const rightContents = fs.readFileSync(lastArg, { encoding: 'utf8' })
78+
.trim().endsWith('testinstall')
79+
return rightExtension && rightFilename && rightContents
80+
},
6681
{ cwd: semverPath }
6782
).exit(1).stdout('test error')
6883
await t.rejects(
@@ -98,7 +113,14 @@ t.test('npm edit editor has flags', async t => {
98113
spawk.spawn('testeditor', ['--flag', semverPath])
99114
spawk.spawn(
100115
scriptShell,
101-
args => args.includes('testinstall'),
116+
args => {
117+
const lastArg = args[args.length - 1]
118+
const rightExtension = lastArg.endsWith('.cmd') || lastArg.endsWith('.sh')
119+
const rightFilename = path.basename(lastArg).startsWith('install')
120+
const rightContents = fs.readFileSync(lastArg, { encoding: 'utf8' })
121+
.trim().endsWith('testinstall')
122+
return rightExtension && rightFilename && rightContents
123+
},
102124
{ cwd: semverPath }
103125
)
104126
await npm.exec('edit', ['semver'])

‎test/lib/commands/restart.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const fs = require('fs')
2+
const path = require('path')
13
const t = require('tap')
24
const tspawk = require('../../fixtures/tspawk')
35
const { load: loadMockNpm } = require('../../fixtures/mock-npm')
@@ -26,8 +28,12 @@ t.test('should run restart script from package.json', async t => {
2628
})
2729
const [scriptShell] = makeSpawnArgs({ path: npm.prefix })
2830
const script = spawk.spawn(scriptShell, (args) => {
29-
t.ok(args.includes('node ./test-restart.js "foo"'), 'ran restart script with extra args')
30-
return true
31+
const lastArg = args[args.length - 1]
32+
const rightExtension = lastArg.endsWith('.cmd') || lastArg.endsWith('.sh')
33+
const rightFilename = path.basename(lastArg).startsWith('restart')
34+
const rightContents = fs.readFileSync(lastArg, { encoding: 'utf8' })
35+
.trim().endsWith('foo')
36+
return rightExtension && rightFilename && rightContents
3137
})
3238
await npm.exec('restart', ['foo'])
3339
t.ok(script.called, 'script ran')

‎test/lib/commands/start.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const fs = require('fs')
2+
const path = require('path')
13
const t = require('tap')
24
const tspawk = require('../../fixtures/tspawk')
35
const { load: loadMockNpm } = require('../../fixtures/mock-npm')
@@ -10,7 +12,6 @@ const spawk = tspawk(t)
1012
const makeSpawnArgs = require('@npmcli/run-script/lib/make-spawn-args.js')
1113

1214
t.test('should run start script from package.json', async t => {
13-
t.plan(2)
1415
const { npm } = await loadMockNpm(t, {
1516
prefixDir: {
1617
'package.json': JSON.stringify({
@@ -27,8 +28,12 @@ t.test('should run start script from package.json', async t => {
2728
})
2829
const [scriptShell] = makeSpawnArgs({ path: npm.prefix })
2930
const script = spawk.spawn(scriptShell, (args) => {
30-
t.ok(args.includes('node ./test-start.js "foo"'), 'ran start script with extra args')
31-
return true
31+
const lastArg = args[args.length - 1]
32+
const rightExtension = lastArg.endsWith('.cmd') || lastArg.endsWith('.sh')
33+
const rightFilename = path.basename(lastArg).startsWith('start')
34+
const rightContents = fs.readFileSync(lastArg, { encoding: 'utf8' })
35+
.trim().endsWith('foo')
36+
return rightExtension && rightFilename && rightContents
3237
})
3338
await npm.exec('start', ['foo'])
3439
t.ok(script.called, 'script ran')

‎test/lib/commands/stop.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const fs = require('fs')
2+
const path = require('path')
13
const t = require('tap')
24
const tspawk = require('../../fixtures/tspawk')
35
const { load: loadMockNpm } = require('../../fixtures/mock-npm')
@@ -26,8 +28,12 @@ t.test('should run stop script from package.json', async t => {
2628
})
2729
const [scriptShell] = makeSpawnArgs({ path: npm.prefix })
2830
const script = spawk.spawn(scriptShell, (args) => {
29-
t.ok(args.includes('node ./test-stop.js "foo"'), 'ran stop script with extra args')
30-
return true
31+
const lastArg = args[args.length - 1]
32+
const rightExtension = lastArg.endsWith('.cmd') || lastArg.endsWith('.sh')
33+
const rightFilename = path.basename(lastArg).startsWith('stop')
34+
const rightContents = fs.readFileSync(lastArg, { encoding: 'utf8' })
35+
.trim().endsWith('foo')
36+
return rightExtension && rightFilename && rightContents
3137
})
3238
await npm.exec('stop', ['foo'])
3339
t.ok(script.called, 'script ran')

‎test/lib/commands/test.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const fs = require('fs')
2+
const path = require('path')
13
const t = require('tap')
24
const tspawk = require('../../fixtures/tspawk')
35
const { load: loadMockNpm } = require('../../fixtures/mock-npm')
@@ -26,8 +28,12 @@ t.test('should run test script from package.json', async t => {
2628
})
2729
const [scriptShell] = makeSpawnArgs({ path: npm.prefix })
2830
const script = spawk.spawn(scriptShell, (args) => {
29-
t.ok(args.includes('node ./test-test.js "foo"'), 'ran test script with extra args')
30-
return true
31+
const lastArg = args[args.length - 1]
32+
const rightExtension = lastArg.endsWith('.cmd') || lastArg.endsWith('.sh')
33+
const rightFilename = path.basename(lastArg).startsWith('test')
34+
const rightContents = fs.readFileSync(lastArg, { encoding: 'utf8' })
35+
.trim().endsWith('foo')
36+
return rightExtension && rightFilename && rightContents
3137
})
3238
await npm.exec('test', ['foo'])
3339
t.ok(script.called, 'script ran')

‎workspaces/arborist/tap-snapshots/test/arborist/rebuild.js.test.cjs

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
exports[`test/arborist/rebuild.js TAP verify dep flags in script environments > saved script results 1`] = `
99
Array [
1010
Object {
11-
"cmd": "node ../../env.js",
11+
"cmd": "{TMP}/postinstall-{TIMESTAMP}",
1212
"code": 0,
1313
"event": "postinstall",
1414
"pkg": Object {
@@ -30,7 +30,7 @@ Array [
3030
"stdout": "npm_package_dev\\n",
3131
},
3232
Object {
33-
"cmd": "node ../../env.js",
33+
"cmd": "{TMP}/postinstall-{TIMESTAMP}",
3434
"code": 0,
3535
"event": "postinstall",
3636
"pkg": Object {
@@ -46,7 +46,7 @@ Array [
4646
"stdout": "npm_package_dev_optional\\n",
4747
},
4848
Object {
49-
"cmd": "node ../../env.js",
49+
"cmd": "{TMP}/postinstall-{TIMESTAMP}",
5050
"code": 0,
5151
"event": "postinstall",
5252
"pkg": Object {
@@ -66,7 +66,7 @@ Array [
6666
),
6767
},
6868
Object {
69-
"cmd": "node ../../env.js",
69+
"cmd": "{TMP}/postinstall-{TIMESTAMP}",
7070
"code": 0,
7171
"event": "postinstall",
7272
"pkg": Object {

‎workspaces/arborist/test/arborist/rebuild.js

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const t = require('tap')
22
const _trashList = Symbol.for('trashList')
33
const Arborist = require('../../lib/arborist/index.js')
44
const { resolve, dirname } = require('path')
5+
const os = require('os')
56
const fs = require('fs')
67
const fixtures = resolve(__dirname, '../fixtures')
78
const relpath = require('../../lib/relpath.js')
@@ -185,6 +186,11 @@ t.test('verify dep flags in script environments', async t => {
185186
localeCompare(patha, pathb) || localeCompare(eventa, eventb))
186187
.map(({ pkg, event, cmd, code, signal, stdout, stderr }) =>
187188
({ pkg, event, cmd, code, signal, stdout, stderr }))
189+
t.cleanSnapshot = (input) => {
190+
return input.replace(new RegExp(os.tmpdir().replace(/\\/g, '\\\\\\\\'), 'g'), '{TMP}')
191+
.replace(/\\\\/g, '/')
192+
.replace(/(\d+)\.(?:sh|cmd)/g, '{TIMESTAMP}')
193+
}
188194
t.matchSnapshot(saved, 'saved script results')
189195

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

0 commit comments

Comments
 (0)
Please sign in to comment.