|
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 | + const tempDirectoryPath = setupTempDirectory(context, {}); |
| 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) => { |
42 | 79 | const tempDirectoryPath = setupTempDirectory(context, {}); |
| 80 | + const infoPlistPath = path.join(tempDirectoryPath, "Info.plist"); |
43 | 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/, |
47 | 85 | ); |
48 | 86 | }); |
49 | 87 | }); |
| 88 | + |
| 89 | + describe("updateInfoPlist", () => { |
| 90 | + it( |
| 91 | + "updates an xml plist", |
| 92 | + { skip: process.platform !== "darwin" }, |
| 93 | + async (context) => { |
| 94 | + const infoPlistSubPath = "Info.plist"; |
| 95 | + const tempDirectoryPath = setupTempDirectory(context, { |
| 96 | + [infoPlistSubPath]: ` |
| 97 | + <?xml version="1.0" encoding="UTF-8"?> |
| 98 | + <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 99 | + <plist version="1.0"> |
| 100 | + <dict> |
| 101 | + <key>CFBundleExecutable</key> |
| 102 | + <string>addon</string> |
| 103 | + </dict> |
| 104 | + </plist> |
| 105 | + `, |
| 106 | + }); |
| 107 | + |
| 108 | + await updateInfoPlist({ |
| 109 | + frameworkPath: tempDirectoryPath, |
| 110 | + oldLibraryName: "addon", |
| 111 | + newLibraryName: "new-addon-name", |
| 112 | + }); |
| 113 | + |
| 114 | + const contents = await fs.promises.readFile( |
| 115 | + path.join(tempDirectoryPath, infoPlistSubPath), |
| 116 | + "utf-8", |
| 117 | + ); |
| 118 | + assert.match(contents, /<\?xml version="1.0" encoding="UTF-8"\?>/); |
| 119 | + assert.match( |
| 120 | + contents, |
| 121 | + /<key>CFBundleExecutable<\/key>\s*<string>new-addon-name<\/string>/, |
| 122 | + ); |
| 123 | + }, |
| 124 | + ); |
| 125 | + |
| 126 | + it( |
| 127 | + "converts a binary plist to xml", |
| 128 | + { skip: process.platform !== "darwin" }, |
| 129 | + async (context) => { |
| 130 | + const tempDirectoryPath = setupTempDirectory(context, {}); |
| 131 | + // Write a binary plist file |
| 132 | + const binaryPlistContents = Buffer.from( |
| 133 | + // Generated running "base64 -i <path-to-binary-plist>" on a plist file from a framework in the node-examples package |
| 134 | + "YnBsaXN0MDDfEBUBAgMEBQYHCAkKCwwNDg8QERITFBUWFxgZGhscHR4cICEiIyQiJSYnJChfEBNCdWlsZE1hY2hpbmVPU0J1aWxkXxAZQ0ZCdW5kbGVEZXZlbG9wbWVudFJlZ2lvbl8QEkNGQnVuZGxlRXhlY3V0YWJsZV8QEkNGQnVuZGxlSWRlbnRpZmllcl8QHUNGQnVuZGxlSW5mb0RpY3Rpb25hcnlWZXJzaW9uXxATQ0ZCdW5kbGVQYWNrYWdlVHlwZV8QGkNGQnVuZGxlU2hvcnRWZXJzaW9uU3RyaW5nXxARQ0ZCdW5kbGVTaWduYXR1cmVfEBpDRkJ1bmRsZVN1cHBvcnRlZFBsYXRmb3Jtc18QD0NGQnVuZGxlVmVyc2lvbl8QFUNTUmVzb3VyY2VzRmlsZU1hcHBlZFpEVENvbXBpbGVyXxAPRFRQbGF0Zm9ybUJ1aWxkXkRUUGxhdGZvcm1OYW1lXxARRFRQbGF0Zm9ybVZlcnNpb25aRFRTREtCdWlsZFlEVFNES05hbWVXRFRYY29kZVxEVFhjb2RlQnVpbGRfEBBNaW5pbXVtT1NWZXJzaW9uXlVJRGV2aWNlRmFtaWx5VjI0RzIzMVdFbmdsaXNoVWFkZG9uXxAPZXhhbXBsZV82LmFkZG9uUzYuMFRGTVdLUzEuMFQ/Pz8/oR9fEA9pUGhvbmVTaW11bGF0b3IJXxAiY29tLmFwcGxlLmNvbXBpbGVycy5sbHZtLmNsYW5nLjFfMFYyMkMxNDZfEA9pcGhvbmVzaW11bGF0b3JUMTguMl8QE2lwaG9uZXNpbXVsYXRvcjE4LjJUMTYyMFgxNkM1MDMyYaEpEAEACAA1AEsAZwB8AJEAsQDHAOQA+AEVAScBPwFKAVwBawF/AYoBlAGcAakBvAHLAdIB2gHgAfIB9gH7Af8CBAIGAhgCGQI+AkUCVwJcAnICdwKAAoIAAAAAAAACAQAAAAAAAAAqAAAAAAAAAAAAAAAAAAAChA==", |
| 135 | + "base64", |
| 136 | + ); |
| 137 | + const binaryPlistPath = path.join(tempDirectoryPath, "Info.plist"); |
| 138 | + await fs.promises.writeFile(binaryPlistPath, binaryPlistContents); |
| 139 | + |
| 140 | + await updateInfoPlist({ |
| 141 | + frameworkPath: tempDirectoryPath, |
| 142 | + oldLibraryName: "addon", |
| 143 | + newLibraryName: "new-addon-name", |
| 144 | + }); |
| 145 | + |
| 146 | + const contents = await fs.promises.readFile(binaryPlistPath, "utf-8"); |
| 147 | + assert.match(contents, /<\?xml version="1.0" encoding="UTF-8"\?>/); |
| 148 | + assert.match( |
| 149 | + contents, |
| 150 | + /<key>CFBundleExecutable<\/key>\s*<string>new-addon-name<\/string>/, |
| 151 | + ); |
| 152 | + }, |
| 153 | + ); |
| 154 | + |
| 155 | + it( |
| 156 | + "throws when not on darwin", |
| 157 | + { skip: process.platform === "darwin" }, |
| 158 | + async (context) => { |
| 159 | + const tempDirectoryPath = setupTempDirectory(context, { |
| 160 | + ["Info.plist"]: '<?xml version="1.0" encoding="UTF-8"?>', |
| 161 | + }); |
| 162 | + |
| 163 | + await assert.rejects( |
| 164 | + () => |
| 165 | + updateInfoPlist({ |
| 166 | + frameworkPath: tempDirectoryPath, |
| 167 | + oldLibraryName: "addon", |
| 168 | + newLibraryName: "new-addon-name", |
| 169 | + }), |
| 170 | + (err) => { |
| 171 | + assert(err instanceof Error); |
| 172 | + assert.match(err.message, /Failed to convert Info.plist at path/); |
| 173 | + assert(err.cause instanceof Error); |
| 174 | + assert.match( |
| 175 | + err.cause.message, |
| 176 | + /Updating Info.plist files are not supported on this platform/, |
| 177 | + ); |
| 178 | + return true; |
| 179 | + }, |
| 180 | + ); |
| 181 | + }, |
| 182 | + ); |
| 183 | + }); |
50 | 184 | }); |
0 commit comments