๐Ÿ“ฆ ionic-team / trapeze

๐Ÿ“„ project.ios.test.ts ยท 532 lines
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
532import { temporaryDirectory } from 'tempy';
import { join } from 'path';
import { copy, pathExists, readFile, rm } from '@ionic/utils-fs';
import { MobileProject, StringsFile, XCConfigFile, XmlFile } from '../src';
import { MobileProjectConfig } from '../src/config';
import { PlistFile } from '../src/plist';

describe('project - ios standard', () => {
  let config: MobileProjectConfig;
  let project: MobileProject;

  let dir: string;
  beforeEach(async () => {
    dir = temporaryDirectory();
    await copy('../common/test/fixtures/ios-and-android', dir);

    config = {
      ios: {
        path: 'ios/App',
      },
      android: {
        path: 'android'
      }
    }

    project = new MobileProject(dir, config);
    await project.load();
  });

  afterEach(async () => {
    await rm(dir, { force: true, recursive: true });
  });

  it('should load pbx project', async () => {
    expect(project.ios?.getPbxProject()).not.toBe(null);
  });

  it('should load targets', async () => {
    const targets = project.ios?.getTargets();
    expect(targets?.length).toBe(5);
    expect(targets?.every(t => t.buildConfigurations.length > 0)).toBe(true);
  });

  it('should get target by name', async () => {
    const target = project.ios?.getTarget('App');
    expect(target).toBeDefined();
  });

  it('should get main app target', async () => {
    const target = project.ios?.getAppTarget();
    expect(target).toBeDefined();
    expect(target?.productName).toBe('App');
  });

  it('should get target bundle id', async () => {
    expect(project.ios?.getBundleId('App', 'Debug')).toBe('io.ionic.wowzaStarter');
    expect(project.ios?.getBundleId('App')).toBe('io.ionic.wowzaStarter');
    expect(project.ios?.getBundleId('My App Clip', 'Debug')).toBe('io.ionic.wowzaStarter.Clip');
  });

  it('should get target build configurations', async () => {
    const configs = project.ios?.getBuildConfigurations('App');
    expect(configs?.length).toBe(2);
  });

  it('should get target build configuration names', async () => {
    const configs = project.ios?.getBuildConfigurationNames('App');
    expect(configs).toMatchObject(['Debug', 'Release']);
  });

  it('should set target bundle id', async () => {
    project.ios?.setBundleId('App', 'Debug', 'io.ionic.betterBundleId');
    let debugBundleId = project.ios?.getBundleId('App', 'Debug');
    let releaseBundleId = project.ios?.getBundleId('App', 'Release');
    expect(debugBundleId).toBe('io.ionic.betterBundleId');
    expect(releaseBundleId).not.toBe('io.ionic.betterBundleId');

    // Test if passing null as a build sets the value for both release and debug
    project.ios?.setBundleId('App', null, 'io.ionic.betterBundleId');
    debugBundleId = project.ios?.getBundleId('App', 'Debug');
    releaseBundleId = project.ios?.getBundleId('App', 'Release');
    expect(debugBundleId).toBe('io.ionic.betterBundleId');
    expect(releaseBundleId).toBe('io.ionic.betterBundleId');
  });

  it('should get target product name', async () => {
    expect(project.ios?.getProductName('App')).toBe('App');
  });

  it('should get build number', async () => {
    expect(await project.ios?.getBuild('App', 'Debug')).toBe(1);
    expect(await project.ios?.getBuild('App', 'Release')).toBe(1);
    expect(await project.ios?.incrementBuild('App', 'Debug'));
    expect(await project.ios?.getBuild('App', 'Debug')).toBe(2);
    expect(await project.ios?.getBuild('App', 'Release')).toBe(1);
    expect(await project.ios?.incrementBuild('App'));
    expect(await project.ios?.getBuild('App', 'Debug')).toBe(2);
    expect(await project.ios?.getBuild('App', 'Release')).toBe(2);

    // Make sure the info plist is updated to use the CURRENT_PROJECT_VERSION
    const filename = await project.ios?.getInfoPlistFilename('App', 'Debug');
    const updated = project.vfs.get<PlistFile>(filename!)?.getData();
    expect(updated?.getDocument()?.['CFBundleVersion']).toBe('$(CURRENT_PROJECT_VERSION)');
  });

  it('should set build number', async () => {
    await project.ios?.setBuild('App', 'Debug', 42);
    expect(await project.ios?.getBuild('App', 'Debug')).toBe(42);
    const filename = await project.ios?.getInfoPlistFilename('App', 'Debug');
    const updated = project.vfs.get<PlistFile>(filename!)?.getData();
    expect(updated?.getDocument()?.['CFBundleVersion']).toBe('$(CURRENT_PROJECT_VERSION)');
  });

  it('should set string build numbers', async () => {
    await project.ios?.setBuild('App', 'Debug', '1.2.3');
    expect(await project.ios?.getBuild('App', 'Debug')).toBe('1.2.3');
  });

  it('should increment build number', async () => {
    await project.ios?.setBuild('App', 'Debug', 42);
    expect(await project.ios?.getBuild('App', 'Debug')).toBe(42);
    await project.ios?.incrementBuild('App', 'Debug');
    expect(await project.ios?.getBuild('App', 'Debug')).toBe(43);
    await project.ios?.setBuild('App', 'Debug', '1.2.3');
    expect(await project.ios?.getBuild('App', 'Debug')).toBe('1.2.3');
    await project.ios?.incrementBuild('App', 'Debug');
    expect(await project.ios?.getBuild('App', 'Debug')).toBe('1.2.3');
  });

  it('should set project version', async () => {
    await project.ios?.setVersion('App', 'Debug', '1.4.5');
    expect(project.ios?.getVersion('App', 'Debug')).toBe('1.4.5');
    // Make sure the info plist is updated to use the MARKETING_VERSION
    const filename = await project.ios?.getInfoPlistFilename('App', 'Debug');
    const updated = project.vfs.get<PlistFile>(filename!)?.getData();
    expect(updated?.getDocument()?.['CFBundleShortVersionString']).toBe('$(MARKETING_VERSION)');
  });

  it('should update build settings', async () => {
    project.ios?.setBuildProperty('App', 'Debug', 'FAKE_PROPERTY', 'YES');
    expect(project.ios?.getBuildProperty('App', 'Debug', 'FAKE_PROPERTY')).toBe('YES');
    project.ios?.setBuildProperty('App', 'Release', 'FAKE_PROPERTY', 'YES');
    expect(project.ios?.getBuildProperty('App', 'Release', 'FAKE_PROPERTY')).toBe('YES');
    project.ios?.setBuildProperty('App', null, 'THING', 'THIS');
    expect(project.ios?.getBuildProperty('App', 'Debug', 'THING')).toBe('THIS');
    expect(project.ios?.getBuildProperty('App', 'Release', 'THING')).toBe('THIS');
  });

  it('should properly quote pbxproj string values that need to be quoted', async () => {
    project.ios?.setBuildProperty('App', 'Debug', 'FAKE_PROPERTY', 'This Is A Long String');
    var actualValue = project.ios?.getPbxProject()?.getBuildProperty('FAKE_PROPERTY', 'Debug', 'App');

    // Value read back shouldn't have quotes
    expect(project.ios?.getBuildProperty('App', 'Debug', 'FAKE_PROPERTY')).toBe('This Is A Long String');
    // Value from pbx file should have quotes
    expect(actualValue).toBe('\"This Is A Long String\"');
  });

  it('should add frameworks', async () => {
    const fwks = ['ImageIO.framework', 'AudioToolbox.framework'];
    fwks.forEach(f => project.ios?.addFramework('App', f));
    const frameworks = project.ios?.getFrameworks('App');
    expect(fwks.every(f => (frameworks?.indexOf(f) ?? -1) >= 0)).toBe(true);
  });

  it('should add frameworks to non-app targets', async () => {
    const fwks = ['WebKit.framework', 'QuartzCore.framework'];
    fwks.forEach(f => project.ios?.addFramework('My App Clip', f));
    const frameworks = project.ios?.getFrameworks('My App Clip');
    expect(fwks.every(f => (frameworks?.indexOf(f) ?? -1) >= 0)).toBe(true);
  });

  it('should get entitlements file for each target', async () => {
    expect(project.ios?.getEntitlementsFile('App', 'Debug')).toBe('App/App.entitlements');
    expect(project.ios?.getEntitlementsFile('App', 'Release')).toBe('App/App.entitlements');
    expect(project.ios?.getEntitlementsFile('My App Clip', 'Debug')).toBe('My App Clip/My_App_Clip.entitlements');
    expect(project.ios?.getEntitlementsFile('My App Clip', 'Release')).toBe('My App Clip/My_App_Clip.entitlements');
    expect(project.ios?.getEntitlementsFile('My Share Extension', 'Debug')).toBeUndefined();
    expect(project.ios?.getEntitlementsFile('My Share Extension', 'Release')).toBeUndefined();
  });

  it('should add entitlements to file', async () => {
    await project.ios?.addEntitlements('App', 'Debug', {
      'keychain-access-groups': [
        'group1', 'group2'
      ]
    });

    let entitlements = await project.ios?.getEntitlements('App', 'Debug');
    expect(entitlements).toEqual({
      'keychain-access-groups': [
        'group1', 'group2'
      ]
    });

    await project.ios?.addEntitlements('App', null, {
      'keychain-access-groups': [
        'group3', 'group4'
      ]
    });

    entitlements = await project.ios?.getEntitlements('App');
    expect(entitlements).toEqual({
      'keychain-access-groups': [
        'group1', 'group2', 'group3', 'group4'
      ]
    });
  });

  it('should set entitlements to file', async () => {
    await project.ios?.setEntitlements('App', 'Debug', {
      'keychain-access-groups': [
        'group1', 'group2'
      ]
    });

    let entitlements = await project.ios?.getEntitlements('App', 'Debug');
    expect(entitlements).toEqual({
      'keychain-access-groups': [
        'group1', 'group2'
      ]
    });

    await project.ios?.setEntitlements('App', null, {
      'keychain-access-groups': [
        'group3', 'group4'
      ]
    });

    entitlements = await project.ios?.getEntitlements('App');
    expect(entitlements).toEqual({
      'keychain-access-groups': [
        'group3', 'group4'
      ]
    });
  });

  it('should create new entitlements file if one does not exist', async () => {
    await project.ios?.addEntitlements('My Share Extension', 'Debug', {
      'keychain-access-groups': [
        'group1', 'group2'
      ]
    });
    expect(project.ios?.getEntitlementsFile('My Share Extension', 'Debug')).toBe('My Share Extension/My_Share_Extension.entitlements');
  });

  it('should get info.plist for each target', async () => {
    expect(await project.ios?.getInfoPlist('App', 'Debug')).toBe('App/Info.plist');
    expect(await project.ios?.getInfoPlist('App', 'Release')).toBe('App/Info.plist');
    expect(await project.ios?.getInfoPlist('App')).toBe('App/Info.plist');
    expect(await project.ios?.getInfoPlist('My App Clip', 'Debug')).toBe('My App Clip/AppClip.plist');
    expect(await project.ios?.getInfoPlist('My App Clip', 'Release')).toBe('My App Clip/AppClip.plist');
    expect(await project.ios?.getInfoPlist('My Share Extension', 'Debug')).toBe('My Share Extension/Info.plist');
    expect(await project.ios?.getInfoPlist('My Share Extension', 'Release')).toBe('My Share Extension/Info.plist');
  });

  it('should set display name for target', async () => {
    await project.ios?.setDisplayName('App', 'Debug', 'Super Duper App');
    expect(await project.ios?.getDisplayName('App', 'Debug')).toBe('Super Duper App');
    const filename = await project.ios?.getInfoPlistFilename('App', 'Debug');
    const updated = project.vfs.get<PlistFile>(filename!)?.getData();
    expect(updated?.getDocument()?.['CFBundleDisplayName']).toBe('Super Duper App');
  });

  it('should update plist with entries', async () => {
    await project.ios?.updateInfoPlist('App', 'Debug', {
      NSFaceIDUsageDescription: 'The better to see you with'
    });

    const filename = await project.ios?.getInfoPlistFilename('App', 'Debug');
    const updated = project.vfs.get<PlistFile>(filename!)?.getData();
    expect(updated?.getDocument()?.['NSFaceIDUsageDescription']).toBe('The better to see you with');
  });

  it('should overwrite existing keys in plist', async () => {
    // https://github.com/ionic-team/capacitor-configure/issues/14
    await project.ios?.updateInfoPlist('App', 'Debug', {
      NSFaceIDUsageDescription: 'The better to see you with'
    });
    await project.ios?.updateInfoPlist('App', 'Debug', {
      NSFaceIDUsageDescription: 'This is new'
    });

    const filename = await project.ios?.getInfoPlistFilename('App', 'Debug');
    const updated = project.vfs.get<PlistFile>(filename!)?.getData();
    expect(updated?.getDocument()?.['NSFaceIDUsageDescription']).toBe('This is new');
  });

  it('should support replacing items to arrays in plist', async () => {
    await project.ios?.updateInfoPlist('App', 'Debug', {
      UISupportedInterfaceOrientations: [
        'AppendThis'
      ]
    });
    let filename = await project.ios?.getInfoPlistFilename('App', 'Debug');
    let updated = project.vfs.get<PlistFile>(filename!)?.getData();
    expect(updated?.getDocument()?.['UISupportedInterfaceOrientations']).toEqual([
      'UIInterfaceOrientationPortrait',
      'UIInterfaceOrientationLandscapeLeft',
      'UIInterfaceOrientationLandscapeRight',
      'AppendThis'
    ]);

    await project.ios?.updateInfoPlist('App', 'Debug', {
      UISupportedInterfaceOrientations: [
        'UIInterfaceOrientationPortrait'
      ]
    }, {
      replace: true
    });
    filename = await project.ios?.getInfoPlistFilename('App', 'Debug');
    updated = project.vfs.get<PlistFile>(filename!)?.getData();
    expect(updated?.getDocument()?.['UISupportedInterfaceOrientations']).toEqual(['UIInterfaceOrientationPortrait']);
  });

  it('should support merging objects in plist', async () => {
    await project.ios?.updateInfoPlist('App', 'Debug', {
      TestDict: {
        'AppendThis': false
      }
    });
    let filename = await project.ios?.getInfoPlistFilename('App', 'Debug');
    let updated = project.vfs.get<PlistFile>(filename!)?.getData();
    expect(updated?.getDocument()?.['TestDict']).toEqual({
      'Item1': 'String1',
      'Item2': true,
      'AppendThis': false
    });

    await project.ios?.updateInfoPlist('App', 'Debug', {
      TestDict: {
        'AppendThis': false
      }
    }, {
      replace: true
    });
    filename = await project.ios?.getInfoPlistFilename('App', 'Debug');
    updated = project.vfs.get<PlistFile>(filename!)?.getData();
    expect(updated?.getDocument()?.['TestDict']).toEqual({
      'AppendThis': false
    });
  });

  it('should not add duplicates to plist when applied multiple times', async () => {
    await project.ios?.updateInfoPlist('App', 'Debug', {
      CFBundleURLTypes: [
        {
          CFBundleURLSchemes: [
            'MyApp'
          ]
        }
      ]
    });
    await project.ios?.updateInfoPlist('App', 'Debug', {
      CFBundleURLTypes: [
        {
          CFBundleURLSchemes: [
            'MyApp'
          ]
        }
      ]
    });
    let filename = await project.ios?.getInfoPlistFilename('App', 'Debug');
    let updated = project.vfs.get<PlistFile>(filename!)?.getData();
    expect(updated?.getDocument()?.['CFBundleURLTypes']).toEqual([
      {
        CFBundleURLSchemes: [
          'MyApp'
        ]
      }
    ]);

  });

  it('should gracefully error when targets not found in project', async () => {
    expect.assertions(1);

    try {
      await project.ios?.setDisplayName('Invalid Target', 'Invalid Build', 'Nothing');
    } catch (e) {
      expect((e as any).message).toBe(`Target 'Invalid Target' not found in project`);
    }
  });

  it('should copy file', async () => {
    await project.ios?.copyFile('json-file.json', 'json-file2.json');
    const src = join(dir, 'ios/App', 'json-file.json');
    const srcContents = await readFile(src);
    const dest = join(dir, 'ios/App', 'json-file2.json');
    const destContents = await readFile(dest);
    expect(srcContents).toEqual(destContents);
  });

  it('should copy URL', async () => {
    await project.ios?.copyFile('https://via.placeholder.com/150C', 'placeholder.png');
    const dest = join(dir, 'ios/App', 'placeholder.png');
    const destContents = await readFile(dest);
    expect(destContents.length).toBeGreaterThan(0);
  });

  it('should add source files when committing', async () => {
    const stringsFile = project.ios?.getProjectFile<StringsFile>(
        "App/NewStrings.strings",
        (filename: string) => new StringsFile(filename, project.vfs, project)
      );
    await stringsFile?.load();

    const xcconfigFile = project.ios?.getProjectFile<XCConfigFile>(
        "NewConfig.xcconfig",
        (filename: string) => new XCConfigFile(filename, project.vfs, project)
      );
    await xcconfigFile?.load();

    const plistFile = project.ios?.getProjectFile<PlistFile>(
        "NewPlist.plist",
        (filename: string) => new PlistFile(filename, project.vfs, project)
      );
    await plistFile?.load();

    const pbx = project.ios?.getPbxProject();
    /*
    const xmlFile = project.ios?.getProjectFile<XmlFile>(
        "NewXml.xml",
        (filename: string) => new XmlFile(filename, project.vfs)
      );
    await xmlFile?.load();
    */
    await project.commit();


    expect(!!pbx?.hasFile('NewStrings.strings')).toBe(true);
    expect(!!pbx?.hasFile('NewConfig.xcconfig')).toBe(true);
    expect(!!pbx?.hasFile('NewPlist.plist')).toBe(true);

    const pbxOnDisk = await readFile(join(dir, 'ios/App/App.xcodeproj/project.pbxproj'), { encoding: 'utf-8' });

    // console.log(pbx?.writeSync());
    expect(pbxOnDisk.indexOf('NewStrings.strings')).toBeGreaterThanOrEqual(0);
    // expect(!!pbx?.hasFile('NewXml.xml')).toBe(true);
  });

});

