diff --git a/src/features/dotnet/store.test.ts b/src/features/dotnet/store.test.ts index 8651c7b3c..2206bb4dc 100644 --- a/src/features/dotnet/store.test.ts +++ b/src/features/dotnet/store.test.ts @@ -1,6 +1,16 @@ -import { afterEach, describe, expect, it, vi } from "vitest"; -import { cancelDotnet, discoveredTests, packageList, selectDotnet, resetDotnet, useDotnet } from "./store"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { + buildDotnet, + cancelDotnet, + discoveredTests, + packageList, + reloadDotnet, + resetDotnet, + selectDotnet, + useDotnet, +} from "./store"; import * as platform from "@/platform/dotnet"; +import { useProject } from "@/platform/project"; afterEach(() => { resetDotnet(); @@ -59,6 +69,93 @@ it("ignores an evaluation completed after cancellation", async () => { expect(useDotnet.getState().error).toBe(""); }); +describe("reloading a .NET workspace", () => { + const path = "/repo/Test.csproj"; + const project: platform.DotnetProject = { + path, + name: "Test", + frameworks: ["net8.0", "net9.0"], + configurations: ["Debug", "Release"], + references: [], + packages: [], + test: true, + runner: "vstest", + adapter: "mstest", + trx: false, + }; + const previousProject = useProject.getState().project; + + beforeEach(() => { + useProject.setState({ project: { root: "/repo" } as NonNullable }); + useDotnet.setState({ selected: path, configuration: "Release", framework: "net9.0" }); + }); + + afterEach(() => useProject.setState({ project: previousProject })); + + it("preserves the project, configuration and framework and evaluates that selection", async () => { + const first = { ...project, path: "/repo/A.csproj" }; + const release = { ...project, frameworks: ["net10.0", "net9.0"] }; + const selected = { ...release, packages: [{ id: "ReleaseOnly", version: "1.0" }] }; + vi.spyOn(platform, "dotnetWorkspace").mockResolvedValue({ projects: [first, project], solutions: [] }); + const evaluate = vi + .spyOn(platform, "evaluateProject") + .mockImplementation(async (_path, _config, framework) => (framework === "net9.0" ? selected : release)); + const command = vi.spyOn(platform, "dotnet").mockResolvedValue(""); + + await reloadDotnet(); + + expect(useDotnet.getState()).toMatchObject({ + selected: path, + configuration: "Release", + framework: "net9.0", + model: { projects: [first, selected] }, + }); + expect(evaluate).toHaveBeenLastCalledWith(path, "Release", "net9.0", expect.any(AbortSignal)); + await buildDotnet("build"); + expect(command).toHaveBeenCalledWith( + ["build", path, "--configuration", "Release", "--framework", "net9.0"], + "/repo", + expect.any(AbortSignal), + ); + }); + + it("uses a supported configuration and framework when the saved choices were removed", async () => { + const updated = { ...project, configurations: ["Profile"], frameworks: ["net10.0"] }; + vi.spyOn(platform, "dotnetWorkspace").mockResolvedValue({ projects: [updated], solutions: [] }); + const evaluate = vi.spyOn(platform, "evaluateProject").mockResolvedValue(updated); + + await reloadDotnet(); + + expect(useDotnet.getState()).toMatchObject({ selected: path, configuration: "Profile", framework: "net10.0" }); + expect(evaluate).toHaveBeenCalledWith(path, "Profile", "", expect.any(AbortSignal)); + }); + + it("keeps a supported configuration when its previously selected framework is removed", async () => { + const release = { ...project, frameworks: ["net10.0"] }; + vi.spyOn(platform, "dotnetWorkspace").mockResolvedValue({ projects: [project], solutions: [] }); + vi.spyOn(platform, "evaluateProject").mockResolvedValue(release); + + await reloadDotnet(); + + expect(useDotnet.getState()).toMatchObject({ selected: path, configuration: "Release", framework: "net10.0" }); + }); + + it("uses the remaining project's defaults when the selected project is removed", async () => { + const remaining = { ...project, path: "/repo/Remaining.csproj" }; + vi.spyOn(platform, "dotnetWorkspace").mockResolvedValue({ projects: [remaining], solutions: [] }); + const evaluate = vi.spyOn(platform, "evaluateProject"); + + await reloadDotnet(); + + expect(useDotnet.getState()).toMatchObject({ + selected: remaining.path, + configuration: "Debug", + framework: "net8.0", + }); + expect(evaluate).not.toHaveBeenCalled(); + }); +}); + describe("real dotnet discovery output", () => { it.each([ "The following Tests are available:\n Passes\n Parameterized (1)\n\nProcess finished with exit code 0", diff --git a/src/features/dotnet/store.ts b/src/features/dotnet/store.ts index 6062ce3c3..041002b6d 100644 --- a/src/features/dotnet/store.ts +++ b/src/features/dotnet/store.ts @@ -80,15 +80,31 @@ export function cancelDotnet() { export function reloadDotnet() { const root = useProject.getState().project?.root; if (!root) return; + const old = useDotnet.getState(); return task(async (signal) => { const model = await dotnetWorkspace(root, signal); - const old = useDotnet.getState(); - const project = model.projects.find((p) => p.path === old.selected) ?? model.projects[0]; + let project = model.projects.find((p) => p.path === old.selected) ?? model.projects[0]; + const sameProject = project?.path === old.selected; + const configurations = project?.configurations ?? []; + const defaultConfiguration = configurations.includes(initial.configuration) + ? initial.configuration + : (configurations[0] ?? initial.configuration); + const configuration = + sameProject && configurations.includes(old.configuration) ? old.configuration : defaultConfiguration; + + // The workspace scan evaluates Debug and the first framework for each project. + if (project && configuration !== initial.configuration) + project = await evaluateProject(project.path, configuration, "", signal); + const framework = + sameProject && project?.frameworks.includes(old.framework) ? old.framework : (project?.frameworks[0] ?? ""); + if (project && framework && framework !== project.frameworks[0]) + project = await evaluateProject(project.path, configuration, framework, signal); + return { - model, + model: { ...model, projects: model.projects.map((item) => (item.path === project?.path ? project : item)) }, selected: project?.path ?? "", - configuration: "Debug", - framework: project?.frameworks[0] ?? "", + configuration, + framework, packages: [], found: [], tests: [],