From fb5229f5bbcc7b4a1ac03bc6d8ba05f3244521d2 Mon Sep 17 00:00:00 2001 From: Nathan Hammond Date: Thu, 1 Dec 2022 03:55:35 +0800 Subject: [PATCH] Add tests for the ESLint caching. (#2859) I pulled the trigger too fast on #2850, these are the tests that should have been written for it. --- .eslintignore | 1 + package.json | 9 +- .../eslint-plugin-turbo/__tests__/cwd.test.ts | 111 ++++++++++++++++++ .../__tests__/fixtures/workspace/.eslintrc.js | 4 + .../fixtures/workspace/child/child.js | 2 + .../__tests__/fixtures/workspace/package.json | 5 + .../__tests__/fixtures/workspace/peer.js | 1 + .../{lib => }/fixtures/workspace/turbo.json | 1 + .../__tests__/lib/findTurboConfig.test.ts | 3 +- packages/eslint-plugin-turbo/jest.config.js | 1 + .../lib/configs/recommended.ts | 2 +- turbo.json | 3 + 12 files changed, 137 insertions(+), 6 deletions(-) create mode 100644 packages/eslint-plugin-turbo/__tests__/cwd.test.ts create mode 100644 packages/eslint-plugin-turbo/__tests__/fixtures/workspace/.eslintrc.js create mode 100644 packages/eslint-plugin-turbo/__tests__/fixtures/workspace/child/child.js create mode 100644 packages/eslint-plugin-turbo/__tests__/fixtures/workspace/package.json create mode 100644 packages/eslint-plugin-turbo/__tests__/fixtures/workspace/peer.js rename packages/eslint-plugin-turbo/__tests__/{lib => }/fixtures/workspace/turbo.json (96%) diff --git a/.eslintignore b/.eslintignore index 3817794984ecf..fc4703d1c2ad2 100644 --- a/.eslintignore +++ b/.eslintignore @@ -6,6 +6,7 @@ dist/ /examples/ +packages/eslint-plugin-turbo/__tests__/fixtures packages/create-turbo/templates packages/turbo-tracing-next-plugin/test/with-mongodb-mongoose crates/*/tests/** diff --git a/package.json b/package.json index eb129dca4597e..787dd91c536d9 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "format:prettier": "prettier -w .", "format:rs": "cargo fmt --all", "format:toml": "taplo format", - "lint": "eslint . --ext js,jsx,ts,tsx -c ./.eslintrc.js", + "lint": "eslint . --ext js,jsx,ts,tsx", "turbo": "cd cli && make turbo && cd .. && node turbow.js", "docs": "pnpm -- turbo run dev --filter=docs --no-cache", "run-example": "./scripts/run-example.sh" @@ -33,10 +33,11 @@ "typescript": "^4.8.4" }, "lint-staged": { - "*.@(js|ts)": [ - "eslint --quiet --fix" + "*.{js,jsx,ts,tsx}": [ + "pnpm run lint --quiet --fix --", + "prettier --write" ], - "*.{ts,tsx,md,mdx,js,jsx,mjs,yml,yaml,css}": [ + "*.{md,mdx,mjs,yml,yaml,css}": [ "prettier --write" ], "*.go": [ diff --git a/packages/eslint-plugin-turbo/__tests__/cwd.test.ts b/packages/eslint-plugin-turbo/__tests__/cwd.test.ts new file mode 100644 index 0000000000000..aa34f2dd28fc6 --- /dev/null +++ b/packages/eslint-plugin-turbo/__tests__/cwd.test.ts @@ -0,0 +1,111 @@ +import fs from "fs"; +import path from "path"; +import JSON5 from "json5"; +import { execSync } from "child_process"; + +describe("eslint settings check", () => { + beforeAll(() => { + const cwd = path.join(__dirname, "fixtures", "workspace"); + execSync(`npm install`, { cwd }); + }); + + afterAll(() => { + const nodeModulesDir = path.join( + __dirname, + "fixtures", + "workspace", + "node_modules" + ); + fs.rmSync(nodeModulesDir, { force: true, recursive: true }); + }); + + it("does the right thing for peers", () => { + const cwd = path.join(__dirname, "fixtures", "workspace"); + const configString = execSync(`eslint --print-config peer.js`, { + cwd, + encoding: "utf8", + }); + const configJson = JSON.parse(configString); + + expect(configJson.settings).toEqual({ + turbo: { envVars: ["CI", "UNORDERED"] }, + }); + }); + + it("does the right thing for child dirs", () => { + const cwd = path.join(__dirname, "fixtures", "workspace", "child"); + const configString = execSync(`eslint --print-config child.js`, { + cwd, + encoding: "utf8", + }); + const configJson = JSON.parse(configString); + + expect(configJson.settings).toEqual({ + turbo: { envVars: ["CI", "UNORDERED"] }, + }); + }); +}); + +describe("eslint cache is busted", () => { + let turboJsonPath: string; + let originalString: string; + + beforeAll(() => { + const cwd = path.join(__dirname, "fixtures", "workspace"); + execSync(`npm install`, { cwd }); + + turboJsonPath = path.join(__dirname, "fixtures", "workspace", "turbo.json"); + originalString = fs.readFileSync(turboJsonPath, { encoding: "utf8" }); + }); + + afterEach(() => { + fs.writeFileSync(turboJsonPath, originalString); + }); + + afterAll(() => { + fs.writeFileSync(turboJsonPath, originalString); + + const nodeModulesDir = path.join( + __dirname, + "fixtures", + "workspace", + "node_modules" + ); + fs.rmSync(nodeModulesDir, { force: true, recursive: true }); + }); + + it("catches a lint error after changing config", () => { + expect.assertions(2); + + // ensure that we populate the cache with a failure. + const cwd = path.join(__dirname, "fixtures", "workspace", "child"); + try { + execSync(`eslint --format=json child.js`, { cwd, encoding: "utf8" }); + } catch (error: any) { + const outputJson = JSON.parse(error.stdout); + expect(outputJson).toMatchObject([ + { + messages: [ + { + message: + "$NONEXISTENT is not listed as a dependency in turbo.json", + }, + ], + }, + ]); + } + + // change the configuration + const turboJson = JSON5.parse(originalString); + turboJson.globalEnv = ["CI", "NONEXISTENT"]; + fs.writeFileSync(turboJsonPath, JSON.stringify(turboJson, null, 2)); + + // test that we invalidated the eslint cache + const output = execSync(`eslint --format=json child.js`, { + cwd, + encoding: "utf8", + }); + const outputJson = JSON.parse(output); + expect(outputJson).toMatchObject([{ errorCount: 0 }]); + }); +}); diff --git a/packages/eslint-plugin-turbo/__tests__/fixtures/workspace/.eslintrc.js b/packages/eslint-plugin-turbo/__tests__/fixtures/workspace/.eslintrc.js new file mode 100644 index 0000000000000..8dc66dca7067c --- /dev/null +++ b/packages/eslint-plugin-turbo/__tests__/fixtures/workspace/.eslintrc.js @@ -0,0 +1,4 @@ +module.exports = { + root: true, + extends: ["plugin:turbo/recommended"], +}; diff --git a/packages/eslint-plugin-turbo/__tests__/fixtures/workspace/child/child.js b/packages/eslint-plugin-turbo/__tests__/fixtures/workspace/child/child.js new file mode 100644 index 0000000000000..b410733f10373 --- /dev/null +++ b/packages/eslint-plugin-turbo/__tests__/fixtures/workspace/child/child.js @@ -0,0 +1,2 @@ +process.env.NONEXISTENT +process.env.CI diff --git a/packages/eslint-plugin-turbo/__tests__/fixtures/workspace/package.json b/packages/eslint-plugin-turbo/__tests__/fixtures/workspace/package.json new file mode 100644 index 0000000000000..fd030e5bc6c36 --- /dev/null +++ b/packages/eslint-plugin-turbo/__tests__/fixtures/workspace/package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "eslint-plugin-turbo": "../../.." + } +} diff --git a/packages/eslint-plugin-turbo/__tests__/fixtures/workspace/peer.js b/packages/eslint-plugin-turbo/__tests__/fixtures/workspace/peer.js new file mode 100644 index 0000000000000..26f81550a14ce --- /dev/null +++ b/packages/eslint-plugin-turbo/__tests__/fixtures/workspace/peer.js @@ -0,0 +1 @@ +process.env.CI diff --git a/packages/eslint-plugin-turbo/__tests__/lib/fixtures/workspace/turbo.json b/packages/eslint-plugin-turbo/__tests__/fixtures/workspace/turbo.json similarity index 96% rename from packages/eslint-plugin-turbo/__tests__/lib/fixtures/workspace/turbo.json rename to packages/eslint-plugin-turbo/__tests__/fixtures/workspace/turbo.json index 416b08bf19434..8079eb24634da 100644 --- a/packages/eslint-plugin-turbo/__tests__/lib/fixtures/workspace/turbo.json +++ b/packages/eslint-plugin-turbo/__tests__/fixtures/workspace/turbo.json @@ -1,5 +1,6 @@ { "$schema": "https://turbo.build/schema.json", + "globalEnv": ["UNORDERED", "CI"], "pipeline": { "build": { // A workspace's `build` task depends on that workspace's diff --git a/packages/eslint-plugin-turbo/__tests__/lib/findTurboConfig.test.ts b/packages/eslint-plugin-turbo/__tests__/lib/findTurboConfig.test.ts index fa0843d1d3478..a5938f4be6538 100644 --- a/packages/eslint-plugin-turbo/__tests__/lib/findTurboConfig.test.ts +++ b/packages/eslint-plugin-turbo/__tests__/lib/findTurboConfig.test.ts @@ -3,9 +3,10 @@ import findTurboConfig from "../../lib/utils/findTurboConfig"; describe("findTurboConfig", () => { it("it parses valid turbo.json file", () => { - const cwd = path.resolve(__dirname, "./fixtures/workspace"); + const cwd = path.join(__dirname, "..", "fixtures", "workspace"); expect(findTurboConfig({ cwd })).toEqual({ $schema: "https://turbo.build/schema.json", + globalEnv: ["UNORDERED", "CI"], pipeline: { build: { dependsOn: ["^build"], diff --git a/packages/eslint-plugin-turbo/jest.config.js b/packages/eslint-plugin-turbo/jest.config.js index 21f8b4d95146e..536bf3abb7485 100644 --- a/packages/eslint-plugin-turbo/jest.config.js +++ b/packages/eslint-plugin-turbo/jest.config.js @@ -4,6 +4,7 @@ module.exports = { transform: { "^.+\\.tsx?$": "ts-jest", }, + testPathIgnorePatterns: [".+/fixtures/.+"], moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"], modulePathIgnorePatterns: ["/node_modules", "/dist"], preset: "ts-jest", diff --git a/packages/eslint-plugin-turbo/lib/configs/recommended.ts b/packages/eslint-plugin-turbo/lib/configs/recommended.ts index 2cd020c5880f0..447ee1aaf3893 100644 --- a/packages/eslint-plugin-turbo/lib/configs/recommended.ts +++ b/packages/eslint-plugin-turbo/lib/configs/recommended.ts @@ -7,7 +7,7 @@ const envVars = getEnvVarDependencies({ }); const settings = { turbo: { - envVars: envVars ? [...envVars] : [], + envVars: envVars ? [...envVars].sort() : [], }, }; diff --git a/turbo.json b/turbo.json index 1ce23e9f7679d..b42c89c0bf174 100644 --- a/turbo.json +++ b/turbo.json @@ -1,6 +1,9 @@ { "$schema": "./docs/public/schema.json", "pipeline": { + "eslint-plugin-turbo#test": { + "dependsOn": ["build"] + }, "test": { "outputs": ["coverage/**/*"], "dependsOn": ["^build"]