|
| 1 | +// @effect-diagnostics nodeBuiltinImport:off - Exercises the real shell installer against a local HTTP fixture. |
| 2 | +import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 3 | +import * as NodeChildProcess from "node:child_process"; |
| 4 | +import * as NodeCrypto from "node:crypto"; |
| 5 | +import * as NodeFSP from "node:fs/promises"; |
| 6 | +import * as NodeHttp from "node:http"; |
| 7 | +import * as NodeOS from "node:os"; |
| 8 | +import * as NodePath from "node:path"; |
| 9 | +import { describe, expect, it } from "vite-plus/test"; |
| 10 | + |
| 11 | +const missingAtomicError = |
| 12 | + "t3: error while loading shared libraries: libatomic.so.1: cannot open shared object file: No such file or directory"; |
| 13 | + |
| 14 | +// Stub uname so the Linux diagnostic can also be exercised on macOS. |
| 15 | +describe.skipIf(HostProcess.Platform.defaultValue() === "win32")( |
| 16 | + "installer executable validation", |
| 17 | + () => { |
| 18 | + it.each([ |
| 19 | + { |
| 20 | + platform: "Linux", |
| 21 | + diagnostic: missingAtomicError, |
| 22 | + exitCode: 127, |
| 23 | + hint: true, |
| 24 | + }, |
| 25 | + { |
| 26 | + platform: "Linux", |
| 27 | + diagnostic: |
| 28 | + "t3: error while loading shared libraries: libatomic.so.1: cannot open shared object file: Permission denied", |
| 29 | + exitCode: 127, |
| 30 | + hint: false, |
| 31 | + }, |
| 32 | + { |
| 33 | + platform: "Darwin", |
| 34 | + diagnostic: missingAtomicError, |
| 35 | + exitCode: 127, |
| 36 | + hint: false, |
| 37 | + }, |
| 38 | + { |
| 39 | + platform: "Linux", |
| 40 | + diagnostic: "t3: startup warning", |
| 41 | + exitCode: 0, |
| 42 | + hint: false, |
| 43 | + }, |
| 44 | + ])( |
| 45 | + "handles $platform validation: $diagnostic", |
| 46 | + async ({ platform, diagnostic, exitCode, hint }) => { |
| 47 | + const root = await NodeFSP.mkdtemp( |
| 48 | + NodePath.join(NodeOS.tmpdir(), "t3-install-validation-"), |
| 49 | + ); |
| 50 | + const version = "1.2.3"; |
| 51 | + const stem = `t3-${version}-${platform.toLowerCase()}-x64`; |
| 52 | + const archiveName = `${stem}.tar.gz`; |
| 53 | + const home = NodePath.join(root, "home"); |
| 54 | + const bin = NodePath.join(root, "bin"); |
| 55 | + const versions = NodePath.join(home, "runtime/versions"); |
| 56 | + const server = NodeHttp.createServer((request, response) => { |
| 57 | + if (request.url?.endsWith("/SHA256SUMS")) { |
| 58 | + response.end(`${checksum} ${archiveName}\n`); |
| 59 | + } else if (request.url?.endsWith(`/${archiveName}`)) { |
| 60 | + response.end(archive); |
| 61 | + } else { |
| 62 | + response.writeHead(404).end(); |
| 63 | + } |
| 64 | + }); |
| 65 | + let archive: Buffer; |
| 66 | + let checksum: string; |
| 67 | + try { |
| 68 | + await NodeFSP.mkdir(NodePath.join(root, stem)); |
| 69 | + await NodeFSP.writeFile( |
| 70 | + NodePath.join(root, stem, "t3"), |
| 71 | + `#!/bin/sh\nprintf '%s\\n' '${diagnostic}' >&2\nexit ${exitCode}\n`, |
| 72 | + { mode: 0o755 }, |
| 73 | + ); |
| 74 | + NodeChildProcess.execFileSync("tar", [ |
| 75 | + "-czf", |
| 76 | + NodePath.join(root, archiveName), |
| 77 | + "-C", |
| 78 | + root, |
| 79 | + stem, |
| 80 | + ]); |
| 81 | + archive = await NodeFSP.readFile(NodePath.join(root, archiveName)); |
| 82 | + checksum = NodeCrypto.createHash("sha256").update(archive).digest("hex"); |
| 83 | + await NodeFSP.mkdir(bin); |
| 84 | + await NodeFSP.writeFile( |
| 85 | + NodePath.join(bin, "uname"), |
| 86 | + `#!/bin/sh\ncase "$1" in -s) echo '${platform}';; -m) echo x86_64;; *) exit 1;; esac\n`, |
| 87 | + { mode: 0o755 }, |
| 88 | + ); |
| 89 | + await new Promise<void>((resolve, reject) => { |
| 90 | + server.once("error", reject); |
| 91 | + server.listen(0, "127.0.0.1", resolve); |
| 92 | + }); |
| 93 | + const address = server.address(); |
| 94 | + if (!address || typeof address === "string") throw new Error("Expected a TCP listener"); |
| 95 | + const result = await new Promise<{ |
| 96 | + code: number | null; |
| 97 | + stdout: string; |
| 98 | + stderr: string; |
| 99 | + }>((resolve, reject) => { |
| 100 | + const child = NodeChildProcess.spawn( |
| 101 | + "sh", |
| 102 | + [NodePath.resolve(import.meta.dirname, "install.sh")], |
| 103 | + { |
| 104 | + env: { |
| 105 | + ...process.env, |
| 106 | + PATH: `${bin}:${process.env.PATH}`, |
| 107 | + T3CODE_VERSION: version, |
| 108 | + T3CODE_HOME: home, |
| 109 | + T3CODE_INSTALL_BIN_DIR: bin, |
| 110 | + T3CODE_RELEASE_BASE_URL: `http://127.0.0.1:${address.port}`, |
| 111 | + }, |
| 112 | + stdio: ["ignore", "pipe", "pipe"], |
| 113 | + }, |
| 114 | + ); |
| 115 | + let stdout = ""; |
| 116 | + let stderr = ""; |
| 117 | + child.stdout.on("data", (chunk: Buffer) => { |
| 118 | + stdout += chunk.toString(); |
| 119 | + }); |
| 120 | + child.stderr.on("data", (chunk: Buffer) => { |
| 121 | + stderr += chunk.toString(); |
| 122 | + }); |
| 123 | + child.on("error", reject); |
| 124 | + child.on("close", (code) => resolve({ code, stdout, stderr })); |
| 125 | + }); |
| 126 | + expect(result.stderr).toContain(diagnostic); |
| 127 | + expect(result.stderr.includes("providing libatomic.so.1")).toBe(hint); |
| 128 | + expect(result.stdout).not.toContain(diagnostic); |
| 129 | + if (exitCode === 0) { |
| 130 | + expect(result.code).toBe(0); |
| 131 | + expect(await NodeFSP.readdir(NodePath.join(versions, version))).not.toContain( |
| 132 | + "version.errors", |
| 133 | + ); |
| 134 | + } else { |
| 135 | + expect(result.code).toBe(1); |
| 136 | + expect(result.stderr).toContain( |
| 137 | + hint ? "rerun the installer" : "the downloaded executable does not run", |
| 138 | + ); |
| 139 | + } |
| 140 | + } finally { |
| 141 | + server.closeAllConnections(); |
| 142 | + await new Promise<void>((resolve) => server.close(() => resolve())); |
| 143 | + await NodeFSP.rm(root, { recursive: true, force: true }); |
| 144 | + } |
| 145 | + }, |
| 146 | + ); |
| 147 | + }, |
| 148 | +); |
0 commit comments