-
-
Notifications
You must be signed in to change notification settings - Fork 360
Expand file tree
/
Copy pathsentry-xcode-scripts.test.ts
More file actions
707 lines (593 loc) · 26.6 KB
/
sentry-xcode-scripts.test.ts
File metadata and controls
707 lines (593 loc) · 26.6 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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
import { execSync } from 'child_process';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
const SCRIPTS_DIR = path.resolve(__dirname, '../../scripts');
const DEBUG_FILES_SCRIPT = path.join(SCRIPTS_DIR, 'sentry-xcode-debug-files.sh');
const XCODE_SCRIPT = path.join(SCRIPTS_DIR, 'sentry-xcode.sh');
describe('sentry-xcode-debug-files.sh', () => {
let tempDir: string;
let mockSentryCliScript: string;
beforeEach(() => {
// Create a temporary directory for test artifacts
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'sentry-xcode-test-'));
// Create a mock sentry-cli script that can simulate success or failure
mockSentryCliScript = path.join(tempDir, 'mock-sentry-cli.js');
fs.writeFileSync(
mockSentryCliScript,
`
const exitCode = process.env.MOCK_CLI_EXIT_CODE || '0';
const output = process.env.MOCK_CLI_OUTPUT || 'Mock upload output';
console.log(output);
process.exit(parseInt(exitCode));
`,
);
});
afterEach(() => {
// Clean up temp directory
if (tempDir && fs.existsSync(tempDir)) {
fs.rmSync(tempDir, { recursive: true, force: true });
}
});
const runScript = (env: Record<string, string> = {}): { stdout: string; stderr: string; exitCode: number } => {
const defaultEnv = {
NODE_BINARY: process.execPath,
SENTRY_CLI_EXECUTABLE: mockSentryCliScript,
DWARF_DSYM_FOLDER_PATH: tempDir,
CONFIGURATION: 'Release',
PROJECT_DIR: tempDir,
DERIVED_FILE_DIR: tempDir,
};
try {
const stdout = execSync(`bash "${DEBUG_FILES_SCRIPT}"`, {
env: { ...process.env, ...defaultEnv, ...env },
encoding: 'utf8',
stdio: 'pipe',
});
return { stdout, stderr: '', exitCode: 0 };
} catch (error: any) {
return {
stdout: error.stdout?.toString() || '',
stderr: error.stderr?.toString() || '',
exitCode: error.status || 1,
};
}
};
it('exits with 0 when upload succeeds', () => {
const result = runScript({
MOCK_CLI_EXIT_CODE: '0',
MOCK_CLI_OUTPUT: 'Upload successful',
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('Upload successful');
});
it('exits with 0 when SENTRY_ALLOW_FAILURE=true and upload fails', () => {
const result = runScript({
MOCK_CLI_EXIT_CODE: '1',
MOCK_CLI_OUTPUT: 'Upload failed: API error',
SENTRY_ALLOW_FAILURE: 'true',
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('warning: sentry-cli');
expect(result.stdout).toContain('continuing build because SENTRY_ALLOW_FAILURE=true');
expect(result.stdout).toContain('Upload failed: API error');
});
it('exits with 0 but prints error when SENTRY_ALLOW_FAILURE not set and upload fails', () => {
const result = runScript({
MOCK_CLI_EXIT_CODE: '1',
MOCK_CLI_OUTPUT: 'Upload failed: API error',
});
// Original behavior: script exits 0, but Xcode fails build due to "error:" prefix
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('error: sentry-cli');
expect(result.stdout).toContain('SENTRY_ALLOW_FAILURE=true');
expect(result.stdout).toContain('Upload failed: API error');
});
it('exits with 0 but prints error when SENTRY_ALLOW_FAILURE=false and upload fails', () => {
const result = runScript({
MOCK_CLI_EXIT_CODE: '1',
MOCK_CLI_OUTPUT: 'Upload failed: Network error',
SENTRY_ALLOW_FAILURE: 'false',
});
// Original behavior: script exits 0, but Xcode fails build due to "error:" prefix
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('error: sentry-cli');
});
it('skips upload when SENTRY_DISABLE_AUTO_UPLOAD=true', () => {
const result = runScript({
SENTRY_DISABLE_AUTO_UPLOAD: 'true',
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('SENTRY_DISABLE_AUTO_UPLOAD=true');
expect(result.stdout).toContain('skipping debug files upload');
});
it('skips upload for Debug configuration', () => {
const result = runScript({
CONFIGURATION: 'Debug',
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('Skipping debug files upload for *Debug* configuration');
});
it('skips upload for debug configuration (case insensitive)', () => {
const result = runScript({
CONFIGURATION: 'debug',
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('Skipping debug files upload for *Debug* configuration');
});
describe('dSYM wait functionality', () => {
it('proceeds immediately when dSYM folder already exists with complete dSYM files', () => {
// Create a complete dSYM bundle structure with DWARF binary
const dsymPath = path.join(tempDir, 'TestApp.app.dSYM');
const dwarfDir = path.join(dsymPath, 'Contents', 'Resources', 'DWARF');
fs.mkdirSync(dwarfDir, { recursive: true });
// Create a non-empty DWARF binary file
fs.writeFileSync(path.join(dwarfDir, 'TestApp'), 'mock dwarf binary content');
const result = runScript({
DWARF_DSYM_FOLDER_PATH: tempDir,
DWARF_DSYM_FILE_NAME: 'TestApp.app.dSYM',
MOCK_CLI_EXIT_CODE: '0',
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('Checking for dSYM files');
expect(result.stdout).toContain('Found');
expect(result.stdout).toContain('dSYM bundle(s)');
expect(result.stdout).toContain('Verified main app dSYM is complete');
// Should not have waited since dSYM exists
expect(result.stdout).not.toContain('Waiting');
});
// Note: Testing "file appears during wait" scenario is difficult with execSync
// as it blocks the Node.js process. The wait logic is adequately covered by
// the "proceeds immediately" and "times out" tests.
it('times out when dSYM never appears', () => {
const dsymFolderPath = path.join(tempDir, 'empty-dsym-folder');
fs.mkdirSync(dsymFolderPath, { recursive: true });
const result = runScript({
DWARF_DSYM_FOLDER_PATH: dsymFolderPath,
DWARF_DSYM_FILE_NAME: 'NonExistent.app.dSYM',
SENTRY_DSYM_WAIT_MAX_ATTEMPTS: '2',
SENTRY_DSYM_WAIT_INTERVAL: '1',
MOCK_CLI_EXIT_CODE: '0',
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('Checking for dSYM files');
expect(result.stdout).toContain('Waiting');
expect(result.stdout).toContain('warning: Timeout waiting for dSYM files');
expect(result.stdout).toContain('This may result in incomplete debug symbol uploads');
});
it('skips wait check when SENTRY_DSYM_WAIT_ENABLED=false', () => {
const result = runScript({
SENTRY_DSYM_WAIT_ENABLED: 'false',
MOCK_CLI_EXIT_CODE: '0',
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('SENTRY_DSYM_WAIT_ENABLED=false');
expect(result.stdout).toContain('skipping dSYM wait check');
expect(result.stdout).not.toContain('Checking for dSYM files');
});
it('proceeds when folder contains any dSYM even without DWARF_DSYM_FILE_NAME', () => {
// Create some complete dSYM bundles
const dsymPath1 = path.join(tempDir, 'Framework1.framework.dSYM');
const dwarfDir1 = path.join(dsymPath1, 'Contents', 'Resources', 'DWARF');
fs.mkdirSync(dwarfDir1, { recursive: true });
fs.writeFileSync(path.join(dwarfDir1, 'Framework1'), 'mock dwarf content');
const dsymPath2 = path.join(tempDir, 'Framework2.framework.dSYM');
const dwarfDir2 = path.join(dsymPath2, 'Contents', 'Resources', 'DWARF');
fs.mkdirSync(dwarfDir2, { recursive: true });
fs.writeFileSync(path.join(dwarfDir2, 'Framework2'), 'mock dwarf content');
const result = runScript({
DWARF_DSYM_FOLDER_PATH: tempDir,
// DWARF_DSYM_FILE_NAME not set
MOCK_CLI_EXIT_CODE: '0',
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('warning: DWARF_DSYM_FILE_NAME not set');
expect(result.stdout).toContain('Found');
expect(result.stdout).toContain('dSYM bundle(s)');
expect(result.stdout).toContain('Found dSYM bundle(s) with valid DWARF content');
});
it('continues waiting if main app dSYM not found but other dSYMs exist', () => {
const dsymFolderPath = path.join(tempDir, 'dsym-folder');
fs.mkdirSync(dsymFolderPath, { recursive: true });
// Create only framework dSYM with complete structure, not the main app dSYM
const frameworkDsym = path.join(dsymFolderPath, 'SomeFramework.framework.dSYM');
const frameworkDwarfDir = path.join(frameworkDsym, 'Contents', 'Resources', 'DWARF');
fs.mkdirSync(frameworkDwarfDir, { recursive: true });
fs.writeFileSync(path.join(frameworkDwarfDir, 'SomeFramework'), 'mock dwarf content');
const result = runScript({
DWARF_DSYM_FOLDER_PATH: dsymFolderPath,
DWARF_DSYM_FILE_NAME: 'MainApp.app.dSYM', // Looking for this specific one
SENTRY_DSYM_WAIT_MAX_ATTEMPTS: '2',
SENTRY_DSYM_WAIT_INTERVAL: '1',
SENTRY_DSYM_DEBUG: 'true',
MOCK_CLI_EXIT_CODE: '0',
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('Main app dSYM not found yet');
expect(result.stdout).toContain('warning: Timeout waiting for dSYM files');
});
it('waits when dSYM directory exists but DWARF binary is missing (incomplete)', () => {
const dsymFolderPath = path.join(tempDir, 'incomplete-dsym-folder');
fs.mkdirSync(dsymFolderPath, { recursive: true });
// Create dSYM directory structure but without DWARF binary (incomplete)
const incompleteDsym = path.join(dsymFolderPath, 'IncompleteApp.app.dSYM');
const dwarfDir = path.join(incompleteDsym, 'Contents', 'Resources', 'DWARF');
fs.mkdirSync(dwarfDir, { recursive: true });
// Note: NOT creating the actual DWARF file
const result = runScript({
DWARF_DSYM_FOLDER_PATH: dsymFolderPath,
DWARF_DSYM_FILE_NAME: 'IncompleteApp.app.dSYM',
SENTRY_DSYM_WAIT_MAX_ATTEMPTS: '2',
SENTRY_DSYM_WAIT_INTERVAL: '1',
SENTRY_DSYM_DEBUG: 'true',
MOCK_CLI_EXIT_CODE: '0',
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('Main app dSYM DWARF directory is empty');
expect(result.stdout).toContain('warning: Timeout waiting for dSYM files');
});
it('waits when dSYM exists but DWARF binary is empty (still being written)', () => {
const dsymFolderPath = path.join(tempDir, 'empty-dwarf-folder');
fs.mkdirSync(dsymFolderPath, { recursive: true });
// Create dSYM with empty DWARF file (simulates file being created but not written yet)
const dsymPath = path.join(dsymFolderPath, 'WritingApp.app.dSYM');
const dwarfDir = path.join(dsymPath, 'Contents', 'Resources', 'DWARF');
fs.mkdirSync(dwarfDir, { recursive: true });
fs.writeFileSync(path.join(dwarfDir, 'WritingApp'), ''); // Empty file
const result = runScript({
DWARF_DSYM_FOLDER_PATH: dsymFolderPath,
DWARF_DSYM_FILE_NAME: 'WritingApp.app.dSYM',
SENTRY_DSYM_WAIT_MAX_ATTEMPTS: '2',
SENTRY_DSYM_WAIT_INTERVAL: '1',
SENTRY_DSYM_DEBUG: 'true',
MOCK_CLI_EXIT_CODE: '0',
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('Main app dSYM DWARF binary is empty (still being written)');
expect(result.stdout).toContain('warning: Timeout waiting for dSYM files');
});
it('handles non-existent dSYM folder path', () => {
const nonExistentPath = path.join(tempDir, 'does-not-exist');
const result = runScript({
DWARF_DSYM_FOLDER_PATH: nonExistentPath,
SENTRY_DSYM_WAIT_MAX_ATTEMPTS: '2',
SENTRY_DSYM_WAIT_INTERVAL: '1',
SENTRY_DSYM_DEBUG: 'true',
MOCK_CLI_EXIT_CODE: '0',
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('dSYM folder does not exist yet');
expect(result.stdout).toContain('warning: Timeout waiting for dSYM files');
});
it('respects custom wait interval and max attempts', () => {
const startTime = Date.now();
const result = runScript({
DWARF_DSYM_FOLDER_PATH: path.join(tempDir, 'nonexistent'),
SENTRY_DSYM_WAIT_MAX_ATTEMPTS: '3',
SENTRY_DSYM_WAIT_INTERVAL: '1',
MOCK_CLI_EXIT_CODE: '0',
});
const duration = Date.now() - startTime;
expect(result.exitCode).toBe(0);
// Should have waited approximately 2 seconds (3 attempts with 1s interval, but no wait after last attempt)
expect(duration).toBeGreaterThanOrEqual(2000);
expect(duration).toBeLessThan(4000); // Allow some margin
});
});
});
describe('sentry-xcode.sh', () => {
let tempDir: string;
let mockSentryCliScript: string;
let mockReactNativeScript: string;
beforeEach(() => {
// Create a temporary directory for test artifacts
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'sentry-xcode-test-'));
// Create a mock sentry-cli script
mockSentryCliScript = path.join(tempDir, 'mock-sentry-cli.js');
fs.writeFileSync(
mockSentryCliScript,
`
const exitCode = process.env.MOCK_CLI_EXIT_CODE || '0';
const output = process.env.MOCK_CLI_OUTPUT || 'Mock upload output';
console.log(output);
process.exit(parseInt(exitCode));
`,
);
// Create a mock react-native-xcode.sh script
mockReactNativeScript = path.join(tempDir, 'react-native-xcode.sh');
fs.writeFileSync(
mockReactNativeScript,
`#!/bin/bash
echo "Mock React Native bundle"
exit 0
`,
);
fs.chmodSync(mockReactNativeScript, '755');
});
afterEach(() => {
// Clean up temp directory
if (tempDir && fs.existsSync(tempDir)) {
fs.rmSync(tempDir, { recursive: true, force: true });
}
});
const runScript = (env: Record<string, string> = {}): { stdout: string; stderr: string; exitCode: number } => {
// Create a mock collect-modules.sh script to prevent script failure
const mockCollectModulesScript = path.join(tempDir, 'collect-modules.sh');
fs.writeFileSync(mockCollectModulesScript, '#!/bin/bash\nexit 0\n');
fs.chmodSync(mockCollectModulesScript, '755');
const defaultEnv = {
NODE_BINARY: process.execPath,
SENTRY_CLI_EXECUTABLE: mockSentryCliScript,
PROJECT_DIR: tempDir,
DERIVED_FILE_DIR: tempDir,
SENTRY_COLLECT_MODULES: mockCollectModulesScript, // Set this to avoid package resolution failure
};
try {
const stdout = execSync(`bash "${XCODE_SCRIPT}" "${mockReactNativeScript}"`, {
env: { ...process.env, ...defaultEnv, ...env },
encoding: 'utf8',
stdio: 'pipe',
});
return { stdout, stderr: '', exitCode: 0 };
} catch (error: any) {
return {
stdout: error.stdout?.toString() || '',
stderr: error.stderr?.toString() || '',
exitCode: error.status || 1,
};
}
};
it('exits with 0 when upload succeeds', () => {
const result = runScript({
MOCK_CLI_EXIT_CODE: '0',
MOCK_CLI_OUTPUT: 'Source maps uploaded successfully',
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('Source maps uploaded successfully');
});
it('exits with 0 when SENTRY_ALLOW_FAILURE=true and upload fails', () => {
const result = runScript({
MOCK_CLI_EXIT_CODE: '1',
MOCK_CLI_OUTPUT: 'Upload failed: Connection timeout',
SENTRY_ALLOW_FAILURE: 'true',
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('warning: sentry-cli');
expect(result.stdout).toContain('continuing build because SENTRY_ALLOW_FAILURE=true');
expect(result.stdout).toContain('Upload failed: Connection timeout');
});
it('exits with 1 when SENTRY_ALLOW_FAILURE not set and upload fails', () => {
const result = runScript({
MOCK_CLI_EXIT_CODE: '1',
MOCK_CLI_OUTPUT: 'Upload failed: Invalid auth token',
});
expect(result.exitCode).toBe(1);
expect(result.stdout).toContain('error: sentry-cli');
expect(result.stdout).toContain('SENTRY_ALLOW_FAILURE=true');
expect(result.stdout).toContain('Upload failed: Invalid auth token');
});
it('exits with 1 when SENTRY_ALLOW_FAILURE=false and upload fails', () => {
const result = runScript({
MOCK_CLI_EXIT_CODE: '1',
MOCK_CLI_OUTPUT: 'Upload failed',
SENTRY_ALLOW_FAILURE: 'false',
});
expect(result.exitCode).toBe(1);
expect(result.stdout).toContain('error: sentry-cli');
});
it('skips upload when SENTRY_DISABLE_AUTO_UPLOAD=true', () => {
const result = runScript({
SENTRY_DISABLE_AUTO_UPLOAD: 'true',
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('SENTRY_DISABLE_AUTO_UPLOAD=true');
expect(result.stdout).toContain('skipping sourcemaps upload');
});
describe('SENTRY_PROJECT_ROOT override', () => {
it('resolves SOURCEMAP_FILE relative to SENTRY_PROJECT_ROOT instead of PROJECT_DIR/..', () => {
const customRoot = path.join(tempDir, 'monorepo-package');
fs.mkdirSync(customRoot, { recursive: true });
const echoScript = path.join(tempDir, 'mock-sentry-cli-echo-sourcemap.js');
fs.writeFileSync(
echoScript,
`
const sourcemapFile = process.env.SOURCEMAP_FILE || 'not-set';
console.log('SOURCEMAP_FILE=' + sourcemapFile);
process.exit(0);
`,
);
const result = runScript({
SENTRY_PROJECT_ROOT: customRoot,
SENTRY_CLI_EXECUTABLE: echoScript,
SOURCEMAP_FILE: 'relative/path.map',
});
const expectedPath = path.join(customRoot, 'relative/path.map');
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain(`SOURCEMAP_FILE=${expectedPath}`);
});
it('resolves SOURCEMAP_FILE relative to PROJECT_DIR/.. when SENTRY_PROJECT_ROOT is not set', () => {
const echoScript = path.join(tempDir, 'mock-sentry-cli-echo-sourcemap.js');
fs.writeFileSync(
echoScript,
`
const sourcemapFile = process.env.SOURCEMAP_FILE || 'not-set';
console.log('SOURCEMAP_FILE=' + sourcemapFile);
process.exit(0);
`,
);
const result = runScript({
SENTRY_CLI_EXECUTABLE: echoScript,
SOURCEMAP_FILE: 'relative/path.map',
});
// Without SENTRY_PROJECT_ROOT, falls back to PROJECT_DIR/..
const projectRoot = path.dirname(tempDir);
const expectedPath = path.join(projectRoot, 'relative/path.map');
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain(`SOURCEMAP_FILE=${expectedPath}`);
});
it('finds sentry.options.json in SENTRY_PROJECT_ROOT', () => {
const customRoot = path.join(tempDir, 'monorepo-package');
fs.mkdirSync(customRoot, { recursive: true });
const optionsContent = JSON.stringify({ dsn: 'https://key@sentry.io/123' });
fs.writeFileSync(path.join(customRoot, 'sentry.options.json'), optionsContent);
const buildDir = path.join(tempDir, 'build');
const resourcesPath = 'Resources';
fs.mkdirSync(path.join(buildDir, resourcesPath), { recursive: true });
const result = runScript({
SENTRY_PROJECT_ROOT: customRoot,
SENTRY_DISABLE_AUTO_UPLOAD: 'true',
SENTRY_COPY_OPTIONS_FILE: 'true',
CONFIGURATION_BUILD_DIR: buildDir,
UNLOCALIZED_RESOURCES_FOLDER_PATH: resourcesPath,
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('Copied');
const destPath = path.join(buildDir, resourcesPath, 'sentry.options.json');
const copied = JSON.parse(fs.readFileSync(destPath, 'utf8'));
expect(copied.dsn).toBe('https://key@sentry.io/123');
});
});
describe('sentry.options.json SENTRY_ENVIRONMENT override', () => {
it('copies file without modification when SENTRY_ENVIRONMENT is not set', () => {
const optionsContent = JSON.stringify({ dsn: 'https://key@sentry.io/123', environment: 'production' });
const optionsFile = path.join(tempDir, 'sentry.options.json');
fs.writeFileSync(optionsFile, optionsContent);
const buildDir = path.join(tempDir, 'build');
const resourcesPath = 'Resources';
fs.mkdirSync(path.join(buildDir, resourcesPath), { recursive: true });
const result = runScript({
SENTRY_DISABLE_AUTO_UPLOAD: 'true',
SENTRY_COPY_OPTIONS_FILE: 'true',
SENTRY_OPTIONS_FILE_PATH: optionsFile,
CONFIGURATION_BUILD_DIR: buildDir,
UNLOCALIZED_RESOURCES_FOLDER_PATH: resourcesPath,
});
expect(result.exitCode).toBe(0);
const destPath = path.join(buildDir, resourcesPath, 'sentry.options.json');
const copied = JSON.parse(fs.readFileSync(destPath, 'utf8'));
expect(copied.dsn).toBe('https://key@sentry.io/123');
expect(copied.environment).toBe('production');
});
it('overrides environment from SENTRY_ENVIRONMENT env var', () => {
const optionsContent = JSON.stringify({ dsn: 'https://key@sentry.io/123', environment: 'production' });
const optionsFile = path.join(tempDir, 'sentry.options.json');
fs.writeFileSync(optionsFile, optionsContent);
const buildDir = path.join(tempDir, 'build');
const resourcesPath = 'Resources';
fs.mkdirSync(path.join(buildDir, resourcesPath), { recursive: true });
const result = runScript({
SENTRY_DISABLE_AUTO_UPLOAD: 'true',
SENTRY_COPY_OPTIONS_FILE: 'true',
SENTRY_OPTIONS_FILE_PATH: optionsFile,
CONFIGURATION_BUILD_DIR: buildDir,
UNLOCALIZED_RESOURCES_FOLDER_PATH: resourcesPath,
SENTRY_ENVIRONMENT: 'staging',
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('Overriding');
const destPath = path.join(buildDir, resourcesPath, 'sentry.options.json');
const copied = JSON.parse(fs.readFileSync(destPath, 'utf8'));
expect(copied.environment).toBe('staging');
expect(copied.dsn).toBe('https://key@sentry.io/123');
});
it('does not modify the source sentry.options.json', () => {
const optionsContent = JSON.stringify({ dsn: 'https://key@sentry.io/123', environment: 'production' });
const optionsFile = path.join(tempDir, 'sentry.options.json');
fs.writeFileSync(optionsFile, optionsContent);
const buildDir = path.join(tempDir, 'build');
const resourcesPath = 'Resources';
fs.mkdirSync(path.join(buildDir, resourcesPath), { recursive: true });
runScript({
SENTRY_DISABLE_AUTO_UPLOAD: 'true',
SENTRY_COPY_OPTIONS_FILE: 'true',
SENTRY_OPTIONS_FILE_PATH: optionsFile,
CONFIGURATION_BUILD_DIR: buildDir,
UNLOCALIZED_RESOURCES_FOLDER_PATH: resourcesPath,
SENTRY_ENVIRONMENT: 'staging',
});
const source = JSON.parse(fs.readFileSync(optionsFile, 'utf8'));
expect(source.environment).toBe('production');
});
it('falls back to plain copy when sentry.options.json contains invalid JSON', () => {
const optionsFile = path.join(tempDir, 'sentry.options.json');
fs.writeFileSync(optionsFile, 'invalid json{{{');
const buildDir = path.join(tempDir, 'build');
const resourcesPath = 'Resources';
fs.mkdirSync(path.join(buildDir, resourcesPath), { recursive: true });
const result = runScript({
SENTRY_DISABLE_AUTO_UPLOAD: 'true',
SENTRY_COPY_OPTIONS_FILE: 'true',
SENTRY_OPTIONS_FILE_PATH: optionsFile,
CONFIGURATION_BUILD_DIR: buildDir,
UNLOCALIZED_RESOURCES_FOLDER_PATH: resourcesPath,
SENTRY_ENVIRONMENT: 'staging',
});
expect(result.exitCode).toBe(0);
const destPath = path.join(buildDir, resourcesPath, 'sentry.options.json');
expect(fs.readFileSync(destPath, 'utf8')).toBe('invalid json{{{');
expect(result.stdout).toContain('Copied');
});
});
describe('SOURCEMAP_FILE path resolution', () => {
// Returns a mock sentry-cli that prints the SOURCEMAP_FILE env var it received.
const makeSourcemapEchoScript = (dir: string): string => {
const scriptPath = path.join(dir, 'mock-sentry-cli-echo-sourcemap.js');
fs.writeFileSync(
scriptPath,
`
const sourcemapFile = process.env.SOURCEMAP_FILE || 'not-set';
console.log('SOURCEMAP_FILE=' + sourcemapFile);
process.exit(0);
`,
);
return scriptPath;
};
it('leaves an absolute SOURCEMAP_FILE unchanged', () => {
const absolutePath = path.join(tempDir, 'absolute', 'main.jsbundle.map');
const echoScript = makeSourcemapEchoScript(tempDir);
const result = runScript({
SENTRY_CLI_EXECUTABLE: echoScript,
SOURCEMAP_FILE: absolutePath,
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain(`SOURCEMAP_FILE=${absolutePath}`);
});
it('resolves a relative SOURCEMAP_FILE against the project root, not ios/', () => {
// PROJECT_DIR is tempDir (simulates the ios/ folder).
// RN_PROJECT_ROOT = PROJECT_DIR/.. = parent of tempDir.
// A user setting SOURCEMAP_FILE=relative/path.map expects it relative to the project root.
const echoScript = makeSourcemapEchoScript(tempDir);
const result = runScript({
SENTRY_CLI_EXECUTABLE: echoScript,
SOURCEMAP_FILE: 'relative/path.map',
});
const projectRoot = path.dirname(tempDir); // PROJECT_DIR/.. = RN_PROJECT_ROOT
const expectedPath = path.join(projectRoot, 'relative/path.map');
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain(`SOURCEMAP_FILE=${expectedPath}`);
});
it('resolves a ./prefixed SOURCEMAP_FILE against the project root', () => {
const echoScript = makeSourcemapEchoScript(tempDir);
const result = runScript({
SENTRY_CLI_EXECUTABLE: echoScript,
SOURCEMAP_FILE: './maps/main.jsbundle.map',
});
// The leading ./ is stripped via ${SOURCEMAP_FILE#./} before concatenation,
// so the result is a clean absolute path without any ./ component.
const projectRoot = path.dirname(tempDir);
const expectedPath = path.join(projectRoot, 'maps/main.jsbundle.map');
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain(`SOURCEMAP_FILE=${expectedPath}`);
});
it('uses the absolute default SOURCEMAP_FILE when not set by the user', () => {
const echoScript = makeSourcemapEchoScript(tempDir);
const result = runScript({
SENTRY_CLI_EXECUTABLE: echoScript,
// SOURCEMAP_FILE intentionally not set — script should default to $DERIVED_FILE_DIR/main.jsbundle.map
DERIVED_FILE_DIR: tempDir,
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain(`SOURCEMAP_FILE=${tempDir}/main.jsbundle.map`);
});
});
});