diff --git a/e2e/nugetAudit.spec.ts b/e2e/nugetAudit.spec.ts new file mode 100644 index 000000000..3da6fd31b --- /dev/null +++ b/e2e/nugetAudit.spec.ts @@ -0,0 +1,165 @@ +/** + * The package audit in the NuGet Packages window and the Problems view (#7439) over a stubbed + * `dotnet` host (`dotnetHost.ts`): the spec checks the markers, the Problems entries and the exact + * commands it starts; `src/features/dotnet/audit.cli.test.ts` runs the real CLI against a local v3 feed. + */ +import type { Page } from "@playwright/test"; +import { expect, test } from "./fixtures"; +import { dotnetRuns, installDotnetHost, openNuget, PROJECT, shot } from "./dotnetHost"; + +const PROPS = "/dev/acme/Directory.Packages.props"; +const CSPROJ = ` + + + + +`; +const PROPS_TEXT = ` + + + + +`; + +async function stubAudit(page: Page, central: boolean) { + await installDotnetHost(page); + await page.evaluate( + ({ central, csproj, propsText, project, propsPath }) => { + window.__ipc.handlers.fs_read_text = (args) => { + if (args.path === project) return central ? csproj.replace(/ Version="1.0.0"/g, "") : csproj; + if (args.path === propsPath) return propsText; + throw { kind: "notFound", message: String(args.path) }; + }; + const json = (value: unknown) => JSON.stringify(value); + const framework = (extra: object) => json({ projects: [{ frameworks: [{ framework: "net9.0", ...extra }] }] }); + const demo = { id: "Demo.Pkg", requestedVersion: "1.0.0", resolvedVersion: "1.0.0" }; + window.__dotnetOut = (args) => { + if (args[0] === "msbuild") { + if (!args.some((a) => a.includes("ManagePackageVersionsCentrally"))) + return json({ Properties: { MSBuildProjectName: "App", TargetFramework: "net9.0" }, Items: {} }); + return json({ + Properties: { ManagePackageVersionsCentrally: central ? "true" : "" }, + Items: { + PackageVersion: [ + { Identity: "Demo.Pkg", Version: "1.0.0", DefiningProjectFullPath: propsPath }, + { Identity: "Old.Pkg", Version: "1.0.0", DefiningProjectFullPath: propsPath }, + ], + PackageReference: [], + }, + }); + } + if (args[0] !== "list") return "ok\n"; + if (args.includes("--vulnerable")) + return framework({ + topLevelPackages: [ + { + ...demo, + vulnerabilities: [{ severity: "High", advisoryurl: "https://github.com/advisories/GHSA-demo" }], + }, + ], + transitivePackages: [ + { + id: "Child.Pkg", + resolvedVersion: "2.0.0", + vulnerabilities: [{ severity: "Moderate", advisoryurl: "https://github.com/advisories/GHSA-child" }], + }, + ], + }); + if (args.includes("--deprecated")) + return framework({ + topLevelPackages: [ + { + id: "Old.Pkg", + requestedVersion: "1.0.0", + resolvedVersion: "1.0.0", + deprecationReasons: ["Legacy"], + alternativePackage: { id: "New.Pkg", versionRange: ">= 0.0.0" }, + }, + ], + }); + if (args.includes("--outdated")) return framework({ topLevelPackages: [{ ...demo, latestVersion: "1.1.0" }] }); + return framework({ + topLevelPackages: [demo, { id: "Old.Pkg", requestedVersion: "1.0.0", resolvedVersion: "1.0.0" }], + transitivePackages: [{ id: "Child.Pkg", resolvedVersion: "2.0.0" }], + }); + }; + }, + { central, csproj: CSPROJ, propsText: PROPS_TEXT, project: PROJECT, propsPath: PROPS }, + ); + await openNuget(page); + if (central) + await page.evaluate(async () => { + const central = "/src/features/dotnet/central.ts"; + const { refreshCentral } = await import(/* @vite-ignore */ central); + await refreshCentral(); + }); +} + +const row = (page: Page, id: string) => page.getByRole("listitem").filter({ hasText: id }).last(); + +async function problems(page: Page) { + await page.getByRole("button", { name: "Problems", exact: true }).click(); + const pane = page.locator('[data-toolwindow="problems"]'); + await pane.getByRole("tab", { name: /Project Errors/ }).click(); + return pane.getByRole("tree", { name: "Problems" }); +} + +test("Audit marks vulnerable, deprecated and outdated packages with their severity, advisory and alternative", async ({ + workbench: page, +}) => { + await stubAudit(page, false); + await page.getByRole("button", { name: "Audit packages" }).click(); + await expect(row(page, "Demo.Pkg").getByRole("link", { name: "Vulnerable (High)" })).toHaveAttribute( + "href", + "https://github.com/advisories/GHSA-demo", + ); + await expect(row(page, "Child.Pkg").getByRole("link", { name: "Vulnerable (Moderate)" })).toBeVisible(); + await expect(row(page, "Old.Pkg")).toContainText("Deprecated: Legacy, use New.Pkg"); + await expect(row(page, "Demo.Pkg")).toContainText("Newer version available: 1.1.0"); + await shot(page, "d3-markers", row(page, "Demo.Pkg")); + // One command per filter, vulnerabilities with transitive packages, then the outdated check. + const runs = (await dotnetRuns(page)).filter((a) => a[0] === "list" && a.some((x) => /^--(vuln|depr|outd)/.test(x))); + expect(runs.map((a) => a.filter((x) => x.startsWith("--") && x !== "--format").join(" "))).toEqual([ + "--vulnerable --include-transitive", + "--deprecated", + "--outdated", + ]); +}); + +test("Problems lists the audit at the PackageReference lines of the project", async ({ workbench: page }) => { + await stubAudit(page, false); + await page.getByRole("button", { name: "Audit packages" }).click(); + await expect(row(page, "Demo.Pkg").getByRole("link", { name: "Vulnerable (High)" })).toBeVisible(); + const tree = await problems(page); + await expect(tree.getByText(/Package Demo\.Pkg 1\.0\.0 has a known High severity vulnerability/)).toBeVisible(); + await expect(tree).toContainText("App.csproj"); + await expect(tree).toContainText("GHSA-demo:3"); + if (process.env.E2E_SHOTS) + await page.screenshot({ path: `${process.env.E2E_SHOTS}/d3-problems.png`, animations: "disabled" }); +}); + +test("under central package management Problems points at Directory.Packages.props", async ({ workbench: page }) => { + await stubAudit(page, true); + await page.getByRole("button", { name: "Audit packages" }).click(); + await expect(row(page, "Demo.Pkg").getByRole("link", { name: "Vulnerable (High)" })).toBeVisible(); + const tree = await problems(page); + await expect(tree).toContainText("Directory.Packages.props"); + await expect(tree.getByText(/Package Demo\.Pkg 1\.0\.0 has a known High severity vulnerability/)).toBeVisible(); + // Line 3 of the props file (the Demo.Pkg PackageVersion); the unpinned transitive package stays on the project. + await expect(tree).toContainText("GHSA-demo:3"); + await expect(tree).toContainText("Directory.Packages.props1 error"); +}); + +test("a failing audit shows the CLI message and no markers", async ({ workbench: page }) => { + await stubAudit(page, false); + await page.evaluate(() => { + const ok = window.__dotnetOut; + window.__dotnetOut = (args) => { + if (args.includes("--vulnerable")) throw new Error("error: Unable to load the service index for source"); + return ok(args); + }; + }); + await page.getByRole("button", { name: "Audit packages" }).click(); + await expect(page.getByRole("alert")).toContainText("Unable to load the service index"); + await expect(page.getByRole("link", { name: /Vulnerable/ })).toHaveCount(0); +}); diff --git a/src/features/dotnet/Nuget.module.css b/src/features/dotnet/Nuget.module.css index 7de2fff02..123c641e9 100644 --- a/src/features/dotnet/Nuget.module.css +++ b/src/features/dotnet/Nuget.module.css @@ -53,3 +53,6 @@ flex-direction: row; align-items: center; } +.advisory { + color: var(--danger); +} diff --git a/src/features/dotnet/NugetView.tsx b/src/features/dotnet/NugetView.tsx index 828cf0158..577519822 100644 --- a/src/features/dotnet/NugetView.tsx +++ b/src/features/dotnet/NugetView.tsx @@ -6,7 +6,9 @@ import { useEffect, useState } from "react"; import { executeAction } from "@/platform/actions"; import { useTx } from "@/platform/i18n"; +import { isOpenable, openExternalUrl } from "@/platform/openUrl"; import { Button, Input } from "@/ui"; +import { auditPackageKey, useAudit } from "./audit"; import { compareVersions, useCentral } from "./central"; import { ensureDotnetModel, useDotnet } from "./store"; import { SourcesPanel } from "./SourcesPanel"; @@ -25,7 +27,9 @@ export function NugetView() { const matches = central.project === s.selected && central.framework === s.framework && central.configuration === s.configuration; const info = matches ? central.info : null; - const working = s.busy || central.busy; + const audit = useAudit(); + const findings = audit.results[s.selected]; + const working = s.busy || central.busy || audit.busy; const knownLatest = (p: { id: string; version: string }) => { const latest = matches ? central.latest[p.id.toLowerCase()] : undefined; return latest && compareVersions(latest, p.version) > 0 ? latest : undefined; @@ -35,10 +39,15 @@ export function NugetView() { const override = info?.overrides[key]; const file = baseName(info?.versions[key]?.file ?? ""); const latest = knownLatest(p); + const deprecation = findings?.deprecated[auditPackageKey(p.id, p.version)]; + const reasons = deprecation?.reasons.join(", "); + const alternative = deprecation?.alternative; + const replacement = reasons && alternative ? tx`${reasons}, use ${alternative}` : reasons; return [ override ? tx`Version override ${override} in the project` : "", !override && file ? (p.transitive ? tx`Pinned in ${file}` : tx`Defined in ${file}`) : "", latest ? tx`Newer version available: ${latest}` : "", + replacement ? tx`Deprecated: ${replacement}` : "", ] .filter(Boolean) .join(" · "); @@ -71,8 +80,16 @@ export function NugetView() { /> {tx("Include prereleases")} + {working && } + {audit.error && ( +
+ {audit.error} +
+ )} {matches && central.error && (
{central.error} @@ -150,12 +167,26 @@ export function NugetView() {