Skip to content

Commit c87025f

Browse files
committed
test: let the unbuilt schema fail in tsc instead of a pre-check
The library-examples test gained, in #456, an existsSync check before tsc ran: when a fence imported the package's settings.schema.json subpath and lib/settings.schema.json was absent, compileExamples rejected with a line naming bun run build:schema, through a textual import scan (importsModule) and a shared mint (schemaNotBuilt) in test/settings-schema.ts. The owner's rule: do not write a check or guard for something that fails on its own. A missing build output, a missing package, a wrong toolchain surface as failures when they happen and are fixed then; a pre-check that restates a failure the tool already produces is deleted. tsc's own TS2307 against the page line is that failure, so both files return to their content before #456. The refusal-messages registry rows and planted row from #456 stay. The same rule removes two more pre-checks of the same class: readSettingsSchema's own existsSync check and not-built message (readFileSync's ENOENT names the path) and refusalSources' existsSync check and census message (read() raises the same ENOENT, and the gone-source control asserts it). Every other existsSync under test/ and .github/scripts/ chooses a path, skips an optional input, or is the test's own assertion, and stays.
1 parent 96cf975 commit c87025f

3 files changed

Lines changed: 12 additions & 86 deletions

File tree

‎test/docs/library-examples.test.ts‎

Lines changed: 8 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
import { describe, expect, test } from "bun:test";
1010
import { spawnSync } from "node:child_process";
11-
import { existsSync, readFileSync, symlinkSync, writeFileSync } from "node:fs";
11+
import { readFileSync, symlinkSync, writeFileSync } from "node:fs";
1212
import { join, resolve } from "node:path";
1313
import { ROOT } from "../root.js";
14-
import { SETTINGS_SCHEMA_PATH, schemaNotBuilt } from "../settings-schema.js";
14+
import { SETTINGS_SCHEMA_PATH } from "../settings-schema.js";
1515
import { withTempDir } from "../temp-dir.js";
1616

