|
1 | 1 | import assert from "node:assert/strict"; |
2 | 2 | import { describe, it } from "node:test"; |
3 | 3 | import path from "node:path"; |
4 | | -import { readInfoPlist } from "./apple"; |
| 4 | +import fs from "node:fs"; |
| 5 | + |
| 6 | +import { |
| 7 | + determineInfoPlistPath, |
| 8 | + readInfoPlist, |
| 9 | + updateInfoPlist, |
| 10 | +} from "./apple"; |
5 | 11 | import { setupTempDirectory } from "../test-utils"; |
6 | 12 |
|
7 | 13 | describe("apple", () => { |
8 | | - describe("Info.plist lookup", () => { |
9 | | - it("should find Info.plist files in unversioned frameworks", async (context) => { |
| 14 | + describe("determineInfoPlistPath", () => { |
| 15 | + it("should find Info.plist files in unversioned frameworks", (context) => { |
10 | 16 | const infoPlistContents = `<?xml version="1.0" encoding="UTF-8"?>...`; |
11 | 17 | const infoPlistSubPath = "Info.plist"; |
12 | 18 | const tempDirectoryPath = setupTempDirectory(context, { |
13 | 19 | [infoPlistSubPath]: infoPlistContents, |
14 | 20 | }); |
15 | 21 |
|
16 | | - const result = await readInfoPlist(tempDirectoryPath); |
17 | | - |
18 | | - assert.strictEqual(result.contents, infoPlistContents); |
19 | 22 | assert.strictEqual( |
20 | | - result.infoPlistPath, |
| 23 | + determineInfoPlistPath(tempDirectoryPath), |
21 | 24 | path.join(tempDirectoryPath, infoPlistSubPath), |
22 | 25 | ); |
23 | 26 | }); |
24 | 27 |
|
25 | | - it("should find Info.plist files in versioned frameworks", async (context) => { |
| 28 | + it("should find Info.plist files in versioned frameworks", (context) => { |
26 | 29 | const infoPlistContents = `<?xml version="1.0" encoding="UTF-8"?>...`; |
27 | 30 | const infoPlistSubPath = "Versions/Current/Resources/Info.plist"; |
28 | 31 | const tempDirectoryPath = setupTempDirectory(context, { |
29 | 32 | [infoPlistSubPath]: infoPlistContents, |
30 | 33 | }); |
31 | 34 |
|
32 | | - const result = await readInfoPlist(tempDirectoryPath); |
33 | | - |
34 | | - assert.strictEqual(result.contents, infoPlistContents); |
35 | 35 | assert.strictEqual( |
36 | | - result.infoPlistPath, |
| 36 | + determineInfoPlistPath(tempDirectoryPath), |
37 | 37 | path.join(tempDirectoryPath, infoPlistSubPath), |
38 | 38 | ); |
39 | 39 | }); |
40 | 40 |
|
41 | | - it("should throw if Info.plist is missing from framework", async (context) => { |
| 41 | + it("should throw if Info.plist is missing from framework", (context) => { |
42 | 42 | const tempDirectoryPath = setupTempDirectory(context, {}); |
43 | 43 |
|
| 44 | + assert.throws( |
| 45 | + () => determineInfoPlistPath(tempDirectoryPath), |
| 46 | + /Unable to locate an Info.plist file within framework./, |
| 47 | + ); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + describe("readInfoPlist", () => { |
| 52 | + it("should read Info.plist contents", async (context) => { |
| 53 | + const infoPlistContents = ` |
| 54 | + <?xml version="1.0" encoding="UTF-8"?> |
| 55 | + <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 56 | + <plist version="1.0"> |
| 57 | + <dict> |
| 58 | + <key>CFBundleExecutable</key> |
| 59 | + <string>ExecutableFileName</string> |
| 60 | + <key>CFBundleIconFile</key> |
| 61 | + <string>AppIcon</string> |
| 62 | + </dict> |
| 63 | + </plist> |
| 64 | + `; |
| 65 | + const infoPlistSubPath = "Info.plist"; |
| 66 | + const tempDirectoryPath = setupTempDirectory(context, { |
| 67 | + [infoPlistSubPath]: infoPlistContents, |
| 68 | + }); |
| 69 | + const infoPlistPath = path.join(tempDirectoryPath, infoPlistSubPath); |
| 70 | + |
| 71 | + const contents = await readInfoPlist(infoPlistPath); |
| 72 | + assert.deepEqual(contents, { |
| 73 | + CFBundleExecutable: "ExecutableFileName", |
| 74 | + CFBundleIconFile: "AppIcon", |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + it("should throw if Info.plist doesn't exist", async (context) => { |
| 79 | + const tempDirectoryPath = setupTempDirectory(context, {}); |
| 80 | + const infoPlistPath = path.join(tempDirectoryPath, "Info.plist"); |
| 81 | + |
44 | 82 | await assert.rejects( |
45 | | - async () => readInfoPlist(tempDirectoryPath), |
46 | | - /Unable to read Info.plist for framework at path ".*?", as an Info.plist file couldn't be found./, |
| 83 | + () => readInfoPlist(infoPlistPath), |
| 84 | + /Unable to read Info.plist at path/, |
| 85 | + ); |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + describe("updateInfoPlist", () => { |
| 90 | + it("updates an xml plist", async (context) => { |
| 91 | + const infoPlistSubPath = "Info.plist"; |
| 92 | + const tempDirectoryPath = setupTempDirectory(context, { |
| 93 | + [infoPlistSubPath]: ` |
| 94 | + <?xml version="1.0" encoding="UTF-8"?> |
| 95 | + <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 96 | + <plist version="1.0"> |
| 97 | + <dict> |
| 98 | + <key>CFBundleExecutable</key> |
| 99 | + <string>addon</string> |
| 100 | + </dict> |
| 101 | + </plist> |
| 102 | + `, |
| 103 | + }); |
| 104 | + |
| 105 | + await updateInfoPlist({ |
| 106 | + frameworkPath: tempDirectoryPath, |
| 107 | + oldLibraryName: "addon", |
| 108 | + newLibraryName: "new-addon-name", |
| 109 | + }); |
| 110 | + |
| 111 | + const contents = await fs.promises.readFile( |
| 112 | + path.join(tempDirectoryPath, infoPlistSubPath), |
| 113 | + "utf-8", |
| 114 | + ); |
| 115 | + assert.match(contents, /<\?xml version="1.0" encoding="UTF-8"\?>/); |
| 116 | + assert.match( |
| 117 | + contents, |
| 118 | + /<key>CFBundleExecutable<\/key>\s*<string>new-addon-name<\/string>/, |
| 119 | + ); |
| 120 | + }); |
| 121 | + |
| 122 | + it("converts a binary plist to xml", async (context) => { |
| 123 | + const tempDirectoryPath = setupTempDirectory(context, {}); |
| 124 | + // Write a binary plist file |
| 125 | + const binaryPlistContents = Buffer.from( |
| 126 | + // Generated running "base64 -i <path-to-binary-plist>" on a plist file from a framework in the node-examples package |
| 127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); |
| 130 | + const binaryPlistPath = path.join(tempDirectoryPath, "Info.plist"); |
| 131 | + await fs.promises.writeFile(binaryPlistPath, binaryPlistContents); |
| 132 | + |
| 133 | + await updateInfoPlist({ |
| 134 | + frameworkPath: tempDirectoryPath, |
| 135 | + oldLibraryName: "addon", |
| 136 | + newLibraryName: "new-addon-name", |
| 137 | + }); |
| 138 | + |
| 139 | + const contents = await fs.promises.readFile(binaryPlistPath, "utf-8"); |
| 140 | + assert.match(contents, /<\?xml version="1.0" encoding="UTF-8"\?>/); |
| 141 | + assert.match( |
| 142 | + contents, |
| 143 | + /<key>CFBundleExecutable<\/key>\s*<string>new-addon-name<\/string>/, |
47 | 144 | ); |
48 | 145 | }); |
49 | 146 | }); |
|
0 commit comments