163 lines
6.7 KiB
TypeScript
163 lines
6.7 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
import { spawnSync } from "node:child_process";
|
|
import { copyFileSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join, resolve } from "node:path";
|
|
import { formatDevVersion, parseGitDescribe, resolveVersionInfo } from "./version.js";
|
|
import { compareSemVer, isNewerVersion } from "./cli/commands/update.js";
|
|
|
|
describe("Git version parsing and formatting", () => {
|
|
test("parses full git describe string with commits ahead and dirty state", () => {
|
|
const parsed = parseGitDescribe("v0.10.15-2-ga521a24-dirty");
|
|
expect(parsed).toEqual({
|
|
baseVersion: "0.10.15",
|
|
commitsAhead: 2,
|
|
commit: "a521a24",
|
|
dirty: true,
|
|
describe: "v0.10.15-2-ga521a24-dirty"
|
|
});
|
|
});
|
|
|
|
test("parses clean git describe string without dirty suffix", () => {
|
|
const parsed = parseGitDescribe("v0.10.15-0-g83ade4d");
|
|
expect(parsed).toEqual({
|
|
baseVersion: "0.10.15",
|
|
commitsAhead: 0,
|
|
commit: "83ade4d",
|
|
dirty: false,
|
|
describe: "v0.10.15-0-g83ade4d"
|
|
});
|
|
});
|
|
|
|
test("parses tag-only git describe with dirty status", () => {
|
|
const parsed = parseGitDescribe("v0.10.15-dirty");
|
|
expect(parsed).toEqual({
|
|
baseVersion: "0.10.15",
|
|
commitsAhead: 0,
|
|
commit: "",
|
|
dirty: true,
|
|
describe: "v0.10.15-dirty"
|
|
});
|
|
});
|
|
|
|
test("parses raw commit hash fallback", () => {
|
|
const parsed = parseGitDescribe("83ade4d-dirty", "0.10.15");
|
|
expect(parsed).toEqual({
|
|
baseVersion: "0.10.15",
|
|
commitsAhead: 0,
|
|
commit: "83ade4d",
|
|
dirty: true,
|
|
describe: "83ade4d-dirty"
|
|
});
|
|
});
|
|
|
|
test("returns null for empty or invalid describe strings", () => {
|
|
expect(parseGitDescribe("")).toBeNull();
|
|
expect(parseGitDescribe(" ")).toBeNull();
|
|
expect(parseGitDescribe("invalid/string/without/semver")).toBeNull();
|
|
});
|
|
|
|
test("formats dev version according to standard SemVer 2.0 rules", () => {
|
|
expect(formatDevVersion("0.10.15", 2, "a521a24", false)).toBe("0.10.15-dev.2+ga521a24");
|
|
expect(formatDevVersion("0.10.15", 2, "a521a24", true)).toBe("0.10.15-dev.2+ga521a24.dirty");
|
|
expect(formatDevVersion("0.10.15", 0, "83ade4d", false)).toBe("0.10.15-dev.0+g83ade4d");
|
|
expect(formatDevVersion("0.10.15", 0, "83ade4d", true)).toBe("0.10.15-dev.0+g83ade4d.dirty");
|
|
});
|
|
});
|
|
|
|
describe("resolveVersionInfo configuration modes", () => {
|
|
test("source version uses its own checkout even when launched inside another Git repository", () => {
|
|
const dir = mkdtempSync(join(tmpdir(), "seal-version-cwd-test-"));
|
|
const originalCwd = process.cwd();
|
|
const emptyBuildEnv = { SEAL_BUILD_VERSION: "", SEAL_BUILD_IS_RELEASE: "", SEAL_FORCE_RELEASE: "" };
|
|
try {
|
|
const repoRoot = resolve(import.meta.dirname, "..");
|
|
const expected = resolveVersionInfo({ env: emptyBuildEnv, cwd: repoRoot });
|
|
for (const args of [["init"], ["config", "user.name", "Fixture"], ["config", "user.email", "fixture@example.test"]]) {
|
|
expect(spawnSync("git", args, { cwd: dir }).status).toBe(0);
|
|
}
|
|
writeFileSync(join(dir, "fixture.txt"), "foreign repository");
|
|
expect(spawnSync("git", ["add", "fixture.txt"], { cwd: dir }).status).toBe(0);
|
|
expect(spawnSync("git", ["commit", "-m", "Fixture"], { cwd: dir }).status).toBe(0);
|
|
expect(spawnSync("git", ["tag", "v99.99.99"], { cwd: dir }).status).toBe(0);
|
|
process.chdir(dir);
|
|
expect(resolveVersionInfo({ env: emptyBuildEnv })).toEqual(expected);
|
|
// Explicit cwd remains available for callers/tests that intentionally inspect another checkout.
|
|
expect(resolveVersionInfo({ env: emptyBuildEnv, cwd: dir }).git?.baseVersion).toBe("99.99.99");
|
|
const copy = join(dir, "node_modules", "seal-use");
|
|
mkdirSync(join(copy, "src"), { recursive: true });
|
|
copyFileSync(join(repoRoot, "src", "version.ts"), join(copy, "src", "version.ts"));
|
|
copyFileSync(join(repoRoot, "package.json"), join(copy, "package.json"));
|
|
const child = spawnSync(process.execPath, ["-e", `import { VERSION_INFO } from ${JSON.stringify(join(copy, "src", "version.ts"))}; console.log(JSON.stringify(VERSION_INFO))`], {
|
|
cwd: dir, encoding: "utf8", env: { ...process.env, ...emptyBuildEnv }
|
|
});
|
|
expect(child.status).toBe(0);
|
|
expect(JSON.parse(child.stdout)).toMatchObject({ version: expect.stringContaining("+local"), channel: "dev" });
|
|
expect(JSON.parse(child.stdout).git).toBeUndefined();
|
|
} finally {
|
|
process.chdir(originalCwd);
|
|
rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
test("resolves inlined environment variables when preset", () => {
|
|
const info = resolveVersionInfo({
|
|
env: {
|
|
SEAL_BUILD_VERSION: "0.10.15-dev.3+g9a7be16",
|
|
SEAL_BUILD_CHANNEL: "dev",
|
|
SEAL_BUILD_IS_RELEASE: "false",
|
|
SEAL_BUILD_GIT_BASE: "0.10.15",
|
|
SEAL_BUILD_GIT_COMMITS_AHEAD: "3",
|
|
SEAL_BUILD_GIT_COMMIT: "9a7be16",
|
|
SEAL_BUILD_GIT_DIRTY: "false",
|
|
SEAL_BUILD_GIT_DESCRIBE: "v0.10.15-3-g9a7be16"
|
|
}
|
|
});
|
|
|
|
expect(info.name).toBe("seal-use");
|
|
expect(info.version).toBe("0.10.15-dev.3+g9a7be16");
|
|
expect(info.channel).toBe("dev");
|
|
expect(info.isRelease).toBe(false);
|
|
expect(info.git).toBeDefined();
|
|
expect(info.git?.baseVersion).toBe("0.10.15");
|
|
expect(info.git?.commitsAhead).toBe(3);
|
|
expect(info.git?.commit).toBe("9a7be16");
|
|
expect(info.git?.dirty).toBe(false);
|
|
});
|
|
|
|
test("resolves release mode when SEAL_FORCE_RELEASE is set", () => {
|
|
const info = resolveVersionInfo({
|
|
env: {
|
|
SEAL_FORCE_RELEASE: "1"
|
|
}
|
|
});
|
|
|
|
expect(info.name).toBe("seal-use");
|
|
expect(info.channel).toBe("release");
|
|
expect(info.isRelease).toBe(true);
|
|
expect(info.git).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
describe("compareSemVer dev version handling", () => {
|
|
test("recognizes future release as newer than dev build", () => {
|
|
expect(isNewerVersion("0.10.16", "0.10.15-dev.2+ga521a24")).toBe(true);
|
|
expect(isNewerVersion("0.11.0", "0.10.15-dev.2+ga521a24")).toBe(true);
|
|
expect(isNewerVersion("1.0.0", "0.10.15-dev.2+ga521a24")).toBe(true);
|
|
});
|
|
|
|
test("recognizes base release as older than ahead dev build", () => {
|
|
expect(compareSemVer("0.10.15", "0.10.15-dev.2+ga521a24")).toBe(-1);
|
|
expect(isNewerVersion("0.10.15", "0.10.15-dev.2+ga521a24")).toBe(false);
|
|
});
|
|
|
|
test("correctly compares dev builds by commits ahead", () => {
|
|
expect(compareSemVer("0.10.15-dev.3", "0.10.15-dev.2")).toBe(1);
|
|
expect(compareSemVer("0.10.15-dev.1", "0.10.15-dev.2")).toBe(-1);
|
|
});
|
|
|
|
test("identifies older legacy release as downgrade", () => {
|
|
expect(compareSemVer("0.3.3", "0.10.15-dev.2+ga521a24")).toBe(-1);
|
|
});
|
|
});
|