diff --git a/install.sh b/install.sh index 2df6548e2..c236fc8ab 100755 --- a/install.sh +++ b/install.sh @@ -3,6 +3,10 @@ set -euo pipefail PACKAGE="rea-agents" REPOSITORY="morluto/rea" +numeric_identifier='(0|[1-9][0-9]*)' +prerelease_identifier="(${numeric_identifier}|[0-9A-Za-z-]*[A-Za-z-][0-9A-Za-z-]*)" +build_identifier='[0-9A-Za-z-]+' +version_pattern="${numeric_identifier}\\.${numeric_identifier}\\.${numeric_identifier}(-${prerelease_identifier}(\\.${prerelease_identifier})*)?(\\+${build_identifier}(\\.${build_identifier})*)?" version="${REA_VERSION:-}" dry_run=false start_setup=true @@ -67,14 +71,20 @@ if ! [[ "$_node_patch" =~ ^[0-9]+(\+[0-9A-Za-z.-]+)?$ ]] || fi if [[ -n "$version" ]]; then - [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$ ]] || fail "version must be an exact semantic version." + [[ "$version" =~ ^${version_pattern}$ ]] || fail "version must be an exact semantic version." else release_json="$(curl -fsSL --proto '=https' --tlsv1.2 --retry 3 -H 'Accept: application/vnd.github+json' "https://api.github.com/repos/$REPOSITORY/releases/latest")" || fail "the latest REA release could not be resolved. Check network access or pass --version VERSION, then retry." tag="$(printf '%s' "$release_json" | node -e 'let s="";process.stdin.on("data",c=>s+=c).on("end",()=>{const v=JSON.parse(s).tag_name;if(typeof v!=="string")process.exit(1);process.stdout.write(v)})' 2>/dev/null)" || fail "the release response was invalid. Retry later or pass --version VERSION." - [[ "$tag" =~ ^rea-agents-([0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?)$ ]] || fail "the latest release tag was invalid. Retry later or pass --version VERSION." + [[ "$tag" =~ ^rea-agents-(${version_pattern})$ ]] || fail "the latest release tag was invalid. Retry later or pass --version VERSION." version="${BASH_REMATCH[1]}" fi +# npm's exact-version resolver cleans build metadata from the requested version, +# so it may install the base release and make the post-install identity check fail. +if [[ "$version" =~ \+ ]]; then + fail "npm cannot install an exact version with build metadata. Pass a version without build metadata, then retry." +fi + prefix_args=() if [[ "$platform" == "Linux" ]]; then prefix_args=(--prefix "$HOME/.local") diff --git a/tests/boundary/filesystem/installScript.test.ts b/tests/boundary/filesystem/installScript.test.ts index f6590e6cb..b0631fcaf 100644 --- a/tests/boundary/filesystem/installScript.test.ts +++ b/tests/boundary/filesystem/installScript.test.ts @@ -181,6 +181,95 @@ describe("curl installer scenarios", { timeout: 20_000 }, () => { }); }); +describe("installer semantic version parsing", { timeout: 20_000 }, () => { + it.each(["01.2.3", "1.2.3-01", "1.2.3-alpha..1"])( + "rejects malformed semantic versions before invoking npm: %s", + async (version) => { + const fixture = await createFixture(); + await expect( + runInstaller(fixture, ["--version", version, "--dry-run"]), + ).rejects.toMatchObject({ + stderr: + "REA installation failed: version must be an exact semantic version.\n", + }); + await expect(readFile(fixture.npmLog, "utf8")).rejects.toMatchObject({ + code: "ENOENT", + }); + }, + ); + + it("rejects valid build metadata before printing a plan or invoking npm", async () => { + const fixture = await createFixture(); + const failure = await runInstaller(fixture, [ + "--version", + "1.2.3+build.01", + "--dry-run", + ]).then( + () => undefined, + (cause: unknown) => cause, + ); + expect(failure).toBeInstanceOf(Error); + const stderr = getProcessOutput(failure, "stderr"); + expect(stderr).toBe( + "REA installation failed: npm cannot install an exact version with build metadata. Pass a version without build metadata, then retry.\n", + ); + expect(getProcessOutput(failure, "stdout")).not.toContain( + "REA install plan", + ); + await expect(readFile(fixture.npmLog, "utf8")).rejects.toMatchObject({ + code: "ENOENT", + }); + }); + + it("accepts a valid prerelease version without build metadata", async () => { + const fixture = await createFixture(); + const result = await runInstaller(fixture, [ + "--version", + "1.2.3-alpha.0", + "--dry-run", + ]); + expect(result.stdout).toContain("Version: 1.2.3-alpha.0"); + await expect(readFile(fixture.npmLog, "utf8")).rejects.toMatchObject({ + code: "ENOENT", + }); + }); + + it("rejects build metadata in the latest release tag before printing a plan", async () => { + const fixture = await createFixture(); + const failure = await runInstaller(fixture, [], { + FAKE_CURL_BODY: '{"tag_name":"rea-agents-1.2.3+build.01"}', + }).then( + () => undefined, + (cause: unknown) => cause, + ); + expect(failure).toBeInstanceOf(Error); + expect(getProcessOutput(failure, "stderr")).toBe( + "REA installation failed: npm cannot install an exact version with build metadata. Pass a version without build metadata, then retry.\n", + ); + expect(getProcessOutput(failure, "stdout")).not.toContain( + "REA install plan", + ); + await expect(readFile(fixture.npmLog, "utf8")).rejects.toMatchObject({ + code: "ENOENT", + }); + }); + + it("rejects an invalid semantic version from the latest release tag", async () => { + const fixture = await createFixture(); + await expect( + runInstaller(fixture, [], { + FAKE_CURL_BODY: '{"tag_name":"rea-agents-1.2.3-01"}', + }), + ).rejects.toMatchObject({ + stderr: + "REA installation failed: the latest release tag was invalid. Retry later or pass --version VERSION.\n", + }); + await expect(readFile(fixture.npmLog, "utf8")).rejects.toMatchObject({ + code: "ENOENT", + }); + }); +}); + interface InstallerFixture { readonly home: string; readonly bin: string; @@ -189,6 +278,20 @@ interface InstallerFixture { readonly reaLog: string; } +const getProcessOutput = ( + failure: unknown, + stream: "stdout" | "stderr", +): string => { + if (!(failure instanceof Error)) return ""; + if (stream === "stdout" && "stdout" in failure) { + return typeof failure.stdout === "string" ? failure.stdout : ""; + } + if (stream === "stderr" && "stderr" in failure) { + return typeof failure.stderr === "string" ? failure.stderr : ""; + } + return ""; +}; + const createFixture = async (): Promise => { const root = await createTestTempDirectory("rea-install-test-"); const home = join(root, "home");