|
1 | 1 | const t = require('tap') |
2 | | -const { resolve } = require('path') |
3 | | -const { EventEmitter } = require('events') |
| 2 | +const path = require('path') |
| 3 | +const tspawk = require('../../fixtures/tspawk') |
| 4 | +const { load: loadMockNpm } = require('../../fixtures/mock-npm') |
4 | 5 |
|
5 | | -let editorBin = null |
6 | | -let editorArgs = null |
7 | | -let editorOpts = null |
8 | | -let EDITOR_CODE = 0 |
9 | | -const childProcess = { |
10 | | - spawn: (bin, args, opts) => { |
11 | | - // save for assertions |
12 | | - editorBin = bin |
13 | | - editorArgs = args |
14 | | - editorOpts = opts |
| 6 | +const spawk = tspawk(t) |
15 | 7 |
|
16 | | - const editorEvents = new EventEmitter() |
17 | | - process.nextTick(() => { |
18 | | - editorEvents.emit('exit', EDITOR_CODE) |
19 | | - }) |
20 | | - return editorEvents |
21 | | - }, |
22 | | -} |
| 8 | +// TODO this ... smells. npm "script-shell" config mentions defaults but those |
| 9 | +// are handled by run-script, not npm. So for now we have to tie tests to some |
| 10 | +// pretty specific internals of runScript |
| 11 | +const makeSpawnArgs = require('@npmcli/run-script/lib/make-spawn-args.js') |
23 | 12 |
|
24 | | -let rebuildArgs = null |
25 | | -let rebuildFail = null |
26 | | -let EDITOR = 'vim' |
27 | | -const npm = { |
| 13 | +const npmConfig = { |
28 | 14 | config: { |
29 | | - get: () => EDITOR, |
| 15 | + 'ignore-scripts': false, |
| 16 | + editor: 'testeditor', |
30 | 17 | }, |
31 | | - dir: resolve(__dirname, '../../../node_modules'), |
32 | | - exec: async (cmd, args) => { |
33 | | - rebuildArgs = args |
34 | | - if (rebuildFail) { |
35 | | - throw rebuildFail |
36 | | - } |
| 18 | + prefixDir: { |
| 19 | + node_modules: { |
| 20 | + semver: { |
| 21 | + 'package.json': JSON.stringify({ |
| 22 | + scripts: { |
| 23 | + install: 'testinstall', |
| 24 | + }, |
| 25 | + }), |
| 26 | + node_modules: { |
| 27 | + abbrev: {}, |
| 28 | + }, |
| 29 | + }, |
| 30 | + '@npmcli': { |
| 31 | + 'scoped-package': {}, |
| 32 | + }, |
| 33 | + }, |
37 | 34 | }, |
38 | 35 | } |
39 | 36 |
|
40 | | -const gracefulFs = require('graceful-fs') |
41 | | -const Edit = t.mock('../../../lib/commands/edit.js', { |
42 | | - child_process: childProcess, |
43 | | - 'graceful-fs': gracefulFs, |
44 | | -}) |
45 | | -const edit = new Edit(npm) |
46 | | - |
47 | 37 | t.test('npm edit', async t => { |
48 | | - t.teardown(() => { |
49 | | - rebuildArgs = null |
50 | | - editorBin = null |
51 | | - editorArgs = null |
52 | | - editorOpts = null |
53 | | - }) |
| 38 | + const { npm, joinedOutput } = await loadMockNpm(t, npmConfig) |
54 | 39 |
|
55 | | - await edit.exec(['semver']) |
56 | | - const path = resolve(__dirname, '../../../node_modules/semver') |
57 | | - t.strictSame(editorBin, EDITOR, 'used the correct editor') |
58 | | - t.strictSame(editorArgs, [path], 'edited the correct directory') |
59 | | - t.strictSame(editorOpts, { stdio: 'inherit' }, 'passed the correct opts') |
60 | | - t.strictSame(rebuildArgs, [path], 'passed the correct path to rebuild') |
| 40 | + const semverPath = path.resolve(npm.prefix, 'node_modules', 'semver') |
| 41 | + const [scriptShell] = makeSpawnArgs({ |
| 42 | + event: 'install', |
| 43 | + path: npm.prefix, |
| 44 | + }) |
| 45 | + spawk.spawn('testeditor', [semverPath]) |
| 46 | + spawk.spawn( |
| 47 | + scriptShell, |
| 48 | + args => args.includes('testinstall'), |
| 49 | + { cwd: semverPath } |
| 50 | + ) |
| 51 | + await npm.exec('edit', ['semver']) |
| 52 | + t.match(joinedOutput(), 'rebuilt dependencies successfully') |
61 | 53 | }) |
62 | 54 |
|
63 | | -t.test('rebuild fails', async t => { |
64 | | - t.teardown(() => { |
65 | | - rebuildFail = null |
66 | | - rebuildArgs = null |
67 | | - editorBin = null |
68 | | - editorArgs = null |
69 | | - editorOpts = null |
| 55 | +t.test('rebuild failure', async t => { |
| 56 | + const { npm } = await loadMockNpm(t, npmConfig) |
| 57 | + const semverPath = path.resolve(npm.prefix, 'node_modules', 'semver') |
| 58 | + const [scriptShell] = makeSpawnArgs({ |
| 59 | + event: 'install', |
| 60 | + path: npm.prefix, |
70 | 61 | }) |
| 62 | + spawk.spawn('testeditor', [semverPath]) |
| 63 | + spawk.spawn( |
| 64 | + scriptShell, |
| 65 | + args => args.includes('testinstall'), |
| 66 | + { cwd: semverPath } |
| 67 | + ).exit(1).stdout('test error') |
| 68 | + await t.rejects( |
| 69 | + npm.exec('edit', ['semver']), |
| 70 | + { message: 'command failed' } |
| 71 | + ) |
| 72 | +}) |
71 | 73 |
|
72 | | - rebuildFail = new Error('test error') |
| 74 | +t.test('editor failure', async t => { |
| 75 | + const { npm } = await loadMockNpm(t, npmConfig) |
| 76 | + const semverPath = path.resolve(npm.prefix, 'node_modules', 'semver') |
| 77 | + spawk.spawn('testeditor', [semverPath]).exit(1).stdout('test editor failure') |
73 | 78 | await t.rejects( |
74 | | - edit.exec(['semver']), |
75 | | - { message: 'test error' } |
| 79 | + npm.exec('edit', ['semver']), |
| 80 | + { message: 'editor process exited with code: 1' } |
76 | 81 | ) |
77 | | - const path = resolve(__dirname, '../../../node_modules/semver') |
78 | | - t.strictSame(editorBin, EDITOR, 'used the correct editor') |
79 | | - t.strictSame(editorArgs, [path], 'edited the correct directory') |
80 | | - t.strictSame(editorOpts, { stdio: 'inherit' }, 'passed the correct opts') |
81 | | - t.strictSame(rebuildArgs, [path], 'passed the correct path to rebuild') |
82 | 82 | }) |
83 | 83 |
|
84 | 84 | t.test('npm edit editor has flags', async t => { |
85 | | - EDITOR = 'code -w' |
86 | | - t.teardown(() => { |
87 | | - rebuildArgs = null |
88 | | - editorBin = null |
89 | | - editorArgs = null |
90 | | - editorOpts = null |
91 | | - EDITOR = 'vim' |
| 85 | + const { npm } = await loadMockNpm(t, { |
| 86 | + ...npmConfig, |
| 87 | + config: { |
| 88 | + ...npmConfig.config, |
| 89 | + editor: 'testeditor --flag', |
| 90 | + }, |
92 | 91 | }) |
93 | 92 |
|
94 | | - await edit.exec(['semver']) |
95 | | - |
96 | | - const path = resolve(__dirname, '../../../node_modules/semver') |
97 | | - t.strictSame(editorBin, 'code', 'used the correct editor') |
98 | | - t.strictSame(editorArgs, ['-w', path], 'edited the correct directory, keeping flags') |
99 | | - t.strictSame(editorOpts, { stdio: 'inherit' }, 'passed the correct opts') |
100 | | - t.strictSame(rebuildArgs, [path], 'passed the correct path to rebuild') |
| 93 | + const semverPath = path.resolve(npm.prefix, 'node_modules', 'semver') |
| 94 | + const [scriptShell] = makeSpawnArgs({ |
| 95 | + event: 'install', |
| 96 | + path: npm.prefix, |
| 97 | + }) |
| 98 | + spawk.spawn('testeditor', ['--flag', semverPath]) |
| 99 | + spawk.spawn( |
| 100 | + scriptShell, |
| 101 | + args => args.includes('testinstall'), |
| 102 | + { cwd: semverPath } |
| 103 | + ) |
| 104 | + await npm.exec('edit', ['semver']) |
101 | 105 | }) |
102 | 106 |
|
103 | 107 | t.test('npm edit no args', async t => { |
| 108 | + const { npm } = await loadMockNpm(t) |
104 | 109 | await t.rejects( |
105 | | - edit.exec([]), |
106 | | - /npm edit/, |
| 110 | + npm.exec('edit', []), |
| 111 | + { code: 'EUSAGE' }, |
107 | 112 | 'throws usage error' |
108 | 113 | ) |
109 | 114 | }) |
110 | 115 |
|
111 | | -t.test('npm edit lstat error propagates', async t => { |
112 | | - const _lstat = gracefulFs.lstat |
113 | | - gracefulFs.lstat = (dir, cb) => { |
114 | | - return cb(new Error('lstat failed')) |
115 | | - } |
116 | | - t.teardown(() => { |
117 | | - gracefulFs.lstat = _lstat |
118 | | - }) |
| 116 | +t.test('npm edit nonexistent package', async t => { |
| 117 | + const { npm } = await loadMockNpm(t, npmConfig) |
119 | 118 |
|
120 | 119 | await t.rejects( |
121 | | - edit.exec(['semver']), |
122 | | - /lstat failed/, |
123 | | - 'user received correct error' |
| 120 | + npm.exec('edit', ['abbrev']), |
| 121 | + /lstat/ |
124 | 122 | ) |
125 | 123 | }) |
126 | 124 |
|
127 | | -t.test('npm edit editor exit code error propagates', async t => { |
128 | | - EDITOR_CODE = 137 |
129 | | - t.teardown(() => { |
130 | | - EDITOR_CODE = 0 |
131 | | - }) |
| 125 | +t.test('scoped package', async t => { |
| 126 | + const { npm } = await loadMockNpm(t, npmConfig) |
| 127 | + const scopedPath = path.resolve(npm.prefix, 'node_modules', '@npmcli', 'scoped-package') |
| 128 | + spawk.spawn('testeditor', [scopedPath]) |
| 129 | + await npm.exec('edit', ['@npmcli/scoped-package']) |
| 130 | +}) |
132 | 131 |
|
133 | | - await t.rejects( |
134 | | - edit.exec(['semver']), |
135 | | - /exited with code: 137/, |
136 | | - 'user received correct error' |
137 | | - ) |
| 132 | +t.test('subdependency', async t => { |
| 133 | + const { npm } = await loadMockNpm(t, npmConfig) |
| 134 | + const subdepPath = path.resolve(npm.prefix, 'node_modules', 'semver', 'node_modules', 'abbrev') |
| 135 | + spawk.spawn('testeditor', [subdepPath]) |
| 136 | + await npm.exec('edit', ['semver/abbrev']) |
138 | 137 | }) |
0 commit comments