diff --git a/packages/core/src/utils.ts b/packages/core/src/utils.ts index a4441dbc..b66c5c8f 100644 --- a/packages/core/src/utils.ts +++ b/packages/core/src/utils.ts @@ -30,15 +30,27 @@ export function getGitDir(from: string): string | undefined { */ export function getCallingFile(depth: number): string { const stack = getStackTrace(); - let callingFile = stack[depth + 1].getFileName(); - const gitDir = getGitDir(callingFile); + return getGitRelativePath(stack[depth + 1].getFileName()); +} + +/** + * Returns `fileName`, a path or a `file://` URL (ES modules), relative to the + * root of the git repository that contains it. + * + * It throws when the file is in no git repository. + */ +export function getGitRelativePath(fileName: string): string { + // Converts the URL first: as a path, a URL resolves against the current + // directory, so the search would find the repository of the current + // directory instead of the one of the file. + const filePath = fileName.startsWith("file://") + ? fileURLToPath(fileName) + : fileName; + const gitDir = getGitDir(filePath); if (gitDir === undefined) { throw new Error("Could not find a git repository"); } - if (callingFile.startsWith("file://")) { - callingFile = fileURLToPath(callingFile); - } - return path.relative(gitDir, callingFile); + return path.relative(gitDir, filePath); } /** diff --git a/packages/core/tests/utils.integ.test.ts b/packages/core/tests/utils.integ.test.ts new file mode 100644 index 00000000..a8a309d8 --- /dev/null +++ b/packages/core/tests/utils.integ.test.ts @@ -0,0 +1,52 @@ +import { mkdirSync, mkdtempSync, realpathSync, rmSync } from "fs"; +import { tmpdir } from "os"; +import path from "path"; +import { pathToFileURL } from "url"; + +import { getGitRelativePath } from "../src/utils"; + +// Replaces the ES-only `stack-trace`, which Jest cannot load and these tests +// do not use. +jest.mock("stack-trace", () => ({ get: jest.fn() })); + +describe("getGitRelativePath", () => { + let root: string; + let cwd: string; + + // Creates `/bench-repo` (holds the benchmark) and `/other-repo` + // (the current directory), each with its own `.git`. + beforeEach(() => { + root = realpathSync(mkdtempSync(path.join(tmpdir(), "codspeed-git-"))); + for (const repo of ["bench-repo", "other-repo"]) { + mkdirSync(path.join(root, repo, ".git"), { recursive: true }); + } + mkdirSync(path.join(root, "bench-repo", "benches")); + mkdirSync(path.join(root, "no-repo")); + cwd = process.cwd(); + process.chdir(path.join(root, "other-repo")); + }); + + afterEach(() => { + process.chdir(cwd); + rmSync(root, { recursive: true, force: true }); + }); + + it("uses the repository of the file for a path", () => { + const file = path.join(root, "bench-repo", "benches", "a.bench.js"); + expect(getGitRelativePath(file)).toBe("benches/a.bench.js"); + }); + + it("uses the repository of the file for a file URL", () => { + const file = path.join(root, "bench-repo", "benches", "a.bench.mjs"); + expect(getGitRelativePath(pathToFileURL(file).href)).toBe( + "benches/a.bench.mjs", + ); + }); + + it("throws when the file is in no repository", () => { + const file = path.join(root, "no-repo", "a.bench.mjs"); + expect(() => getGitRelativePath(pathToFileURL(file).href)).toThrow( + "Could not find a git repository", + ); + }); +});