Файли
openidea/src/features/vcs/worktrees.test.ts
T
l0ng-ai 401c542647 feat(vcs): improve Git workflows (#5174)
Improve OpenIDEA's Git workflows across commit and staging, branch and remote operations, merge and rebase, Git Log and signing, worktrees, stash and shelf, patch application, and clone settings. Adds a one-click stash for tracked changes across Git roots and lets users open a worktree branch's associated GitHub review from its context menu. Cloning now includes submodules by default (Settings › Version Control › Git, can be turned off).

Repository entry points added here run in each repository's execution scope, so they use the project's own git executable.
2026-11-22 21:57:30 +08:00

127 рядки
4.9 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
groupWorktrees,
isCurrentWorktree,
worktreeContainsProject,
worktreeLocation,
worktreeMoveTarget,
worktreePath,
type RepoWorktrees,
} from "./worktrees";
const worktree = (path: string, main = false) => ({
path,
head: "0123456789abcdef",
branch: path.split("/").pop() ?? null,
main,
locked: false,
prunable: false,
});
describe("isCurrentWorktree", () => {
it("matches the repository root this window works in", () => {
expect(isCurrentWorktree("/work/app", "/work/app")).toBe(true);
expect(isCurrentWorktree("/work/app-wt", "/work/app")).toBe(false);
expect(isCurrentWorktree("/work/app/", "/work/app")).toBe(true);
expect(isCurrentWorktree("/work/app", null)).toBe(false);
});
it("ignores the drive letter's case and separators on Windows", () => {
expect(isCurrentWorktree("c:/Work/app", "C:/Work/app")).toBe(true);
expect(isCurrentWorktree("C:\\Work\\app", "C:/Work/app")).toBe(true);
// Only drive paths: elsewhere the case is part of the name.
expect(isCurrentWorktree("/Work/App", "/work/app")).toBe(false);
});
});
describe("groupWorktrees", () => {
it("keeps one group per repository", () => {
const groups = groupWorktrees([
{ root: "/work/app", worktrees: [worktree("/work/app", true)] },
{ root: "/work/web", worktrees: [worktree("/work/web", true)] },
]);
expect(groups.map((g) => g.root)).toEqual(["/work/app", "/work/web"]);
});
it("merges the roots of a linked worktree that list the same repository", () => {
const worktrees = [worktree("/work/app", true), worktree("/work/app-feature")];
const groups = groupWorktrees([
{ root: "/work/app-feature", worktrees },
{ root: "/work/app", worktrees },
]);
// The main folder names the group even when the linked root came first.
expect(groups).toEqual([{ root: "/work/app", worktrees }]);
});
it("matches the main worktree case-insensitively on Windows", () => {
const worktrees = [worktree("C:/Work/app", true), worktree("C:/Work/app-feature")];
const groups = groupWorktrees([
{ root: "c:/work/app", worktrees },
{ root: "C:/Work/app-feature", worktrees },
]);
expect(groups).toHaveLength(1);
expect(groups[0]?.root).toBe("c:/work/app");
});
it("files a root that failed under the repository that lists it", () => {
const failed: RepoWorktrees = { root: "/work/app-feature", worktrees: [], error: "not a git repository" };
const loaded: RepoWorktrees = {
root: "/work/app",
worktrees: [worktree("/work/app", true), worktree("/work/app-feature")],
};
const groups = groupWorktrees([failed, loaded]);
expect(groups).toHaveLength(1);
// The group keeps the root that loaded, so its actions keep working.
expect(groups[0]?.root).toBe("/work/app");
expect(groups[0]?.worktrees).toBe(loaded.worktrees);
// `error` is absent rather than the stale message.
expect(groups[0]?.error).toBeUndefined();
});
it("keeps errors from roots whose repository was not listed by another root", () => {
const failed: RepoWorktrees = { root: "/work/other-feature", worktrees: [], error: "permission denied" };
const loaded: RepoWorktrees = {
root: "/work/app",
worktrees: [worktree("/work/app", true), worktree("/work/app-feature")],
};
expect(groupWorktrees([failed, loaded])).toEqual([loaded, failed]);
expect(groupWorktrees([failed])).toEqual([failed]);
});
});
describe("worktreeContainsProject", () => {
it.each([
["/p/front", "/p/front", true],
["/p/front/", "/p/front/src", true],
["/p/front", "/p/front-wt", false],
["/p/front/linked", "/p/front", false],
["/", "/p/front", true],
["C:\\Work\\App", "c:/work/app/src", true],
["C:/Work/App/", "c:/work/app-wt", false],
["/Work/App", "/work/app", false],
["/p/front", null, false],
])("compares %s with project %s", (path, root, expected) => {
expect(worktreeContainsProject(path, root)).toBe(expected);
});
});
describe("worktreeMoveTarget", () => {
it("keeps the worktree's folder name unless the chosen folder already has it", () => {
expect(worktreeMoveTarget("/work/app-fix", "/projects")).toBe("/projects/app-fix");
expect(worktreeMoveTarget("/work/app-fix", "/projects/")).toBe("/projects/app-fix");
expect(worktreeMoveTarget("/work/app-fix", "/projects/app-fix")).toBe("/projects/app-fix");
expect(worktreeMoveTarget("C:/work/app-fix", "D:\\wt")).toBe("D:\\wt\\app-fix");
expect(worktreeMoveTarget("C:/work/app-fix", "D:\\wt\\app-fix")).toBe("D:\\wt\\app-fix");
});
});
it.each([
["/src/app/", "/src", "/src/app-feature"],
["/app", "/", "/app-feature"],
["C:/app", "C:/", "C:/app-feature"],
["C:\\src\\app", "C:\\src", "C:\\src\\app-feature"],
])("suggests a worktree next to %s", (root, parent, target) => {
expect(worktreeLocation(root)).toBe(parent);
expect(worktreePath(parent, "app-feature")).toBe(target);
});