1 /**
2 * Shared test utilities for the rule specs.
3 *
4 * This file lives at `src/test-utils.ts` (not under a `__tests__/` directory)
5 * on purpose: Jest's default `testMatch` treats every `.ts` file inside a
6 * `__tests__/` folder as a test suite, so a helper placed there would fail as
7 * an "empty test suite." It is excluded from the build via `tsconfig.json`.
8 */
9 import * as fs from "fs"
10 import * as os from "os"
11 import * as path from "path"
12 import { RuleTester } from "@typescript-eslint/rule-tester"
13
14 /**
15 * Wires `@typescript-eslint/rule-tester`'s static lifecycle hooks to Jest's
16 * globals and returns a configured `RuleTester`. Every spec needs this same
17 * four-line dance before constructing a tester — call this instead.
18 */
19 export function createRuleTester(
20 config?: ConstructorParameters<typeof RuleTester>[0]
21 ): RuleTester {
22 RuleTester.afterAll = afterAll
23 RuleTester.describe = describe
24 RuleTester.it = it
25 RuleTester.itOnly = it.only
26 return new RuleTester(config)
27 }
28
29 /** A single file to materialize inside a fixture workspace. */
30 export type FixtureFile = { rel: string; content: string }
31
32 export interface FixtureWorkspace {
33 /** Absolute path of the temp root (the `mkdtemp` directory). */
34 root: string
35 /** Absolute path of `<root>/<subPath>`, where the fixture files live. */
36 dir: string
37 /** Resolves a path relative to `dir` into an absolute path. */
38 resolve: (rel: string) => string
39 }
40
41 const tempRoots: string[] = []
42
43 /**
44 * Creates a throwaway directory tree on disk for cross-file rules that read
45 * sibling files (e.g. `scheduled-job-name-unique`,
46 * `loader-must-be-exported-in-module-definition`, `use-validated-body-or-query`).
47 *
48 * Each `files` entry is written to `<tmp>/<subPath>/<rel>`, creating parent
49 * directories as needed. Lint the file under test with an absolute filename via
50 * the returned `resolve` so the rule's disk scan sees the same content.
51 *
52 * Register `cleanupFixtureWorkspaces` in the spec's `afterAll` to remove the
53 * directories when the suite finishes.
54 *
55 * @param subPath POSIX-style path under the temp root, e.g. `"src/jobs"`.
56 */
57 export function createFixtureWorkspace(
58 subPath: string,
59 files: FixtureFile[]
60 ): FixtureWorkspace {
61 const root = fs.mkdtempSync(path.join(os.tmpdir(), "medusa-eslint-"))
62 tempRoots.push(root)
63 const dir = path.join(root, ...subPath.split("/"))
64 fs.mkdirSync(dir, { recursive: true })
65 for (const f of files) {
66 const abs = path.join(dir, f.rel)
67 fs.mkdirSync(path.dirname(abs), { recursive: true })
68 fs.writeFileSync(abs, f.content, "utf8")
69 }
70 return {
71 root,
72 dir,
73 resolve: (rel: string) => path.join(dir, rel),
74 }
75 }
76
77 /**
78 * Removes every fixture workspace created via `createFixtureWorkspace` in the
79 * current test file. Safe to call when none were created — pass it directly to
80 * `afterAll(cleanupFixtureWorkspaces)`.
81 */
82 export function cleanupFixtureWorkspaces(): void {
83 while (tempRoots.length) {
84 const dir = tempRoots.pop()!
85 try {
86 fs.rmSync(dir, { recursive: true, force: true })
87 } catch {
88 // ignore
89 }
90 }
91 }