test: reuse git commit module exports

This commit is contained in:
Peter Steinberger 2026-03-17 08:15:32 +00:00
parent 2b980bfcee
commit a3f09d519d
1 changed files with 4 additions and 23 deletions

View File

@ -40,13 +40,15 @@ async function makeFakeGitRepo(
describe("git commit resolution", () => {
const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../..");
let resolveCommitHash: (typeof import("./git-commit.js"))["resolveCommitHash"];
let __testing: (typeof import("./git-commit.js"))["__testing"];
beforeEach(async () => {
vi.restoreAllMocks();
vi.doUnmock("node:fs");
vi.doUnmock("node:module");
vi.resetModules();
const { __testing } = await import("./git-commit.js");
({ resolveCommitHash, __testing } = await import("./git-commit.js"));
__testing.clearCachedGitCommits();
});
@ -54,9 +56,8 @@ describe("git commit resolution", () => {
vi.restoreAllMocks();
vi.doUnmock("node:fs");
vi.doUnmock("node:module");
vi.resetModules();
const { __testing } = await import("./git-commit.js");
__testing.clearCachedGitCommits();
vi.resetModules();
});
it("resolves commit metadata from the caller module root instead of the caller cwd", async () => {
@ -85,7 +86,6 @@ describe("git commit resolution", () => {
.trim()
.slice(0, 7);
const { resolveCommitHash } = await import("./git-commit.js");
const entryModuleUrl = pathToFileURL(path.join(repoRoot, "src", "entry.ts")).href;
vi.spyOn(process, "cwd").mockReturnValue(otherRepo);
@ -101,7 +101,6 @@ describe("git commit resolution", () => {
.trim()
.slice(0, 7);
const { resolveCommitHash } = await import("./git-commit.js");
const entryModuleUrl = pathToFileURL(path.join(repoRoot, "src", "entry.ts")).href;
expect(
@ -117,7 +116,6 @@ describe("git commit resolution", () => {
it("caches build-info fallback results per resolved search directory", async () => {
const temp = await makeTempDir("git-commit-build-info-cache");
const { resolveCommitHash } = await import("./git-commit.js");
const readBuildInfoCommit = vi.fn(() => "deadbee");
expect(resolveCommitHash({ cwd: temp, env: {}, readers: { readBuildInfoCommit } })).toBe(
@ -133,7 +131,6 @@ describe("git commit resolution", () => {
it("caches package.json fallback results per resolved search directory", async () => {
const temp = await makeTempDir("git-commit-package-json-cache");
const { resolveCommitHash } = await import("./git-commit.js");
const readPackageJsonCommit = vi.fn(() => "badc0ff");
expect(
@ -169,8 +166,6 @@ describe("git commit resolution", () => {
.trim()
.slice(0, 7);
const { resolveCommitHash } = await import("./git-commit.js");
expect(() =>
resolveCommitHash({ moduleUrl: "not-a-file-url", cwd: repoRoot, env: {} }),
).not.toThrow();
@ -201,8 +196,6 @@ describe("git commit resolution", () => {
);
const moduleUrl = pathToFileURL(path.join(packageRoot, "dist", "entry.js")).href;
const { resolveCommitHash } = await import("./git-commit.js");
expect(
resolveCommitHash({
moduleUrl,
@ -227,8 +220,6 @@ describe("git commit resolution", () => {
head: "89abcdef0123456789abcdef0123456789abcdef\n",
});
const { resolveCommitHash } = await import("./git-commit.js");
expect(resolveCommitHash({ cwd: repoA, env: {} })).toBe("0123456");
expect(resolveCommitHash({ cwd: repoB, env: {} })).toBe("89abcde");
expect(resolveCommitHash({ cwd: repoA, env: {} })).toBe("0123456");
@ -241,7 +232,6 @@ describe("git commit resolution", () => {
head: "not-a-commit\n",
});
const { resolveCommitHash } = await import("./git-commit.js");
const readGitCommit = vi.fn(() => null);
expect(resolveCommitHash({ cwd: repoRoot, env: {}, readers: { readGitCommit } })).toBeNull();
@ -257,7 +247,6 @@ describe("git commit resolution", () => {
await makeFakeGitRepo(repoRoot, {
head: "0123456789abcdef0123456789abcdef01234567\n",
});
const { resolveCommitHash } = await import("./git-commit.js");
const readGitCommit = vi.fn(() => {
const error = Object.assign(new Error(`EACCES: permission denied`), {
code: "EACCES",
@ -294,8 +283,6 @@ describe("git commit resolution", () => {
it("formats env-provided commit strings consistently", async () => {
const temp = await makeTempDir("git-commit-env");
const { resolveCommitHash } = await import("./git-commit.js");
expect(resolveCommitHash({ cwd: temp, env: { GIT_COMMIT: "ABCDEF0123456789" } })).toBe(
"abcdef0",
);
@ -308,8 +295,6 @@ describe("git commit resolution", () => {
it("rejects unsafe HEAD refs and accepts valid refs", async () => {
const temp = await makeTempDir("git-commit-refs");
const { resolveCommitHash } = await import("./git-commit.js");
const absoluteRepo = path.join(temp, "absolute");
await makeFakeGitRepo(absoluteRepo, { head: "ref: /tmp/evil\n" });
expect(resolveCommitHash({ cwd: absoluteRepo, env: {} })).toBeNull();
@ -347,8 +332,6 @@ describe("git commit resolution", () => {
commondir: "../common-git",
});
const { resolveCommitHash } = await import("./git-commit.js");
expect(resolveCommitHash({ cwd: repoRoot, env: {} })).toBe("bbbbbbb");
});
@ -363,8 +346,6 @@ describe("git commit resolution", () => {
},
});
const { resolveCommitHash } = await import("./git-commit.js");
expect(resolveCommitHash({ cwd: repoRoot, env: {} })).toBe("ccccccc");
});
});