Skip to content

Commit 222f4fe

Browse files
nlfwraithgar
authored andcommitted
chore: fix tests for @npmcli/run-script@4.2.1
1 parent 76e477c commit 222f4fe

File tree

5 files changed

+29
-83
lines changed

5 files changed

+29
-83
lines changed

test/lib/commands/edit.js

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const t = require('tap')
2-
const fs = require('fs')
32
const path = require('path')
43
const tspawk = require('../../fixtures/tspawk')
54
const { load: loadMockNpm } = require('../../fixtures/mock-npm')
@@ -39,49 +38,27 @@ t.test('npm edit', async t => {
3938
const { npm, joinedOutput } = await loadMockNpm(t, npmConfig)
4039

4140
const semverPath = path.resolve(npm.prefix, 'node_modules', 'semver')
42-
const [scriptShell] = makeSpawnArgs({
41+
const [scriptShell, scriptArgs] = makeSpawnArgs({
4342
event: 'install',
4443
path: npm.prefix,
4544
cmd: 'testinstall',
4645
})
4746
spawk.spawn('testeditor', [semverPath])
48-
spawk.spawn(
49-
scriptShell,
50-
args => {
51-
const lastArg = args[args.length - 1]
52-
const rightExtension = lastArg.endsWith('.cmd') || lastArg.endsWith('.sh')
53-
const rightFilename = path.basename(lastArg).startsWith('install')
54-
const rightContents = fs.readFileSync(lastArg, { encoding: 'utf8' })
55-
.trim().endsWith('testinstall')
56-
return rightExtension && rightFilename && rightContents
57-
},
58-
{ cwd: semverPath }
59-
)
47+
spawk.spawn(scriptShell, scriptArgs, { cwd: semverPath })
6048
await npm.exec('edit', ['semver'])
6149
t.match(joinedOutput(), 'rebuilt dependencies successfully')
6250
})
6351

6452
t.test('rebuild failure', async t => {
6553
const { npm } = await loadMockNpm(t, npmConfig)
6654
const semverPath = path.resolve(npm.prefix, 'node_modules', 'semver')
67-
const [scriptShell] = makeSpawnArgs({
55+
const [scriptShell, scriptArgs] = makeSpawnArgs({
6856
event: 'install',
6957
path: npm.prefix,
7058
cmd: 'testinstall',
7159
})
7260
spawk.spawn('testeditor', [semverPath])
73-
spawk.spawn(
74-
scriptShell,
75-
args => {
76-
const lastArg = args[args.length - 1]
77-
const rightExtension = lastArg.endsWith('.cmd') || lastArg.endsWith('.sh')
78-
const rightFilename = path.basename(lastArg).startsWith('install')
79-
const rightContents = fs.readFileSync(lastArg, { encoding: 'utf8' })
80-
.trim().endsWith('testinstall')
81-
return rightExtension && rightFilename && rightContents
82-
},
83-
{ cwd: semverPath }
84-
).exit(1).stdout('test error')
61+
spawk.spawn(scriptShell, scriptArgs, { cwd: semverPath }).exit(1).stdout('test error')
8562
await t.rejects(
8663
npm.exec('edit', ['semver']),
8764
{ message: 'command failed' }
@@ -108,24 +85,13 @@ t.test('npm edit editor has flags', async t => {
10885
})
10986

11087
const semverPath = path.resolve(npm.prefix, 'node_modules', 'semver')
111-
const [scriptShell] = makeSpawnArgs({
88+
const [scriptShell, scriptArgs] = makeSpawnArgs({
11289
event: 'install',
11390
path: npm.prefix,
11491
cmd: 'testinstall',
11592
})
11693
spawk.spawn('testeditor', ['--flag', semverPath])
117-
spawk.spawn(
118-
scriptShell,
119-
args => {
120-
const lastArg = args[args.length - 1]
121-
const rightExtension = lastArg.endsWith('.cmd') || lastArg.endsWith('.sh')
122-
const rightFilename = path.basename(lastArg).startsWith('install')
123-
const rightContents = fs.readFileSync(lastArg, { encoding: 'utf8' })
124-
.trim().endsWith('testinstall')
125-
return rightExtension && rightFilename && rightContents
126-
},
127-
{ cwd: semverPath }
128-
)
94+
spawk.spawn(scriptShell, scriptArgs, { cwd: semverPath })
12995
await npm.exec('edit', ['semver'])
13096
})
13197

test/lib/commands/restart.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const fs = require('fs')
2-
const path = require('path')
31
const t = require('tap')
42
const tspawk = require('../../fixtures/tspawk')
53
const { load: loadMockNpm } = require('../../fixtures/mock-npm')
@@ -26,15 +24,14 @@ t.test('should run restart script from package.json', async t => {
2624
loglevel: 'silent',
2725
},
2826
})
29-
const [scriptShell] = makeSpawnArgs({ path: npm.prefix, cmd: 'node ./test-restart.js' })
30-
const script = spawk.spawn(scriptShell, (args) => {
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
27+
const [scriptShell, scriptArgs] = makeSpawnArgs({
28+
path: npm.prefix,
29+
cmd: 'node ./test-restart.js',
3730
})
31+
let scriptContent = scriptArgs.pop()
32+
scriptContent += ' foo'
33+
scriptArgs.push(scriptContent)
34+
const script = spawk.spawn(scriptShell, scriptArgs)
3835
await npm.exec('restart', ['foo'])
3936
t.ok(script.called, 'script ran')
4037
})

test/lib/commands/start.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const fs = require('fs')
2-
const path = require('path')
31
const t = require('tap')
42
const tspawk = require('../../fixtures/tspawk')
53
const { load: loadMockNpm } = require('../../fixtures/mock-npm')
@@ -26,15 +24,12 @@ t.test('should run start script from package.json', async t => {
2624
loglevel: 'silent',
2725
},
2826
})
29-
const [scriptShell] = makeSpawnArgs({ path: npm.prefix, cmd: 'node ./test-start.js' })
30-
const script = spawk.spawn(scriptShell, (args) => {
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
37-
})
27+
const [scriptShell, scriptArgs] = makeSpawnArgs({ path: npm.prefix, cmd: 'node ./test-start.js' })
28+
// we're calling the script with 'foo' as an argument, so add that to the script
29+
let scriptContent = scriptArgs.pop()
30+
scriptContent += ' foo'
31+
scriptArgs.push(scriptContent)
32+
const script = spawk.spawn(scriptShell, scriptArgs)
3833
await npm.exec('start', ['foo'])
3934
t.ok(script.called, 'script ran')
4035
})

test/lib/commands/stop.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const fs = require('fs')
2-
const path = require('path')
31
const t = require('tap')
42
const tspawk = require('../../fixtures/tspawk')
53
const { load: loadMockNpm } = require('../../fixtures/mock-npm')
@@ -26,15 +24,11 @@ t.test('should run stop script from package.json', async t => {
2624
loglevel: 'silent',
2725
},
2826
})
29-
const [scriptShell] = makeSpawnArgs({ path: npm.prefix, cmd: 'node ./test-stop.js' })
30-
const script = spawk.spawn(scriptShell, (args) => {
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
37-
})
27+
const [scriptShell, scriptArgs] = makeSpawnArgs({ path: npm.prefix, cmd: 'node ./test-stop.js' })
28+
let scriptContent = scriptArgs.pop()
29+
scriptContent += ' foo'
30+
scriptArgs.push(scriptContent)
31+
const script = spawk.spawn(scriptShell, scriptArgs)
3832
await npm.exec('stop', ['foo'])
3933
t.ok(script.called, 'script ran')
4034
})

test/lib/commands/test.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const fs = require('fs')
2-
const path = require('path')
31
const t = require('tap')
42
const tspawk = require('../../fixtures/tspawk')
53
const { load: loadMockNpm } = require('../../fixtures/mock-npm')
@@ -26,15 +24,11 @@ t.test('should run test script from package.json', async t => {
2624
loglevel: 'silent',
2725
},
2826
})
29-
const [scriptShell] = makeSpawnArgs({ path: npm.prefix, cmd: 'node ./test-test.js' })
30-
const script = spawk.spawn(scriptShell, (args) => {
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
37-
})
27+
const [scriptShell, scriptArgs] = makeSpawnArgs({ path: npm.prefix, cmd: 'node ./test-test.js' })
28+
let scriptContent = scriptArgs.pop()
29+
scriptContent += ' foo'
30+
scriptArgs.push(scriptContent)
31+
const script = spawk.spawn(scriptShell, scriptArgs)
3832
await npm.exec('test', ['foo'])
3933
t.ok(script.called, 'script ran')
4034
})

0 commit comments

Comments
 (0)