Repository navigation
Expand file tree
/
Copy pathMasonfile.test.ts
More file actions
929 lines (897 loc) · 30.9 KB
/
Copy pathMasonfile.test.ts
File metadata and controls
929 lines (897 loc) · 30.9 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
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
import { afterEach, describe, expect, it } from '@jest/globals'
import {
copyFileSync,
mkdirSync,
mkdtempSync,
readFileSync,
rmSync,
writeFileSync
} from 'fs'
import { tmpdir } from 'os'
import { join } from 'path'
import {
androidApks,
androidSigning,
buildAndroid,
buildIos,
configsFor,
runTests,
secretDirectory,
setAndroidVersion,
setup,
signAndroid,
signIos,
appFor,
MAESTRO_BRANCHES,
buildIosMaestro,
buildAndroidMaestro,
signAndroidMaestro,
setMaestroFlag,
simulatorApp,
appStoreVersion,
uploadIos,
uploadsToTestflight,
versionSuffix,
writeSecrets
} from './Masonfile'
import { stampGradle } from './scripts/updateVersion'
type Call = [string, ...unknown[]]
/** A recording `Setup` for one branch. */
function recordSetup(branch: string): { calls: Call[]; m: any } {
const calls: Call[] = []
const m = {
branch,
apiVersion: (...args: unknown[]) => calls.push(['apiVersion', ...args]),
buildNumber: (...args: unknown[]) => calls.push(['buildNumber', ...args]),
stage: (...args: unknown[]) => calls.push(['stage', ...args]),
parallel: (...args: unknown[]) => calls.push(['parallel', ...args]),
// The workflow's stages are recorded as if declared on `m`.
workflow: (name: string, options: unknown, declare: (w: any) => void) => {
calls.push(['workflow', name, options])
declare(m)
}
}
return { calls, m }
}
/** The temporary checkouts the tests made, removed after each test. */
const roots: string[] = []
afterEach(() => {
for (const root of roots.splice(0))
rmSync(root, { recursive: true, force: true })
})
/** A checkout with package.json and build.gradle, and a recording `Stage` on it. */
function recordStage(branch: string): {
calls: Call[]
ctx: any
root: string
} {
const root = mkdtempSync(join(tmpdir(), 'masonfile-'))
roots.push(root)
mkdirSync(join(root, 'android/app'), { recursive: true })
// The repository's own build.gradle, so the stamping follows its real lines.
copyFileSync(
join(__dirname, 'android/app/build.gradle'),
join(root, 'android/app/build.gradle')
)
writeFileSync(join(root, 'package.json'), '{"version":"4.53.0"}')
const calls: Call[] = []
const ctx = {
workspace: root,
run: { branch },
sh: async (...args: unknown[]) => calls.push(['sh', ...args]),
exec: async (...args: unknown[]) => calls.push(['exec', ...args]),
publish: async (...args: unknown[]) => calls.push(['publish', ...args]),
writeSecretFile: async (...args: unknown[]) =>
calls.push(['writeSecretFile', ...args]),
getBuildNumber: (key: string) => {
calls.push(['getBuildNumber', key])
return '26103001'
},
artifacts: {
save: async (...args: unknown[]) => calls.push(['save', ...args])
},
stash: async (...args: unknown[]) => calls.push(['stash', ...args]),
unstash: async (...args: unknown[]) => calls.push(['unstash', ...args]),
// A declared secret file, written where the worker would keep it.
secretFile: (name: string) => {
calls.push(['secretFile', name])
return join(root, 'secrets', name)
},
output: async (stage: string) => {
calls.push(['output', stage])
return { version: '4.53.0-d', build: '26103001' }
},
log: {
section: async (title: string, body: () => Promise<unknown>) => {
calls.push(['section', title])
return await body()
}
},
fs: {
read: async (path: string) => readFileSync(join(root, path), 'utf8')
}
}
return { calls, ctx, root }
}
/** Where xcodebuild writes the iPhone Simulator app. */
const SIMULATOR_PRODUCTS =
'build/derivedData/Build/Products/Release-iphonesimulator'
const commands = (calls: Call[]): unknown[] =>
calls.filter(([kind]) => kind === 'sh').map(call => call[1])
describe('Masonfile', () => {
it('tests first, then builds each platform with its tools', () => {
for (const branch of [
'develop',
'beta',
'coinhub',
'airbitz',
'yolo',
'master',
'feature/x'
]) {
const { calls, m } = recordSetup(branch)
setup(m)
expect(calls.map(call => call[0])).toEqual([
'apiVersion',
'buildNumber',
'workflow',
'stage',
'parallel'
])
// Pushes to these branches build; any other branch on request.
expect(calls[2]).toEqual([
'workflow',
'build',
{
triggers: [
{
push: true,
branches: ['master', 'develop', 'staging', 'beta']
},
{ request: true }
]
}
])
// Each branch numbers its own app ID's builds.
expect(calls[1][1]).toBe(appFor(branch))
expect(calls[1][2]).toEqual({
format: 'YYMMDDCC',
timeZone: 'America/Los_Angeles',
block: branch === 'master' ? 3 : 1
})
// The node type is what starts a pool machine; Linux is asked first.
const linux = { type: 'linux-x86', os: 'linux', arch: 'x64' }
const mac = { type: 'macos-arm64', os: 'macos', arch: 'arm64' }
const [, name, test] = calls[3] as [string, string, any]
expect(name).toBe('test')
expect(test.on).toEqual([linux, mac])
expect(test.tools).toEqual({ node: '22' })
expect(test.timeout).toBe('1h')
expect(test.activityTimeout).toBe('30m')
expect(test.secrets).toBeUndefined()
expect(test.run).toBe(runTests)
const [, group, stages, options] = calls[4] as [string, string, any, any]
expect(group).toBe('build')
expect(options).toEqual({ failFast: false })
const configs = configsFor(branch)
// iOS archives unsigned on a pool Mac, then signs for the App Store.
const app = appFor(branch)
const [ios, iosSign, iosUpload] = stages.ios
expect(ios.name).toBe('build')
expect(ios.tools).toEqual({
node: '22',
xcode: '>=26',
cocoapods: '1.16.2'
})
expect(ios.on).toEqual(mac)
expect(ios.run).toBe(buildIos)
expect(iosSign).toEqual({
name: 'sign',
on: mac,
checkout: false,
apple: { distribution: 'adHoc', app },
timeout: '2h',
activityTimeout: '30m',
publishes: [app],
run: signIos
})
// Only master's build is signed for the App Store and sent to TestFlight.
expect(stages.ios.length).toBe(uploadsToTestflight(branch) ? 3 : 2)
if (uploadsToTestflight(branch))
expect(iosUpload).toEqual({
name: 'testflight',
on: mac,
checkout: false,
apple: { distribution: 'appStore', app },
timeout: '2h',
activityTimeout: '30m',
run: uploadIos
})
// Android builds on a pool machine, then signs on the signing node.
const [build, sign] = stages.android
expect(build.name).toBe('build')
expect(build.on).toEqual([linux, mac])
// Until these builds ship, they upload nothing to Sentry.
for (const stage of [ios, build]) {
expect(stage.env).toEqual({ SENTRY_DISABLE_AUTO_UPLOAD: 'true' })
expect([stage.timeout, stage.activityTimeout]).toEqual(['3h', '30m'])
}
expect(ios.configs).toEqual(configs)
expect(ios.secrets).toContain('GoogleService-Info.plist')
expect(ios.publishes).toBeUndefined()
expect(ios.buildNumbers).toEqual([app])
expect(build.tools.androidLicenses).toBe('accept')
expect(build.tools.android).toEqual(
expect.arrayContaining(['platforms;android-35', 'build-tools;35.0.0'])
)
expect(build.configs).toEqual(configs)
expect(build.secrets).toContain('google-services.json')
expect(build.publishes).toBeUndefined()
expect(build.buildNumbers).toEqual([app])
expect(build.run).toBe(buildAndroid)
// Only the sign stage, on the signing node and with no checkout, gets
// the keystores and deploy-config.json; no other stage declares them.
expect(sign).toMatchObject({
name: 'sign',
on: { labels: ['android-signing'] },
checkout: false,
configs,
publishes: [app],
run: signAndroid
})
expect(sign.secrets).toEqual([
'deploy-config.json',
'edge-release-keystore.jks',
'edge-beta-keystore.jks',
'edge-develop-keystore.jks',
'coinhub-keystore.jks',
'airbitzbd-keystore.jks'
])
expect(sign.tools).toEqual({
java: '17',
android: ['build-tools;36.1.0'],
androidLicenses: 'accept'
})
for (const stage of [test, ios, iosSign, build])
expect(
(stage.secrets ?? []).some(
(name: string) =>
name.endsWith('.jks') || name === 'deploy-config.json'
)
).toBe(false)
}
})
it('runs the Jest suite after a clean install and prepare', async () => {
const { calls, ctx } = recordStage('develop')
await runTests(ctx)
expect(commands(calls)).toEqual([
'npm ci',
'npm run prepare',
'npm test -- --ci'
])
})
it('names each stage’s phases, in order, as Jenkins did', async () => {
const sections = async (
branch: string,
stage: (ctx: any) => Promise<unknown>
): Promise<unknown[]> => {
const { calls, ctx } = recordStage(branch)
await stage(ctx)
return calls.filter(([kind]) => kind === 'section').map(call => call[1])
}
expect(await sections('develop', runTests)).toEqual([
'Install packages',
'Prepare',
'Jest'
])
const prepared = ['Secret files', 'Install packages', 'Prepare']
expect(await sections('develop', buildIos)).toEqual([
...prepared,
'Version 4.53.0-d (26103001)',
'CocoaPods',
'Archive',
'Save the archive',
'Stash the archive for signing'
])
expect(await sections('develop', buildAndroid)).toEqual([
...prepared,
'Version 4.53.0-d (26103001)',
'Build the APK',
'Stash the APKs for signing'
])
expect(await sections('master', buildAndroid)).toEqual([
...prepared,
'Version 4.53.0 (26103001)',
'Build the AAB and APKs',
'Save the AAB',
'Stash the APKs for signing'
])
})
it('builds master’s universal APK and its two ABI APKs, and stashes them for signing', async () => {
const { calls, ctx, root } = recordStage('master')
expect(await buildAndroid(ctx)).toEqual({ version: '4.53.0' })
expect(commands(calls)).toEqual([
'npm ci',
'npm run prepare',
'./gradlew bundleRelease assembleRelease -PabiSplits'
])
const gradle = readFileSync(join(root, 'android/app/build.gradle'), 'utf8')
expect(gradle).toContain('versionCode 26103001')
expect(gradle).toContain('versionName "4.53.0"')
// Only those lines change: the split APKs' offsets stay as they are.
const original = readFileSync(
join(__dirname, 'android/app/build.gradle'),
'utf8'
)
expect(gradle.split('\n').length).toBe(original.split('\n').length)
expect(gradle).toContain("['armeabi-v7a': 1, 'arm64-v8a': 2]")
expect(calls.filter(([kind]) => kind === 'save')).toEqual([
['save', 'android-aab', 'android/app/build/outputs/bundle/release/*.aab']
])
// Nothing is published from the pool machine: the APKs go to signing.
expect(calls.filter(([kind]) => kind === 'publish')).toEqual([])
const apks = 'android/app/build/outputs/apk/release'
expect(calls.filter(([kind]) => kind === 'stash')).toEqual([
[
'stash',
'android-apks',
[
`${apks}/app-universal-release.apk`,
`${apks}/app-armeabi-v7a-release.apk`,
`${apks}/app-arm64-v8a-release.apk`
]
]
])
// The Firebase config ships in the app, so it is readable; the rest stay owner-only.
expect(calls.filter(([kind]) => kind === 'writeSecretFile')).toEqual([
['writeSecretFile', 'config.json', '.', {}],
['writeSecretFile', 'edgeKey.json', '.', {}],
['writeSecretFile', 'keys.json', '.', {}],
[
'writeSecretFile',
'google-services.json',
'android/app',
{ mode: 0o644 }
]
])
})
it('builds one APK on other branches and stashes it', async () => {
const { calls, ctx, root } = recordStage('develop')
expect(await buildAndroid(ctx)).toEqual({ version: '4.53.0-d' })
expect(commands(calls)).toContain('./gradlew assembleRelease')
expect(calls.filter(([kind]) => kind === 'save')).toEqual([])
expect(calls.filter(([kind]) => kind === 'stash')).toEqual([
[
'stash',
'android-apks',
['android/app/build/outputs/apk/release/app-release.apk']
]
])
expect(
readFileSync(join(root, 'android/app/build.gradle'), 'utf8')
).toContain('versionName "4.53.0-d"')
})
it('signs the stashed APKs with the branch’s keystore and publishes them', async () => {
const deployConfig = {
edge: {
androidKeyStore: 'edge-release-keystore.jks',
androidKeyStoreAlias: 'edge',
androidKeyStorePassword: 'release-pass',
android: {
develop: {
androidKeyStore: 'edge-develop-keystore.jks',
androidKeyStorePassword: 'develop-pass'
}
}
}
}
const sign = async (branch: string) => {
const { calls, ctx, root } = recordStage(branch)
mkdirSync(join(root, 'secrets'))
writeFileSync(
join(root, 'secrets', 'deploy-config.json'),
JSON.stringify(deployConfig)
)
await signAndroid(ctx)
return { calls, root }
}
const { calls, root } = await sign('develop')
expect(calls[0]).toEqual(['section', 'Unstash the APKs'])
expect(calls[1]).toEqual(['unstash', 'android-apks'])
expect(calls.filter(([kind]) => kind === 'output')).toEqual([
['output', 'build/android/build']
])
expect(calls.filter(([kind]) => kind === 'secretFile')).toEqual([
['secretFile', 'deploy-config.json'],
['secretFile', 'edge-develop-keystore.jks']
])
const keystore = join(root, 'secrets', 'edge-develop-keystore.jks')
const apksigner = '"$ANDROID_HOME/build-tools/36.1.0/apksigner"'
// The password and alias reach apksigner by its environment, never its arguments.
expect(calls.filter(([kind]) => kind === 'sh')).toEqual([
['sh', 'mkdir -p signed'],
[
'sh',
`${apksigner} sign --ks "${keystore}" --ks-key-alias "$KEY_ALIAS" --ks-pass env:KEY_PASSWORD --key-pass env:KEY_PASSWORD --out signed/app-release.apk android/app/build/outputs/apk/release/app-release.apk`,
{ env: { KEY_ALIAS: 'edge', KEY_PASSWORD: 'develop-pass' } }
],
['sh', `${apksigner} verify signed/app-release.apk`]
])
expect(calls.filter(([kind]) => kind === 'publish')).toEqual([
[
'publish',
{
app: 'app.edge.develop',
platform: 'android',
path: 'signed/app-release.apk',
changelog: 'develop 4.53.0-d'
}
]
])
expect(
calls.filter(([kind]) => kind === 'section').map(call => call[1])
).toEqual([
'Unstash the APKs',
'Sign with edge-develop-keystore.jks',
'Publish'
])
// master signs and publishes the universal APK and each ABI's as a variant.
const master = await sign('master')
expect(
master.calls.filter(([kind]) => kind === 'publish').map(call => call[1])
).toEqual([
expect.objectContaining({ path: 'signed/app-universal-release.apk' }),
expect.objectContaining({
variant: 'armeabi-v7a',
path: 'signed/app-armeabi-v7a-release.apk'
}),
expect.objectContaining({
variant: 'arm64-v8a',
path: 'signed/app-arm64-v8a-release.apk'
})
])
expect(
master.calls.find(
([kind, line]) => kind === 'sh' && String(line).includes(' sign ')
)![2]
).toEqual({ env: { KEY_ALIAS: 'edge', KEY_PASSWORD: 'release-pass' } })
})
it('merges a branch’s Android signing as scripts/deploy.ts does, and refuses a gap', () => {
const config = {
edge: {
androidKeyStore: 'a.jks',
androidKeyStoreAlias: 'top',
android: {
androidKeyStorePassword: 'android-pass',
beta: { androidKeyStore: 'b.jks' }
}
}
}
expect(androidSigning(config, 'beta')).toEqual({
keystore: 'b.jks',
alias: 'top',
password: 'android-pass'
})
expect(androidSigning(config, 'feature/x').keystore).toBe('a.jks')
expect(() => androidSigning({}, 'develop')).toThrow(
'deploy-config.json has no androidKeyStore for develop'
)
expect(() => androidSigning(null, 'develop')).toThrow(
'deploy-config.json has no androidKeyStore for develop'
)
expect(androidApks('develop')).toEqual([{ file: 'app-release.apk' }])
})
it('stamps the iOS version with agvtool and archives', async () => {
const { calls, ctx } = recordStage('yolo')
expect(await buildIos(ctx)).toEqual({
version: '4.53.0-yolo',
build: '26103001'
})
expect(calls.filter(([kind]) => kind === 'exec').slice(-2)).toEqual([
[
'exec',
'agvtool',
['new-marketing-version', '4.53.0-yolo'],
{ cwd: 'ios' }
],
['exec', 'agvtool', ['new-version', '-all', '26103001'], { cwd: 'ios' }]
])
expect(calls.filter(([kind]) => kind === 'writeSecretFile')).toEqual([
['writeSecretFile', 'config.json', '.', {}],
['writeSecretFile', 'edgeKey.json', '.', {}],
['writeSecretFile', 'keys.json', '.', {}],
[
'writeSecretFile',
'GoogleService-Info.plist',
'ios/edge',
{ mode: 0o644 }
]
])
const sh = commands(calls)
expect(sh.slice(-2)[0]).toBe('npm run prepare.ios')
// CocoaPods runs without forced colour, so podspecs read plain output.
expect(
calls.find(
([kind, line]) => kind === 'sh' && line === 'npm run prepare.ios'
)
).toEqual(['sh', 'npm run prepare.ios', { color: false }])
expect(sh.at(-1)).toMatch(/^xcodebuild archive /)
// The archive is kept, and stashed with the entitlements for signing.
expect(calls.slice(-5)).toEqual([
['section', 'Save the archive'],
['save', 'ios-archive', 'build/edge.xcarchive/**'],
['section', 'Stash the archive for signing'],
[
'stash',
'ios-build',
['build/edge.xcarchive/**', 'ios/edge/edge.entitlements']
],
// Handed to the sign stage, which reserves no number of its own.
['getBuildNumber', 'co.edgesecure.app']
])
})
it('gives the App Store the numeric version without the branch suffix', () => {
expect(appStoreVersion('4.53.0-d')).toBe('4.53.0')
expect(appStoreVersion('4.53.0-paul-mason')).toBe('4.53.0')
expect(appStoreVersion('4.53.0')).toBe('4.53.0')
})
it('builds each branch under its app ID: develop, beta and coinhub their own', () => {
expect(appFor('develop')).toBe('app.edge.develop')
expect(appFor('beta')).toBe('app.edge.beta')
expect(appFor('coinhub')).toBe('app.coinhubatm.wallet')
for (const branch of ['master', 'staging', 'feature/x'])
expect(appFor(branch)).toBe('co.edgesecure.app')
})
it('signs the stashed archive ad hoc and publishes it under the branch’s app ID', async () => {
for (const [branch, app] of [
['develop', 'app.edge.develop'],
['beta', 'app.edge.beta'],
['master', 'co.edgesecure.app']
]) {
const { calls, ctx } = recordStage(branch)
// Like Mason, a stage without buildNumbers has no build number.
ctx.getBuildNumber = (key: string) => {
throw new Error(`Stage build/ios/sign has no build number for ${key}`)
}
ctx.apple = {
resign: async (...args: unknown[]) => {
calls.push(['resign', ...args])
return { ipa: 'build/edge-adhoc.ipa', bundles: [] }
}
}
expect(await signIos(ctx)).toEqual({ version: '4.53.0-d' })
expect(calls.filter(([kind]) => kind !== 'section')).toEqual([
['unstash', 'ios-build'],
['output', 'build/ios/build'],
[
'resign',
{
path: 'build/edge.xcarchive',
output: 'build/edge-adhoc.ipa',
entitlements: { [app]: 'ios/edge/edge.entitlements' }
}
],
[
'publish',
{
app,
platform: 'ios',
path: 'build/edge-adhoc.ipa',
changelog: `${branch} 4.53.0-d`
}
]
])
}
})
it('signs master’s archive for the App Store and uploads it to TestFlight', async () => {
expect(uploadsToTestflight('master')).toBe(true)
for (const branch of ['develop', 'beta', 'staging', 'feature/x'])
expect(uploadsToTestflight(branch)).toBe(false)
const { calls, ctx } = recordStage('master')
ctx.run.commit = '0123456789abcdef'
ctx.getBuildNumber = (key: string) => {
throw new Error(
`Stage build/ios/testflight has no build number for ${key}`
)
}
ctx.apple = {
resign: async (...args: unknown[]) => {
calls.push(['resign', ...args])
return { ipa: 'build/edge-appstore.ipa', bundles: [] }
},
testflight: async (...args: unknown[]) => {
calls.push(['testflight', ...args])
return { buildId: 'b', version: '4.53.0', buildNumber: '26103001' }
}
}
expect(await uploadIos(ctx)).toEqual({
version: '4.53.0-d',
testflight: '26103001'
})
expect(calls.filter(([kind]) => kind !== 'section')).toEqual([
['unstash', 'ios-build'],
['output', 'build/ios/build'],
[
'resign',
{
path: 'build/edge.xcarchive',
output: 'build/edge-appstore.ipa',
entitlements: { 'co.edgesecure.app': 'ios/edge/edge.entitlements' },
version: '4.53.0'
}
],
['save', 'ios-appstore-ipa', 'build/edge-appstore.ipa'],
[
'testflight',
{
path: 'build/edge-appstore.ipa',
usesNonExemptEncryption: false,
whatToTest: 'master 4.53.0-d (26103001), 012345678'
}
]
])
})
it('builds Maestro variants only on the Jenkinsfile’s Maestro branches', () => {
const mac = { type: 'macos-arm64', os: 'macos', arch: 'arm64' }
const linux = { type: 'linux-x86', os: 'linux', arch: 'x64' }
for (const branch of [
'develop',
'staging',
'master',
'beta',
'testMaestro',
'yolo',
'feature/x'
]) {
const { calls, m } = recordSetup(branch)
setup(m)
const stages = calls[4][2] as any
const maestro = MAESTRO_BRANCHES.includes(branch)
expect(Object.keys(stages)).toEqual(
maestro
? ['ios', 'android', 'ios-maestro', 'android-maestro']
: ['ios', 'android']
)
if (!maestro) continue
const app = appFor(branch)
expect(stages['ios-maestro']).toMatchObject({
on: mac,
secrets: expect.arrayContaining(['config.json']),
buildNumbers: [app],
publishes: [app],
run: buildIosMaestro
})
const [build, sign] = stages['android-maestro']
expect(build).toMatchObject({
name: 'build',
on: [linux, mac],
buildNumbers: [app],
run: buildAndroidMaestro
})
expect(sign).toMatchObject({
name: 'sign',
on: { labels: ['android-signing'] },
checkout: false,
publishes: [app],
run: signAndroidMaestro
})
}
})
it('sets the Maestro flag in config.json and keeps the rest', async () => {
const { root } = recordStage('develop')
writeFileSync(
join(root, 'config.json'),
'{"A":1,"ENABLE_MAESTRO_BUILD":false}'
)
await setMaestroFlag(root)
expect(JSON.parse(readFileSync(join(root, 'config.json'), 'utf8'))).toEqual(
{
A: 1,
ENABLE_MAESTRO_BUILD: true
}
)
})
it('builds the iOS Maestro variant for the iPhone Simulator and publishes the zipped app', async () => {
const { calls, ctx, root } = recordStage('develop')
writeFileSync(join(root, 'config.json'), '{"A":1}')
// develop's deploy patch names the app Edge Develop, as xcodebuild writes it.
mkdirSync(join(root, SIMULATOR_PRODUCTS, 'Edge Develop.app'), {
recursive: true
})
mkdirSync(join(root, SIMULATOR_PRODUCTS, 'Edge Develop.app.dSYM'))
expect(await buildIosMaestro(ctx)).toEqual({ version: '4.53.0-d' })
expect(JSON.parse(readFileSync(join(root, 'config.json'), 'utf8'))).toEqual(
{
A: 1,
ENABLE_MAESTRO_BUILD: true
}
)
expect(commands(calls).slice(-2)).toEqual([
'npm run prepare.ios',
'xcodebuild build -workspace ios/edge.xcworkspace -scheme edge -sdk iphonesimulator -arch arm64 -configuration Release -derivedDataPath build/derivedData'
])
expect(calls.filter(([kind]) => kind === 'exec').at(-1)).toEqual([
'exec',
'ditto',
[
'-c',
'-k',
'--keepParent',
`${SIMULATOR_PRODUCTS}/Edge Develop.app`,
'build/edge-maestro.zip'
]
])
expect(calls.filter(([kind]) => kind === 'publish')).toEqual([
[
'publish',
{
app: 'app.edge.develop',
platform: 'ios',
variant: 'maestro',
path: 'build/edge-maestro.zip',
changelog: 'develop 4.53.0-d'
}
]
])
})
it('zips the one simulator app the build wrote, whatever the branch names it', async () => {
const { root } = recordStage('beta')
const products = join(root, SIMULATOR_PRODUCTS)
mkdirSync(products, { recursive: true })
await expect(simulatorApp(root)).rejects.toThrow(
`${SIMULATOR_PRODUCTS} should hold one app, but holds none`
)
mkdirSync(join(products, 'Edge Beta.app'))
expect(await simulatorApp(root)).toBe(`${SIMULATOR_PRODUCTS}/Edge Beta.app`)
mkdirSync(join(products, 'Edge.app'))
await expect(simulatorApp(root)).rejects.toThrow(
`${SIMULATOR_PRODUCTS} should hold one app, but holds `
)
})
it('builds the Android Maestro APK with the flag and stashes it for signing', async () => {
const { calls, ctx, root } = recordStage('master')
writeFileSync(join(root, 'config.json'), '{}')
expect(await buildAndroidMaestro(ctx)).toEqual({ version: '4.53.0' })
expect(
JSON.parse(readFileSync(join(root, 'config.json'), 'utf8'))
.ENABLE_MAESTRO_BUILD
).toBe(true)
// No split APKs: a Maestro build is the plain release APK.
expect(commands(calls).at(-1)).toBe('./gradlew assembleRelease')
expect(calls.filter(([kind]) => kind === 'stash')).toEqual([
[
'stash',
'android-maestro-apk',
['android/app/build/outputs/apk/release/app-release.apk']
]
])
})
it('signs the Android Maestro APK with the branch’s keystore and publishes it as the maestro variant', async () => {
const { calls, ctx, root } = recordStage('beta')
mkdirSync(join(root, 'secrets'))
writeFileSync(
join(root, 'secrets', 'deploy-config.json'),
JSON.stringify({
edge: {
androidKeyStore: 'edge-release-keystore.jks',
androidKeyStoreAlias: 'edge',
androidKeyStorePassword: 'release-pass',
android: { beta: { androidKeyStore: 'edge-beta-keystore.jks' } }
}
})
)
await signAndroidMaestro(ctx)
expect(calls[1]).toEqual(['unstash', 'android-maestro-apk'])
expect(calls.filter(([kind]) => kind === 'output')).toEqual([
['output', 'build/android-maestro/build']
])
expect(calls.filter(([kind]) => kind === 'secretFile')).toContainEqual([
'secretFile',
'edge-beta-keystore.jks'
])
expect(calls.filter(([kind]) => kind === 'publish')).toEqual([
[
'publish',
{
app: 'app.edge.beta',
platform: 'android',
variant: 'maestro',
path: 'signed/app-release.apk',
changelog: 'beta 4.53.0-d'
}
]
])
})
it('refuses a build.gradle missing its version name or code', async () => {
const { root } = recordStage('develop')
writeFileSync(
join(root, 'android/app/build.gradle'),
'versionName "1.0.0"\n'
)
await expect(setAndroidVersion(root, '1.2.3', '7')).rejects.toThrow(
'android/app/build.gradle has no /versionCode [0-9]+/g'
)
writeFileSync(join(root, 'android/app/build.gradle'), 'versionCode 1\n')
await expect(setAndroidVersion(root, '1.2.3', '7')).rejects.toThrow(
'android/app/build.gradle has no /versionName "[^"]*"/g'
)
})
it('knows where every secret file goes, by its file name', () => {
expect(
[
'config.json',
'edgeKey.json',
'keys.json',
'deploy-config.json',
'GoogleService-Info.plist',
'google-services.json',
'edge-release-keystore.jks',
'airbitzbd-keystore.jks',
'master/config.json',
'fastlane.json'
].map(secretDirectory)
).toEqual([
'.',
'.',
'.',
'.',
'ios/edge',
'android/app',
'keystores',
'keystores',
undefined,
undefined
])
})
it('refuses a secret file with no directory', async () => {
const { ctx } = recordStage('develop')
await expect(writeSecrets(ctx, ['fastlane.json'])).rejects.toThrow(
'No directory for fastlane.json'
)
})
it('stamps build.gradle exactly as scripts/updateVersion.ts does', async () => {
const original = readFileSync(
join(__dirname, 'android/app/build.gradle'),
'utf8'
)
const { root } = recordStage('develop')
await setAndroidVersion(root, '4.53.0-d', '26103001')
expect(readFileSync(join(root, 'android/app/build.gradle'), 'utf8')).toBe(
stampGradle(original, { version: '4.53.0-d', build: 26103001 })
)
// A file missing either line fails both the same way.
for (const pattern of [/versionName "[^"]*"/, /versionCode [0-9]+/]) {
const broken = original.replace(pattern, '')
writeFileSync(join(root, 'android/app/build.gradle'), broken)
let jenkins = ''
try {
stampGradle(broken, { version: '1.0.0', build: 1 })
} catch (error: unknown) {
jenkins = String(error)
}
expect(jenkins).toMatch(/has no/)
await expect(setAndroidVersion(root, '1.0.0', '1')).rejects.toThrow(
jenkins.replace(/^Error: /, '')
)
}
})
it('gives each branch its suffix and configs', () => {
expect(versionSuffix('develop')).toBe('-d')
expect(versionSuffix('master')).toBe('')
expect(versionSuffix('test-feta')).toBe('-feta')
expect(versionSuffix('')).toBe('')
expect(versionSuffix('paul/some_thing')).toBe('-paul-some-thing')
// Every branch Jenkins' gitVersionFile.ts names gets the same suffix here.
const jenkins = readFileSync(
join(__dirname, 'scripts/gitVersionFile.ts'),
'utf8'
)
const table = /const specialBranches[^{]*\{([^}]*)\}/.exec(jenkins)?.[1]
const entries = [...(table ?? '').matchAll(/'?([\w-]+)'?: '([^']*)'/g)]
expect(entries.length).toBeGreaterThan(10)
for (const [, branch, suffix] of entries)
expect([branch, versionSuffix(branch!)]).toEqual([branch, suffix])
expect(configsFor('beta')).toEqual(['beta'])
expect(configsFor('master')).toEqual([])
})
})