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

utils.ts

159 lines5.0 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​
51const collectStreamAsString = <E>(stream: Stream.Stream<Uint8Array, E>): Effect.Effect<string, E> =>
52 stream.pipe(
53 Stream.decodeText(),
54 Stream.runFold(
55 () => "",
56 (acc, chunk) => acc + chunk,
57 ),
58 );
59​
60const spawnAndCollectOutput = Effect.fnUntraced(function* (command: ChildProcess.Command) {
61 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
62 const child = yield* spawner.spawn(command);
63​
64 const [stdout, stderr, exitCode] = yield* Effect.all(
65 [
66 collectStreamAsString(child.stdout),
67 collectStreamAsString(child.stderr),
68 child.exitCode.pipe(Effect.map(Number)),
69 ],
70 { concurrency: "unbounded" },
71 );
72​
73 return { exitCode, stdout, stderr };
74}, Effect.scoped);
75​
76export const createOxlintRuleHarness = (ruleName: string): RuleHarness => {
77 const [pluginName, shortRuleName] = ruleName.split("/");
78 const diagnosticRuleName =
79 pluginName && shortRuleName ? `${pluginName}\\(${shortRuleName}\\)` : ruleName;
80 const test = it.layer(NodeServices.layer);
81​
82 const run: RuleHarness["run"] = Effect.fnUntraced(function* (source: string) {
83 const fs = yield* FileSystem.FileSystem;
84 const path = yield* Path.Path;
85 const fixtureDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3code-oxlint-" });
86 const configPath = path.join(fixtureDir, ".oxlintrc.json");
87 const sourcePath = path.join(fixtureDir, "fixture.ts");
88 const repoRoot = path.join(import.meta.dirname, "..", "..");
89 const oxlintBin = path.join(
90 repoRoot,
91 "node_modules",
92 ".pnpm",
93 "node_modules",
94 ".bin",
95 "oxlint",
96 );
97 const pluginPath = path.join(repoRoot, "oxlint-plugin-t3code", "index.ts");
98​
99 yield* fs.writeFileString(
100 configPath,
101 yield* encodeOxlintConfig({
102 jsPlugins: [{ name: "t3code", specifier: pluginPath }],
103 rules: { [ruleName]: "error" },
104 }),
105 );
106 yield* fs.writeFileString(sourcePath, source);
107​
108 const output = yield* spawnAndCollectOutput(
109 ChildProcess.make(oxlintBin, ["--config", configPath, sourcePath], { cwd: repoRoot }),
110 );
111​
112 if (output.exitCode !== 0) {
113 return yield* new OxlintFixtureFailure({
114 exitCode: output.exitCode,
115 stdout: output.stdout,
116 stderr: output.stderr,
117 });
118 }
119​
120 return `${output.stdout}${output.stderr}`;
121 }, Effect.scoped);
122​
123 const runAndExpectFailure: RuleHarness["runAndExpectFailure"] = (source) =>
124 run(source).pipe(
125 Effect.matchEffect({
126 onFailure: (error) =>
127 OxlintFixtureFailure.is(error)
128 ? Effect.succeed(
129 `oxlint fixture failed with exit code ${error.exitCode}\n${error.stdout}\n${error.stderr}`,
130 )
131 : Effect.fail(error),
132 onSuccess: () => Effect.fail(new OxlintFixtureExpectedFailure({ ruleName })),
133 }),
134 );
135​
136 return {
137 run,
138 runAndExpectFailure,
139 valid(name, source) {
140 test(name, (it) => {
141 it.effect("passes", () => run(source));
142 });
143 },
144 invalid(name, source, assertion) {
145 test(name, (it) => {
146 it.effect("reports the rule diagnostic", () =>
147 runAndExpectFailure(source).pipe(
148 Effect.tap((output) =>
149 Effect.sync(() => {
150 assert.match(output, new RegExp(diagnosticRuleName));
151 assertion?.(output);
152 }),
153 ),
154 ),
155 );
156 });
157 },
158 };
159};