Файли
openidea/src/platform/dotnetSolutionFolders.test.ts
T
LU XIANGRUIтаwildcatDownstairs 310d923480 feat(dotnet): show solution folders and solution items in the Project view (#7435)
Solution folders from .sln and .slnx files appear as nested nodes under the solution, holding their projects and solution items; projects outside every folder follow.

Parent folders an SLNX file only implies are created, literal slashes in legacy folder names are kept, and uppercase .SLNX extensions are accepted.

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

148 рядки
6.1 KiB
TypeScript

import { execFileSync } from "node:child_process";
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
import { solutionFolders } from "./dotnet";
const FOLDER = "{2150E333-8FDC-42A3-9474-1A3956D46DE8}";
const CS = "{9A19103F-16F7-4668-BE54-9A1E7A4F7556}";
const sln = (...blocks: string[]) =>
`Microsoft Visual Studio Solution File, Format Version 12.00\r\n${blocks.join("\r\n")}\r\nGlobal\r\nEndGlobal\r\n`;
const folder = (name: string, guid: string, items: string[] = []) =>
`Project("${FOLDER}") = "${name}", "${name}", "{${guid}}"\r\n` +
(items.length
? `\tProjectSection(SolutionItems) = preProject\r\n${items.map((i) => `\t\t${i} = ${i}`).join("\r\n")}\r\n\tEndProjectSection\r\n`
: "") +
"EndProject";
const project = (name: string, path: string, guid: string) =>
`Project("${CS}") = "${name}", "${path}", "{${guid}}"\r\nEndProject`;
const nested = (...pairs: [string, string][]) =>
`Global\r\n\tGlobalSection(NestedProjects) = preSolution\r\n${pairs.map(([c, p]) => `\t\t{${c}} = {${p}}`).join("\r\n")}\r\n\tEndGlobalSection\r\nEndGlobal`;
describe("solutionFolders (.sln)", () => {
it("returns nothing for a solution without folders", () => {
expect(solutionFolders(sln(project("A", "A\\A.csproj", "A1")), false, "/s")).toEqual([]);
});
it("nests folders 3 deep with projects and solution items, parents first", () => {
const text = sln(
folder("src", "F1", ["README.md"]),
folder("libs", "F2"),
folder("core", "F3", [".editorconfig"]),
project("Core", "src\\libs\\core\\Core.csproj", "P1"),
project("App", "App\\App.csproj", "P2"),
nested(["F2", "F1"], ["F3", "F2"], ["P1", "F3"]),
).replace("Global\r\nEndGlobal\r\n", "");
expect(solutionFolders(text, false, "/s")).toEqual([
{ path: ["src"], files: ["/s/README.md"], projects: [] },
{ path: ["src", "libs"], files: [], projects: [] },
{ path: ["src", "libs", "core"], files: ["/s/.editorconfig"], projects: ["/s/src/libs/core/Core.csproj"] },
]);
});
it("keeps same-named folders under different parents apart", () => {
const text = sln(
folder("a", "F1"),
folder("b", "F2"),
folder("shared", "F3"),
folder("shared", "F4"),
nested(["F3", "F1"], ["F4", "F2"]),
);
expect(solutionFolders(text, false, "/s").map((f) => f.path.join("/"))).toEqual(["a", "b", "a/shared", "b/shared"]);
});
it("distinguishes a literal slash in a solution folder name from nesting", () => {
const text = sln(
folder("a/b", "F1", ["literal.txt"]),
folder("a", "F2"),
folder("b", "F3", ["nested.txt"]),
nested(["F3", "F2"]),
);
const folders = solutionFolders(text, false, "/s");
expect(folders.find((f) => f.path.length === 1 && f.path[0] === "a/b")?.files).toEqual(["/s/literal.txt"]);
expect(folders.find((f) => f.path.length === 2)?.files).toEqual(["/s/nested.txt"]);
});
it("resolves projects outside the solution folder, with spaces and 中文", () => {
const text = sln(
folder("测试 folder", "F1"),
project("库", "..\\其它 dir\\库 Lib\\库.csproj", "P1"),
nested(["P1", "F1"]),
);
expect(solutionFolders(text, false, "/work/sln")).toEqual([
{ path: ["测试 folder"], files: [], projects: ["/work/其它 dir/库 Lib/库.csproj"] },
]);
});
it("ignores a NestedProjects cycle", () => {
const text = sln(folder("a", "F1"), folder("b", "F2"), nested(["F1", "F2"], ["F2", "F1"]));
expect(solutionFolders(text, false, "/s")).toEqual([]);
});
it("accepts lowercase GUIDs and a project whose file is missing on disk", () => {
const text = sln(folder("f", "aa11"), project("Gone", "Gone\\Gone.csproj", "bb22"), nested(["bb22", "aa11"]));
expect(solutionFolders(text, false, "/s")[0]!.projects).toEqual(["/s/Gone/Gone.csproj"]);
});
});
describe("solutionFolders (.slnx)", () => {
it("reads nested Folder names, files and projects", () => {
const text = `<Solution>
<Folder Name="/Solution Items/">
<File Path="README.md" />
</Folder>
<Folder Name="/src/libs/core/">
<Project Path="src/Core/Core.csproj" />
<File Path="src/notes 中文.txt" />
</Folder>
<Project Path="App/App.csproj" />
</Solution>`;
expect(solutionFolders(text, true, "/s")).toEqual([
{ path: ["Solution Items"], files: ["/s/README.md"], projects: [] },
{ path: ["src", "libs", "core"], files: ["/s/src/notes 中文.txt"], projects: ["/s/src/Core/Core.csproj"] },
]);
});
it("returns nothing for an empty solution", () => {
expect(solutionFolders("<Solution />", true, "/s")).toEqual([]);
});
});
// CI installs the SDK (setup-dotnet), so a missing one there must fail, not skip.
const hasDotnet = (() => {
try {
execFileSync("dotnet", ["--version"], { stdio: "ignore" });
return true;
} catch {
return false;
}
})();
describe.each(["sln", "slnx"] as const)("folders written by the real dotnet CLI (.%s)", (format) => {
it.skipIf(!hasDotnet && !process.env.CI)("round-trips `dotnet sln add --solution-folder`", () => {
const dir = mkdtempSync(join(tmpdir(), "dotnet-folders-"));
try {
mkdirSync(join(dir, "Core"));
writeFileSync(
join(dir, "Core/Core.csproj"),
'<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><TargetFramework>net9.0</TargetFramework></PropertyGroup></Project>',
);
execFileSync("dotnet", ["new", "sln", "-n", "App", "--format", format, "-o", dir], { stdio: "ignore" });
execFileSync(
"dotnet",
["sln", `${dir}/App.${format}`, "add", `${dir}/Core/Core.csproj`, "--solution-folder", "src/libs"],
{
stdio: "ignore",
},
);
const text = readFileSync(`${dir}/App.${format}`, "utf8");
const found = solutionFolders(text, format === "slnx", dir);
const leaf = found.find((f) => f.path.join("/") === "src/libs")!;
expect(leaf.projects).toEqual([`${dir}/Core/Core.csproj`]);
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
});