Файли
openidea/src/features/libraries/libraries.test.ts
T
LU XIANGRUIтаwildcatDownstairs 950bf986d6 fix(libraries): tell same-named library roots apart in External Libraries (#7463)
A uv venv reports its own .venv/Lib beside the base interpreter's Lib, so External Libraries showed two rows called Lib. Roots sharing a name in one SDK group now show their parent folder after "library root". A root inside a sibling root (.venv/Lib/site-packages under .venv/Lib) also got the same tree key as its row in the sibling's listing, so the virtual list drew two rows on top of each other; nested roots now get their own key namespace.

Refs #7444

Co-authored-by: wildcatDownstairs <[email protected]>
2026-12-02 18:21:02 +08:00

245 рядки
9.8 KiB
TypeScript

import { afterEach, describe, expect, it } from "vitest";
import { isLibraryPath, setLibraryRoots } from "@/editor/api";
import { librariesRoot, openedLibraryGroup, rootDetail, touchesManifest } from "./index";
import { mergeLibraryGroups } from "@/platform/libraries";
describe("External Libraries", () => {
let dispose = () => {};
afterEach(() => dispose());
it("groups each SDK's folders under one read-only root", () => {
expect(librariesRoot([])).toBeNull();
const root = librariesRoot([
{
name: "Python 3.12 (.venv)",
kind: "python",
roots: [
{ name: "python3.12", path: "/py/lib/python3.12" },
{ name: "site-packages", path: "/p/.venv/lib/python3.12/site-packages" },
],
},
])!;
expect(root).toMatchObject({ id: "externalLibraries", label: "External Libraries", readOnly: true });
expect(root.children?.[0]).toMatchObject({ label: "< Python 3.12 (.venv) >", icon: "file-python" });
expect(root.children?.[0]?.children?.map((c) => [c.label, c.path, c.detail])).toEqual([
["python3.12", "/py/lib/python3.12", "library root"],
["site-packages", "/p/.venv/lib/python3.12/site-packages", "library root"],
]);
});
it("gives each ecosystem's groups their icon", () => {
const root = librariesRoot(
[
"jdk",
"nodeCore",
"nodeModules",
"rust",
"cargo",
"go",
"goModules",
"maven",
"gradle",
"zig",
"zigPackages",
"other",
].map((kind) => ({
name: kind,
kind,
roots: [{ name: "r", path: `/libs/${kind}` }],
})),
)!;
expect(Object.fromEntries(root.children!.map((g) => [g.label, g.icon]))).toEqual({
"< jdk >": "file-java",
nodeCore: "file-js",
nodeModules: "file-npm",
"< rust >": "file-rust",
cargo: "file-cargo",
"< go >": "file-go",
goModules: "file-gomod",
maven: "maven-project",
gradle: "file-gradle",
"< zig >": "file-zig",
zigPackages: "file-zig",
other: "library",
});
});
it("lists SDKs first, then libraries by name, whatever order they were detected in", () => {
const group = (name: string, kind: string) => ({ name, kind, roots: [{ name: "r", path: `/libs/${name}` }] });
const detected = [
group("Maven: org.junit:junit:4.13", "maven"),
group("temurin-21", "jdk"),
group("node_modules", "nodeModules"),
group("Python 3.14", "python"),
group("Cargo dependencies", "cargo"),
group("Gradle: com.acme:lib:1.0", "gradle"),
group("Go SDK 1.27.4", "go"),
group("Maven: com.acme:core:2.1.27", "maven"),
group("Node.js Core", "nodeCore"),
group("Zig 0.16.0", "zig"),
group("Zig packages", "zigPackages"),
];
const labels = (groups: typeof detected) => librariesRoot(groups)!.children!.map((g) => g.label);
expect(labels(detected)).toEqual([
"< Go SDK 1.27.4 >",
"< Python 3.14 >",
"< temurin-21 >",
"< Zig 0.16.0 >",
"Cargo dependencies",
"Gradle: com.acme:lib:1.0",
"Maven: com.acme:core:2.1.27",
"Maven: org.junit:junit:4.13",
"node_modules",
"Node.js Core",
"Zig packages",
]);
expect(labels([...detected].reverse())).toEqual(labels(detected));
});
it("lists archives (src.zip, dependency jars) as browsable roots", () => {
const root = librariesRoot([
{
name: "temurin-21",
kind: "jdk",
roots: [
{ name: "temurin-21", path: "/jdk" },
{ name: "src.zip", path: "/jdk/lib/src.zip!" },
],
},
{ name: "Maven: g:a:1", kind: "maven", roots: [{ name: "a-1-sources.jar", path: "/m2/g/a/1/a-1-sources.jar!" }] },
])!;
expect(root.children?.[0]?.children?.map((c) => c.icon)).toEqual(["folder-library", "file-archive"]);
// As in IDEA: SDKs in angle brackets, dependencies without.
expect(root.children?.[0]).toMatchObject({ label: "< temurin-21 >" });
expect(root.children?.[1]).toMatchObject({ label: "Maven: g:a:1", icon: "maven-project" });
dispose = setLibraryRoots("test", ["/jdk/lib/src.zip!"]);
expect(isLibraryPath("/jdk/lib/src.zip!/java.base/java/lang/String.java")).toBe(true);
});
it("adds the classifier jar opened from a library class when the build model omits it", () => {
const uri =
"jdt://contents/demo-lib-1.0-jdk21.jar/com.example.lib/Helper.class?=demo/%5C/m2%5C/com%5C/example%5C/demo-lib%5C/1.0%5C/demo-lib-1.0-jdk21.jar=/maven.groupId=/com.example=/=/maven.artifactId=/demo-lib=/=/maven.version=/1.0=/=/maven.classifier=/jdk21=/=%3Ccom.example.lib(Helper.class";
expect(openedLibraryGroup(uri, [])).toEqual({
name: "Maven: com.example:demo-lib:1.0:jdk21",
kind: "classpath",
roots: [
{
name: "demo-lib-1.0-jdk21.jar",
path: "/m2/com/example/demo-lib/1.0/demo-lib-1.0-jdk21.jar!",
},
],
});
});
it("does not duplicate an archive the build model already lists", () => {
const uri = "jdt://contents/a.jar/p/C.class?=demo/%5C/m2%5C/a.jar%3Cp%28C.class";
const group = { name: "a.jar", kind: "maven", roots: [{ name: "a.jar", path: "/m2/a.jar!" }] };
expect(openedLibraryGroup(uri, [group])).toBeNull();
});
it("refreshes on manifest changes outside node_modules", () => {
expect(touchesManifest(["/p/src/main.rs", "/p/Cargo.toml"])).toBe(true);
expect(touchesManifest(["C:\\p\\go.mod"])).toBe(true);
expect(touchesManifest(["/p/.venv/pyvenv.cfg"])).toBe(true);
expect(touchesManifest(["/p/pnpm-workspace.yaml", "/p/Gemfile.lock"])).toBe(true);
expect(touchesManifest(["/p/build.zig.zon", "/p/go.work"])).toBe(true);
expect(touchesManifest(["/p/node_modules/react/package.json"])).toBe(false);
expect(touchesManifest(["/p/src/package.json.bak", "/p/README.md"])).toBe(false);
});
it("refreshes when an applied Gradle script or properties file changes", () => {
expect(touchesManifest(["/p/gradle/dependencies.gradle"])).toBe(true);
expect(touchesManifest(["C:\\p\\gradle\\dependencies.gradle.kts"])).toBe(true);
expect(touchesManifest(["/p/gradle.properties"])).toBe(true);
expect(touchesManifest(["/p/node_modules/tool/dependencies.gradle"])).toBe(false);
});
it("marks files below library roots for read-only editors", () => {
dispose = setLibraryRoots("test", ["/py/lib/python3.12"]);
expect(isLibraryPath("/py/lib/python3.12/os.py")).toBe(true);
expect(isLibraryPath("/py/lib/python3.120/x.py")).toBe(false);
dispose();
expect(isLibraryPath("/py/lib/python3.12/os.py")).toBe(false);
});
it("merges streamed module groups without dropping ecosystems or distinct roots", () => {
const group = (kind: string, path: string) => ({ name: kind, kind, roots: [{ name: "dep", path }] });
expect(
mergeLibraryGroups([
group("cargo", "/cargo/serde"),
group("nodeModules", "/web/node_modules/dep"),
group("maven", "/m2/dep.jar!"),
group("nodeModules", "/api/node_modules/dep"),
group("cargo", "/cargo/serde"),
]),
).toEqual([
group("cargo", "/cargo/serde"),
{
name: "nodeModules",
kind: "nodeModules",
roots: [
{ name: "dep", path: "/web/node_modules/dep" },
{ name: "dep", path: "/api/node_modules/dep" },
],
},
group("maven", "/m2/dep.jar!"),
]);
});
describe("same-named library roots (#7444)", () => {
const uvVenv = [
{ name: "Lib", path: "D:\\uv\\cpython-3.12-windows-x86_64-none\\Lib" },
{ name: "Lib", path: "E:\\proj\\.venv\\Lib" },
{ name: "site-packages", path: "E:\\proj\\.venv\\Lib\\site-packages" },
{ name: "DLLs", path: "D:\\uv\\cpython-3.12-windows-x86_64-none\\DLLs" },
];
it("keeps the plain detail for unique names", () => {
for (const r of uvVenv.slice(2)) expect(rootDetail(uvVenv, r)).toEqual({ detail: "library root" });
expect(rootDetail([], { name: "a", path: "/a" })).toEqual({ detail: "library root" });
});
it("names the parent folder (Windows paths)", () => {
expect(rootDetail(uvVenv, uvVenv[0]!)).toEqual({
detail: "library root",
note: "cpython-3.12-windows-x86_64-none",
});
expect(rootDetail(uvVenv, uvVenv[1]!)).toEqual({ detail: "library root", note: ".venv" });
});
it("handles POSIX paths, three siblings and a root without a parent", () => {
const roots = [
{ name: "lib", path: "/a/lib" },
{ name: "lib", path: "/b/lib" },
{ name: "lib", path: "/c/lib" },
{ name: "lib", path: "/lib" },
];
const out = roots.map((r) => rootDetail(roots, r));
expect(out.map((o) => o.note)).toEqual(["a", "b", "c", undefined]);
});
it("finds the parent folder with mixed and UNC separators", () => {
const roots = ["C:\\a\\Lib", "C:/b/Lib", "\\\\server\\share\\c\\Lib", "/d/Lib", "C:\\e/Lib", "/f/Lib"].map(
(path) => ({ name: "Lib", path }),
);
const out = roots.map((r) => rootDetail(roots, r));
expect(out.map((o) => o.note)).toEqual(["a", "b", "c", "d", "e", "f"]);
});
it("treats names case-sensitively and ignores trailing separators", () => {
const roots = [
{ name: "Lib", path: "C:\\py\\Lib\\" },
{ name: "lib", path: "C:\\py2\\lib" },
];
expect(roots.map((r) => rootDetail(roots, r))).toEqual([{ detail: "library root" }, { detail: "library root" }]);
});
it("reaches the tree nodes of the contribution", () => {
const kids = librariesRoot([{ name: "Python 3.12 (.venv)", kind: "python", roots: uvVenv }])!.children![0]!
.children!;
expect(kids.map((c) => c.detail)).toEqual(Array(4).fill("library root"));
expect(kids.map((c) => c.note)).toEqual(["cpython-3.12-windows-x86_64-none", ".venv", undefined, undefined]);
});
});
});