/
edit.js
131 lines (109 loc) · 3.59 KB
/
edit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const t = require('tap')
const path = require('path')
const tspawk = require('../../fixtures/tspawk')
const { load: loadMockNpm } = require('../../fixtures/mock-npm')
const spawk = tspawk(t)
const npmConfig = {
config: {
'ignore-scripts': false,
editor: 'testeditor',
scriptShell: process.platform === 'win32' ? process.env.COMSPEC : 'sh',
},
prefixDir: {
node_modules: {
semver: {
'package.json': JSON.stringify({
scripts: {
install: 'testinstall',
},
}),
node_modules: {
abbrev: {},
},
},
'@npmcli': {
'scoped-package': {},
},
},
},
}
const isCmdRe = /(?:^|\\)cmd(?:\.exe)?$/i
t.test('npm edit', async t => {
const { npm, joinedOutput } = await loadMockNpm(t, npmConfig)
const semverPath = path.resolve(npm.prefix, 'node_modules', 'semver')
spawk.spawn('testeditor', [semverPath])
const scriptShell = npm.config.get('scriptShell')
const scriptArgs = isCmdRe.test(scriptShell)
? ['/d', '/s', '/c', 'testinstall']
: ['-c', 'testinstall']
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')
spawk.spawn('testeditor', [semverPath])
const scriptShell = npm.config.get('scriptShell')
const scriptArgs = isCmdRe.test(scriptShell)
? ['/d', '/s', '/c', 'testinstall']
: ['-c', 'testinstall']
spawk.spawn(scriptShell, scriptArgs, { cwd: semverPath }).exit(1).stdout('test error')
await t.rejects(
npm.exec('edit', ['semver']),
{ message: 'command failed' }
)
})
t.test('editor failure', async t => {
const { npm } = await loadMockNpm(t, npmConfig)
const semverPath = path.resolve(npm.prefix, 'node_modules', 'semver')
spawk.spawn('testeditor', [semverPath]).exit(1).stdout('test editor failure')
await t.rejects(
npm.exec('edit', ['semver']),
{ message: 'editor process exited with code: 1' }
)
})
t.test('npm edit editor has flags', async t => {
const { npm } = await loadMockNpm(t, {
...npmConfig,
config: {
...npmConfig.config,
editor: 'testeditor --flag',
},
})
const semverPath = path.resolve(npm.prefix, 'node_modules', 'semver')
spawk.spawn('testeditor', ['--flag', semverPath])
const scriptShell = npm.config.get('scriptShell')
const scriptArgs = isCmdRe.test(scriptShell)
? ['/d', '/s', '/c', 'testinstall']
: ['-c', 'testinstall']
spawk.spawn(scriptShell, scriptArgs, { cwd: semverPath })
await npm.exec('edit', ['semver'])
})
t.test('npm edit no args', async t => {
const { npm } = await loadMockNpm(t)
await t.rejects(
npm.exec('edit', []),
{ code: 'EUSAGE' },
'throws usage error'
)
})
t.test('npm edit nonexistent package', async t => {
const { npm } = await loadMockNpm(t, npmConfig)
await t.rejects(
npm.exec('edit', ['abbrev']),
/lstat/
)
})
t.test('scoped package', async t => {
const { npm } = await loadMockNpm(t, npmConfig)
const scopedPath = path.resolve(npm.prefix, 'node_modules', '@npmcli', 'scoped-package')
spawk.spawn('testeditor', [scopedPath])
await npm.exec('edit', ['@npmcli/scoped-package'])
})
t.test('subdependency', async t => {
const { npm } = await loadMockNpm(t, npmConfig)
const subdepPath = path.resolve(npm.prefix, 'node_modules', 'semver', 'node_modules', 'abbrev')
spawk.spawn('testeditor', [subdepPath])
await npm.exec('edit', ['semver/abbrev'])
})