|
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 | + editor: 'testeditor', |
30 | 16 | }, |
31 | | - dir: resolve(__dirname, '../../../node_modules'), |
32 | | - exec: async (cmd, args) => { |
33 | | - rebuildArgs = args |
34 | | - if (rebuildFail) { |
35 | | - throw rebuildFail |
36 | | - } |
| 17 | + prefixDir: { |
| 18 | + node_modules: { |
| 19 | + semver: { |
| 20 | + 'package.json': JSON.stringify({ |
| 21 | + scripts: { |
| 22 | + install: 'testinstall', |
| 23 | + }, |
| 24 | + }), |
| 25 | + node_modules: { |
| 26 | + abbrev: {}, |
| 27 | + }, |
| 28 | + }, |
| 29 | + '@npmcli': { |
| 30 | + 'scoped-package': {}, |
| 31 | + }, |
| 32 | + }, |
37 | 33 | }, |
38 | 34 | } |
39 | 35 |
|
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 | 36 | t.test('npm edit', async t => { |
48 | | - t.teardown(() => { |
49 | | - rebuildArgs = null |
50 | | - editorBin = null |
51 | | - editorArgs = null |
52 | | - editorOpts = null |
53 | | - }) |
| 37 | + const { npm, joinedOutput } = await loadMockNpm(t, npmConfig) |
54 | 38 |
|
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') |
| 39 | + const semverPath = path.resolve(npm.prefix, 'node_modules', 'semver') |
| 40 | + const [scriptShell] = makeSpawnArgs({ |
| 41 | + event: 'install', |
| 42 | + path: npm.prefix, |
| 43 | + }) |
| 44 | + spawk.spawn('testeditor', [semverPath]) |
| 45 | + spawk.spawn( |
| 46 | + scriptShell, |
| 47 | + args => args.includes('testinstall'), |
| 48 | + { cwd: semverPath } |
| 49 | + ) |
| 50 | + await npm.exec('edit', ['semver']) |
| 51 | + t.match(joinedOutput(), 'rebuilt dependencies successfully') |
61 | 52 | }) |
62 | 53 |
|
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 |
| 54 | +t.test('rebuild failure', async t => { |
| 55 | + const { npm } = await loadMockNpm(t, npmConfig) |
| 56 | + const semverPath = path.resolve(npm.prefix, 'node_modules', 'semver') |
| 57 | + const [scriptShell] = makeSpawnArgs({ |
| 58 | + event: 'install', |
| 59 | + path: npm.prefix, |
70 | 60 | }) |
| 61 | + spawk.spawn('testeditor', [semverPath]) |
| 62 | + spawk.spawn( |
| 63 | + scriptShell, |
| 64 | + args => args.includes('testinstall'), |
| 65 | + { cwd: semverPath } |
| 66 | + ).exit(1).stdout('test error') |
| 67 | + await t.rejects( |
| 68 | + npm.exec('edit', ['semver']), |
| 69 | + { message: 'command failed' } |
| 70 | + ) |
| 71 | +}) |
71 | 72 |
|
72 | | - rebuildFail = new Error('test error') |
| 73 | +t.test('editor failure', async t => { |
| 74 | + const { npm } = await loadMockNpm(t, npmConfig) |
| 75 | + const semverPath = path.resolve(npm.prefix, 'node_modules', 'semver') |
| 76 | + spawk.spawn('testeditor', [semverPath]).exit(1).stdout('test editor failure') |
73 | 77 | await t.rejects( |
74 | | - edit.exec(['semver']), |
75 | | - { message: 'test error' } |
| 78 | + npm.exec('edit', ['semver']), |
| 79 | + { message: 'editor process exited with code: 1' } |
76 | 80 | ) |
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 | 81 | }) |
83 | 82 |
|
84 | 83 | 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' |
| 84 | + const { npm } = await loadMockNpm(t, { |
| 85 | + ...npmConfig, |
| 86 | + config: { |
| 87 | + editor: 'testeditor --flag', |
| 88 | + }, |
92 | 89 | }) |
93 | 90 |
|
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') |
| 91 | + const semverPath = path.resolve(npm.prefix, 'node_modules', 'semver') |
| 92 | + const [scriptShell] = makeSpawnArgs({ |
| 93 | + event: 'install', |
| 94 | + path: npm.prefix, |
| 95 | + }) |
| 96 | + spawk.spawn('testeditor', ['--flag', semverPath]) |
| 97 | + spawk.spawn( |
| 98 | + scriptShell, |
| 99 | + args => args.includes('testinstall'), |
| 100 | + { cwd: semverPath } |
| 101 | + ) |
| 102 | + await npm.exec('edit', ['semver']) |
101 | 103 | }) |
102 | 104 |
|
103 | 105 | t.test('npm edit no args', async t => { |
| 106 | + const { npm } = await loadMockNpm(t) |
104 | 107 | await t.rejects( |
105 | | - edit.exec([]), |
106 | | - /npm edit/, |
| 108 | + npm.exec('edit', []), |
| 109 | + { code: 'EUSAGE' }, |
107 | 110 | 'throws usage error' |
108 | 111 | ) |
109 | 112 | }) |
110 | 113 |
|
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 | | - }) |
| 114 | +t.test('npm edit nonexistent package', async t => { |
| 115 | + const { npm } = await loadMockNpm(t, npmConfig) |
119 | 116 |
|
120 | 117 | await t.rejects( |
121 | | - edit.exec(['semver']), |
122 | | - /lstat failed/, |
123 | | - 'user received correct error' |
| 118 | + npm.exec('edit', ['abbrev']), |
| 119 | + /lstat/ |
124 | 120 | ) |
125 | 121 | }) |
126 | 122 |
|
127 | | -t.test('npm edit editor exit code error propagates', async t => { |
128 | | - EDITOR_CODE = 137 |
129 | | - t.teardown(() => { |
130 | | - EDITOR_CODE = 0 |
131 | | - }) |
| 123 | +t.test('scoped package', async t => { |
| 124 | + const { npm } = await loadMockNpm(t, npmConfig) |
| 125 | + const scopedPath = path.resolve(npm.prefix, 'node_modules', '@npmcli', 'scoped-package') |
| 126 | + spawk.spawn('testeditor', [scopedPath]) |
| 127 | + await npm.exec('edit', ['@npmcli/scoped-package']) |
| 128 | +}) |
132 | 129 |
|
133 | | - await t.rejects( |
134 | | - edit.exec(['semver']), |
135 | | - /exited with code: 137/, |
136 | | - 'user received correct error' |
137 | | - ) |
| 130 | +t.test('subdependency', async t => { |
| 131 | + const { npm } = await loadMockNpm(t, npmConfig) |
| 132 | + const subdepPath = path.resolve(npm.prefix, 'node_modules', 'semver', 'node_modules', 'abbrev') |
| 133 | + spawk.spawn('testeditor', [subdepPath]) |
| 134 | + await npm.exec('edit', ['semver/abbrev']) |
138 | 135 | }) |
0 commit comments