-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathapple.test.ts
More file actions
398 lines (365 loc) · 13.6 KB
/
apple.test.ts
File metadata and controls
398 lines (365 loc) · 13.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
import assert from "node:assert/strict";
import { describe, it } from "node:test";
import path from "node:path";
import fs from "node:fs";
import cp from "node:child_process";
import {
linkFlatFramework,
readAndParsePlist,
readFrameworkInfo,
readXcframeworkInfo,
restoreFrameworkLinks,
} from "./apple";
import { setupTempDirectory } from "../test-utils";
describe("apple", { skip: process.platform !== "darwin" }, () => {
describe("readInfoPlist", () => {
it("should read Info.plist contents, plus extra keys not in schema", async (context) => {
const infoPlistContents = `
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleExecutable</key>
<string>ExecutableFileName</string>
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
</dict>
</plist>
`;
const infoPlistSubPath = "Info.plist";
const tempDirectoryPath = setupTempDirectory(context, {
[infoPlistSubPath]: infoPlistContents,
});
const infoPlistPath = path.join(tempDirectoryPath, infoPlistSubPath);
const contents = await readAndParsePlist(infoPlistPath);
assert.deepEqual(contents, {
CFBundleExecutable: "ExecutableFileName",
CFBundlePackageType: "FMWK",
CFBundleInfoDictionaryVersion: "6.0",
});
});
it("should throw if Info.plist doesn't exist", async (context) => {
const tempDirectoryPath = setupTempDirectory(context, {});
const infoPlistPath = path.join(tempDirectoryPath, "Info.plist");
await assert.rejects(
() => readAndParsePlist(infoPlistPath),
/Expected an Info.plist/,
);
});
});
describe("readXcframeworkInfo", () => {
it("should read xcframework Info.plist contents, plus extra keys not in schema", async (context) => {
const tempDirectoryPath = setupTempDirectory(context, {
"foo.xcframework": {
"Info.plist": `
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>AvailableLibraries</key>
<array>
<dict>
<key>BinaryPath</key>
<string>hello.framework/hello</string>
<key>LibraryIdentifier</key>
<string>tvos-arm64</string>
<key>LibraryPath</key>
<string>hello.framework</string>
<key>SupportedArchitectures</key>
<array>
<string>arm64</string>
</array>
<key>SupportedPlatform</key>
<string>tvos</string>
</dict>
</array>
<key>CFBundlePackageType</key>
<string>XFWK</string>
<key>XCFrameworkFormatVersion</key>
<string>1.0</string>
</dict>
</plist>
`,
},
});
const result = await readXcframeworkInfo(
path.join(tempDirectoryPath, "foo.xcframework", "Info.plist"),
);
assert.deepEqual(result, {
AvailableLibraries: [
{
BinaryPath: "hello.framework/hello",
LibraryIdentifier: "tvos-arm64",
LibraryPath: "hello.framework",
SupportedArchitectures: ["arm64"],
SupportedPlatform: "tvos",
},
],
CFBundlePackageType: "XFWK",
XCFrameworkFormatVersion: "1.0",
});
});
});
describe("readFrameworkInfo", () => {
it("should read framework Info.plist contents, plus extra keys not in schema", async (context) => {
const tempDirectoryPath = setupTempDirectory(context, {
"foo.framework": {
"Info.plist": `
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleExecutable</key>
<string>example-0--hello</string>
<key>CFBundleIdentifier</key>
<string>example_0.hello</string>
<key>CFBundleSupportedPlatforms</key>
<array>
<string>XRSimulator</string>
</array>
</dict>
</plist>
`,
},
});
const result = await readFrameworkInfo(
path.join(tempDirectoryPath, "foo.framework", "Info.plist"),
);
assert.deepEqual(result, {
CFBundlePackageType: "FMWK",
CFBundleInfoDictionaryVersion: "6.0",
CFBundleExecutable: "example-0--hello",
CFBundleIdentifier: "example_0.hello",
CFBundleSupportedPlatforms: ["XRSimulator"],
});
});
});
describe("linkFlatFramework", () => {
it("updates an xml plist, preserving extra keys", async (context) => {
const infoPlistSubPath = "Info.plist";
const tempDirectoryPath = setupTempDirectory(context, {
"foo.framework": {
[infoPlistSubPath]: `
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleExecutable</key>
<string>addon</string>
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>MyExtraKey</key>
<string>MyExtraValue</string>
</dict>
</plist>
`,
},
});
// Create a dummy binary file
cp.spawnSync("clang", [
"-dynamiclib",
"-o",
path.join(tempDirectoryPath, "foo.framework", "addon"),
"-xc",
"/dev/null",
]);
await linkFlatFramework({
frameworkPath: path.join(tempDirectoryPath, "foo.framework"),
newLibraryName: "new-addon-name",
});
const contents = await fs.promises.readFile(
path.join(
tempDirectoryPath,
"new-addon-name.framework",
infoPlistSubPath,
),
"utf-8",
);
assert.match(contents, /<\?xml version="1.0" encoding="UTF-8"\?>/);
assert.match(
contents,
/<key>CFBundleExecutable<\/key>\s*<string>new-addon-name<\/string>/,
);
// Assert the install name was updated correctly
const { stdout: otoolOutput } = cp.spawnSync(
"otool",
[
"-L",
path.join(
tempDirectoryPath,
"new-addon-name.framework",
"new-addon-name",
),
],
{ encoding: "utf-8" },
);
assert.match(
otoolOutput,
/@rpath\/new-addon-name.framework\/new-addon-name/,
);
// It should preserve extra keys
assert.match(
contents,
/<key>MyExtraKey<\/key>\s*<string>MyExtraValue<\/string>/,
);
});
it("converts a binary plist to xml", async (context) => {
const tempDirectoryPath = setupTempDirectory(context, {});
await fs.promises.mkdir(path.join(tempDirectoryPath, "foo.framework"));
// Write a binary plist file
const binaryPlistContents = Buffer.from(
// Generated running "base64 -i <path-to-binary-plist>" on a plist file from a framework in the node-examples package
"YnBsaXN0MDDfEBUBAgMEBQYHCAkKCwwNDg8QERITFBUWFxgZGhscHR4cICEiIyQiJSYnJChfEBNCdWlsZE1hY2hpbmVPU0J1aWxkXxAZQ0ZCdW5kbGVEZXZlbG9wbWVudFJlZ2lvbl8QEkNGQnVuZGxlRXhlY3V0YWJsZV8QEkNGQnVuZGxlSWRlbnRpZmllcl8QHUNGQnVuZGxlSW5mb0RpY3Rpb25hcnlWZXJzaW9uXxATQ0ZCdW5kbGVQYWNrYWdlVHlwZV8QGkNGQnVuZGxlU2hvcnRWZXJzaW9uU3RyaW5nXxARQ0ZCdW5kbGVTaWduYXR1cmVfEBpDRkJ1bmRsZVN1cHBvcnRlZFBsYXRmb3Jtc18QD0NGQnVuZGxlVmVyc2lvbl8QFUNTUmVzb3VyY2VzRmlsZU1hcHBlZFpEVENvbXBpbGVyXxAPRFRQbGF0Zm9ybUJ1aWxkXkRUUGxhdGZvcm1OYW1lXxARRFRQbGF0Zm9ybVZlcnNpb25aRFRTREtCdWlsZFlEVFNES05hbWVXRFRYY29kZVxEVFhjb2RlQnVpbGRfEBBNaW5pbXVtT1NWZXJzaW9uXlVJRGV2aWNlRmFtaWx5VjI0RzIzMVdFbmdsaXNoVWFkZG9uXxAPZXhhbXBsZV82LmFkZG9uUzYuMFRGTVdLUzEuMFQ/Pz8/oR9fEA9pUGhvbmVTaW11bGF0b3IJXxAiY29tLmFwcGxlLmNvbXBpbGVycy5sbHZtLmNsYW5nLjFfMFYyMkMxNDZfEA9pcGhvbmVzaW11bGF0b3JUMTguMl8QE2lwaG9uZXNpbXVsYXRvcjE4LjJUMTYyMFgxNkM1MDMyYaEpEAEACAA1AEsAZwB8AJEAsQDHAOQA+AEVAScBPwFKAVwBawF/AYoBlAGcAakBvAHLAdIB2gHgAfIB9gH7Af8CBAIGAhgCGQI+AkUCVwJcAnICdwKAAoIAAAAAAAACAQAAAAAAAAAqAAAAAAAAAAAAAAAAAAAChA==",
"base64",
);
await fs.promises.writeFile(
path.join(tempDirectoryPath, "foo.framework", "Info.plist"),
binaryPlistContents,
);
// Create a dummy binary file
cp.spawnSync("clang", [
"-dynamiclib",
"-o",
path.join(tempDirectoryPath, "foo.framework", "addon"),
"-xc",
"/dev/null",
]);
await linkFlatFramework({
frameworkPath: path.join(tempDirectoryPath, "foo.framework"),
newLibraryName: "new-addon-name",
});
const contents = await fs.promises.readFile(
path.join(tempDirectoryPath, "new-addon-name.framework", "Info.plist"),
"utf-8",
);
assert.match(contents, /<\?xml version="1.0" encoding="UTF-8"\?>/);
assert.match(
contents,
/<key>CFBundleExecutable<\/key>\s*<string>new-addon-name<\/string>/,
);
});
});
describe("restoreFrameworkLinks", () => {
it("restores a versioned framework", async (context) => {
const infoPlistContents = `
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleExecutable</key>
<string>example-addon</string>
</dict>
</plist>
`;
const tempDirectoryPath = setupTempDirectory(context, {
"foo.framework": {
Versions: {
A: {
Resources: {
"Info.plist": infoPlistContents,
},
"example-addon": "",
},
},
},
});
const frameworkPath = path.join(tempDirectoryPath, "foo.framework");
const currentVersionPath = path.join(
frameworkPath,
"Versions",
"Current",
);
const binaryLinkPath = path.join(frameworkPath, "example-addon");
const realBinaryPath = path.join(
frameworkPath,
"Versions",
"A",
"example-addon",
);
async function assertVersionedFramework() {
const currentStat = await fs.promises.lstat(currentVersionPath);
assert(
currentStat.isSymbolicLink(),
"Expected Current symlink to be restored",
);
assert.equal(
await fs.promises.realpath(currentVersionPath),
path.join(frameworkPath, "Versions", "A"),
);
const binaryStat = await fs.promises.lstat(binaryLinkPath);
assert(
binaryStat.isSymbolicLink(),
"Expected binary symlink to be restored",
);
assert.equal(
await fs.promises.realpath(binaryLinkPath),
realBinaryPath,
);
}
await restoreFrameworkLinks(frameworkPath);
await assertVersionedFramework();
// Calling again to expect a no-op
await restoreFrameworkLinks(frameworkPath);
await assertVersionedFramework();
});
it("throws on a flat framework", async (context) => {
const tempDirectoryPath = setupTempDirectory(context, {
"foo.framework": {
"Info.plist": `
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleExecutable</key>
<string>example-addon</string>
</dict>
</plist>
`,
},
});
const frameworkPath = path.join(tempDirectoryPath, "foo.framework");
await assert.rejects(
() => restoreFrameworkLinks(frameworkPath),
/Expected "Versions" directory inside versioned framework/,
);
});
});
});
describe("apple on non-darwin", { skip: process.platform === "darwin" }, () => {
it("throws", async (context) => {
const tempDirectoryPath = setupTempDirectory(context, {
["Info.plist"]: '<?xml version="1.0" encoding="UTF-8"?>',
});
await assert.rejects(
() =>
linkFlatFramework({
frameworkPath: path.join(tempDirectoryPath, "Info.plist"),
newLibraryName: "new-addon-name",
}),
(err) => {
assert(err instanceof Error);
assert.match(
err.message,
/Linking Apple addons are only supported on macOS/,
);
return true;
},
);
});
});