-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforge.test.js
More file actions
447 lines (354 loc) · 19.1 KB
/
forge.test.js
File metadata and controls
447 lines (354 loc) · 19.1 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
// Mock modules
const path = require('path');
const mockSpawn = jest.fn();
const mockSpawnSync = jest.fn();
const mockExistsSync = jest.fn();
jest.mock('child_process', () => ({
spawn: mockSpawn,
spawnSync: mockSpawnSync,
}));
jest.mock('fs', () => ({
existsSync: mockExistsSync,
}));
describe('forge.js - Platform Detection and Binary Naming', () => {
let originalPlatform;
let originalArch;
let originalEnv;
// Helper to create mock spawnSync result with proper toString() methods
const createSpawnSyncResult = (status, stdout, stderr) => ({
status,
stdout: { toString: () => stdout },
stderr: { toString: () => stderr }
});
// Helper to mock Linux libc detection
const mockLinuxLibc = (libcOutput) => {
mockSpawnSync.mockImplementation((cmd, args) => {
if (cmd === 'ldd') {
return createSpawnSyncResult(0, '', libcOutput);
}
if (cmd === 'getprop') {
return createSpawnSyncResult(1, '', ''); // Android check fails
}
return createSpawnSyncResult(1, '', '');
});
};
beforeEach(() => {
originalPlatform = process.platform;
originalArch = process.arch;
originalEnv = { ...process.env };
jest.resetModules();
mockSpawn.mockClear();
mockSpawnSync.mockClear();
mockExistsSync.mockClear();
// Clean env variables
delete process.env.ANDROID_ROOT;
delete process.env.ANDROID_DATA;
delete process.env.PREFIX;
delete process.env.FORGE_BINARY_PATH;
delete process.env.FORCE_MUSL;
});
afterEach(() => {
Object.defineProperty(process, 'platform', { value: originalPlatform, writable: true, configurable: true });
Object.defineProperty(process, 'arch', { value: originalArch, writable: true, configurable: true });
process.env = originalEnv;
});
describe('Binary naming conventions', () => {
test('macOS x64 should use x86_64-apple-darwin', () => {
Object.defineProperty(process, 'platform', { value: 'darwin', writable: true, configurable: true });
Object.defineProperty(process, 'arch', { value: 'x64', writable: true, configurable: true });
mockSpawnSync.mockReturnValue(createSpawnSyncResult(0, '', ''));
mockExistsSync.mockImplementation(path => !path.includes('/system/build.prop'));
const forge = require('./forge.js');
expect(forge.getBinaryPath()).toContain('forge-x86_64-apple-darwin');
});
test('macOS ARM64 should use aarch64-apple-darwin', () => {
Object.defineProperty(process, 'platform', { value: 'darwin', writable: true, configurable: true });
Object.defineProperty(process, 'arch', { value: 'arm64', writable: true, configurable: true });
mockSpawnSync.mockReturnValue(createSpawnSyncResult(0, '', ''));
mockExistsSync.mockImplementation(path => !path.includes('/system/build.prop'));
const forge = require('./forge.js');
expect(forge.getBinaryPath()).toContain('forge-aarch64-apple-darwin');
});
test('Linux x64 GNU should use x86_64-unknown-linux-gnu', () => {
Object.defineProperty(process, 'platform', { value: 'linux', writable: true, configurable: true });
Object.defineProperty(process, 'arch', { value: 'x64', writable: true, configurable: true });
mockLinuxLibc('ldd (GNU libc) 2.39');
mockExistsSync.mockImplementation(path => !path.includes('/system/build.prop'));
const forge = require('./forge.js');
expect(forge.getBinaryPath()).toContain('forge-x86_64-unknown-linux-gnu');
});
test('Linux x64 musl should use x86_64-unknown-linux-musl', () => {
Object.defineProperty(process, 'platform', { value: 'linux', writable: true, configurable: true });
Object.defineProperty(process, 'arch', { value: 'x64', writable: true, configurable: true });
process.env.FORCE_MUSL = '1';
mockLinuxLibc('musl libc');
mockExistsSync.mockImplementation(path => !path.includes('/system/build.prop'));
const forge = require('./forge.js');
expect(forge.getBinaryPath()).toContain('forge-x86_64-unknown-linux-musl');
});
test('Linux ARM64 GNU should use aarch64-unknown-linux-gnu', () => {
Object.defineProperty(process, 'platform', { value: 'linux', writable: true, configurable: true });
Object.defineProperty(process, 'arch', { value: 'arm64', writable: true, configurable: true });
mockLinuxLibc('ldd (GNU libc) 2.39');
mockExistsSync.mockImplementation(path => !path.includes('/system/build.prop'));
const forge = require('./forge.js');
expect(forge.getBinaryPath()).toContain('forge-aarch64-unknown-linux-gnu');
});
test('Linux ARM64 musl should use aarch64-unknown-linux-musl', () => {
Object.defineProperty(process, 'platform', { value: 'linux', writable: true, configurable: true });
Object.defineProperty(process, 'arch', { value: 'arm64', writable: true, configurable: true });
process.env.FORCE_MUSL = '1';
mockLinuxLibc('musl libc');
mockExistsSync.mockImplementation(path => !path.includes('/system/build.prop'));
const forge = require('./forge.js');
expect(forge.getBinaryPath()).toContain('forge-aarch64-unknown-linux-musl');
});
test('Android ARM64 should use aarch64-linux-android', () => {
Object.defineProperty(process, 'platform', { value: 'linux', writable: true, configurable: true });
Object.defineProperty(process, 'arch', { value: 'arm64', writable: true, configurable: true });
process.env.ANDROID_ROOT = '/system';
mockSpawnSync.mockReturnValue(createSpawnSyncResult(0, '', ''));
mockExistsSync.mockImplementation(path => path.includes('/system/build.prop') || !path.includes('/system'));
const forge = require('./forge.js');
expect(forge.getBinaryPath()).toContain('forge-aarch64-linux-android');
});
test('Windows x64 should use x86_64-pc-windows-msvc.exe', () => {
Object.defineProperty(process, 'platform', { value: 'win32', writable: true, configurable: true });
Object.defineProperty(process, 'arch', { value: 'x64', writable: true, configurable: true });
mockSpawnSync.mockReturnValue(createSpawnSyncResult(0, '', ''));
mockExistsSync.mockImplementation(path => !path.includes('/system/build.prop'));
const forge = require('./forge.js');
expect(forge.getBinaryPath()).toContain('forge-x86_64-pc-windows-msvc.exe');
});
test('Windows ARM64 should use aarch64-pc-windows-msvc.exe', () => {
Object.defineProperty(process, 'platform', { value: 'win32', writable: true, configurable: true });
Object.defineProperty(process, 'arch', { value: 'arm64', writable: true, configurable: true });
mockSpawnSync.mockReturnValue(createSpawnSyncResult(0, '', ''));
mockExistsSync.mockImplementation(path => !path.includes('/system/build.prop'));
const forge = require('./forge.js');
expect(forge.getBinaryPath()).toContain('forge-aarch64-pc-windows-msvc.exe');
});
});
describe('Android detection', () => {
test('should detect Android via ANDROID_ROOT', () => {
Object.defineProperty(process, 'platform', { value: 'linux', writable: true, configurable: true });
Object.defineProperty(process, 'arch', { value: 'arm64', writable: true, configurable: true });
process.env.ANDROID_ROOT = '/system';
mockSpawnSync.mockReturnValue(createSpawnSyncResult(0, '', ''));
mockExistsSync.mockImplementation(path => path.includes('/system/build.prop') || !path.includes('/system'));
const forge = require('./forge.js');
expect(forge.getBinaryPath()).toContain('android');
});
test('should detect Android via ANDROID_DATA', () => {
Object.defineProperty(process, 'platform', { value: 'linux', writable: true, configurable: true });
Object.defineProperty(process, 'arch', { value: 'arm64', writable: true, configurable: true });
process.env.ANDROID_DATA = '/data';
mockSpawnSync.mockReturnValue(createSpawnSyncResult(0, '', ''));
mockExistsSync.mockImplementation(path => path.includes('/system/build.prop') || !path.includes('/system'));
const forge = require('./forge.js');
expect(forge.getBinaryPath()).toContain('android');
});
test('should detect Termux via PREFIX', () => {
Object.defineProperty(process, 'platform', { value: 'linux', writable: true, configurable: true });
Object.defineProperty(process, 'arch', { value: 'arm64', writable: true, configurable: true });
process.env.PREFIX = '/data/data/com.termux/files/usr';
mockSpawnSync.mockReturnValue(createSpawnSyncResult(0, '', ''));
mockExistsSync.mockImplementation(path => path.includes('/system/build.prop') || !path.includes('/system'));
const forge = require('./forge.js');
expect(forge.getBinaryPath()).toContain('android');
});
test('should detect Android via /system/build.prop', () => {
Object.defineProperty(process, 'platform', { value: 'linux', writable: true, configurable: true });
Object.defineProperty(process, 'arch', { value: 'arm64', writable: true, configurable: true });
mockSpawnSync.mockReturnValue(createSpawnSyncResult(0, '', ''));
mockExistsSync.mockImplementation(path => path.includes('/system/build.prop'));
const forge = require('./forge.js');
expect(forge.getBinaryPath()).toContain('android');
});
});
describe('Glibc version detection', () => {
test('should detect glibc 2.39 as sufficient', () => {
Object.defineProperty(process, 'platform', { value: 'linux', writable: true, configurable: true });
Object.defineProperty(process, 'arch', { value: 'x64', writable: true, configurable: true });
mockLinuxLibc('ldd (GNU libc) 2.39');
mockExistsSync.mockImplementation(path => !path.includes('/system/build.prop'));
const forge = require('./forge.js');
expect(forge.getBinaryPath()).toContain('gnu');
});
test('should detect glibc 2.40 as sufficient', () => {
Object.defineProperty(process, 'platform', { value: 'linux', writable: true, configurable: true });
Object.defineProperty(process, 'arch', { value: 'x64', writable: true, configurable: true });
mockLinuxLibc('ldd (GNU libc) 2.40');
mockExistsSync.mockImplementation(path => !path.includes('/system/build.prop'));
const forge = require('./forge.js');
expect(forge.getBinaryPath()).toContain('gnu');
});
test('should fall back to musl for glibc 2.28', () => {
Object.defineProperty(process, 'platform', { value: 'linux', writable: true, configurable: true });
Object.defineProperty(process, 'arch', { value: 'x64', writable: true, configurable: true });
mockLinuxLibc('ldd (GNU libc) 2.28');
mockExistsSync.mockImplementation(path => !path.includes('/system/build.prop'));
const forge = require('./forge.js');
expect(forge.getBinaryPath()).toContain('musl');
});
test('should detect musl libc', () => {
Object.defineProperty(process, 'platform', { value: 'linux', writable: true, configurable: true });
Object.defineProperty(process, 'arch', { value: 'x64', writable: true, configurable: true });
mockLinuxLibc('musl libc (x86_64)');
mockExistsSync.mockImplementation(path => !path.includes('/system/build.prop'));
const forge = require('./forge.js');
expect(forge.getBinaryPath()).toContain('musl');
});
});
describe('Environment variable overrides', () => {
test('should respect FORGE_BINARY_PATH override', () => {
process.env.FORGE_BINARY_PATH = '/custom/path/to/forge';
mockSpawnSync.mockReturnValue(createSpawnSyncResult(0, '', ''));
mockExistsSync.mockReturnValue(true);
const forge = require('./forge.js');
expect(forge.getBinaryPath()).toBe('/custom/path/to/forge');
});
test('should respect FORCE_MUSL flag on Linux', () => {
Object.defineProperty(process, 'platform', { value: 'linux', writable: true, configurable: true });
Object.defineProperty(process, 'arch', { value: 'x64', writable: true, configurable: true });
process.env.FORCE_MUSL = '1';
mockLinuxLibc('ldd (GNU libc) 2.35');
mockExistsSync.mockImplementation(path => !path.includes('/system/build.prop'));
const forge = require('./forge.js');
expect(forge.getBinaryPath()).toContain('musl');
});
});
describe('Binary path construction', () => {
test('should construct correct path for darwin x64', () => {
Object.defineProperty(process, 'platform', { value: 'darwin', writable: true, configurable: true });
Object.defineProperty(process, 'arch', { value: 'x64', writable: true, configurable: true });
mockSpawnSync.mockReturnValue(createSpawnSyncResult(0, '', ''));
mockExistsSync.mockImplementation(path => !path.includes('/system/build.prop'));
const forge = require('./forge.js');
const binaryPath = forge.getBinaryPath();
// Use path.join for OS-agnostic path comparison
expect(binaryPath).toContain(path.join('bin', 'darwin', 'x64'));
expect(binaryPath).toContain('forge-x86_64-apple-darwin');
});
test('should construct correct path for linux arm64', () => {
Object.defineProperty(process, 'platform', { value: 'linux', writable: true, configurable: true });
Object.defineProperty(process, 'arch', { value: 'arm64', writable: true, configurable: true });
mockLinuxLibc('ldd (GNU libc) 2.39');
mockExistsSync.mockImplementation(path => !path.includes('/system/build.prop'));
const forge = require('./forge.js');
const binaryPath = forge.getBinaryPath();
// Use path.join for OS-agnostic path comparison
expect(binaryPath).toContain(path.join('bin', 'linux', 'arm64'));
expect(binaryPath).toContain('forge-aarch64-unknown-linux-gnu');
});
test('should construct correct path for win32 x64', () => {
Object.defineProperty(process, 'platform', { value: 'win32', writable: true, configurable: true });
Object.defineProperty(process, 'arch', { value: 'x64', writable: true, configurable: true });
mockSpawnSync.mockReturnValue(createSpawnSyncResult(0, '', ''));
mockExistsSync.mockImplementation(path => !path.includes('/system/build.prop'));
const forge = require('./forge.js');
const binaryPath = forge.getBinaryPath();
// Use path.join for OS-agnostic path comparison
expect(binaryPath).toContain(path.join('bin', 'win32', 'x64'));
expect(binaryPath).toContain('forge-x86_64-pc-windows-msvc.exe');
});
});
describe('Edge cases and error handling', () => {
test('should handle when ldd returns no version but getconf works', () => {
Object.defineProperty(process, 'platform', { value: 'linux', writable: true, configurable: true });
Object.defineProperty(process, 'arch', { value: 'x64', writable: true, configurable: true });
mockSpawnSync.mockImplementation((cmd, args) => {
if (cmd === 'ldd') {
// ldd returns no version info
return createSpawnSyncResult(0, '', 'ldd');
}
if (cmd === 'getconf') {
// getconf returns version
return createSpawnSyncResult(0, 'glibc 2.39', '');
}
if (cmd === 'getprop') {
return createSpawnSyncResult(1, '', '');
}
return createSpawnSyncResult(1, '', '');
});
mockExistsSync.mockImplementation(path => !path.includes('/system/build.prop'));
const forge = require('./forge.js');
expect(forge.getBinaryPath()).toContain('gnu');
});
test('should handle when ldd fails completely and fall back to musl', () => {
Object.defineProperty(process, 'platform', { value: 'linux', writable: true, configurable: true });
Object.defineProperty(process, 'arch', { value: 'x64', writable: true, configurable: true });
mockSpawnSync.mockImplementation((cmd, args) => {
if (cmd === 'ldd') {
// ldd returns nothing useful
return createSpawnSyncResult(0, '', '');
}
if (cmd === 'getconf') {
// getconf also fails
return createSpawnSyncResult(1, '', '');
}
if (cmd === 'getprop') {
return createSpawnSyncResult(1, '', '');
}
return createSpawnSyncResult(1, '', '');
});
mockExistsSync.mockImplementation(path => !path.includes('/system/build.prop'));
const forge = require('./forge.js');
// Should fall back to musl when no version info available (safer default)
expect(forge.getBinaryPath()).toContain('musl');
});
test('should return null for unsupported platform', () => {
Object.defineProperty(process, 'platform', { value: 'freebsd', writable: true, configurable: true });
Object.defineProperty(process, 'arch', { value: 'x64', writable: true, configurable: true });
mockSpawnSync.mockReturnValue(createSpawnSyncResult(0, '', ''));
mockExistsSync.mockReturnValue(false);
const forge = require('./forge.js');
expect(forge.getBinaryPath()).toBeNull();
});
test('should return null for unsupported architecture', () => {
Object.defineProperty(process, 'platform', { value: 'darwin', writable: true, configurable: true });
Object.defineProperty(process, 'arch', { value: 'ia32', writable: true, configurable: true });
mockSpawnSync.mockReturnValue(createSpawnSyncResult(0, '', ''));
mockExistsSync.mockReturnValue(false);
const forge = require('./forge.js');
expect(forge.getBinaryPath()).toBeNull();
});
test('should handle getconf with no version match and fall back to musl', () => {
Object.defineProperty(process, 'platform', { value: 'linux', writable: true, configurable: true });
Object.defineProperty(process, 'arch', { value: 'x64', writable: true, configurable: true });
mockSpawnSync.mockImplementation((cmd, args) => {
if (cmd === 'ldd') {
return createSpawnSyncResult(0, '', 'some text without version');
}
if (cmd === 'getconf') {
return createSpawnSyncResult(0, 'no version here', '');
}
if (cmd === 'getprop') {
return createSpawnSyncResult(1, '', '');
}
return createSpawnSyncResult(1, '', '');
});
mockExistsSync.mockImplementation(path => !path.includes('/system/build.prop'));
const forge = require('./forge.js');
// Should fall back to musl when version cannot be parsed (safer default)
expect(forge.getBinaryPath()).toContain('musl');
});
test('should handle spawnSync throwing error and default to gnu', () => {
Object.defineProperty(process, 'platform', { value: 'linux', writable: true, configurable: true });
Object.defineProperty(process, 'arch', { value: 'x64', writable: true, configurable: true });
mockSpawnSync.mockImplementation((cmd, args) => {
if (cmd === 'ldd') {
throw new Error('Command not found');
}
if (cmd === 'getprop') {
return createSpawnSyncResult(1, '', '');
}
return createSpawnSyncResult(1, '', '');
});
mockExistsSync.mockImplementation(path => !path.includes('/system/build.prop'));
const forge = require('./forge.js');
// When error is caught, returns 'unknown' type which defaults to gnu
expect(forge.getBinaryPath()).toContain('gnu');
});
});
});