Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions packages/core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
52 changes: 52 additions & 0 deletions packages/core/tests/utils.integ.test.ts
Original file line number Diff line number Diff line change
@@ -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 `<root>/bench-repo` (holds the benchmark) and `<root>/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",
);
});
});
Loading