/** * Package sources in the NuGet Packages window (#7420) over a stubbed `dotnet` host * (`dotnetHost.ts`): the spec checks what the window shows and the exact commands it starts; * `src/features/dotnet/sources.cli.test.ts` runs the real CLI. */ import { expect, test } from "./fixtures"; import { dotnetRuns, installDotnetHost, openNuget, shot } from "./dotnetHost"; const CONFIG = "/dev/acme/NuGet.Config"; const USER_CONFIG = "/Users/dev/.nuget/NuGet/NuGet.Config"; async function stubSources(page: Parameters[0]) { await installDotnetHost(page); await page.evaluate( ({ config, userConfig }) => { const sources = [ { name: "local", url: "/dev/acme/feed", enabled: true }, { name: "shared", url: "https://pkgs.example.com/v3/index.json", enabled: true }, // A password inside the URL: the window must not show it. { name: "private", url: "https://bob:s3cret-pw@pkgs.example.com/v3/index.json", enabled: false }, ]; window.__dotnetOut = (args) => { const [, verb, , name] = args; if (args[0] === "list") return '{"projects":[]}'; // Other windows of the NuGet view (central package state) ask MSBuild too. if (args[0] === "msbuild") return '{"Properties":{},"Items":{}}'; if (args[1] === "config") return `${config}\n${userConfig}\n`; if (verb === "list") { const target = args.at(-1); const listed = target === config ? sources.filter((s) => s.name !== "shared") : target === userConfig ? sources.filter((s) => s.name === "shared") : sources; return `Registered Sources:\n\n${listed .map((s, i) => ` ${i + 1}. ${s.name} [${s.enabled ? "Enabled" : "Disabled"}]\n ${s.url}`) .join("\n")}\n`; } if (verb === "add") sources.push({ name: args[args.indexOf("--name") + 1]!, url: args[3]!, enabled: true }); else if (verb === "remove") sources.splice( sources.findIndex((s) => s.name === name), 1, ); else sources.find((s) => s.name === name)!.enabled = verb === "enable"; return "ok\n"; }; }, { config: CONFIG, userConfig: USER_CONFIG }, ); await openNuget(page); await page.getByText("Package sources", { exact: true }).click(); } test("lists the effective sources, masks URL credentials and toggles a source in the workspace file", async ({ workbench: page, }) => { await stubSources(page); const list = page.getByRole("list", { name: "Package sources" }); await expect(list.getByText(/local/)).toBeVisible(); await expect(list.getByText(/private.*Disabled/)).toBeVisible(); await expect(list).toContainText("https://***@pkgs.example.com"); await expect(page.getByText("s3cret-pw")).toHaveCount(0); await expect(page.getByLabel("NuGet.Config file")).toHaveValue(CONFIG); await expect( list.getByRole("listitem").filter({ hasText: "shared" }).getByRole("button", { name: "Disable" }), ).toBeDisabled(); await shot(page, "d1-sources", list.getByText(/private/)); await list.getByRole("listitem").filter({ hasText: "private" }).getByRole("button", { name: "Enable" }).click(); await expect .poll(() => dotnetRuns(page)) .toContainEqual(["nuget", "enable", "source", "private", "--configfile", CONFIG]); await expect(list.getByText(/private.*Disabled/)).toHaveCount(0); await list.getByRole("listitem").filter({ hasText: "local" }).getByRole("button", { name: "Disable" }).click(); await expect .poll(() => dotnetRuns(page)) .toContainEqual(["nuget", "disable", "source", "local", "--configfile", CONFIG]); await expect(list.getByText(/local.*Disabled/)).toBeVisible(); }); test("adds and removes a source; the user-wide file is a labelled, explicit choice", async ({ workbench: page }) => { await stubSources(page); await page.getByLabel("Source name").fill("team"); await page.getByLabel("Source URL or folder").fill("https://nuget.team.example/v3/index.json"); await page.getByRole("button", { name: "Add source" }).click(); await expect .poll(() => dotnetRuns(page)) .toContainEqual([ "nuget", "add", "source", "https://nuget.team.example/v3/index.json", "--name", "team", "--configfile", CONFIG, ]); const list = page.getByRole("list", { name: "Package sources" }); await expect(list.getByText(/team/)).toBeVisible(); // The user-wide file is last, labelled, and warns once picked. await expect(page.getByRole("status")).toHaveCount(0); await page.getByLabel("NuGet.Config file").selectOption(USER_CONFIG); await expect(page.getByRole("status")).toContainText("every project"); await shot(page, "d1-user-wide", page.getByRole("status")); await expect( list.getByRole("listitem").filter({ hasText: "shared" }).getByRole("button", { name: "Disable" }), ).toBeEnabled(); await page.getByLabel("NuGet.Config file").selectOption(CONFIG); await list.getByRole("listitem").filter({ hasText: "team" }).getByRole("button", { name: "Remove" }).click(); await expect .poll(() => dotnetRuns(page)) .toContainEqual(["nuget", "remove", "source", "team", "--configfile", CONFIG]); await expect(list.getByText(/team/)).toHaveCount(0); }); test("a failing dotnet command shows its message and keeps the list", async ({ workbench: page }) => { await stubSources(page); await page.evaluate(() => { const ok = window.__dotnetOut; window.__dotnetOut = (args) => { if (args[2] === "source" && args[1] === "remove") throw new Error("error: Unable to find a source named 'nope'."); return ok(args); }; }); await page .getByRole("list", { name: "Package sources" }) .getByRole("listitem") .first() .getByRole("button", { name: "Remove" }) .click(); await expect(page.getByRole("alert")).toContainText("Unable to find a source"); await expect(page.getByRole("list", { name: "Package sources" }).getByRole("listitem")).toHaveCount(3); });