Repository navigation
Expand file tree
/
Copy pathstartPackager.test.js
More file actions
287 lines (267 loc) · 11.7 KB
/
Copy pathstartPackager.test.js
File metadata and controls
287 lines (267 loc) · 11.7 KB
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
const { execFileSync, spawn, spawnSync } = require('child_process');
const fs = require('fs');
const os = require('os');
const path = require('path');
const { createHermeticHost } = require('./e2eHermeticHost');
const repoRoot = path.resolve(__dirname, '../../..');
const scriptPath = path.join(repoRoot, 'scripts/e2e/start-packager.sh');
const rootPkg = JSON.parse(fs.readFileSync(path.join(repoRoot, 'package.json'), 'utf8'));
describe('start-packager.sh yarn surface', function () {
const src = fs.readFileSync(scriptPath, 'utf8');
it('honors RCT_METRO_PORT and interpolates TMPDIR with the listen port', function () {
expect(src).toMatch(/PORT="\$\{RCT_METRO_PORT:-/);
expect(src).toMatch(/RNFB_METRO_CACHE_ROOT:-\$\{HOME\}\/\.metro\}/);
expect(src).toMatch(/CACHE_DIR="\$\{METRO_CACHE_ROOT\}\/rnfb-\$\{PORT\}"/);
});
it('SIGTERM then SIGKILL only recorded listeners on the Metro port (no PGID / setsid)', function () {
expect(src).toMatch(/kill_port_listeners/);
expect(src).toMatch(/owned_port_listeners/);
expect(src).toMatch(/SIGTERM/);
expect(src).toMatch(/SIGKILL/);
expect(src).not.toMatch(/os\.setsid|setsid -w|exec-new-session/);
});
it('records Metro at spawn, so a start that never becomes ready stays this checkout’s', function () {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'rnfb-packager-spawn-'));
for (const [name, body] of [
['lsof', '#!/bin/bash\nexit 1\n'],
['curl', '#!/bin/bash\nexit 7\n'],
['watchman', '#!/bin/bash\nexit 0\n'],
// Stands in for `yarn react-native start`: alive, never ready.
['yarn', '#!/bin/bash\nexec /bin/sleep 60\n'],
]) {
fs.writeFileSync(path.join(dir, name), body);
fs.chmodSync(path.join(dir, name), 0o755);
}
const env = { ...process.env };
for (const key of Object.keys(env)) {
if (key.startsWith('RNFB_') || key.startsWith('E2E_') || key.startsWith('JET_')) {
delete env[key];
}
}
Object.assign(env, {
HOME: dir,
PATH: `${dir}:${env.PATH}`,
RNFB_E2E_RUN_RECORD_DIR: path.join(dir, 'rec'),
RNFB_METRO_LOG: path.join(dir, 'metro.log'),
RNFB_METRO_STATUS_WAIT_SEC: '1',
});
delete env.RCT_METRO_PORT;
let metroPid;
try {
const result = spawnSync('/bin/bash', [scriptPath, 'tests'], { encoding: 'utf8', env });
expect(result.status).toBe(1);
expect(result.stderr).toMatch(/timed out after 1s/);
const recDir = path.join(dir, 'rec', 'serial');
const entries = fs
.readdirSync(recDir)
.filter(f => f.startsWith('pid-'))
.map(f => fs.readFileSync(path.join(recDir, f), 'utf8').trim().split('\t'));
const metro = entries.find(fields => fields[3] === 'metro:8081');
expect(metro).toBeDefined();
metroPid = Number(metro[1]);
// The recorded pid is the still-running orphan Metro, not a dead wrapper.
expect(() => process.kill(metroPid, 0)).not.toThrow();
} finally {
if (metroPid) {
try {
process.kill(metroPid, 'SIGKILL');
} catch (_) {
// already gone
}
}
fs.rmSync(dir, { recursive: true, force: true });
}
});
it('fails fast without killing a Metro port holder this checkout did not record', async function () {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'rnfb-packager-foreign-'));
const holder = spawn('/bin/sleep', ['60'], { stdio: 'ignore' });
const exited = new Promise(resolve => holder.once('exit', (_code, signal) => resolve(signal)));
const yarnLog = path.join(dir, 'yarn.log');
for (const [name, body] of [
['lsof', `#!/bin/bash\n[[ "$*" == *"-t" ]] && echo ${holder.pid} && exit 0\nexit 1\n`],
['yarn', `#!/bin/bash\necho "$*" >> "${yarnLog}"\n`],
['sleep', '#!/bin/bash\nexit 0\n'],
]) {
fs.writeFileSync(path.join(dir, name), body);
fs.chmodSync(path.join(dir, name), 0o755);
}
const env = { ...process.env };
for (const key of Object.keys(env)) {
if (key.startsWith('RNFB_') || key.startsWith('E2E_') || key.startsWith('JET_')) {
delete env[key];
}
}
Object.assign(env, {
HOME: dir,
PATH: `${dir}:${env.PATH}`,
RNFB_E2E_RUN_RECORD_DIR: path.join(dir, 'rec'),
});
delete env.RCT_METRO_PORT;
try {
const result = spawnSync('/bin/bash', [scriptPath, 'tests'], { encoding: 'utf8', env });
expect(result.status).toBe(1);
expect(result.stderr).toMatch(
new RegExp(`:8081 is held by pid=${holder.pid}\\b[\\s\\S]*not killed`),
);
expect(result.stderr).toMatch(/yarn tests:e2e:export-slot-env/);
expect(fs.existsSync(yarnLog)).toBe(false);
const state = await Promise.race([
exited,
new Promise(resolve => setTimeout(() => resolve('running'), 200).unref()),
]);
expect(state).toBe('running');
} finally {
holder.kill('SIGKILL');
await exited;
fs.rmSync(dir, { recursive: true, force: true });
}
});
it('conditionally watch-del metro root; always allowlists; never del REPO_ROOT', function () {
expect(src).toMatch(/e2e_sibling_metro_owns_root/);
expect(src).toMatch(/RNFB_METRO_FORCE_WATCHMAN_RESET:-\}" != "1"/);
expect(src).toMatch(/watchman skip watch-del \(sibling Metro owns/);
expect(src).toMatch(/watchman watch-del "\$METRO_ROOT"/);
expect(src).not.toMatch(/watchman watch-del "\$REPO_ROOT"/);
expect(src).toMatch(/watchman watch-project "\$METRO_ROOT"/);
expect(src).toMatch(/watchman watch-project "\$dir"/);
});
it('waits for /status packager-status:running then fails on timeout', function () {
expect(src).toMatch(/packager-status:running/);
expect(src).toMatch(/RNFB_METRO_STATUS_WAIT_SEC/);
expect(src).toMatch(/timed out after \$\{STATUS_WAIT_SEC\}s/);
});
it('root yarn packager scripts call start-packager.sh and drop slotted aliases', function () {
expect(rootPkg.scripts['tests:packager:jet']).toMatch(/start-packager\.sh tests/);
expect(rootPkg.scripts['tests:packager:jet-reset-cache']).toMatch(
/start-packager\.sh tests --reset-cache/,
);
expect(rootPkg.scripts['tests:macos:packager:jet']).toMatch(/start-packager\.sh tests-macos/);
expect(rootPkg.scripts['tests:e2e:slotted-packager']).toBeUndefined();
expect(rootPkg.scripts['tests:e2e:slotted-test-cover']).toBeUndefined();
expect(rootPkg.scripts['tests:e2e:check']).toMatch(/check-e2e-resources\.sh/);
expect(rootPkg.scripts['tests:e2e:release']).toMatch(/release-e2e-resources\.sh/);
});
it('root yarn exposes tests:e2e:clear-slot-env', function () {
expect(rootPkg.scripts['tests:e2e:clear-slot-env']).toMatch(/clear-slot-env\.sh/);
});
it(':test-cover scripts sanitize serial env and do not one-shot restart Metro', function () {
expect(rootPkg.scripts['tests:android:test-cover']).toMatch(/e2e_sanitize_serial_env/);
expect(rootPkg.scripts['tests:android:test-cover']).toMatch(
/e2e_assert_android_apk_metro_port/,
);
expect(rootPkg.scripts['tests:android:test']).toMatch(/e2e_assert_android_apk_metro_port/);
expect(rootPkg.scripts['tests:ios:test-cover']).toMatch(/e2e_sanitize_serial_env/);
expect(rootPkg.scripts['tests:macos:test-cover']).toMatch(/e2e_sanitize_serial_env/);
expect(rootPkg.scripts['tests:android:test-cover']).not.toMatch(
/slotted-packager|start-packager/,
);
expect(rootPkg.scripts['tests:ios:test-cover']).not.toMatch(/slotted-packager|start-packager/);
expect(rootPkg.scripts['tests:macos:test-cover']).not.toMatch(
/slotted-packager|start-packager/,
);
});
});
describe('start-packager.sh temp and cache paths (stubbed Metro)', function () {
let host;
beforeEach(function () {
host = createHermeticHost('rnfb-start-packager-');
const stubs = {
// Serial sanitize pins :8081; with no listeners reported nothing on the host is killed.
lsof: 'exit 1',
watchman: 'exit 0',
// Ready only once the stub Metro has written its log line, so the log
// is complete when start-packager.sh returns.
curl: '[[ -f "$METRO_STUB_READY" ]] && echo packager-status:running\nexit 0',
// Stands in for `yarn react-native start`; stdout is the Metro log. Stays
// alive (bounded) until afterEach removes the sentinel with the scratch dir.
yarn: [
'echo "metro TMPDIR=$TMPDIR"',
'touch "$METRO_STUB_READY"',
'for _ in $(seq 1 120); do [[ -f "$METRO_STUB_READY" ]] || exit 0; sleep 0.5; done',
].join('\n'),
};
for (const [name, body] of Object.entries(stubs)) {
const file = path.join(host.bin, name);
fs.writeFileSync(file, `#!/bin/bash\n${body}\n`);
fs.chmodSync(file, 0o755);
}
});
afterEach(function () {
host.cleanup();
});
function run(extraEnv) {
const env = host.env();
for (const key of Object.keys(env)) {
if (key.startsWith('RNFB_') || key.startsWith('JET_') || key === 'RCT_METRO_PORT') {
delete env[key];
}
}
const home = path.join(host.root, 'home');
fs.mkdirSync(home, { recursive: true });
return execFileSync('bash', [scriptPath, 'tests'], {
encoding: 'utf8',
cwd: repoRoot,
env: {
...env,
HOME: home,
RNFB_E2E_RUN_RECORD_DIR: host.runRecordDir,
METRO_STUB_READY: path.join(host.root, 'metro-ready'),
RNFB_METRO_FORCE_WATCHMAN_RESET: '1',
RNFB_METRO_STATUS_WAIT_SEC: '10',
...extraEnv,
},
stdio: ['pipe', 'pipe', 'pipe'],
});
}
it('defaults the Metro log to the caller TMPDIR and the cache to $HOME/.metro', function () {
const out = run({ TMPDIR: `${host.tmp}/` });
const log = path.join(host.tmp, 'rnfb-metro-8081.log');
const cache = path.join(host.root, 'home/.metro/rnfb-8081');
expect(out).toContain(`TMPDIR=${cache} log=${log}`);
expect(fs.readFileSync(log, 'utf8')).toContain(`metro TMPDIR=${cache}`);
const recorded = fs
.readdirSync(path.join(host.runRecordDir, 'serial'))
.map(f => fs.readFileSync(path.join(host.runRecordDir, 'serial', f), 'utf8'));
expect(recorded.some(line => /\tmetro:8081$/m.test(line))).toBe(true);
});
it('RNFB_METRO_CACHE_ROOT moves the Metro cache', function () {
const cacheRoot = path.join(host.tmp, 'metro-cache');
const out = run({ RNFB_METRO_CACHE_ROOT: `${cacheRoot}/` });
expect(out).toContain(`TMPDIR=${cacheRoot}/rnfb-8081 `);
expect(fs.readFileSync(path.join(host.tmp, 'rnfb-metro-8081.log'), 'utf8')).toContain(
`metro TMPDIR=${cacheRoot}/rnfb-8081`,
);
expect(fs.existsSync(path.join(cacheRoot, 'rnfb-8081'))).toBe(true);
expect(fs.existsSync(path.join(host.root, 'home/.metro'))).toBe(false);
});
it.each(['', '/'])('RNFB_METRO_CACHE_ROOT=%j falls back to $HOME/.metro', function (value) {
const cache = path.join(host.root, 'home/.metro/rnfb-8081');
const out = run({ RNFB_METRO_CACHE_ROOT: value });
expect(out).toContain(`TMPDIR=${cache} `);
expect(fs.readFileSync(path.join(host.tmp, 'rnfb-metro-8081.log'), 'utf8')).toContain(
`metro TMPDIR=${cache}`,
);
expect(fs.existsSync(cache)).toBe(true);
});
});
describe('check-e2e-resources.sh CLI', function () {
const checkPath = path.join(repoRoot, 'scripts/e2e/check-e2e-resources.sh');
it('rejects --mellifera (core surface is RNFB_* env only)', function () {
const host = createHermeticHost('rnfb-check-cli-');
let status = 0;
try {
execFileSync('bash', [checkPath, '--mellifera'], {
encoding: 'utf8',
cwd: repoRoot,
env: host.env(),
stdio: ['pipe', 'pipe', 'pipe'],
});
} catch (e) {
status = e.status;
expect(`${e.stderr || ''}`).toMatch(/unknown arg: --mellifera/);
} finally {
host.cleanup();
}
expect(status).toBe(2);
});
});