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
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,24 @@ describe("orderDiffFiles", () => {
"src/orphan.spec.ts",
]);
});

it("pairs a test with the source whose name carries its own dotted suffix", () => {
expect(
order([
file("src/c.browser.test.tsx"),
file("src/b.test.ts"),
file("src/a.logic.test.ts"),
file("src/c.tsx"),
file("src/b.ts"),
file("src/a.logic.ts"),
]),
).toEqual([
"src/a.logic.ts",
"src/b.ts",
"src/c.tsx",
"src/a.logic.test.ts",
"src/b.test.ts",
"src/c.browser.test.tsx",
]);
});
});
20 changes: 15 additions & 5 deletions apps/web/src/components/pullRequest/pullRequestFileOrder.logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,17 @@ function orderByImports(
return ordered;
}

function testedBaseName(path: string): string {
return (path.split("/").at(-1) ?? "").replace(/\.(?:test|spec)\..*$/, "").replace(/\.[^.]+$/, "");
// `a.logic.test.ts` covers `a.logic.ts` while `a.browser.test.ts` covers `a.ts`, so the longest
// dotted prefix that names a source file wins.
function testedSourcePosition(path: string, sourcePositions: ReadonlyMap<string, number>): number {
let name = (path.split("/").at(-1) ?? "").replace(/\.(?:test|spec)\..*$/, "");
while (true) {
const position = sourcePositions.get(name);
if (position !== undefined) return position;
const dot = name.lastIndexOf(".");
if (dot <= 0) return Number.MAX_SAFE_INTEGER;
name = name.slice(0, dot);
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}

export function orderDiffFiles(
Expand Down Expand Up @@ -160,9 +169,10 @@ export function orderDiffFiles(
const orderedTests = [...byPath.keys()]
.filter((path) => tiers.get(path) === "test")
.sort((left, right) => {
const leftPosition = sourcePositions.get(testedBaseName(left)) ?? Number.MAX_SAFE_INTEGER;
const rightPosition = sourcePositions.get(testedBaseName(right)) ?? Number.MAX_SAFE_INTEGER;
return leftPosition - rightPosition || left.localeCompare(right);
return (
testedSourcePosition(left, sourcePositions) -
testedSourcePosition(right, sourcePositions) || left.localeCompare(right)
);
});

const orderedGenerated = [...byPath.keys()]
Expand Down
Loading