|
| 1 | +import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | +import { assert, describe, it } from "@effect/vitest"; |
| 3 | +import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 4 | +import * as Effect from "effect/Effect"; |
| 5 | +import * as FileSystem from "effect/FileSystem"; |
| 6 | +import * as Path from "effect/Path"; |
| 7 | +import * as Stream from "effect/Stream"; |
| 8 | +import { ChildProcess, ChildProcessSpawner } from "effect/process"; |
| 9 | + |
| 10 | +const run = Effect.fn("test.aur.run")(function* ( |
| 11 | + command: string, |
| 12 | + args: ReadonlyArray<string>, |
| 13 | + cwd: string, |
| 14 | + env: Record<string, string | undefined>, |
| 15 | +) { |
| 16 | + const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; |
| 17 | + const child = yield* spawner.spawn( |
| 18 | + ChildProcess.make(command, args, { cwd, env, extendEnv: false }), |
| 19 | + ); |
| 20 | + const collect = <E>(stream: Stream.Stream<Uint8Array, E>) => |
| 21 | + stream.pipe( |
| 22 | + Stream.decodeText(), |
| 23 | + Stream.runFold( |
| 24 | + () => "", |
| 25 | + (left, right) => left + right, |
| 26 | + ), |
| 27 | + ); |
| 28 | + const [stdout, stderr, code] = yield* Effect.all( |
| 29 | + [collect(child.stdout), collect(child.stderr), child.exitCode], |
| 30 | + { concurrency: "unbounded" }, |
| 31 | + ); |
| 32 | + return { stdout, stderr, code }; |
| 33 | +}, Effect.scoped); |
| 34 | + |
| 35 | +describe.skipIf(HostProcess.Platform.defaultValue() !== "linux")("AUR release", () => { |
| 36 | + it.live.each([ |
| 37 | + { nightly: false, naming: "versioned", failure: undefined }, |
| 38 | + { nightly: true, naming: "versioned", failure: undefined }, |
| 39 | + { nightly: false, naming: "versionless", failure: undefined }, |
| 40 | + { nightly: true, naming: "versionless", failure: undefined }, |
| 41 | + { nightly: false, naming: "versioned", failure: "missing" }, |
| 42 | + { nightly: false, naming: "versioned", failure: "digest" }, |
| 43 | + ])("updates architecture-specific sources: %j", ({ nightly, naming, failure }) => |
| 44 | + Effect.gen(function* () { |
| 45 | + const fs = yield* FileSystem.FileSystem; |
| 46 | + const path = yield* Path.Path; |
| 47 | + const inherited = yield* HostProcess.Environment; |
| 48 | + const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-aur-release-" }); |
| 49 | + const aur = path.join(root, "packaging/aur"); |
| 50 | + yield* fs.copy(path.join(import.meta.dirname, "../packaging/aur"), aur); |
| 51 | + const tools = path.join(root, "tools"); |
| 52 | + yield* fs.makeDirectory(tools); |
| 53 | + for (const [name, source] of Object.entries({ |
| 54 | + gh: '#!/bin/bash\nif [[ "$*" == *releases/tags/* ]]; then cat "$AUR_FIXTURE_RELEASE"; else echo "fixture license"; fi\n', |
| 55 | + id: "#!/bin/sh\necho 1000\n", |
| 56 | + namcap: "#!/bin/sh\nexit 0\n", |
| 57 | + makepkg: '#!/bin/sh\nif [ "$1" = --packagelist ]; then echo fixture.pkg.tar.zst; fi\n', |
| 58 | + })) { |
| 59 | + const file = path.join(tools, name); |
| 60 | + yield* fs.writeFileString(file, source); |
| 61 | + yield* fs.chmod(file, 0o755); |
| 62 | + } |
| 63 | + const version = nightly ? "1.2.4-nightly.20261104.1" : "1.2.3"; |
| 64 | + const pkgname = nightly ? "t3code-nightly-bin" : "t3code-bin"; |
| 65 | + const prefix = |
| 66 | + naming === "versioned" ? `T3-Code-${version}` : nightly ? "T3-Code-Nightly" : "T3-Code"; |
| 67 | + const assets = [ |
| 68 | + { name: `${prefix}-x86_64.AppImage`, digest: `sha256:${"a".repeat(64)}` }, |
| 69 | + ...(failure === "missing" |
| 70 | + ? [] |
| 71 | + : [ |
| 72 | + { |
| 73 | + name: `${prefix}-arm64.AppImage`, |
| 74 | + digest: failure === "digest" ? "invalid" : `sha256:${"b".repeat(64)}`, |
| 75 | + }, |
| 76 | + ]), |
| 77 | + ]; |
| 78 | + const fixture = path.join(root, "release.json"); |
| 79 | + yield* fs.writeFileString(fixture, JSON.stringify({ assets })); |
| 80 | + const packageDir = path.join(aur, pkgname); |
| 81 | + const recipe = path.join(packageDir, "PKGBUILD"); |
| 82 | + const before = yield* fs.readFileString(recipe); |
| 83 | + const env = { |
| 84 | + PATH: `${tools}:${inherited.PATH}`, |
| 85 | + RELEASE_TAG: `v${version}`, |
| 86 | + AUR_FIXTURE_RELEASE: fixture, |
| 87 | + AUR_SSH_PRIVATE_KEY: "", |
| 88 | + }; |
| 89 | + const result = yield* run("bash", [path.join(aur, "scripts/release.sh")], root, env); |
| 90 | + if (failure) { |
| 91 | + assert.notEqual(Number(result.code), 0, result.stdout + result.stderr); |
| 92 | + assert.equal(yield* fs.readFileString(recipe), before); |
| 93 | + return; |
| 94 | + } |
| 95 | + assert.equal(Number(result.code), 0, result.stdout + result.stderr); |
| 96 | + for (const [arch, upstreamArch, checksum] of [ |
| 97 | + ["x86_64", "x86_64", "a"], |
| 98 | + ["aarch64", "arm64", "b"], |
| 99 | + ] as const) { |
| 100 | + const selected = yield* run( |
| 101 | + "bash", |
| 102 | + [ |
| 103 | + "-c", |
| 104 | + `source PKGBUILD |
| 105 | + declare -n sources="source_$CARCH" sums="sha256sums_$CARCH" |
| 106 | + printf '%s\\n' "$pkgver" "\${arch[*]}" "\${sources[@]}" "\${sums[@]}" "$_appimage"`, |
| 107 | + ], |
| 108 | + packageDir, |
| 109 | + { ...env, CARCH: arch }, |
| 110 | + ); |
| 111 | + assert.equal(Number(selected.code), 0, selected.stderr); |
| 112 | + const localName = `T3-Code-${version}-${upstreamArch}.AppImage`; |
| 113 | + assert.deepEqual(selected.stdout.trim().split("\n"), [ |
| 114 | + version.replace("-nightly.", "_nightly."), |
| 115 | + "x86_64 aarch64", |
| 116 | + `${localName}::https://github.com/pingdotgg/t3code/releases/download/v${version}/${prefix}-${upstreamArch}.AppImage`, |
| 117 | + checksum.repeat(64), |
| 118 | + localName, |
| 119 | + ]); |
| 120 | + } |
| 121 | + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), |
| 122 | + ); |
| 123 | +}); |
0 commit comments