Порівняти коміти

...
Автор SHA1 Повідомлення Дата
l0ng-ai 19ff82668e fix(run): make test report export paths easier to edit 2026-11-22 22:17:56 +08:00
5 змінених файлів з 198 додано та 58 видалено

No files matched your search

+51
Переглянути файл
@@ -581,3 +581,54 @@ test("CMake builds the selected target in Build before starting only the applica
await expect(run.getByRole("tab", { name: "hello [Release, release]" })).toBeVisible();
await expect(run.getByText("COMPILER_OUTPUT", { exact: true })).toHaveCount(0);
});
test("test report export separates the format and destination in a narrow window", async ({ workbench: page }) => {
await page.setViewportSize({ width: 410, height: 500 });
await stubRun(
page,
[
{
kind: "stdout",
text: '{"Package":"example.com/checkout","Action":"pass","Test":"TestReceipt","Elapsed":0.1}\n',
},
],
0,
{ id: "go:report-tests", name: "Checkout tests", kind: "go", command: "go", args: ["test", "-json"], isTest: true },
);
await page.evaluate(() => {
window.__ipc.handlers.fs_entry_kind = () => {
throw { kind: "NotFound", message: "Not found" };
};
window.__ipc.handlers.fs_create_file_bytes = () => null;
});
await page.keyboard.press("Control+r");
const results = page.getByRole("toolbar", { name: "Test results" });
await results.getByRole("button", { name: "More Actions" }).click();
const exportItem = page.getByRole("menuitem", { name: "Export Test Results…" });
await expect(exportItem).toBeEnabled();
await exportItem.click();
const dialog = page.getByRole("dialog", { name: "Export Test Results" });
const html = dialog.getByRole("radio", { name: "HTML", exact: true });
const xml = dialog.getByRole("radio", { name: "XML", exact: true });
await expect(html).toBeVisible();
expect((await xml.boundingBox())!.y).toBeGreaterThan((await html.boundingBox())!.y);
const file = dialog.getByLabel("File name", { exact: true });
const folder = dialog.getByLabel("Folder", { exact: true });
const browse = dialog.getByRole("button", { name: "Browse…" });
await expect(file).toBeFocused();
await expect(file).toHaveValue("Test Results - Checkout tests.html");
await expect(folder).toHaveValue("/dev/acme-api");
await expect(browse).toBeInViewport({ ratio: 1 });
expect((await folder.boundingBox())!.width).toBeGreaterThan(180);
await file.fill("receipts.html");
await folder.fill("reports");
await xml.check();
await expect(file).toHaveValue("receipts.xml");
await expect(dialog.getByRole("checkbox", { name: "Open exported file in browser" })).toBeDisabled();
await dialog.getByRole("button", { name: "OK", exact: true }).click();
await expect(dialog).toBeHidden();
const [write] = await ipcCalls(page, "fs_create_file_bytes");
expect(write).toMatchObject({ path: "/dev/acme-api/reports/receipts.xml" });
});
+35 -19
Переглянути файл
@@ -1,7 +1,7 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
import { commands } from "@/platform/ipc";
import { ExportTestResultsDialog, defaultExportPath, loadExportOptions } from "./ExportTestResultsDialog";
import { ExportTestResultsDialog, defaultExportName, loadExportOptions } from "./ExportTestResultsDialog";
import { TestResults } from "./testing";
const actions = vi.hoisted(() => ({ executeAction: vi.fn(async () => {}) }));
@@ -56,13 +56,15 @@ function open(onClose = vi.fn()) {
return onClose;
}
const file = () => screen.getByLabelText("Output file");
const file = () => screen.getByLabelText("File name");
const folder = () => screen.getByLabelText("Folder");
const press = (name: string) => fireEvent.click(screen.getByRole("button", { name }));
describe("ExportTestResultsDialog", () => {
it("writes an HTML report to the project folder under IDEA's name and opens it in the browser", async () => {
it("writes an HTML report to the project folder and opens it in the browser", async () => {
const onClose = open();
expect(file()).toHaveValue("/work/app/Test Results - go test _calc.html");
expect(file()).toHaveValue("Test Results - go test _calc.html");
expect(folder()).toHaveValue("/work/app/");
expect(screen.getByRole("radio", { name: "HTML" })).toBeChecked();
press("OK");
await waitFor(() => expect(onClose).toHaveBeenCalled());
@@ -92,7 +94,7 @@ describe("ExportTestResultsDialog", () => {
it("switches the extension with the format, keeps XML out of the browser, and remembers the choice", async () => {
const onClose = open();
fireEvent.click(screen.getByRole("radio", { name: "XML" }));
expect(file()).toHaveValue("/work/app/Test Results - go test _calc.xml");
expect(file()).toHaveValue("Test Results - go test _calc.xml");
// Only an HTML report opens in the browser.
expect(screen.getByRole("checkbox", { name: "Open exported file in browser" })).toBeDisabled();
press("OK");
@@ -143,18 +145,36 @@ describe("ExportTestResultsDialog", () => {
});
it("doesn't ask again for a file the save dialog already confirmed", async () => {
onDisk["/tmp/picked.html"] = "file";
dialog.save.mockResolvedValueOnce("/tmp/picked.html");
const path = "C:\\reports\\picked.html";
onDisk[path] = "file";
dialog.save.mockResolvedValueOnce(path);
const onClose = open();
press("Browse…");
await waitFor(() => expect(file()).toHaveValue("/tmp/picked.html"));
await waitFor(() => expect(file()).toHaveValue("picked.html"));
expect(folder()).toHaveValue("C:\\reports\\");
expect(dialog.save).toHaveBeenCalledWith(
expect.objectContaining({ filters: [{ name: "HTML", extensions: ["html"] }] }),
expect.objectContaining({
defaultPath: "/work/app/Test Results - go test _calc.html",
filters: [{ name: "HTML", extensions: ["html"] }],
}),
);
press("OK");
await waitFor(() => expect(onClose).toHaveBeenCalled());
expect(messages.confirm).not.toHaveBeenCalled();
expect(written.map((w) => w.path)).toEqual(["/tmp/picked.html"]);
expect(written.map((w) => w.path)).toEqual([path]);
});
it("confirms again when the destination changes after using the save dialog", async () => {
dialog.save.mockResolvedValueOnce("/tmp/picked.html");
onDisk["/work/app/reports/picked.html"] = "file";
messages.confirm.mockResolvedValueOnce(false);
open();
press("Browse…");
await waitFor(() => expect(file()).toHaveValue("picked.html"));
fireEvent.change(folder(), { target: { value: "reports" } });
press("OK");
await waitFor(() => expect(messages.confirm).toHaveBeenCalledOnce());
expect(written).toHaveLength(0);
});
it("reports a directory or a failed write and stays open", async () => {
@@ -187,15 +207,11 @@ describe("ExportTestResultsDialog", () => {
});
});
describe("defaultExportPath", () => {
describe("defaultExportName", () => {
it("replaces characters file systems refuse in the configuration's name", () => {
expect(defaultExportPath("C:\\proj\\", 'a/b\\c:d*e?f"g<h>i|j', "xml")).toBe(
"C:\\proj/Test Results - a_b_c_d_e_f_g_h_i_j.xml",
);
expect(defaultExportPath("/p", "go test ./...", "html")).toBe("/p/Test Results - go test.html");
expect(defaultExportPath("/p", "Tests in 'com.acme.Cart'", "html")).toBe(
"/p/Test Results - Tests in 'com_acme_Cart'.html",
);
expect(defaultExportPath("/p", "...", "html")).toBe("/p/Test Results - Tests.html");
expect(defaultExportName('a/b\\c:d*e?f"g<h>i|j', "xml")).toBe("Test Results - a_b_c_d_e_f_g_h_i_j.xml");
expect(defaultExportName("go test ./...", "html")).toBe("Test Results - go test.html");
expect(defaultExportName("Tests in 'com.acme.Cart'", "html")).toBe("Test Results - Tests in 'com_acme_Cart'.html");
expect(defaultExportName("...", "html")).toBe("Test Results - Tests.html");
});
});
+75 -33
Переглянути файл
@@ -2,7 +2,7 @@
* IDEA's Export Test Results dialog: HTML or XML, the output file, and whether to
* open an HTML report in the browser once it is written.
*/
import { useState } from "react";
import { useId, useState } from "react";
import { executeAction } from "@/platform/actions";
import { tx } from "@/platform/i18n";
import { call, commands } from "@/platform/ipc";
@@ -38,13 +38,35 @@ function saveExportOptions(o: ExportOptions) {
}
}
/**
* IDEA's default name, `Test Results - <configuration>.html`. Like IDEA's `PathUtil.suggestFileName`, runs of
* dots and characters file systems refuse become one `_` (`go test ./...` gives `go test`).
*/
export function defaultExportPath(dir: string, name: string, format: TestExportFormat): string {
/** Runs of dots and characters file systems refuse become one `_`. */
export function defaultExportName(name: string, format: TestExportFormat): string {
const safe = name.replace(/[/\\:*?"<>|.]+/g, "_").replace(/^[\s_]+|[\s_]+$/g, "");
return `${dir.replace(/[\\/]+$/, "")}/Test Results - ${safe || "Tests"}.${format}`;
return `Test Results - ${safe || "Tests"}.${format}`;
}
interface Destination {
folder: string;
fileName: string;
/** The native picker already asked before replacing this exact path. */
pickedPath?: string;
}
function destinationFromPicker(path: string): Destination {
const slash = path.lastIndexOf("/");
const posix = path.startsWith("/") && !path.startsWith("//");
const end = (posix ? slash : Math.max(slash, path.lastIndexOf("\\"))) + 1;
return { folder: path.slice(0, end), fileName: path.slice(end), pickedPath: path };
}
function resolveDestination({ folder, fileName, pickedPath }: Destination, cwd: string): string {
if (pickedPath) return pickedPath;
const file = fileName.trim();
if (!file || isAbsolutePath(file)) return file;
const dir = folder.trim();
const posix = dir.startsWith("/") && !dir.startsWith("//");
const base = dir.replace(posix ? /\/+$/ : /[\\/]+$/, "");
const path = dir ? `${base}/${file}` : file;
return isAbsolutePath(path) ? path : `${cwd.replace(/[\\/]+$/, "")}/${path}`;
}
async function exists(path: string): Promise<"file" | "dir" | null> {
@@ -111,44 +133,47 @@ export function ExportTestResultsDialog({
cwd: string;
onClose: () => void;
}) {
const fileNameId = useId();
const folderId = useId();
const [options, setOptions] = useState(loadExportOptions);
const [path, setPath] = useState(() => defaultExportPath(cwd, name, options.format));
// The save dialog already asked before replacing this file.
const [picked, setPicked] = useState<string | null>(null);
const [destination, setDestination] = useState<Destination>(() => ({
folder: cwd,
fileName: defaultExportName(name, options.format),
}));
const [busy, setBusy] = useState(false);
const html = options.format === "html";
const { folder, fileName, pickedPath } = destination;
const target = resolveDestination(destination, cwd);
const update = (next: Partial<ExportOptions>) => {
const merged = { ...options, ...next };
setOptions(merged);
saveExportOptions(merged);
// The file follows the format's extension, as in the Export Data dialog.
if (next.format) setPath((p) => p.replace(/\.(html|xml)$/i, `.${next.format}`));
if (next.format) {
const nextName = fileName.replace(/\.(html|xml)$/i, `.${next.format}`);
if (nextName !== fileName) setDestination({ folder, fileName: nextName });
}
};
const browse = async () => {
try {
const { save } = await import("@/platform/dialog");
const chosen = await save({
defaultPath: path,
defaultPath: target || folder || cwd,
filters: [{ name: html ? "HTML" : "XML", extensions: [options.format] }],
});
if (chosen) {
setPath(chosen);
setPicked(chosen);
}
if (chosen) setDestination(destinationFromPicker(chosen));
} catch {
// No dialog (tests): keep typing the path.
}
};
const run = async () => {
const raw = path.trim();
if (!raw) return;
const target = isAbsolutePath(raw) ? raw : `${cwd.replace(/[\\/]+$/, "")}/${raw}`;
if (!target) return;
setBusy(true);
const text = exportTestResults(options.format, results, { name, cwd, date: new Date() });
const ok = await writeTestReport(target, text, picked === raw);
const ok = await writeTestReport(target, text, pickedPath !== undefined);
setBusy(false);
if (!ok) return;
onClose();
@@ -167,29 +192,46 @@ export function ExportTestResultsDialog({
open
title="Export Test Results"
onClose={onClose}
width={520}
width={500}
buttons={[
{ label: "OK", role: "default", disabled: busy || !path.trim(), onClick: () => void run() },
{ label: "OK", role: "default", disabled: busy || !target, onClick: () => void run() },
{ label: "Cancel", role: "cancel" },
]}
>
<div className={styles.dialogBody}>
<div className={styles.exportFormats} role="radiogroup" aria-label="Export format">
<span>
<div className={styles.exportReport}>
<fieldset className={styles.exportFormats} role="radiogroup">
<legend>
<T>Export format:</T>
</span>
</legend>
<Radio name="test-export-format" label="HTML" checked={html} onChange={() => update({ format: "html" })} />
<Radio name="test-export-format" label="XML" checked={!html} onChange={() => update({ format: "xml" })} />
</div>
<label className={styles.exportField}>
<span>
<T>Output file:</T>
</span>
</fieldset>
<fieldset className={styles.exportOutput}>
<legend>
<T>Output</T>
</legend>
<label htmlFor={fileNameId}>
<T>File name</T>
</label>
<Input
id={fileNameId}
autoSelect
data-autofocus=""
value={fileName}
onChange={(e) => setDestination({ folder, fileName: e.target.value })}
/>
<label htmlFor={folderId}>
<T>Folder</T>
</label>
<div className={styles.inputWithButton}>
<Input mono aria-label="Output file" value={path} onChange={(e) => setPath(e.target.value)} />
<Input
id={folderId}
value={folder}
onChange={(e) => setDestination({ folder: e.target.value, fileName })}
/>
<IconButton icon="folder" title="Browse…" onClick={() => void browse()} />
</div>
</label>
</fieldset>
<Checkbox
checked={html && options.openInBrowser}
disabled={!html}
+35 -5
Переглянути файл
@@ -1134,18 +1134,48 @@
gap: var(--space-2);
padding: 12px 16px;
}
.exportFormats {
.exportReport {
display: flex;
align-items: center;
gap: var(--space-4);
flex-direction: column;
gap: var(--space-3);
padding: var(--space-3) var(--space-4) var(--space-1);
font-size: var(--fs-ui);
}
.exportField {
.exportFormats,
.exportOutput {
min-inline-size: 0;
margin: 0;
padding: 0;
border: 0;
}
.exportReport legend {
margin-block-end: var(--space-2);
padding: 0;
color: var(--fg-strong);
font-weight: 600;
}
.exportFormats {
display: grid;
grid-template-columns: auto minmax(0, 1fr);
justify-items: start;
gap: var(--space-1);
}
.exportOutput {
display: grid;
grid-template-columns: max-content minmax(0, 1fr);
align-items: center;
gap: var(--space-2);
}
.exportOutput > label {
max-inline-size: 120px;
overflow-wrap: anywhere;
color: var(--fg);
}
/* Folded console lines ("<N internal lines>"). */
.fold {
color: var(--fg-fold-placeholder);
+2 -1
Переглянути файл
@@ -375,7 +375,8 @@ describe("Export Test Results", () => {
fireEvent.click(screen.getByRole("button", { name: "Cancel" }));
act(() => testTree.exportResults!());
expect(screen.getByRole("dialog", { name: "Export Test Results" })).toBeInTheDocument();
expect(screen.getByLabelText("Output file")).toHaveValue("/p/Test Results - calc tests.html");
expect(screen.getByLabelText("File name")).toHaveValue("Test Results - calc tests.html");
expect(screen.getByLabelText("Folder")).toHaveValue("/p");
fireEvent.click(screen.getByRole("button", { name: "Cancel" }));
expect(screen.queryByRole("dialog", { name: "Export Test Results" })).toBeNull();
unmount();