1717
const PAGE = "docs/reference/library.md";
@@ -55,13 +55,9 @@ export function tsFences(markdown: string): Fence[] {
5555
* The fences as one module: only the two package paths are rewritten, on the
5656
* lines they already occupy, so a program line is a page line by offset alone
5757
* and nothing else about a fence is repaired before tsc sees it. The page
58-
* imports each name once, in the first example that uses it. `schema` is where
59-
* the package's schema subpath lands.
58+
* imports each name once, in the first example that uses it.
6059
*/
61-
export function examplesProgram(
62-
fences: readonly Fence[],
63-
schema: string,
64-
): {
60+
export function examplesProgram(fences: readonly Fence[]): {
6561
text: string;
6662
pageLine: (programLine: number) => number;
6763
} {
@@ -71,7 +67,7 @@ export function examplesProgram(
7167
for (const fence of fences) {
7268
starts.push(line);
7369
const body = fence.body
74-
.replaceAll(`"${PACKAGE}/settings.schema.json"`, JSON.stringify(schema))
70+
.replaceAll(`"${PACKAGE}/settings.schema.json"`, JSON.stringify(SETTINGS_SCHEMA_PATH))
7571
.replaceAll(`"${PACKAGE}"`, JSON.stringify(ENTRY));
7672
parts.push(body);
7773
line += body.split("\n").length;
@@ -96,39 +92,15 @@ export function examplesProgram(
9692
/** `tsc --pretty false` prints `<file>(<line>,<col>): error TS<code>: <message>`, continuation lines indented. */
9793
const DIAGNOSTIC = /^(.+?)\((\d+),(\d+)\): error (TS\d+): (.*)$/;
9894

99-
/**
100-
* Whether the program imports `module` (a static import, a re-export, a dynamic import, each spelled as the page
101-
* spells it: `from`, or `import` and an optional parenthesis, then the quoted path); a string value naming the path
102-
* is not a dependency.
103-
*/
104-
function importsModule(program: string, module: string): boolean {
105-
const quoted = JSON.stringify(module);
106-
for (let at = program.indexOf(quoted); at !== -1; at = program.indexOf(quoted, at + 1)) {
107-
if (/\b(?:from|import)\s*\(?\s*$/.test(program.slice(0, at))) {
108-
return true;
109-
}
110-
}
111-
return false;
112-
}
113-
11495
/**
11596
* Every diagnostic tsc reports for the page's fences, as `<label>:<page line>: TS<code>: <message>`.
11697
* A diagnostic outside the examples file keeps its own path. The compile runs in a temp project
11798
* that symlinks this checkout's node_modules, so `types` and the runtime dependencies resolve
118-
* exactly as they do for src/; the project is removed on every path. A fence that imports the
119-
* schema subpath needs the built schema, so its absence is named before tsc would misreport it as
120-
* a module the page cannot find.
99+
* exactly as they do for src/; the project is removed on every path.
121100
*/
122-
export function compileExamples(
123-
markdown: string,
124-
label: string,
125-
schema = SETTINGS_SCHEMA_PATH,
126-
): Promise<string[]> {
101+
export function compileExamples(markdown: string, label: string): Promise<string[]> {
127102
const fences = tsFences(markdown);
128-
const { text, pageLine } = examplesProgram(fences, schema);
129-
if (importsModule(text, schema) && !existsSync(schema)) {
130-
return Promise.reject(schemaNotBuilt(schema));
131-
}
103+
const { text, pageLine } = examplesProgram(fences);
132104
return withTempDir("gsac-library-examples-", (dir) => {
133105
symlinkSync(join(ROOT, "node_modules"), join(dir, "node_modules"), "dir");
134106
const examples = join(dir, "examples.ts");
@@ -261,31 +233,4 @@ describe(`${PAGE} examples`, () => {
261233
}
262234
},
263235
);
264-
265-
// The schema subpath resolves to a built file no checkout starts with; a page importing it names the build step,
266-
// and a page that only spells the subpath as a string value compiles without the file.
267-
test("a fence importing the schema subpath on an unbuilt checkout fails naming the build step, not a missing module", () =>
268-
withTempDir("gsac-library-unbuilt-", async (dir) => {
269-
const unbuilt = join(dir, "lib", "settings.schema.json");
270-
const importsSchema = [
271-
"```ts",
272-
'import schema from "@vivswan/github-settings-as-code/settings.schema.json" with { type: "json" };',
273-
"",
274-
"console.log(schema.$schema);",
275-
"```",
276-
"",
277-
].join("\n");
278-
await expect(compileExamples(importsSchema, "page.md", unbuilt)).rejects.toThrow(
279-
/lib\/settings\.schema\.json is not built; run `bun run build:schema`$/,
280-
);
281-
const noSchema = [
282-
"```ts",
283-
'import { SECTION_KEYS } from "@vivswan/github-settings-as-code";',
284-
"",
285-
'console.log(SECTION_KEYS.length, "@vivswan/github-settings-as-code/settings.schema.json");',
286-
"```",
287-
"",
288-
].join("\n");
289-
expect(await compileExamples(noSchema, "page.md", unbuilt)).toEqual([]);
290-
}));
291236
});

‎test/sections/refusal-messages.test.ts‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515

1616
import { describe, expect, test } from "bun:test";
17-
import { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from "node:fs";
17+
import { mkdirSync, readdirSync, readFileSync, writeFileSync } from "node:fs";
1818
import { join } from "node:path";
1919
import { walk as walkTree } from "estree-walker";
2020
import { parseSync } from "oxc-parser";
@@ -865,11 +865,6 @@ function read(root: string, path: string): string {
865865
export function refusalSources(root: string): Source[] {
866866
const sources: Source[] = [];
867867
const add = (path: string, sites: Sites) => {
868-
if (!existsSync(join(root, path))) {
869-
throw new Error(
870-
`${path} is a refusal source of this census but does not exist; a moved or renamed source is listed again under its new path`,
871-
);
872-
}
873868
sources.push({ path, text: read(root, path), sites });
874869
};
875870
const sectionDirs = readdirSync(join(root, "src/sections"), { withFileTypes: true })
@@ -960,9 +955,7 @@ describe("every parse-refusal message a user can read is pinned by a test", () =
960955
withTempDir("refusal-sources-", (root) => {
961956
mkdirSync(join(root, "src/sections/planted"), { recursive: true });
962957
writeFileSync(join(root, "src/sections/planted/schema.ts"), "export const a = 1;\n");
963-
expect(() => refusalSources(root)).toThrow(
964-
/^src\/sections\/planted\/index\.ts is a refusal source of this census but does not exist/,
965-
);
958+
expect(() => refusalSources(root)).toThrow(/ENOENT.*src\/sections\/planted\/index\.ts/);
966959
}));
967960

968961
/** One literal per exclusion position, then a message in every spelling a source can give one. */

‎test/settings-schema.ts‎

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { existsSync, readFileSync } from "node:fs";
2-
import { join, relative } from "node:path";
1+
import { readFileSync } from "node:fs";
2+
import { join } from "node:path";
33
import { ROOT } from "./root.js";
44

55
/** The built, gitignored schema; `bun run test` and `bun run fuzz` run `bun run build:schema` before loading it. */
@@ -11,20 +11,8 @@ export interface SettingsSchemaFile {
1111
[keyword: string]: unknown;
1212
}
1313

14-
/**
15-
* The one line a test fails with when the built schema is absent, raised before any step that would misreport the
16-
* absence as a defect of its own (tsc's TS2307 against a docs page, ajv's compile error): the generator only writes
17-
* the file into the tree, so a test cannot build it elsewhere and names the build step instead.
18-
*/
19-
export function schemaNotBuilt(path: string): Error {
20-
return new Error(`${relative(ROOT, path)} is not built; run \`bun run build:schema\``);
21-
}
22-
2314
/** Read at run time rather than imported: `tsc` resolves a JSON import, so an import would fail every typecheck on
2415
* a checkout that has not built the file. */
2516
export function readSettingsSchema(): SettingsSchemaFile {
26-
if (!existsSync(SETTINGS_SCHEMA_PATH)) {
27-
throw schemaNotBuilt(SETTINGS_SCHEMA_PATH);
28-
}
2917
return JSON.parse(readFileSync(SETTINGS_SCHEMA_PATH, "utf8")) as SettingsSchemaFile;
3018
}

0 commit comments

Comments
 (0)