-
Notifications
You must be signed in to change notification settings - Fork 31.2k
/
Copy pathtest-node-run.js
270 lines (242 loc) Β· 9.45 KB
/
test-node-run.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
'use strict';
const common = require('../common');
common.requireNoPackageJSONAbove();
const { it, describe } = require('node:test');
const assert = require('node:assert');
const fixtures = require('../common/fixtures');
const envSuffix = common.isWindows ? '-windows' : '';
describe('node --run [command]', () => {
it('returns error on non-existent file', async () => {
const child = await common.spawnPromisified(
process.execPath,
[ '--run', 'test'],
{ cwd: __dirname },
);
assert.match(child.stderr, /Can't find package\.json[\s\S]*/);
// Ensure we show the path that starting path for the search
assert(child.stderr.includes(__dirname));
assert.strictEqual(child.stdout, '');
assert.strictEqual(child.code, 1);
});
it('runs a valid command', async () => {
// Run a script that just log `no test specified`
const child = await common.spawnPromisified(
process.execPath,
[ '--run', 'test', '--no-warnings'],
{ cwd: fixtures.path('run-script') },
);
assert.match(child.stdout, /Error: no test specified/);
assert.strictEqual(child.code, 1);
});
it('adds node_modules/.bin to path', async () => {
const child = await common.spawnPromisified(
process.execPath,
[ '--run', `ada${envSuffix}`],
{ cwd: fixtures.path('run-script') },
);
assert.match(child.stdout, /06062023/);
assert.strictEqual(child.stderr, '');
assert.strictEqual(child.code, 0);
});
it('chdirs into package directory', async () => {
const child = await common.spawnPromisified(
process.execPath,
[ '--run', `pwd${envSuffix}`],
{ cwd: fixtures.path('run-script/sub-directory') },
);
assert.strictEqual(child.stdout.trim(), fixtures.path('run-script'));
assert.strictEqual(child.stderr, '');
assert.strictEqual(child.code, 0);
});
it('includes actionable info when possible', async () => {
{
const child = await common.spawnPromisified(
process.execPath,
[ '--run', 'missing'],
{ cwd: fixtures.path('run-script') },
);
assert.strictEqual(child.stdout, '');
assert(child.stderr.includes(fixtures.path('run-script/package.json')));
assert(child.stderr.includes('no test specified'));
assert.strictEqual(child.code, 1);
}
{
const child = await common.spawnPromisified(
process.execPath,
[ '--run', 'test'],
{ cwd: fixtures.path('run-script/missing-scripts') },
);
assert.strictEqual(child.stdout, '');
assert(child.stderr.includes(fixtures.path('run-script/missing-scripts/package.json')));
assert.strictEqual(child.code, 1);
}
{
const child = await common.spawnPromisified(
process.execPath,
[ '--run', 'test'],
{ cwd: fixtures.path('run-script/invalid-json') },
);
assert.strictEqual(child.stdout, '');
assert(child.stderr.includes(fixtures.path('run-script/invalid-json/package.json')));
assert.strictEqual(child.code, 1);
}
{
const child = await common.spawnPromisified(
process.execPath,
[ '--run', 'array'],
{ cwd: fixtures.path('run-script/invalid-schema') },
);
assert.strictEqual(child.stdout, '');
assert(child.stderr.includes(fixtures.path('run-script/invalid-schema/package.json')));
assert.strictEqual(child.code, 1);
}
{
const child = await common.spawnPromisified(
process.execPath,
[ '--run', 'boolean'],
{ cwd: fixtures.path('run-script/invalid-schema') },
);
assert.strictEqual(child.stdout, '');
assert(child.stderr.includes(fixtures.path('run-script/invalid-schema/package.json')));
assert.strictEqual(child.code, 1);
}
{
const child = await common.spawnPromisified(
process.execPath,
[ '--run', 'null'],
{ cwd: fixtures.path('run-script/invalid-schema') },
);
assert.strictEqual(child.stdout, '');
assert(child.stderr.includes(fixtures.path('run-script/invalid-schema/package.json')));
assert.strictEqual(child.code, 1);
}
{
const child = await common.spawnPromisified(
process.execPath,
[ '--run', 'number'],
{ cwd: fixtures.path('run-script/invalid-schema') },
);
assert.strictEqual(child.stdout, '');
assert(child.stderr.includes(fixtures.path('run-script/invalid-schema/package.json')));
assert.strictEqual(child.code, 1);
}
{
const child = await common.spawnPromisified(
process.execPath,
[ '--run', 'object'],
{ cwd: fixtures.path('run-script/invalid-schema') },
);
assert.strictEqual(child.stdout, '');
assert(child.stderr.includes(fixtures.path('run-script/invalid-schema/package.json')));
assert.strictEqual(child.code, 1);
}
});
it('appends positional arguments', async () => {
const child = await common.spawnPromisified(
process.execPath,
[ '--run', `positional-args${envSuffix}`, '--', '--help "hello world test"', 'A', 'B', 'C'],
{ cwd: fixtures.path('run-script') },
);
if (common.isWindows) {
assert.match(child.stdout, /Arguments: '--help ""hello world test"" A B C'/);
} else {
assert.match(child.stdout, /Arguments: '--help "hello world test" A B C'/);
}
assert.match(child.stdout, /The total number of arguments are: 4/);
assert.strictEqual(child.stderr, '');
assert.strictEqual(child.code, 0);
});
it('should set PATH environment variable with paths appended with node_modules/.bin', async () => {
const child = await common.spawnPromisified(
process.execPath,
[ '--run', `path-env${envSuffix}`],
{ cwd: fixtures.path('run-script/sub-directory') },
);
assert.ok(child.stdout.includes(fixtures.path('run-script/node_modules/.bin')));
// The following test ensures that we do not add paths that does not contain
// "node_modules/.bin"
assert.ok(!child.stdout.includes(fixtures.path('node_modules/.bin')));
// The following test ensures that we add paths that contains "node_modules/.bin"
assert.ok(child.stdout.includes(fixtures.path('run-script/sub-directory/node_modules/.bin')));
assert.strictEqual(child.stderr, '');
assert.strictEqual(child.code, 0);
});
it('should set special environment variables', async () => {
const scriptName = `special-env-variables${envSuffix}`;
const packageJsonPath = fixtures.path('run-script/package.json');
const child = await common.spawnPromisified(
process.execPath,
[ '--run', scriptName],
{ cwd: fixtures.path('run-script') },
);
assert.ok(child.stdout.includes(scriptName));
assert.ok(child.stdout.includes(packageJsonPath));
assert.strictEqual(child.stderr, '');
assert.strictEqual(child.code, 0);
});
it('will search parent directories for a package.json file', async () => {
const packageJsonPath = fixtures.path('run-script/package.json');
const child = await common.spawnPromisified(
process.execPath,
[ '--run', `special-env-variables${envSuffix}`],
{ cwd: fixtures.path('run-script/sub-directory') },
);
assert.ok(child.stdout.includes(packageJsonPath));
assert.strictEqual(child.stderr, '');
assert.strictEqual(child.code, 0);
});
it('returns error on unparsable file', async () => {
const child = await common.spawnPromisified(
process.execPath,
[ '--run', 'test'],
{ cwd: fixtures.path('run-script/cannot-parse') },
);
assert.match(child.stderr, /Can't parse/);
assert.strictEqual(child.stdout, '');
assert.strictEqual(child.code, 1);
});
it('returns error when there is no "scripts" field file', async () => {
const child = await common.spawnPromisified(
process.execPath,
[ '--run', 'test'],
{ cwd: fixtures.path('run-script/cannot-find-script') },
);
assert.match(child.stderr, /Can't find "scripts" field in/);
assert.strictEqual(child.stdout, '');
assert.strictEqual(child.code, 1);
});
it('runs script in a custom working directory using --run-from', async () => {
const workingDir = fixtures.path('run-script');
const child = await common.spawnPromisified(
process.execPath,
[ '--run-from', workingDir, '--run', `pwd${envSuffix}` ],
{ env: fixtures.path('run-script/sub-directory') }
);
assert.strictEqual(child.stdout.trim(), workingDir);
assert.strictEqual(child.stderr, '');
assert.strictEqual(child.code, 0);
});
it('runs script in a custom working directory using --run-from is given a package.json', async () => {
const packageJson = fixtures.path('run-script/package.json');
const workingDir = fixtures.path('run-script');
const child = await common.spawnPromisified(
process.execPath,
[ '--run-from', packageJson, '--run', `pwd${envSuffix}` ],
{ env: fixtures.path('run-script/sub-directory') }
);
assert.strictEqual(child.stdout.trim(), workingDir);
assert.strictEqual(child.stderr, '');
assert.strictEqual(child.code, 0);
});
it('--run-from should be no-op when used without --run', async () => {
const packageJson = fixtures.path('run-script/package.json');
const child = await common.spawnPromisified(
process.execPath,
[ '--run-from', packageJson, '--print', 'process.cwd()' ],
{ cwd: process.cwd() }
);
assert.strictEqual(child.stdout.trim(), process.cwd());
assert.strictEqual(child.stderr, '');
assert.strictEqual(child.code, 0);
});
});