describe('ios - no info plist case', () => {
  let config: MobileProjectConfig;
  let project: MobileProject;
  let dir: string;
  beforeEach(async () => {
    dir = temporaryDirectory();
    await copy('../common/test/fixtures/ios-no-info-plist', dir);

    config = {
      ios: {
        path: 'ios/App'
      }
    }

    project = new MobileProject(dir, config);
    await project.load();
  });

  it('should create info plist when updating', async () => {
    const plistName = await project.ios?.getInfoPlist('App');
    const plistPath = join(project.config.ios?.path!, plistName!);
    expect(await pathExists(plistPath)).toBe(false);
    await project.ios?.updateInfoPlist('App', 'Debug', {
      NSFaceIDUsageDescription: 'The better to see you with'
    });
    await project.commit();
    expect(await pathExists(plistPath)).toBe(true);
    const plistContents = await readFile(plistPath, { encoding: 'utf-8' });
    expect(plistContents.indexOf('NSFaceIDUsageDescription') >= 0);
  });
});

describe('ios - empty template case', () => {
  let config: MobileProjectConfig;
  let project: MobileProject;
  let dir: string;
  beforeEach(async () => {
    dir = temporaryDirectory();
    await copy('../common/test/fixtures/ios-no-current-project-version', dir);

    config = {
      ios: {
        path: 'ios/App'
      },
      android: {
        path: 'android'
      }
    }

    project = new MobileProject(dir, config);
    await project.load();
  });

  it('should get build number when CURRENT_PROJECT_VERSION missing', async () => {
    expect(await project.ios?.getBuild(null)).toBe("1");
  });
});

https://github.com/ionic-team/trapeze/pull/83
describe('ios - issue #83', () => {
  let config: MobileProjectConfig;
  let project: MobileProject;
  let dir: string;
  beforeEach(async () => {
    dir = temporaryDirectory();
    await copy('../common/test/fixtures/ios-cfbundleversion-pbx-83', dir);

    config = {
      ios: {
        path: 'ios/App'
      },
      android: {
        path: 'android'
      }
    }

    project = new MobileProject(dir, config);
    await project.load();
  });

  it('should increment build with empty target', async () => {
    expect(await project.ios?.getBuild(null)).toBe("${CURRENT_PROJECT_VERSION}");
    await project.ios!.incrementBuild();
    expect(await project.ios?.getBuild(null)).toBe(1);
    await project.ios!.incrementBuild();
    expect(await project.ios?.getBuild(null)).toBe(2);
  });
});