|
| 1 | +import { spawnSync } from "node:child_process"; |
| 2 | +import * as fs from "node:fs"; |
| 3 | +import os from "node:os"; |
| 4 | +import * as path from "node:path"; |
| 5 | +import { fileURLToPath } from "node:url"; |
| 6 | +import { afterAll, describe, expect, it } from "vite-plus/test"; |
| 7 | +import { initGitRepo, setupReactProject, writeFile } from "../regressions/_helpers.js"; |
| 8 | + |
| 9 | +interface CliDiagnostic { |
| 10 | + readonly plugin: string; |
| 11 | + readonly rule: string; |
| 12 | +} |
| 13 | + |
| 14 | +interface CliReport { |
| 15 | + readonly diagnostics: ReadonlyArray<CliDiagnostic>; |
| 16 | +} |
| 17 | + |
| 18 | +const currentDirectory = path.dirname(fileURLToPath(import.meta.url)); |
| 19 | +const builtCliPath = path.resolve(currentDirectory, "../../dist/cli.js"); |
| 20 | +const hasBuiltCli = fs.existsSync(builtCliPath); |
| 21 | +const temporaryRoot = fs.mkdtempSync(path.join(os.tmpdir(), "rd-staged-rule-parity-")); |
| 22 | + |
| 23 | +afterAll(() => { |
| 24 | + fs.rmSync(temporaryRoot, { recursive: true, force: true }); |
| 25 | +}); |
| 26 | + |
| 27 | +const runJsonScan = (directory: string, argumentsList: ReadonlyArray<string>): CliReport => { |
| 28 | + const result = spawnSync(process.execPath, [builtCliPath, ".", ...argumentsList], { |
| 29 | + cwd: directory, |
| 30 | + encoding: "utf8", |
| 31 | + env: { ...process.env, CI: "1", FORCE_COLOR: "0" }, |
| 32 | + }); |
| 33 | + |
| 34 | + expect(result.status).toBe(1); |
| 35 | + return JSON.parse(result.stdout); |
| 36 | +}; |
| 37 | + |
| 38 | +const diagnosticRuleKeys = (report: CliReport): string[] => |
| 39 | + report.diagnostics.map((diagnostic) => `${diagnostic.plugin}/${diagnostic.rule}`); |
| 40 | + |
| 41 | +describe.skipIf(!hasBuiltCli)("staged rule parity", () => { |
| 42 | + it("reports an enabled no-clone-element finding in a newly staged file", () => { |
| 43 | + const projectDirectory = setupReactProject(temporaryRoot, "issue-1691", { |
| 44 | + files: { |
| 45 | + "doctor.config.json": `${JSON.stringify({ |
| 46 | + noScore: true, |
| 47 | + rules: { "react-doctor/no-clone-element": "error" }, |
| 48 | + })}\n`, |
| 49 | + }, |
| 50 | + }); |
| 51 | + initGitRepo(projectDirectory, { commit: true }); |
| 52 | + |
| 53 | + writeFile( |
| 54 | + path.join(projectDirectory, "src/tab-pill.tsx"), |
| 55 | + `import { cloneElement, type ReactElement } from "react"; |
| 56 | +
|
| 57 | +export const TabPill = ({ children }: { children: ReactElement }) => |
| 58 | + cloneElement(children, { className: "tab" }); |
| 59 | +`, |
| 60 | + ); |
| 61 | + spawnSync("git", ["add", "src/tab-pill.tsx"], { cwd: projectDirectory }); |
| 62 | + |
| 63 | + const commonArguments = ["--json", "--no-score", "--no-supply-chain", "--no-telemetry"]; |
| 64 | + const fullReport = runJsonScan(projectDirectory, ["--yes", ...commonArguments]); |
| 65 | + const stagedReport = runJsonScan(projectDirectory, ["--staged", ...commonArguments]); |
| 66 | + |
| 67 | + expect(diagnosticRuleKeys(fullReport)).toContain("react-doctor/no-clone-element"); |
| 68 | + expect(diagnosticRuleKeys(stagedReport)).toContain("react-doctor/no-clone-element"); |
| 69 | + }, 60_000); |
| 70 | +}); |
0 commit comments