дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-06 18:17:15 +02:00
Import a Coverage Report now reads coverage.py's .coverage data file; choosing one .coverage.* file of a parallel run imports all of them. The data only records executed lines and arcs, so coverage.py in the project's interpreter combines it into a scratch file (combine --keep, the user's files untouched) and writes Cobertura XML (xml -i) for the existing importer, run in the project directory so [paths] and excluded lines apply. Running the interpreter needs a trusted project; without trust, an interpreter or coverage.py the error says so and points to coverage xml output. Files without source in the project are listed without lines and the notification says to map them with [paths]. Closes #6392 Co-authored-by: wildcatDownstairs <[email protected]>
187 рядки
6.9 KiB
TypeScript
187 рядки
6.9 KiB
TypeScript
/**
|
|
* Coverage reports collected elsewhere: "Import a report collected in CI from
|
|
* disk" (lcov, JaCoCo or Cobertura XML, Go cover profile, SimpleCov, coverage.py data) and "Manage Coverage Reports"
|
|
* (choose the shown suite, delete suites and remembered imports).
|
|
*/
|
|
import { useMemo, useState } from "react";
|
|
import { create } from "zustand";
|
|
import { choosePath } from "@/platform/choosePath";
|
|
import { tx } from "@/platform/i18n";
|
|
import { call, commands, isTauri } from "@/platform/ipc";
|
|
import { basename } from "@/platform/project";
|
|
import { Dialog, Icon, List, T } from "@/ui";
|
|
import { notifications, toolWindowService } from "@/workbench/contributions";
|
|
import { formatPercent } from "./model";
|
|
import {
|
|
addSuite,
|
|
captureCoverageProject,
|
|
closeSuite,
|
|
forgetImport,
|
|
importedReports,
|
|
rememberImport,
|
|
reportEntries,
|
|
selectSuite,
|
|
useCoverage,
|
|
type ReportEntry,
|
|
type Suite,
|
|
} from "./store";
|
|
import styles from "./Coverage.module.css";
|
|
|
|
const errorText = (e: unknown) => (e instanceof Error ? e.message : String(e));
|
|
|
|
async function pickReport(): Promise<string | null> {
|
|
return choosePath({
|
|
directory: false,
|
|
title: "Import Coverage Report",
|
|
filters: [
|
|
{
|
|
name: "Coverage reports (lcov, JaCoCo XML, Go cover profile)",
|
|
extensions: ["info", "lcov", "xml", "out", "txt", "cov", "coverprofile", "json"],
|
|
},
|
|
{ name: "All files", extensions: ["*"] },
|
|
],
|
|
});
|
|
}
|
|
|
|
/** Says how many files of an import are not in the project; coverage.py data maps them with `[paths]`. */
|
|
function outsideText(outside: number, runner: string): string | undefined {
|
|
if (!outside) return undefined;
|
|
if (runner === "coverage.py")
|
|
return outside === 1
|
|
? "1 file of the data has no source in this project. Map it with [paths] in the coverage.py configuration."
|
|
: tx`${outside} files of the data have no source in this project. Map them with [paths] in the coverage.py configuration.`;
|
|
return outside === 1
|
|
? "1 file of the report is not in this project."
|
|
: tx`${outside} files of the report are not in this project.`;
|
|
}
|
|
|
|
/** Imports `path` (asks for a file without one) and shows it like a run's suite. */
|
|
export async function importReport(path?: string): Promise<Suite | null> {
|
|
if (!isTauri()) {
|
|
notifications().notify({ group: "run", level: "warning", title: "Importing coverage needs the desktop app" });
|
|
return null;
|
|
}
|
|
const project = captureCoverageProject();
|
|
if (!project) return null;
|
|
const file = path ?? (await pickReport());
|
|
if (!file || !project.isCurrent()) return null;
|
|
try {
|
|
const data = await call(commands.coverageImport(file));
|
|
const name = basename(file);
|
|
const suite = project.isCurrent() ? addSuite(name, data, file) : null;
|
|
rememberImport(project.root, { path: file, name, importedAt: suite?.createdAt ?? Date.now() });
|
|
if (!suite) return null;
|
|
try {
|
|
toolWindowService().show("coverage");
|
|
} catch {
|
|
/* no workbench (tests) */
|
|
}
|
|
const [covered, total] = data.files.reduce((a, f) => [a[0]! + f.linesCovered, a[1]! + f.linesTotal], [0, 0]);
|
|
// Paths the backend could not map into the project keep the machine's they were recorded on.
|
|
const prefix = project.root.replace(/\/$/, "") + "/";
|
|
const outside = data.files.filter((f) => !f.path.startsWith(prefix)).length;
|
|
notifications().notify({
|
|
group: "run",
|
|
level: outside ? "warning" : "info",
|
|
key: "coverage-import",
|
|
title: `Imported coverage '${suite.name}': ${formatPercent(covered!, total!)} lines`,
|
|
body: !data.files.length ? "The report covers no files of this project." : outsideText(outside, data.runner),
|
|
});
|
|
return suite;
|
|
} catch (e) {
|
|
if (!project.isCurrent()) return null;
|
|
notifications().notify({
|
|
group: "run",
|
|
level: "error",
|
|
key: "coverage-import",
|
|
title: `Cannot import ${basename(file)}`,
|
|
body: errorText(e),
|
|
});
|
|
return null;
|
|
}
|
|
}
|
|
|
|
const useManageDialog = create<{ open: boolean }>(() => ({ open: false }));
|
|
|
|
export const openManageReports = () => useManageDialog.setState({ open: true });
|
|
export const closeManageReports = () => useManageDialog.setState({ open: false });
|
|
|
|
export function ManageReportsOverlay() {
|
|
const open = useManageDialog((s) => s.open);
|
|
return open ? <ManageReportsDialog onClose={closeManageReports} /> : null;
|
|
}
|
|
|
|
function ManageReportsDialog({ onClose }: { onClose: () => void }) {
|
|
const suites = useCoverage((s) => s.suites);
|
|
const activeId = useCoverage((s) => s.activeId);
|
|
const [imported, setImported] = useState(() => importedReports());
|
|
const entries = useMemo(() => reportEntries(suites, imported), [suites, imported]);
|
|
const [active, setActive] = useState(() =>
|
|
Math.max(
|
|
0,
|
|
entries.findIndex((e) => e.suite?.id === activeId),
|
|
),
|
|
);
|
|
const current: ReportEntry | undefined = entries[Math.min(active, entries.length - 1)];
|
|
|
|
const show = async (entry: ReportEntry | undefined) => {
|
|
if (!entry) return;
|
|
if (entry.suite) selectSuite(entry.suite.id);
|
|
else if (entry.file && !(await importReport(entry.file))) return;
|
|
onClose();
|
|
};
|
|
const remove = (entry: ReportEntry | undefined) => {
|
|
if (!entry) return;
|
|
if (entry.suite) closeSuite(entry.suite.id);
|
|
if (entry.file) {
|
|
forgetImport(entry.file);
|
|
setImported(importedReports());
|
|
}
|
|
};
|
|
const importMore = async () => {
|
|
if (await importReport()) setImported(importedReports());
|
|
};
|
|
|
|
return (
|
|
<Dialog
|
|
open
|
|
title="Coverage Reports"
|
|
onClose={onClose}
|
|
width={560}
|
|
buttons={[
|
|
{ label: "Import…", role: "extra", onClick: () => void importMore() },
|
|
{ label: "Delete", role: "extra", disabled: !current, onClick: () => remove(current) },
|
|
{ label: "Show Selected", role: "default", disabled: !current, onClick: () => void show(current) },
|
|
{ label: "Cancel", role: "cancel" },
|
|
]}
|
|
>
|
|
<div className={styles.reports}>
|
|
<List<ReportEntry>
|
|
aria-label="Coverage reports"
|
|
items={entries}
|
|
getKey={(e) => e.key}
|
|
activeIndex={active}
|
|
onActiveChange={setActive}
|
|
onActivate={(e) => void show(e)}
|
|
renderItem={(e) => (
|
|
<span className={styles.reportRow}>
|
|
<Icon name={e.file ? "folder-open" : "run-coverage"} size={16} className={styles.icon} />
|
|
<span className={styles.label}>{e.name}</span>
|
|
<span className={styles.reportDetail}>{e.detail}</span>
|
|
<span className={styles.reportTime}>
|
|
{e.suite?.id === activeId ? <T>shown</T> : e.suite ? null : <T>not loaded</T>}{" "}
|
|
{new Date(e.time).toLocaleString()}
|
|
</span>
|
|
</span>
|
|
)}
|
|
empty={
|
|
<div className={styles.reportsEmpty}>
|
|
<T>No coverage reports. Run with coverage or import a report.</T>
|
|
</div>
|
|
}
|
|
/>
|
|
</div>
|
|
</Dialog>
|
|
);
|
|
}
|