|
| 1 | +import * as fs from "fs"; |
| 2 | +import * as os from "os"; |
| 3 | +import * as path from "path"; |
| 4 | +import { assert } from "chai"; |
| 5 | +import { LinuxAppBundler } from "../../lib/services/linux-app-bundle"; |
| 6 | +import * as stubs from "../stubs"; |
| 7 | + |
| 8 | +/** Fakes ns-bundle: `pack` writes the bundle and patches the executable. */ |
| 9 | +function fakeTool(exitCode = 0) { |
| 10 | + const calls: string[][] = []; |
| 11 | + return { |
| 12 | + calls, |
| 13 | + spawnFromEvent: async (_command: string, args: string[]) => { |
| 14 | + calls.push(args); |
| 15 | + if (exitCode === 0 && args[0] === "pack") { |
| 16 | + const output = args[args.indexOf("--output") + 1]; |
| 17 | + fs.writeFileSync(output, "NSB2"); |
| 18 | + fs.mkdirSync(`${output}.unpacked/fonts`, { recursive: true }); |
| 19 | + fs.writeFileSync(args[args.indexOf("--exe") + 1], "patched"); |
| 20 | + } else if (args[0] === "pack") { |
| 21 | + // a failed run may leave a half patched executable |
| 22 | + fs.writeFileSync(args[args.indexOf("--exe") + 1], "half"); |
| 23 | + } |
| 24 | + return { exitCode, stdout: "ok", stderr: exitCode ? "boom" : "" }; |
| 25 | + }, |
| 26 | + }; |
| 27 | +} |
| 28 | + |
| 29 | +describe("linuxAppBundle", () => { |
| 30 | + let root: string; |
| 31 | + let stage: string; |
| 32 | + let executable: string; |
| 33 | + let toolsDir: string; |
| 34 | + |
| 35 | + beforeEach(() => { |
| 36 | + root = fs.mkdtempSync(path.join(os.tmpdir(), "ns-linux-bundle-")); |
| 37 | + stage = path.join(root, "stage"); |
| 38 | + toolsDir = path.join(root, "tools"); |
| 39 | + executable = path.join(stage, "app-exe"); |
| 40 | + fs.mkdirSync(path.join(stage, "app"), { recursive: true }); |
| 41 | + fs.writeFileSync(path.join(stage, "app", "bundle.js"), ""); |
| 42 | + fs.writeFileSync(executable, "runtime"); |
| 43 | + fs.mkdirSync(toolsDir); |
| 44 | + fs.writeFileSync(path.join(toolsDir, "ns-bundle"), ""); |
| 45 | + }); |
| 46 | + |
| 47 | + afterEach(() => fs.rmSync(root, { recursive: true, force: true })); |
| 48 | + |
| 49 | + const pack = (proc: any, extra: any = {}) => |
| 50 | + new LinuxAppBundler(proc, new stubs.LoggerStub()).pack({ |
| 51 | + stage, |
| 52 | + executable, |
| 53 | + toolsDir, |
| 54 | + config: {}, |
| 55 | + ...extra, |
| 56 | + }); |
| 57 | + |
| 58 | + it("bundles by default and links app/ to the unpacked files", async () => { |
| 59 | + const proc = fakeTool(); |
| 60 | + await pack(proc); |
| 61 | + const [args] = proc.calls; |
| 62 | + assert.equal(args[0], "pack"); |
| 63 | + assert.notInclude(args, "--seal"); |
| 64 | + assert.includeMembers(args, [ |
| 65 | + "bundleIntegrity=on", |
| 66 | + "onlyLoadAppFromBundle=on", |
| 67 | + "inspector=off", |
| 68 | + "envOverrides=off", |
| 69 | + ]); |
| 70 | + assert.equal( |
| 71 | + fs.readlinkSync(path.join(stage, "app")), |
| 72 | + "app.nsbundle.unpacked", |
| 73 | + ); |
| 74 | + }); |
| 75 | + |
| 76 | + it("seals with the given key and unpack globs", async () => { |
| 77 | + const proc = fakeTool(); |
| 78 | + await pack(proc, { |
| 79 | + sourceProtect: true, |
| 80 | + keyHex: "ab".repeat(32), |
| 81 | + config: { bundleUnpack: ["data/**"], fuses: { inspector: true } }, |
| 82 | + }); |
| 83 | + const [args] = proc.calls; |
| 84 | + assert.includeMembers(args, [ |
| 85 | + "--seal", |
| 86 | + "--key-hex", |
| 87 | + "ab".repeat(32), |
| 88 | + "--no-default-unpack", |
| 89 | + "data/**", |
| 90 | + "inspector=on", |
| 91 | + ]); |
| 92 | + }); |
| 93 | + |
| 94 | + it("only sets fuses with bundle: false", async () => { |
| 95 | + const proc = fakeTool(); |
| 96 | + await pack(proc, { config: { bundle: false } }); |
| 97 | + assert.equal(proc.calls[0][0], "fuses"); |
| 98 | + assert.includeMembers(proc.calls[0], [ |
| 99 | + "bundleIntegrity=off", |
| 100 | + "inspector=off", |
| 101 | + ]); |
| 102 | + assert.isTrue(fs.statSync(path.join(stage, "app")).isDirectory()); |
| 103 | + }); |
| 104 | + |
| 105 | + it("keeps the plain folder when bundling fails", async () => { |
| 106 | + await pack(fakeTool(1)); |
| 107 | + assert.isTrue(fs.statSync(path.join(stage, "app")).isDirectory()); |
| 108 | + assert.isFalse(fs.existsSync(path.join(stage, "app.nsbundle"))); |
| 109 | + assert.equal(fs.readFileSync(executable, "utf8"), "runtime"); |
| 110 | + }); |
| 111 | + |
| 112 | + it("fails the build when source protection fails", async () => { |
| 113 | + let error: Error; |
| 114 | + await pack(fakeTool(1), { sourceProtect: true }).catch((e) => (error = e)); |
| 115 | + assert.match(error.message, /Source protection failed/); |
| 116 | + assert.equal(fs.readFileSync(executable, "utf8"), "runtime"); |
| 117 | + fs.rmSync(path.join(toolsDir, "ns-bundle")); |
| 118 | + error = undefined; |
| 119 | + await pack(fakeTool(), { |
| 120 | + config: { sourceProtect: true }, |
| 121 | + }).catch((e) => (error = e)); |
| 122 | + assert.match(error.message, /no ns-bundle/); |
| 123 | + // a plain release still builds without the tool |
| 124 | + await pack(fakeTool()); |
| 125 | + }); |
| 126 | + |
| 127 | + it("lets --no-source-protect override the config", async () => { |
| 128 | + const proc = fakeTool(); |
| 129 | + await pack(proc, { sourceProtect: false, config: { sourceProtect: true } }); |
| 130 | + assert.notInclude(proc.calls[0], "--seal"); |
| 131 | + }); |
| 132 | +}); |
0 commit comments