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

utils.ts

152 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(repoRoot, "node_modules", ".bin", "oxlint");
90 const pluginPath = path.join(repoRoot, "oxlint-plugin-t3code", "index.ts");
91​
92 yield* fs.writeFileString(
93 configPath,
94 yield* encodeOxlintConfig({
95 jsPlugins: [{ name: "t3code", specifier: pluginPath }],
96 rules: { [ruleName]: "error" },
97 }),
98 );
99 yield* fs.writeFileString(sourcePath, source);
100​
101 const output = yield* spawnAndCollectOutput(
102 ChildProcess.make(oxlintBin, ["--config", configPath, sourcePath], { cwd: repoRoot }),
103 );
104​
105 if (output.exitCode !== 0) {
106 return yield* new OxlintFixtureFailure({
107 exitCode: output.exitCode,
108 stdout: output.stdout,
109 stderr: output.stderr,
110 });
111 }
112​
113 return `${output.stdout}${output.stderr}`;
114 }, Effect.scoped);
115​
116 const runAndExpectFailure: RuleHarness["runAndExpectFailure"] = (source) =>
117 run(source).pipe(
118 Effect.matchEffect({
119 onFailure: (error) =>
120 OxlintFixtureFailure.is(error)
121 ? Effect.succeed(
122 `oxlint fixture failed with exit code ${error.exitCode}\n${error.stdout}\n${error.stderr}`,
123 )
124 : Effect.fail(error),
125 onSuccess: () => Effect.fail(new OxlintFixtureExpectedFailure({ ruleName })),
126 }),
127 );
128​
129 return {
130 run,
131 runAndExpectFailure,
132 valid(name, source) {
133 test(name, (it) => {
134 it.effect("passes", () => run(source));
135 });
136 },
137 invalid(name, source, assertion) {
138 test(name, (it) => {
139 it.effect("reports the rule diagnostic", () =>
140 runAndExpectFailure(source).pipe(
141 Effect.tap((output) =>
142 Effect.sync(() => {
143 assert.match(output, new RegExp(diagnosticRuleName));
144 assertion?.(output);
145 }),
146 ),
147 ),
148 );
149 });
150 },
151 };
152};