1import { IOSConfig } from '@expo/config-plugins';
2import { vol } from 'memfs';
3import path from 'path';
4
5import {
6  getCodeSigningInfoForPbxproj,
7  mutateXcodeProjectWithAutoCodeSigningInfo,
8} from '../xcodeCodeSigning';
9
10jest.mock('fs');
11
12const originalFs = jest.requireActual('fs');
13
14describe(mutateXcodeProjectWithAutoCodeSigningInfo, () => {
15  const projectRoot = '/';
16  afterEach(() => vol.reset());
17
18  it(`mutates the xcode project with code signing info`, () => {
19    vol.fromJSON(
20      {
21        'ios/testproject.xcodeproj/project.pbxproj': originalFs.readFileSync(
22          path.join(__dirname, 'fixtures/minimal.pbxproj'),
23          'utf-8'
24        ),
25      },
26      projectRoot
27    );
28
29    const project = IOSConfig.XcodeUtils.resolvePathOrProject(projectRoot);
30
31    mutateXcodeProjectWithAutoCodeSigningInfo({
32      project,
33      appleTeamId: '12345',
34    });
35
36    // PBXProject
37    expect(
38      project.hash.project.objects.PBXProject['X00000000000000000000000'].attributes
39        .TargetAttributes['X00000000000000000000001'].DevelopmentTeam
40    ).toBe('"12345"');
41    delete project.hash.project.objects.PBXProject;
42
43    // XCBuildConfiguration
44    ['X00000000000000000000016', 'X00000000000000000000017'].forEach((key) => {
45      expect(
46        project.hash.project.objects.XCBuildConfiguration[key].buildSettings.DEVELOPMENT_TEAM
47      ).toBe('"12345"');
48    });
49    delete project.hash.project.objects.XCBuildConfiguration;
50
51    // No other changes should have been made
52    expect(JSON.stringify(project.hash.project.objects)).not.toMatch(/12345/);
53  });
54});
55
56describe(getCodeSigningInfoForPbxproj, () => {
57  const projectRoot = '/testproject';
58
59  afterEach(() => vol.reset());
60
61  it(`returns the no existing code signing info`, () => {
62    vol.fromJSON(
63      {
64        'ios/testproject.xcodeproj/project.pbxproj': originalFs.readFileSync(
65          path.join(__dirname, 'fixtures/project.pbxproj'),
66          'utf-8'
67        ),
68      },
69      projectRoot
70    );
71
72    expect(getCodeSigningInfoForPbxproj(projectRoot)).toStrictEqual({
73      '13B07F861A680F5B00A75B9A': {
74        developmentTeams: [],
75        provisioningProfiles: [],
76      },
77    });
78  });
79  it(`returns the existing code signing info`, () => {
80    vol.fromJSON(
81      {
82        'ios/testproject.xcodeproj/project.pbxproj': originalFs.readFileSync(
83          path.join(__dirname, 'fixtures/signed-project.pbxproj'),
84          'utf-8'
85        ),
86      },
87      projectRoot
88    );
89
90    expect(getCodeSigningInfoForPbxproj(projectRoot)).toStrictEqual({
91      '13B07F861A680F5B00A75B9A': {
92        developmentTeams: ['QQ57RJ5UTD', 'QQ57RJ5UTD'],
93        provisioningProfiles: [],
94      },
95    });
96  });
97});
98