Skip to content

Commit dc508e6

Browse files
author
Yosif Yosifov
committed
Adding merge entitlements tests
1 parent 001179e commit dc508e6

File tree

3 files changed

+160
-7
lines changed

3 files changed

+160
-7
lines changed

lib/services/ios-entitlements-service.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,18 @@ export class IOSEntitlementsService {
88
private $logger: ILogger) {
99
}
1010

11-
private getDefaultEntitlementsName() : string {
12-
return "app.entitlements";
13-
}
11+
public static readonly DefaultEntitlementsName: string = "app.entitlements";
1412

1513
private getDefaultAppEntitlementsPath(projectData: IProjectData) : string {
16-
let entitlementsName = this.getDefaultEntitlementsName();
14+
let entitlementsName = IOSEntitlementsService.DefaultEntitlementsName;
1715
let entitlementsPath = path.join(projectData.projectDir,
1816
constants.APP_FOLDER_NAME, constants.APP_RESOURCES_FOLDER_NAME,
1917
constants.IOS_PLATFORM_NORMALIZED_NAME,
2018
entitlementsName);
2119
return entitlementsPath;
2220
}
2321

24-
private getPlatformsEntitlementsPath(projectData: IProjectData) : string {
22+
public getPlatformsEntitlementsPath(projectData: IProjectData) : string {
2523
return path.join(projectData.platformsDir, constants.IOS_PLATFORM_NAME,
2624
projectData.projectName, projectData.projectName + ".entitlements");
2725
}
@@ -48,11 +46,10 @@ export class IOSEntitlementsService {
4846

4947
let allPlugins = await this.getAllInstalledPlugins(projectData);
5048
for (let plugin of allPlugins) {
51-
let pluginInfoPlistPath = path.join(plugin.pluginPlatformsFolderPath(constants.IOS_PLATFORM_NAME), this.getDefaultEntitlementsName());
49+
let pluginInfoPlistPath = path.join(plugin.pluginPlatformsFolderPath(constants.IOS_PLATFORM_NAME), IOSEntitlementsService.DefaultEntitlementsName);
5250
makePatch(pluginInfoPlistPath);
5351
}
5452

55-
// for older ios-app-templates or if has been deleted.
5653
let appEntitlementsPath = this.getDefaultAppEntitlementsPath(projectData);
5754
if (this.$fs.exists(appEntitlementsPath)) {
5855
makePatch(appEntitlementsPath);

test/ios-entitlements-service.ts

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
import temp = require("temp");
2+
import { EOL } from "os";
3+
import { assert } from "chai";
4+
import { IOSEntitlementsService } from "../lib/services/ios-entitlements-service";
5+
import * as yok from "../lib/common/yok";
6+
import * as stubs from "./stubs";
7+
import * as FsLib from "../lib/common/file-system";
8+
import * as path from "path";
9+
10+
// start tracking temporary folders/files
11+
temp.track();
12+
13+
describe("IOSEntitlements Service Tests", () => {
14+
const createTestInjector = (): IInjector => {
15+
const testInjector = new yok.Yok();
16+
17+
testInjector.register('platformsData', stubs.PlatformsDataStub);
18+
testInjector.register("logger", stubs.LoggerStub);
19+
testInjector.register('iOSEntitlementsService', IOSEntitlementsService);
20+
21+
testInjector.register("fs", FsLib.FileSystem);
22+
23+
testInjector.register("pluginsService", {
24+
getAllInstalledPlugins: async () => []
25+
});
26+
27+
return testInjector;
28+
};
29+
30+
let injector: IInjector,
31+
platformsData: any,
32+
projectData: IProjectData,
33+
fs: IFileSystem,
34+
iOSEntitlementsService: IOSEntitlementsService,
35+
destinationFilePath: string;
36+
37+
beforeEach(function() {
38+
injector = createTestInjector();
39+
40+
platformsData = injector.resolve("platformsData");
41+
projectData = <IProjectData>platformsData.getPlatformData();
42+
projectData.projectName = 'testApp';
43+
44+
projectData.platformsDir = temp.mkdirSync("platformsDir");
45+
projectData.projectDir = temp.mkdirSync("projectDir");
46+
47+
fs = injector.resolve("$fs");
48+
49+
iOSEntitlementsService = injector.resolve("iOSEntitlementsService");
50+
destinationFilePath = iOSEntitlementsService.getPlatformsEntitlementsPath(projectData);
51+
});
52+
53+
describe("Ensure paths constructed are correct", () => {
54+
it("Ensure destination entitlements relative path is calculated correctly.", () => {
55+
const expected = "testApp/testApp.entitlements";
56+
let actual = iOSEntitlementsService.getPlatformsEntitlementsRelativePath(projectData);
57+
assert.equal(actual, expected);
58+
});
59+
});
60+
61+
describe("Merge", () => {
62+
const defaultPlistContent = `<?xml version="1.0" encoding="UTF-8"?>
63+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
64+
<plist version="1.0">
65+
<dict/>
66+
</plist>`;
67+
const defaultAppResourcesEntitlementsContent = `<?xml version="1.0" encoding="UTF-8"?>
68+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
69+
<plist version="1.0">
70+
<dict>
71+
<key>aps-environment</key>
72+
<string>development</string>
73+
</dict>
74+
</plist>`;
75+
const defaultPluginEntitlementsContent = `<?xml version="1.0" encoding="UTF-8"?>
76+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
77+
<plist version="1.0">
78+
<dict>
79+
<key>aps-environment</key>
80+
<string>production</string>
81+
</dict>
82+
</plist>`;
83+
84+
function assertContent(actual: string, expected: string) {
85+
let strip = (x: string) => {
86+
return x.replace(EOL, '').trim();
87+
};
88+
assert.equal(strip(actual), strip(expected));
89+
}
90+
91+
it("Merge creates a default entitlements file.", async () => {
92+
// act
93+
await iOSEntitlementsService.merge(projectData);
94+
95+
// assert
96+
let actual = fs.readText(destinationFilePath);
97+
assertContent(actual, defaultPlistContent);
98+
});
99+
100+
it("Merge uses the entitlements from App_Resources folder", async () => {
101+
let appResourcesEntitlement = (<any>iOSEntitlementsService).getDefaultAppEntitlementsPath(projectData);
102+
fs.writeFile(appResourcesEntitlement, defaultAppResourcesEntitlementsContent);
103+
104+
// act
105+
await iOSEntitlementsService.merge(projectData);
106+
107+
// assert
108+
let actual = fs.readText(destinationFilePath);
109+
assertContent(actual, defaultAppResourcesEntitlementsContent);
110+
});
111+
112+
it("Merge uses the entitlements file from a Plugin", async () => {
113+
let pluginsService = injector.resolve("pluginsService");
114+
let testPluginFolderPath = temp.mkdirSync("testPlugin");
115+
pluginsService.getAllInstalledPlugins = async() => [{
116+
pluginPlatformsFolderPath: (platform: string) => {
117+
return testPluginFolderPath;
118+
}
119+
}];
120+
let pluginAppEntitlementsPath = path.join(testPluginFolderPath, IOSEntitlementsService.DefaultEntitlementsName);
121+
fs.writeFile(pluginAppEntitlementsPath, defaultPluginEntitlementsContent);
122+
123+
// act
124+
await iOSEntitlementsService.merge(projectData);
125+
126+
// assert
127+
let actual = fs.readText(destinationFilePath);
128+
assertContent(actual, defaultPluginEntitlementsContent);
129+
});
130+
});
131+
});

test/ios-project-service.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as path from "path";
2+
import { EOL } from "os";
23
import * as ChildProcessLib from "../lib/common/child-process";
34
import * as ConfigLib from "../lib/config";
45
import * as ErrorsLib from "../lib/common/errors";
@@ -830,4 +831,28 @@ describe("Merge Project XCConfig files", () => {
830831
assertPropertyValues(expected, destinationFilePath, testInjector);
831832
}
832833
});
834+
835+
it("The user specified entitlements property takes percedence", async () => {
836+
// setup app_resource build.xcconfig
837+
const expectedEntitlementsFile = 'user.entitlements';
838+
let xcconfigEntitlements = appResourceXCConfigContent + `${EOL}CODE_SIGN_ENTITLEMENTS = ${expectedEntitlementsFile}`;
839+
fs.writeFile(appResourcesXcconfigPath, xcconfigEntitlements);
840+
841+
// run merge for all release: debug|release
842+
for (let release in [true, false]) {
843+
await (<any>iOSProjectService).mergeProjectXcconfigFiles(release, projectData);
844+
845+
let destinationFilePath = release ? (<any>iOSProjectService).getPluginsReleaseXcconfigFilePath(projectData)
846+
: (<any>iOSProjectService).getPluginsDebugXcconfigFilePath(projectData);
847+
848+
assert.isTrue(fs.exists(destinationFilePath), 'Target build xcconfig is missing for release: ' + release);
849+
let expected = {
850+
'ASSETCATALOG_COMPILER_APPICON_NAME': 'AppIcon',
851+
'ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME': 'LaunchImage',
852+
'CODE_SIGN_IDENTITY': 'iPhone Distribution',
853+
'CODE_SIGN_ENTITLEMENTS': expectedEntitlementsFile
854+
};
855+
assertPropertyValues(expected, destinationFilePath, testInjector);
856+
}
857+
});
833858
});

0 commit comments

Comments
 (0)