Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

utils.ts

166 lines5.2 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, it } from "@effect/vitest";
3import * as Data from "effect/Data";
4import * as Effect from "effect/Effect";
5import * as FileSystem from "effect/FileSystem";
6import * as Path from "effect/Path";
7import * as PlatformError from "effect/PlatformError";
8import * as Predicate from "effect/Predicate";
9import * as Schema from "effect/Schema";
10import * as Stream from "effect/Stream";
11import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
12​
13class OxlintFixtureFailure extends Data.TaggedError("OxlintFixtureFailure")<{
14 readonly exitCode: number;
15 readonly stdout: string;
16 readonly stderr: string;
17}> {
18 static readonly is = (u: unknown): u is OxlintFixtureFailure =>
19 Predicate.isTagged(u, "OxlintFixtureFailure");
21​
22class OxlintFixtureExpectedFailure extends Data.TaggedError("OxlintFixtureExpectedFailure")<{
23 readonly ruleName: string;
24}> {
25 override get message() {
26 return `Expected oxlint to report a failure for rule ${this.ruleName}, but it passed.`;
27 }
29​
30const encodeOxlintConfig = Schema.encodeEffect(Schema.UnknownFromJsonString);
31​
32interface RuleHarness {
33 readonly run: (
34 source: string,
35 ) => Effect.Effect<
36 string,
37 OxlintFixtureFailure | PlatformError.PlatformError | Schema.SchemaError,
38 NodeServices.NodeServices
39 >;
40 readonly runAndExpectFailure: (
41 source: string,
42 ) => Effect.Effect<
43 string,
44 OxlintFixtureExpectedFailure | PlatformError.PlatformError | Schema.SchemaError,
45 NodeServices.NodeServices
46 >;
47 readonly valid: (name: string, source: string) => void;
48 readonly invalid: (name: string, source: string, assertion?: (output: string) => void) => void;
50​
51interface RuleHarnessOptions {
52 readonly filename?: string;
54​
55const collectStreamAsString = <E>(stream: Stream.Stream<Uint8Array, E>): Effect.Effect<string, E> =>
56 stream.pipe(
57 Stream.decodeText(),
58 Stream.runFold(
59 () => "",
60 (acc, chunk) => acc + chunk,
61 ),
62 );
63​
64const spawnAndCollectOutput = Effect.fnUntraced(function* (command: ChildProcess.Command) {
65 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
66 const child = yield* spawner.spawn(command);
67​
68 const [stdout, stderr, exitCode] = yield* Effect.all(
69 [
70 collectStreamAsString(child.stdout),
71 collectStreamAsString(child.stderr),
72 child.exitCode.pipe(Effect.map(Number)),
73 ],
74 { concurrency: "unbounded" },
75 );
76​
77 return { exitCode, stdout, stderr };
78}, Effect.scoped);
79​
80export const createOxlintRuleHarness = (
81 ruleName: string,
82 options: RuleHarnessOptions = {},
83): RuleHarness => {
84 const [pluginName, shortRuleName] = ruleName.split("/");
85 const diagnosticRuleName =
86 pluginName && shortRuleName ? `${pluginName}\\(${shortRuleName}\\)` : ruleName;
87 const test = it.layer(NodeServices.layer);
88​
89 const run: RuleHarness["run"] = Effect.fnUntraced(function* (source: string) {
90 const fs = yield* FileSystem.FileSystem;
91 const path = yield* Path.Path;
92 const fixtureDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3code-oxlint-" });
93 const configPath = path.join(fixtureDir, ".oxlintrc.json");
94 const sourcePath = path.join(fixtureDir, options.filename ?? "fixture.ts");
95 const repoRoot = path.join(import.meta.dirname, "..", "..");
96 const oxlintBin = path.join(
97 repoRoot,
98 "node_modules",
99 ".pnpm",
100 "node_modules",
101 ".bin",
102 "oxlint",
103 );
104 const pluginPath = path.join(repoRoot, "oxlint-plugin-t3code", "index.ts");
105​
106 yield* fs.writeFileString(
107 configPath,
108 yield* encodeOxlintConfig({
109 jsPlugins: [{ name: "t3code", specifier: pluginPath }],
110 rules: { [ruleName]: "error" },
111 }),
112 );
113 yield* fs.writeFileString(sourcePath, source);
114​
115 const output = yield* spawnAndCollectOutput(
116 ChildProcess.make(oxlintBin, ["--config", configPath, sourcePath], { cwd: repoRoot }),
117 );
118​
119 if (output.exitCode !== 0) {
120 return yield* new OxlintFixtureFailure({
121 exitCode: output.exitCode,
122 stdout: output.stdout,
123 stderr: output.stderr,
124 });
125 }
126​
127 return `${output.stdout}${output.stderr}`;
128 }, Effect.scoped);
129​
130 const runAndExpectFailure: RuleHarness["runAndExpectFailure"] = (source) =>
131 run(source).pipe(
132 Effect.matchEffect({
133 onFailure: (error) =>
134 OxlintFixtureFailure.is(error)
135 ? Effect.succeed(
136 `oxlint fixture failed with exit code ${error.exitCode}\n${error.stdout}\n${error.stderr}`,
137 )
138 : Effect.fail(error),
139 onSuccess: () => Effect.fail(new OxlintFixtureExpectedFailure({ ruleName })),
140 }),
141 );
142​
143 return {
144 run,
145 runAndExpectFailure,
146 valid(name, source) {
147 test(name, (it) => {
148 it.effect("passes", () => run(source));
149 });
150 },
151 invalid(name, source, assertion) {
152 test(name, (it) => {
153 it.effect("reports the rule diagnostic", () =>
154 runAndExpectFailure(source).pipe(
155 Effect.tap((output) =>
156 Effect.sync(() => {
157 assert.match(output, new RegExp(diagnosticRuleName));
158 assertion?.(output);
159 }),
160 ),
161 ),
162 );
163 });
164 },
165 };
166};