Public: anyone can read this; the crew can change it.

Dependency manifests beyond package.json list their dependencies as a table: go.mod, Cargo.toml, requirements.txt, pyproject.toml #14583

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/14552-dependency-manifests-beyond-package-json → main 10 commits fork 1 thread · 1 unresolved

  • Merged. scout/14552-dependency-manifests-beyond-package-json was merged into main as 46f3a52.

Review

Sign in to review this change.
10 files changed +391 −1
docs/features/file-kinds.md +1
@@ -57,6 +57,7 @@ The forge draws a file on its page when its kind is below and it is within the s
5757 - **Env example**, drawn up to 1 MB: `.env.example`, `.env.sample`, `.env.template`
5858 - **OpenAPI**, drawn up to 1 MB: `openapi.json`, `openapi.yaml`, `openapi.yml`, `*.openapi.*`
5959 - **package.json**, drawn up to 1 MB: `package.json`
60 - **Dependency manifest**, drawn up to 1 MB: `go.mod`, `Cargo.toml`, `pyproject.toml`, `requirements*.txt`
6061 - **Makefile**, drawn up to 1 MB: `Makefile`, `GNUmakefile`, `*.mk`
6162 - **Dockerfile**, drawn up to 1 MB: `Dockerfile`, `Containerfile`, `*.Dockerfile`
6263 - **GitHub workflow**, drawn up to 1 MB: `.github/workflows/*.yml`, `.github/workflows/*.yaml`
scripts/kinds-guide.ts +1 −1
@@ -16,7 +16,7 @@ export const NAMES: Readonly<Record<FileKind, string>> = {
1616 geojson: "GeoJSON", kml: "KML", calendar: "Calendar", otio: "OpenTimelineIO", edl: "Edit list", fcpxml: "Final Cut XML",
1717 contacts: "Contacts", mail: "Email", gcode: "G-code", dxf: "DXF drawing", gerber: "Gerber layer", mermaid: "Mermaid diagram", drawio: "draw.io diagram",
1818 excalidraw: "Excalidraw", step: "STEP model", sarif: "SARIF report", lcov: "Coverage (lcov)", junit: "JUnit report",
19 opml: "OPML", feed: "RSS or Atom feed", env: "Env example", openapi: "OpenAPI", package: "package.json", makefile: "Makefile",
19 opml: "OPML", feed: "RSS or Atom feed", env: "Env example", openapi: "OpenAPI", package: "package.json", manifest: "Dependency manifest", makefile: "Makefile",
2020 dockerfile: "Dockerfile", workflow: "GitHub workflow", compose: "Compose file", sql: "SQL", graphql: "GraphQL",
2121 epub: "EPUB book", mbox: "Mailbox", proto: "Protocol Buffers", ini: "INI or properties", rtf: "Rich text", xml: "XML", text: "Other text", binary: "Other binary",
2222 };
src/tui/screens/workspace.ts +3
@@ -95,6 +95,7 @@ import { sheetView } from "./workspace_sheet.ts";
9595 import { sqliteStep, sqliteView, type SqliteAt } from "./workspace_sqlite.ts";
9696 import { officeView } from "./workspace_office.ts";
9797 import { parquetView } from "./workspace_parquet.ts";
98 import { depsView } from "./workspace_deps.ts";
9899 import { layoutTable, type Grid } from "../ui/grid.ts";
99100 import type { Ctx, Screen } from "../app/app.ts";
100101 import { stripAnsi } from "../term/ansi.ts";
@@ -1572,6 +1573,8 @@ export function workspaceScreen(
15721573 }
15731574 } else {
15741575 raw = read.value.body; // .html included: source here, the page preview is web-only
1576 const deps = depsView(t.path, read.value.body);
1577 if (deps !== null && read.value.partial !== true) sheet = deps;
15751578 // A big JSON file arrives cut: its first level is drawn, as the page draws it.
15761579 if (read.value.partial === true && isJsonPath(t.path)) {
15771580 raw = new TextEncoder().encode(jsonOutlineText(read.value.body, read.value.size, downloadAt(t.path)));
src/tui/screens/workspace_deps.ts added +24
@@ -0,0 +1,24 @@
1 /**
2 * A dependency manifest (`go.mod`, `Cargo.toml`, `requirements.txt`, `pyproject.toml`) in the
3 * terminal workspace, read the way the page reads it: the same `readManifest`, drawn as one table
4 * with a row per entry, the section it came from first. A file that cannot be read answers
5 * `null` and the workspace keeps showing its source.
6 */
7
8 import { manifestFormat, readManifest } from "../../web/render/deps-manifest.ts";
9 import type { Table } from "../../text/csv.ts";
10
11 /** A cell from the file, for a terminal: control characters become a replacement mark. */
12 const plain = (s: string): string => s.replace(/[\u0000-\u001f\u007f-\u009f]/g, "�").slice(0, 200);
13
14 export function depsView(path: string, body: Uint8Array): Table | null {
15 if (manifestFormat(path) === undefined) return null;
16 const read = readManifest(path, new TextDecoder().decode(body));
17 if (!read.ok || read.doc.sections.length === 0) return null;
18 const { sections } = read.doc;
19 const width = Math.max(...sections.map((s) => s.columns.length));
20 const header = ["Section", ...Array.from({ length: width }, (_, i) => sections.find((s) => s.columns.length > i)!.columns[i]!)];
21 const rows = sections.flatMap((s) => s.rows.map((r) => [plain(s.title), ...r.map(plain)]));
22 const more = sections.reduce((a, s) => a + (s.total - s.rows.length), 0);
23 return { header, rows, more, delimiter: ",", columns: header.length };
24 }
src/web/render/deps-manifest.ts added +228
@@ -0,0 +1,228 @@
1 /**
2 * A dependency manifest beyond `package.json`, drawn as tables: `go.mod`,
3 * `Cargo.toml`, `requirements.txt` and `pyproject.toml`.
4 * Pure over the text, one small reader per file; a file one cannot read comes
5 * back with a one-line reason and the page keeps its source view.
6 * Cargo and pyproject go through the TOML reader `toml-config.ts` already has.
7 * Every cell is escaped by the template; at most `ROW_CAP` rows per section.
8 */
9
10 import { html, type Html } from "../html.ts";
11 import { fileSize } from "../ui.ts";
12 import type { Json } from "./json-doc.ts";
13 import type { Rendered, RenderInput } from "./renderers.ts";
14 import { manifestFormat } from "./kinds.ts";
15 import { parseToml } from "./toml-config.ts";
16
17 export { manifestFormat };
18 export const ROW_CAP = 500;
19
20 export interface DepSection {
21 readonly title: string;
22 readonly columns: readonly string[];
23 readonly rows: readonly (readonly string[])[];
24 /** Entries in the file, drawn or not. */
25 readonly total: number;
26 }
27
28 export interface DepDoc {
29 readonly title: string;
30 /** The one-line facts: `go 1.22`, `edition 2021`. */
31 readonly facts: readonly string[];
32 readonly sections: readonly DepSection[];
33 }
34
35 export type DepRead = { readonly ok: true; readonly doc: DepDoc } | { readonly ok: false; readonly reason: string };
36
37 const section = (title: string, columns: readonly string[], all: readonly (readonly string[])[]): DepSection | undefined =>
38 all.length === 0 ? undefined : { title, columns, rows: all.slice(0, ROW_CAP), total: all.length };
39 const some = (xs: readonly (DepSection | undefined)[]): DepSection[] => xs.filter((s): s is DepSection => s !== undefined);
40 const ok = (title: string, facts: string[], sections: readonly (DepSection | undefined)[]): DepRead => ({
41 ok: true,
42 doc: { title, facts, sections: some(sections) },
43 });
44
45 /** `// indirect` and any other trailing comment, split from the code. */
46 const goSplit = (line: string): [string, string] => {
47 let quote = "";
48 let i = -1;
49 for (let k = 0; k < line.length; k++) {
50 const c = line[k]!;
51 if (quote !== "") {
52 if (c === quote) quote = "";
53 } else if (c === '"' || c === "`") quote = c;
54 else if (c === "/" && line[k + 1] === "/") {
55 i = k;
56 break;
57 }
58 }
59 return i < 0 ? [line.trim(), ""] : [line.slice(0, i).trim(), line.slice(i + 2).trim()];
60 };
61
62 export function parseGoMod(text: string): DepRead {
63 let module = "";
64 let go = "";
65 let toolchain = "";
66 const req: string[][] = [];
67 let block = "";
68 const lines = text.replace(/^/, "").split(/\r?\n/);
69 for (const [n, raw] of lines.entries()) {
70 const [line, comment] = goSplit(raw);
71 if (line === "") continue;
72 const unquote = (s: string): string => s.replace(/^["`](.*)["`]$/, "$1");
73 if (block !== "") {
74 if (line === ")") {
75 block = "";
76 continue;
77 }
78 if (block === "require") {
79 const m = /^(\S+)\s+(\S+)$/.exec(line);
80 if (!m) return { ok: false, reason: `Line ${n + 1} is not a require entry.` };
81 req.push([unquote(m[1]!), m[2]!, /^indirect\b/.test(comment) ? "indirect" : ""]);
82 }
83 continue;
84 }
85 const m = /^(\w+)\s*(.*)$/.exec(line);
86 if (!m) return { ok: false, reason: `Line ${n + 1} is not a go.mod directive.` };
87 const [, word, rest] = m as unknown as [string, string, string];
88 if (rest === "(") {
89 block = word;
90 } else if (word === "module") module = unquote(rest);
91 else if (word === "go") go = rest;
92 else if (word === "toolchain") toolchain = rest;
93 else if (word === "require") {
94 const r = /^(\S+)\s+(\S+)$/.exec(rest);
95 if (!r) return { ok: false, reason: `Line ${n + 1} is not a require entry.` };
96 req.push([unquote(r[1]!), r[2]!, /^indirect\b/.test(comment) ? "indirect" : ""]);
97 }
98 }
99 if (block !== "") return { ok: false, reason: `The ${block} block is never closed.` };
100 if (module === "") return { ok: false, reason: "No module line." };
101 const facts = [go !== "" ? `go ${go}` : "", toolchain].filter((f) => f !== "");
102 return ok(module, facts, [section("Requires", ["Module", "Version", "Note"], req)]);
103 }
104
105 const asObject = (j: Json | undefined): readonly (readonly [string, Json])[] | undefined => (j?.t === "object" ? j.entries : undefined);
106 const get = (j: Json | undefined, k: string): Json | undefined => asObject(j)?.find(([n]) => n === k)?.[1];
107 const text = (j: Json | undefined): string => (j?.t === "string" || j?.t === "number" ? j.v : "");
108
109 /** A Cargo dependency's one-cell spec: its version, else where it comes from. */
110 function cargoSpec(v: Json): string {
111 if (v.t === "string") return v.v;
112 const version = text(get(v, "version"));
113 const from = ["path", "git", "registry"].map((k) => [k, text(get(v, k))] as const).find(([, s]) => s !== "");
114 const ws = get(v, "workspace");
115 return [version, ws?.t === "boolean" && ws.v ? "workspace" : "", from ? `${from[0]} ${from[1]}` : ""].filter((s) => s !== "").join(" · ");
116 }
117
118 const cargoRows = (j: Json | undefined): string[][] => (asObject(j) ?? []).map(([k, v]) => [k, cargoSpec(v)]);
119
120 export function parseCargo(source: string): DepRead {
121 const t = parseToml(source);
122 if (!t.ok) return { ok: false, reason: `Not valid TOML: ${t.error.message} (line ${t.error.line}).` };
123 const pkg = get(t.value, "package");
124 const name = text(get(pkg, "name"));
125 const deps = cargoRows(get(t.value, "dependencies"));
126 const dev = cargoRows(get(t.value, "dev-dependencies"));
127 const build = cargoRows(get(t.value, "build-dependencies"));
128 if (name === "" && deps.length + dev.length + build.length === 0)
129 return { ok: false, reason: "No [package] name and no dependencies." };
130 const version = text(get(pkg, "version"));
131 const edition = text(get(pkg, "edition"));
132 const cols = ["Name", "Version"];
133 return ok(
134 name === "" ? "Cargo.toml" : version === "" ? name : `${name} ${version}`,
135 edition === "" ? [] : [`edition ${edition}`],
136 [section("Dependencies", cols, deps), section("Dev dependencies", cols, dev), section("Build dependencies", cols, build)],
137 );
138 }
139
140 /** A PEP 508 requirement split into name (with extras) and the rest. */
141 const REQ = /^([A-Za-z0-9][A-Za-z0-9._-]*(?:\[[^\]]*\])?)\s*(.*)$/;
142
143 export function parseRequirements(source: string): DepRead {
144 const rows: string[][] = [];
145 const lines = source.replace(/^/, "").replace(/\\\r?\n/g, " ").split(/\r?\n/);
146 for (const [n, raw] of lines.entries()) {
147 const line = raw.trim();
148 if (line === "") continue;
149 if (line.startsWith("#")) continue;
150 const code = line.replace(/\s+#.*$/, "");
151 const inc = /^(-r|-c|--requirement|--constraint)\s*=?\s*(\S+)$/.exec(code);
152 if (inc) {
153 rows.push([inc[2]!, "", inc[1] === "-c" || inc[1] === "--constraint" ? "constraint" : "include"]);
154 continue;
155 }
156 if (code.startsWith("-")) {
157 rows.push([code, "", "option"]);
158 continue;
159 }
160 if (/^(\.{0,2}\/|[A-Za-z][\w+.-]*:\/\/|file:)/.test(code)) {
161 rows.push([code, "", "url"]);
162 continue;
163 }
164 const m = REQ.exec(code);
165 if (!m || (m[2] !== "" && !/^[<>=!~;@(]/.test(m[2]!))) return { ok: false, reason: `Line ${n + 1} is not a requirement.` };
166 const [spec, marker = ""] = m[2]!.split(/\s*;\s*/, 2);
167 rows.push([m[1]!, spec!.replace(/^\((.*)\)$/, "$1"), marker]);
168 }
169 const reqs = rows.filter((r) => !["include", "constraint", "option"].includes(r[2]!)).length;
170 if (reqs === 0) return { ok: false, reason: "Nothing to list." };
171 const doc = section("Requirements", ["Requirement", "Specifier", "Note"], rows)!;
172 return { ok: true, doc: { title: "requirements", facts: [`${reqs} ${reqs === 1 ? "requirement" : "requirements"}`], sections: [doc] } };
173 }
174
175 const strings = (j: Json | undefined): string[][] =>
176 j?.t === "array" ? j.items.filter((i) => i.t === "string").map((i) => splitReq(text(i))) : [];
177 function splitReq(s: string): string[] {
178 const m = REQ.exec(s.trim());
179 return m ? [m[1]!, m[2]!] : [s, ""];
180 }
181
182 export function parsePyproject(source: string): DepRead {
183 const t = parseToml(source);
184 if (!t.ok) return { ok: false, reason: `Not valid TOML: ${t.error.message} (line ${t.error.line}).` };
185 const project = get(t.value, "project");
186 if (project === undefined) return { ok: false, reason: "No [project] table." };
187 const name = text(get(project, "name"));
188 const version = text(get(project, "version"));
189 const python = text(get(project, "requires-python"));
190 const cols = ["Name", "Specifier"];
191 const groups = (asObject(get(project, "optional-dependencies")) ?? []).map(([g, v]) =>
192 section(`Optional: ${g}`, cols, strings(v)),
193 );
194 return ok(
195 name === "" ? "pyproject.toml" : version === "" ? name : `${name} ${version}`,
196 python === "" ? [] : [`python ${python}`],
197 [section("Dependencies", cols, strings(get(project, "dependencies"))), ...groups],
198 );
199 }
200
201 export function readManifest(path: string, source: string): DepRead {
202 switch (manifestFormat(path)) {
203 case "go":
204 return parseGoMod(source);
205 case "cargo":
206 return parseCargo(source);
207 case "requirements":
208 return parseRequirements(source);
209 case "pyproject":
210 return parsePyproject(source);
211 default:
212 return { ok: false, reason: "Not a dependency manifest." };
213 }
214 }
215
216 export function depsManifest(input: RenderInput): Rendered {
217 const read = readManifest(input.path, new TextDecoder().decode(input.body));
218 if (!read.ok) return { content: html``, lines: 0, fallback: read.reason };
219 const { doc } = read;
outdated

Principle 7 and accessibility: comment rows have r[0] empty, so this emits an empty row header. Put the text in the first cell or move comments out of the table.

220 const blocks: Html[] = doc.sections.map((s) => {
221 const rows = s.rows.map((r) => html`<tr><th scope="row" class="break">${r[0]!}</th>${r.slice(1).map((c) => html`<td><code class="break">${c}</code></td>`)}</tr>`);
222 const more = s.total - s.rows.length;
223 return html`<h3>${s.title}</h3><div class="table-scroll"><table class="table" data-dense aria-label="${s.title}"><thead><tr>${s.columns.map((c) => html`<th scope="col">${c}</th>`)}</tr></thead><tbody>${rows}</tbody></table></div>${more > 0 ? html`<p class="muted small">${more} more not shown</p>` : html``}`;
224 });
225 const content = html`<div class="package-view"><h2>${doc.title}</h2>${doc.facts.length > 0 ? html`<p class="muted small">${doc.facts.join(" · ")}</p>` : html``}${blocks.length > 0 ? blocks : html`<p class="muted">No dependencies listed.</p>`}</div>`;
226 const n = doc.sections.reduce((a, s) => a + s.total, 0);
227 return { content, lines: 0, summary: `${n} ${n === 1 ? "entry" : "entries"} · ${fileSize(input.size)}` };
228 }
src/web/render/kinds.ts +19
@@ -63,6 +63,7 @@ export type FileKind =
6363 | "env"
6464 | "openapi"
6565 | "package"
66 | "manifest"
6667 | "makefile"
6768 | "dockerfile"
6869 | "workflow"
@@ -148,6 +149,7 @@ export const RENDER_CAP: Readonly<Record<FileKind, number>> = {
148149 env: 1 * MB,
149150 openapi: 1 * MB,
150151 package: 1 * MB,
152 manifest: 1 * MB,
151153 makefile: 1 * MB,
152154 dockerfile: 1 * MB,
153155 workflow: 1 * MB,
@@ -329,6 +331,20 @@ const isOpenApiName = (path: string): boolean => /(^|\/|\.)openapi\.(json|ya?ml)
329331 /** A manifest is named exactly `package.json`; `package-lock.json` and the rest stay config. */
330332 const isPackageName = (path: string): boolean => /(^|\/)package\.json$/.test(path);
331333
334 /** `go.mod`, `Cargo.toml`, `requirements*.txt` and `pyproject.toml`, by name. */
335 export type ManifestFormat = "go" | "cargo" | "requirements" | "pyproject";
336
337 /** Which manifest reader a path takes, by its file name; undefined for any other file. */
338 export function manifestFormat(path: string): ManifestFormat | undefined {
339 const name = path.slice(path.lastIndexOf("/") + 1);
340 if (name === "go.mod") return "go";
341 if (name === "Cargo.toml") return "cargo";
342 if (name === "pyproject.toml") return "pyproject";
343 if (/^requirements([-_.][\w.-]+)?\.txt$/.test(name)) return "requirements";
344 return undefined;
345 }
346 const isManifestName = (path: string): boolean => manifestFormat(path) !== undefined;
347
332348 /** `Makefile`, `GNUmakefile` and `*.mk`, by name. */
333349 const isMakefileName = (path: string): boolean => /(^|\/)(makefile|gnumakefile|[^/]+\.mk)$/i.test(path);
334350 /** `Dockerfile`, `Containerfile` and `*.Dockerfile`, in any case. */
@@ -488,6 +504,7 @@ export function fileKind(path: string, head: Uint8Array): FileKind {
488504 if (xmlFeed !== undefined) return xmlFeed;
489505 if (isOpenApiName(path)) return "openapi";
490506 if (isPackageName(path)) return "package";
507 if (isManifestName(path)) return "manifest";
491508 if (isMakefileName(path)) return "makefile";
492509 if (isDockerfileName(path)) return "dockerfile";
493510 if (isWorkflowName(path)) return "workflow";
@@ -519,6 +536,7 @@ export function kindByName(path: string): FileKind {
519536 if (isEnvExample(path)) return "env";
520537 if (isOpenApiName(path)) return "openapi";
521538 if (isPackageName(path)) return "package";
539 if (isManifestName(path)) return "manifest";
522540 if (isMakefileName(path)) return "makefile";
523541 if (isDockerfileName(path)) return "dockerfile";
524542 if (isWorkflowName(path)) return "workflow";
@@ -603,6 +621,7 @@ const BY_NAME: Readonly<Partial<Record<FileKind, readonly string[]>>> = {
603621 env: [".env.example", ".env.sample", ".env.template"],
604622 openapi: ["openapi.json", "openapi.yaml", "openapi.yml", "*.openapi.*"],
605623 package: ["package.json"],
624 manifest: ["go.mod", "Cargo.toml", "pyproject.toml", "requirements*.txt"],
606625 makefile: ["Makefile", "GNUmakefile", "*.mk"],
607626 dockerfile: ["Dockerfile", "Containerfile", "*.Dockerfile"],
608627 workflow: [".github/workflows/*.yml", ".github/workflows/*.yaml"],
src/web/render/renderers.ts +2
@@ -46,6 +46,7 @@ import { makefile } from "./makefile.ts";
4646 import { markdown } from "./markdown.ts";
4747 import { model3d } from "./model3d.ts";
4848 import { notebook } from "./notebook.ts";
49 import { depsManifest } from "./deps-manifest.ts";
4950 import { packageJson, readPackage } from "./package-json.ts";
5051 import { patch } from "./patch.ts";
5152 import { psd } from "./psd.ts";
@@ -171,6 +172,7 @@ export const RENDERERS: Partial<Readonly<Record<FileKind, Renderer>>> = {
171172 xml,
172173 openapi,
173174 package: packageJson,
175 manifest: depsManifest,
174176 makefile,
175177 dockerfile,
176178 workflow,
test/fuzz-renderers.test.ts +4
@@ -168,6 +168,10 @@ export const SAMPLES: Readonly<Record<string, { path: string; files: readonly st
168168 path: "settings.toml",
169169 files: ['name = "x"\nport = 8080\n[nested]\nurl = "javascript:alert(1)"\n'],
170170 },
171 manifest: {
172 path: "go.mod",
173 files: ['module example.test/x\n\ngo 1.22\n\nrequire (\n\tgithub.com/a/b v1.2.3 // indirect\n\t"><script>alert(1)</script> v0.0.1\n)\n'],
174 },
171175 markdown: {
172176 path: "docs/README.md",
173177 files: [
test/render-deps-manifest.test.ts added +93
@@ -0,0 +1,93 @@
1 import { describe, expect, test } from "bun:test";
2 import { depsManifest, parseGoMod, parseRequirements } from "../src/web/render/deps-manifest.ts";
3 import { fileKind, kindByName } from "../src/web/render/kinds.ts";
4 import { RENDERERS } from "../src/web/render/renderers.ts";
5
6 const enc = (s: string) => new TextEncoder().encode(s);
7 const draw = (path: string, text: string) => {
8 const body = enc(text);
9 const r = depsManifest({ path, size: body.length, body, media: "", download: "" });
10 return { html: String(r.content), summary: r.summary, fallback: r.fallback };
11 };
12
13 describe("dependency manifests", () => {
14 test("the registry names them, and they win over plain config", () => {
15 for (const p of ["go.mod", "svc/Cargo.toml", "pyproject.toml", "requirements.txt", "a/requirements-dev.txt"]) {
16 expect(fileKind(p, enc("x"))).toBe("manifest");
17 expect(kindByName(p)).toBe("manifest");
18 }
19 expect(fileKind("notes.txt", enc("x"))).toBe("text");
20 expect(fileKind("Cargo.lock", enc("x"))).not.toBe("manifest");
21 expect(RENDERERS.manifest).toBe(depsManifest);
22 });
23
24 test("go.mod: module, go version, requires with indirect", () => {
25 const src = `module example.com/<b>app\n\ngo 1.22\n\nrequire github.com/a/b v1.2.3\nrequire (\n\tgolang.org/x/text v0.14.0 // indirect\n\tgithub.com/c/d v0.1.0\n)\n`;
26 const r = draw("go.mod", src);
27 expect(r.fallback).toBeUndefined();
28 expect(r.html).toContain("example.com/&lt;b&gt;app");
29 expect(r.html).toContain("go 1.22");
30 expect(r.html).toContain("github.com/a/b");
31 expect(r.html).toContain("v0.14.0");
32 expect(r.html).toContain("indirect");
33 expect(r.summary).toContain("3 entries");
34 });
35
36 test("Cargo.toml: package and the dependency tables", () => {
37 const src = `[package]\nname = "demo"\nversion = "0.3.0"\nedition = "2021"\n[dependencies]\nserde = "1.0"\ntokio = { version = "1", features = ["full"] }\nlocal = { path = "../local" }\n[dev-dependencies]\ncriterion = "0.5"\n`;
38 const r = draw("Cargo.toml", src);
39 expect(r.fallback).toBeUndefined();
40 expect(r.html).toContain("demo 0.3.0");
41 expect(r.html).toContain("edition 2021");
42 expect(r.html).toContain("Dev dependencies");
43 expect(r.html).toContain("criterion");
44 expect(r.html).toContain("path ../local");
45 expect(r.html).toContain("tokio");
46 });
47
48 test("requirements.txt: specifiers, comments and -r lines as such", () => {
49 const src = `# base <i>\n-r other.txt\nrequests[socks]>=2.0,<3 ; python_version>"3"\nflask==2.3.1 # web\nnumpy\n`;
50 expect(parseRequirements(src).ok).toBe(true);
51 const r = draw("requirements.txt", src);
52 expect(r.html).not.toContain("base");
53 expect(r.html).not.toContain('<th scope="row" class="break"></th>');
54 expect(r.html).toContain("other.txt");
55 expect(r.html).toContain("include");
56 expect(r.html).toContain("requests[socks]");
57 expect(r.html).toContain("&gt;=2.0,&lt;3");
58 expect(r.html).toContain("==2.3.1");
59 expect(r.html).not.toContain("# web");
60 });
61
62 test("pyproject.toml: project, dependencies and optional groups", () => {
63 const src = `[project]\nname = "pkg"\nversion = "1.0"\nrequires-python = ">=3.10"\ndependencies = ["httpx>=0.27", "rich"]\n[project.optional-dependencies]\ndev = ["pytest>=8"]\n`;
64 const r = draw("pyproject.toml", src);
65 expect(r.fallback).toBeUndefined();
66 expect(r.html).toContain("pkg 1.0");
67 expect(r.html).toContain("python &gt;=3.10");
68 expect(r.html).toContain("httpx");
69 expect(r.html).toContain("Optional: dev");
70 expect(r.html).toContain("pytest");
71 });
72
73 test("malformed input falls back with one plain reason", () => {
74 expect(draw("go.mod", "module m\nrequire (\n a v1\n").fallback).toBe("The require block is never closed.");
75 expect(parseGoMod("go 1.22\n").ok).toBe(false);
76 expect(draw("Cargo.toml", "[package\nname=").fallback).toContain("Not valid TOML");
77 expect(draw("pyproject.toml", "[tool.x]\na = 1\n").fallback).toBe("No [project] table.");
78 expect(draw("requirements.txt", "ok==1\n!!! nonsense\n").fallback).toBe("Line 2 is not a requirement.");
79 });
80
81 test("requirements.txt with nothing to list falls back; a lone URL or path is a row", () => {
82 expect(draw("requirements.txt", "# only\n-r other.txt\n--index-url https://x\n").fallback).toBe("Nothing to list.");
83 const r = draw("requirements.txt", "https://host/pkg.whl\n./local\n");
84 expect(r.fallback).toBeUndefined();
85 expect(r.html).toContain("https://host/pkg.whl");
86 expect(r.html).toContain("./local");
87 });
88
89 test("go.mod: // inside a quoted path is not a comment", () => {
90 const r = parseGoMod('module "ex.com/a//b"\nrequire x.com/y v1.0.0 // indirect\n');
91 expect(r.ok && r.doc.title).toBe("ex.com/a//b");
92 });
93 });
test/workspace-deps.test.ts added +16
@@ -0,0 +1,16 @@
1 import { expect, test } from "bun:test";
2 import { depsView } from "../src/tui/screens/workspace_deps.ts";
3
4 const enc = (s: string) => new TextEncoder().encode(s);
5
6 test("a go.mod is a table of its requirements", () => {
7 const t = depsView("go.mod", enc("module x\n\ngo 1.22\n\nrequire github.com/a/b v1.2.3\n"));
8 expect(t).not.toBeNull();
9 expect(t!.header[0]).toBe("Section");
10 expect(t!.rows.some((r) => r.includes("github.com/a/b"))).toBe(true);
11 });
12
13 test("a file that is not a manifest, or has no entries, stays source", () => {
14 expect(depsView("notes.txt", enc("hello"))).toBeNull();
15 expect(depsView("requirements.txt", enc(""))).toBeNull();
16 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs