|
1 | 1 | const t = require('tap')
|
2 |
| -const requireInject = require('require-inject') |
3 |
| -const isWindows = require('../lib/is-windows.js') |
4 |
| - |
5 |
| -if (!process.env.__FAKE_TESTING_PLATFORM__) { |
6 |
| - const fake = isWindows ? 'posix' : 'win32' |
7 |
| - t.spawn(process.execPath, [__filename, fake], { env: { |
8 |
| - ...process.env, |
9 |
| - __FAKE_TESTING_PLATFORM__: fake, |
10 |
| - } }) |
11 |
| -} |
12 |
| - |
13 |
| -const { dirname } = require('path') |
14 |
| -const resolve = (...args) => { |
15 |
| - const root = isWindows ? 'C:\\Temp' : '/tmp' |
16 |
| - return [root, ...args].join(isWindows ? '\\' : '/') |
17 |
| -} |
18 |
| - |
19 |
| -const makeSpawnArgs = requireInject('../lib/make-spawn-args.js', { |
20 |
| - path: { |
21 |
| - dirname, |
22 |
| - resolve, |
| 2 | +const spawk = require('spawk') |
| 3 | +const runScript = require('..') |
| 4 | + |
| 5 | +const pkg = { |
| 6 | + name: '@npmcli/run-script-test-package', |
| 7 | + version: '1.0.0-test', |
| 8 | + config: { |
| 9 | + test_string: 'a value', |
| 10 | + test_array: ['a string', 'another string'], |
| 11 | + test_null: null, |
| 12 | + test_false: false, |
23 | 13 | },
|
24 |
| -}) |
25 |
| - |
26 |
| -if (isWindows) { |
27 |
| - t.test('windows', t => { |
28 |
| - const comSpec = process.env.ComSpec |
29 |
| - process.env.ComSpec = 'C:\\Windows\\System32\\cmd.exe' |
30 |
| - t.teardown(() => { |
31 |
| - process.env.ComSpec = comSpec |
32 |
| - }) |
33 |
| - |
34 |
| - t.test('simple script', (t) => { |
35 |
| - const [cmd, args, opts] = makeSpawnArgs({ |
36 |
| - event: 'event', |
37 |
| - path: 'path', |
38 |
| - cmd: 'script "quoted parameter"; second command', |
39 |
| - }) |
40 |
| - t.equal(cmd, 'script "quoted parameter"; second command') |
41 |
| - t.strictSame(args, []) |
42 |
| - t.hasStrict(opts, { |
43 |
| - env: { |
44 |
| - npm_package_json: 'C:\\Temp\\path\\package.json', |
45 |
| - npm_lifecycle_event: 'event', |
46 |
| - npm_lifecycle_script: 'script "quoted parameter"; second command', |
47 |
| - npm_config_node_gyp: require.resolve('node-gyp/bin/node-gyp.js'), |
48 |
| - }, |
49 |
| - shell: true, |
50 |
| - stdio: undefined, |
51 |
| - cwd: 'path', |
52 |
| - }, 'got expected options') |
53 |
| - |
54 |
| - t.end() |
55 |
| - }) |
56 |
| - |
57 |
| - t.test('event with invalid characters runs', (t) => { |
58 |
| - const [cmd, args, opts] = makeSpawnArgs({ |
59 |
| - event: 'event<:>\x03', // everything after the word "event" is invalid |
60 |
| - path: 'path', |
61 |
| - cmd: 'script "quoted parameter"; second command', |
62 |
| - }) |
63 |
| - t.equal(cmd, 'script "quoted parameter"; second command') |
64 |
| - t.strictSame(args, []) |
65 |
| - t.hasStrict(opts, { |
66 |
| - env: { |
67 |
| - npm_package_json: 'C:\\Temp\\path\\package.json', |
68 |
| - npm_lifecycle_event: 'event<:>\x03', |
69 |
| - npm_lifecycle_script: 'script "quoted parameter"; second command', |
70 |
| - npm_config_node_gyp: require.resolve('node-gyp/bin/node-gyp.js'), |
71 |
| - }, |
72 |
| - shell: true, |
73 |
| - stdio: undefined, |
74 |
| - cwd: 'path', |
75 |
| - }, 'got expected options') |
76 |
| - |
77 |
| - t.end() |
78 |
| - }) |
79 |
| - |
80 |
| - t.test('with a funky scriptShell', (t) => { |
81 |
| - const [cmd, args, opts] = makeSpawnArgs({ |
82 |
| - event: 'event', |
83 |
| - path: 'path', |
84 |
| - cmd: 'script "quoted parameter"; second command', |
85 |
| - scriptShell: 'blrpop', |
86 |
| - }) |
87 |
| - t.equal(cmd, 'script "quoted parameter"; second command') |
88 |
| - t.strictSame(args, []) |
89 |
| - t.hasStrict(opts, { |
90 |
| - env: { |
91 |
| - npm_package_json: 'C:\\Temp\\path\\package.json', |
92 |
| - npm_lifecycle_event: 'event', |
93 |
| - npm_lifecycle_script: 'script "quoted parameter"; second command', |
94 |
| - }, |
95 |
| - shell: 'blrpop', |
96 |
| - stdio: undefined, |
97 |
| - cwd: 'path', |
98 |
| - }, 'got expected options') |
99 |
| - |
100 |
| - t.end() |
101 |
| - }) |
102 |
| - |
103 |
| - t.test('with cmd.exe as scriptShell', (t) => { |
104 |
| - const [cmd, args, opts] = makeSpawnArgs({ |
105 |
| - event: 'event', |
106 |
| - path: 'path', |
107 |
| - cmd: 'script', |
108 |
| - args: ['"quoted parameter";', 'second command'], |
109 |
| - scriptShell: 'cmd.exe', |
110 |
| - }) |
111 |
| - t.equal(cmd, 'script') |
112 |
| - t.strictSame(args, ['"quoted parameter";', 'second command']) |
113 |
| - t.hasStrict(opts, { |
114 |
| - env: { |
115 |
| - npm_package_json: 'C:\\Temp\\path\\package.json', |
116 |
| - npm_lifecycle_event: 'event', |
117 |
| - npm_lifecycle_script: 'script', |
118 |
| - }, |
119 |
| - shell: 'cmd.exe', |
120 |
| - stdio: undefined, |
121 |
| - cwd: 'path', |
122 |
| - }, 'got expected options') |
123 |
| - |
124 |
| - t.end() |
125 |
| - }) |
| 14 | + engines: { |
| 15 | + node: '>0.10', |
| 16 | + npm: '>1.2.3', |
| 17 | + }, |
| 18 | + bin: 'index.js', |
| 19 | + scripts: { |
| 20 | + test: 'echo test', |
| 21 | + 'weird<x>\x04': 'echo weird', |
| 22 | + }, |
| 23 | +} |
126 | 24 |
|
127 |
| - t.end() |
| 25 | +t.test('spawn args', async t => { |
| 26 | + const testdir = t.testdir({}) |
| 27 | + await t.test('defaults', async t => { |
| 28 | + spawk.spawn( |
| 29 | + /.*/, |
| 30 | + a => a.includes('echo test'), |
| 31 | + e => { |
| 32 | + return e.cwd === testdir && |
| 33 | + e.stdio === 'pipe' && |
| 34 | + e.stdioString === undefined && |
| 35 | + e.shell === false && |
| 36 | + e.env.npm_package_json.endsWith('package.json') && |
| 37 | + e.env.npm_package_name === pkg.name && |
| 38 | + e.env.npm_package_version === pkg.version && |
| 39 | + e.env.npm_package_config_test_null === '' && |
| 40 | + e.env.npm_package_config_test_false === '' && |
| 41 | + e.env.npm_package_config_test_string === pkg.config.test_string && |
| 42 | + e.env.npm_package_config_test_array === pkg.config.test_array.join('\n\n') && |
| 43 | + e.env.npm_package_bin === pkg.bin && |
| 44 | + e.env.npm_package_engines_npm === pkg.engines.npm && |
| 45 | + e.env.npm_package_engines_node === pkg.engines.node && |
| 46 | + e.env.npm_lifecycle_event === 'test' && |
| 47 | + e.env.npm_lifecycle_script === 'echo test' && |
| 48 | + e.env.npm_config_node_gyp === require.resolve('node-gyp/bin/node-gyp.js') |
| 49 | + } |
| 50 | + ) |
| 51 | + await t.resolves(() => runScript({ |
| 52 | + pkg, |
| 53 | + path: testdir, |
| 54 | + event: 'test', |
| 55 | + })) |
| 56 | + t.ok(spawk.done()) |
128 | 57 | })
|
129 |
| -} else { |
130 |
| - t.test('posix', t => { |
131 |
| - t.test('simple script', (t) => { |
132 |
| - const [cmd, args, opts] = makeSpawnArgs({ |
133 |
| - event: 'event', |
134 |
| - path: 'path', |
135 |
| - cmd: 'script', |
136 |
| - args: ['"quoted parameter";', 'second command'], |
137 |
| - }) |
138 |
| - t.equal(cmd, 'script') |
139 |
| - t.strictSame(args, ['"quoted parameter";', 'second command']) |
140 |
| - t.hasStrict(opts, { |
141 |
| - env: { |
142 |
| - npm_package_json: '/tmp/path/package.json', |
143 |
| - npm_lifecycle_event: 'event', |
144 |
| - npm_lifecycle_script: 'script', |
145 |
| - }, |
146 |
| - shell: true, |
147 |
| - stdio: undefined, |
148 |
| - cwd: 'path', |
149 |
| - windowsVerbatimArguments: undefined, |
150 |
| - }, 'got expected options') |
151 | 58 |
|
152 |
| - t.end() |
153 |
| - }) |
| 59 | + await t.test('provided env', async t => { |
| 60 | + spawk.spawn( |
| 61 | + /.*/, |
| 62 | + a => a.includes('echo test'), |
| 63 | + e => { |
| 64 | + return e.env.test_fixture === 'a string' |
| 65 | + } |
| 66 | + ) |
| 67 | + await t.resolves(() => runScript({ |
| 68 | + pkg, |
| 69 | + path: testdir, |
| 70 | + env: { |
| 71 | + test_fixture: 'a string', |
| 72 | + }, |
| 73 | + event: 'test', |
| 74 | + })) |
| 75 | + t.ok(spawk.done()) |
| 76 | + }) |
154 | 77 |
|
155 |
| - t.test('event with invalid characters runs', (t) => { |
156 |
| - const [cmd, args, opts] = makeSpawnArgs({ |
157 |
| - event: 'event<:>/\x04', |
158 |
| - path: 'path', |
159 |
| - cmd: 'script', |
160 |
| - args: ['"quoted parameter";', 'second command'], |
161 |
| - }) |
162 |
| - t.equal(cmd, 'script') |
163 |
| - t.strictSame(args, ['"quoted parameter";', 'second command']) |
164 |
| - t.hasStrict(opts, { |
165 |
| - env: { |
166 |
| - npm_package_json: '/tmp/path/package.json', |
167 |
| - npm_lifecycle_event: 'event<:>/\x04', |
168 |
| - npm_lifecycle_script: 'script', |
169 |
| - }, |
170 |
| - shell: true, |
171 |
| - stdio: undefined, |
172 |
| - cwd: 'path', |
173 |
| - windowsVerbatimArguments: undefined, |
174 |
| - }, 'got expected options') |
| 78 | + await t.test('provided args', async t => { |
| 79 | + spawk.spawn( |
| 80 | + /.*/, |
| 81 | + a => a.find(arg => arg.includes('echo test') && arg.includes('argtest')) |
| 82 | + ) |
| 83 | + await t.resolves(() => runScript({ |
| 84 | + pkg, |
| 85 | + path: testdir, |
| 86 | + args: ['argtest'], |
| 87 | + event: 'test', |
| 88 | + })) |
| 89 | + t.ok(spawk.done()) |
| 90 | + }) |
175 | 91 |
|
176 |
| - t.end() |
177 |
| - }) |
| 92 | + t.test('event with invalid characters', async t => { |
| 93 | + spawk.spawn( |
| 94 | + /.*/, |
| 95 | + a => a.includes('echo weird'), |
| 96 | + e => { |
| 97 | + return e.env.npm_lifecycle_event === 'weird<x>\x04' && |
| 98 | + e.env.npm_lifecycle_script === 'echo weird' |
| 99 | + } |
| 100 | + ) |
| 101 | + await t.resolves(() => runScript({ |
| 102 | + pkg, |
| 103 | + path: testdir, |
| 104 | + event: 'weird<x>\x04', |
| 105 | + })) |
| 106 | + t.ok(spawk.done()) |
| 107 | + }) |
178 | 108 |
|
179 |
| - t.end() |
| 109 | + await t.test('provided binPaths', async t => { |
| 110 | + spawk.spawn( |
| 111 | + /.*/, |
| 112 | + false, |
| 113 | + e => (e.env.PATH || e.env.Path).startsWith('/tmp/test-fixture/binpath') |
| 114 | + ) |
| 115 | + await t.resolves(() => runScript({ |
| 116 | + pkg, |
| 117 | + binPaths: ['/tmp/test-fixture/binpath'], |
| 118 | + path: testdir, |
| 119 | + args: ['test arg'], |
| 120 | + event: 'test', |
| 121 | + })) |
| 122 | + t.ok(spawk.done()) |
180 | 123 | })
|
181 |
| -} |
| 124 | +}) |
0 commit comments