Skip to content

Commit

Permalink
fix(lint): turbo-ignore (vercel#5787)
Browse files Browse the repository at this point in the history
  • Loading branch information
tknickman authored Aug 23, 2023
1 parent 3e826ed commit 529dfd7
Show file tree
Hide file tree
Showing 28 changed files with 177 additions and 104 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1266,6 +1266,10 @@ jobs:
# job in this workflow. We can move that in here in the next step.
run: turbo run lint:prettier --filter=!cli

# workspaces are added here as their lint issues are fixed
- name: Lint check
run: turbo run lint --filter=turbo-ignore --filter=@turbo/types --filter=eslint-config-turbo

final:
name: Ok
needs:
Expand Down
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"typescript",
"typescriptreact"
],
"eslint.packageManager": "pnpm",
"eslint.workingDirectories": [{ "mode": "auto" }],
"debug.javascript.unmapMissingSources": true,
"go.lintTool": "golangci-lint",
"go.buildTags": "rust",
Expand Down
2 changes: 1 addition & 1 deletion examples/basic/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"extends": "tsconfig/base.json",
"extends": "tsconfig/base.json"
}
2 changes: 1 addition & 1 deletion examples/with-tailwind/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"extends": "tsconfig/base.json",
"extends": "tsconfig/base.json"
}
2 changes: 1 addition & 1 deletion packages/devlow-bench/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
extends: ["@turbo/eslint-config/library"]
extends: ["@turbo/eslint-config/library"],
};
8 changes: 3 additions & 5 deletions packages/devlow-bench/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
"types": ["node"],
"outDir": "dist",
"declaration": true,
"declarationDir": "dist",
"declarationDir": "dist"
},
"include": [
"src/**/*"
],
}
"include": ["src/**/*"]
}
1 change: 1 addition & 0 deletions packages/eslint-config/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ module.exports = {
rules: {
"unicorn/filename-case": ["off"],
"@typescript-eslint/explicit-function-return-type": ["off"],
"@typescript-eslint/array-type": ["error", { default: "generic" }],
},
};
30 changes: 15 additions & 15 deletions packages/turbo-ignore/__tests__/args.test.ts
Original file line number Diff line number Diff line change
@@ -1,99 +1,99 @@
import parseArgs, { help } from "../src/args";
import pkg from "../package.json";
import { spyConsole, spyExit } from "@turbo/test-utils";
import { parseArgs, help } from "../src/args";
import pkg from "../package.json";

describe("parseArgs()", () => {
const mockConsole = spyConsole();
const mockExit = spyExit();

it("does not throw with no args", async () => {
it("does not throw with no args", () => {
const result = parseArgs({ argv: [] });
expect(result.workspace).toBe(undefined);
expect(result.fallback).toBe(undefined);
expect(result.task).toBe(undefined);
});

it("outputs help text (--help)", async () => {
it("outputs help text (--help)", () => {
parseArgs({ argv: ["--help"] });
expect(mockExit.exit).toHaveBeenCalledWith(0);
expect(mockConsole.log).toHaveBeenCalledWith(help);
});

it("outputs help text (-h)", async () => {
it("outputs help text (-h)", () => {
parseArgs({ argv: ["-h"] });
expect(mockExit.exit).toHaveBeenCalledWith(0);
expect(mockConsole.log).toHaveBeenCalledWith(help);
});

it("outputs version text (--version)", async () => {
it("outputs version text (--version)", () => {
parseArgs({ argv: ["--version"] });
expect(mockExit.exit).toHaveBeenCalledWith(0);
expect(mockConsole.log).toHaveBeenCalledWith(pkg.version);
});

it("outputs version text (-v)", async () => {
it("outputs version text (-v)", () => {
parseArgs({ argv: ["-v"] });
expect(mockExit.exit).toHaveBeenCalledWith(0);
expect(mockConsole.log).toHaveBeenCalledWith(pkg.version);
});

it("correctly finds workspace", async () => {
it("correctly finds workspace", () => {
const result = parseArgs({ argv: ["this-workspace"] });
expect(result.workspace).toBe("this-workspace");
expect(result.fallback).toBe(undefined);
expect(result.task).toBe(undefined);
expect(mockExit.exit).toHaveBeenCalledTimes(0);
});

it("correctly finds fallback", async () => {
it("correctly finds fallback", () => {
const result = parseArgs({ argv: ["--fallback=HEAD^"] });
expect(result.workspace).toBe(undefined);
expect(result.fallback).toBe("HEAD^");
expect(result.task).toBe(undefined);
expect(mockExit.exit).toHaveBeenCalledTimes(0);
});

it("correctly finds task", async () => {
it("correctly finds task", () => {
const result = parseArgs({ argv: ["--task=some-workspace#build"] });
expect(result.workspace).toBe(undefined);
expect(result.fallback).toBe(undefined);
expect(result.task).toBe("some-workspace#build");
expect(mockExit.exit).toHaveBeenCalledTimes(0);
});

it("uses default fallback if incorrectly specified", async () => {
it("uses default fallback if incorrectly specified", () => {
const result = parseArgs({ argv: ["--fallback"] });
expect(result.workspace).toBe(undefined);
expect(result.fallback).toBe(undefined);
expect(result.task).toBe(undefined);
expect(mockExit.exit).toHaveBeenCalledTimes(0);
});

it("uses default fallback if empty string", async () => {
it("uses default fallback if empty string", () => {
const result = parseArgs({ argv: ["--fallback="] });
expect(result.workspace).toBe(undefined);
expect(result.fallback).toBe(undefined);
expect(result.task).toBe(undefined);
expect(mockExit.exit).toHaveBeenCalledTimes(0);
});

it("uses default task if incorrectly specified", async () => {
it("uses default task if incorrectly specified", () => {
const result = parseArgs({ argv: ["--task"] });
expect(result.workspace).toBe(undefined);
expect(result.fallback).toBe(undefined);
expect(result.task).toBe(undefined);
expect(mockExit.exit).toHaveBeenCalledTimes(0);
});

it("uses default task if empty string", async () => {
it("uses default task if empty string", () => {
const result = parseArgs({ argv: ["--task="] });
expect(result.workspace).toBe(undefined);
expect(result.fallback).toBe(undefined);
expect(result.task).toBe(undefined);
expect(mockExit.exit).toHaveBeenCalledTimes(0);
});

it("correctly finds fallback and workspace", async () => {
it("correctly finds fallback and workspace", () => {
const result = parseArgs({
argv: [
"this-workspace",
Expand Down
5 changes: 3 additions & 2 deletions packages/turbo-ignore/__tests__/checkCommit.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import child_process from "child_process";
import { checkCommit } from "../src/checkCommit";
// eslint-disable-next-line camelcase
import child_process from "node:child_process";
import { mockEnv } from "@turbo/test-utils";
import { checkCommit } from "../src/checkCommit";

describe("checkCommit()", () => {
describe("on Vercel", () => {
Expand Down
52 changes: 45 additions & 7 deletions packages/turbo-ignore/__tests__/getComparison.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { getComparison } from "../src/getComparison";
import { spyConsole, mockEnv } from "@turbo/test-utils";
import { getComparison } from "../src/getComparison";

describe("getComparison()", () => {
mockEnv();
const mockConsole = spyConsole();
it("uses headRelative comparison when not running Vercel CI", async () => {
it("uses headRelative comparison when not running Vercel CI", () => {
expect(getComparison({ workspace: "test-workspace" }))
.toMatchInlineSnapshot(`
Object {
Expand All @@ -14,17 +14,17 @@ describe("getComparison()", () => {
`);
});

it("returns null when running in Vercel CI with no VERCEL_GIT_PREVIOUS_SHA", async () => {
it("returns null when running in Vercel CI with no VERCEL_GIT_PREVIOUS_SHA", () => {
process.env.VERCEL = "1";
process.env.VERCEL_GIT_COMMIT_REF = "my-branch";
expect(getComparison({ workspace: "test-workspace" })).toBeNull();
expect(mockConsole.log).toHaveBeenCalledWith(
"≫ ",
'No previous deployments found for "test-workspace" on branch "my-branch".'
'No previous deployments found for "test-workspace" on branch "my-branch"'
);
});

it("uses custom fallback when running in Vercel CI with no VERCEL_GIT_PREVIOUS_SHA", async () => {
it("uses custom fallback when running in Vercel CI with no VERCEL_GIT_PREVIOUS_SHA", () => {
process.env.VERCEL = "1";
process.env.VERCEL_GIT_COMMIT_REF = "my-branch";
expect(getComparison({ workspace: "test-workspace", fallback: "HEAD^2" }))
Expand All @@ -37,7 +37,28 @@ describe("getComparison()", () => {
expect(mockConsole.log).toHaveBeenNthCalledWith(
1,
"≫ ",
'No previous deployments found for "test-workspace" on branch "my-branch".'
'No previous deployments found for "test-workspace" on branch "my-branch"'
);
expect(mockConsole.log).toHaveBeenNthCalledWith(
2,
"≫ ",
"Falling back to ref HEAD^2"
);
});

it("modifies output when running in Vercel CI with no VERCEL_GIT_PREVIOUS_SHA and no VERCEL_GIT_COMMIT_REF", () => {
process.env.VERCEL = "1";
expect(getComparison({ workspace: "test-workspace", fallback: "HEAD^2" }))
.toMatchInlineSnapshot(`
Object {
"ref": "HEAD^2",
"type": "customFallback",
}
`);
expect(mockConsole.log).toHaveBeenNthCalledWith(
1,
"≫ ",
'No previous deployments found for "test-workspace"'
);
expect(mockConsole.log).toHaveBeenNthCalledWith(
2,
Expand All @@ -46,7 +67,7 @@ describe("getComparison()", () => {
);
});

it("uses previousDeploy when running in Vercel CI with VERCEL_GIT_PREVIOUS_SHA", async () => {
it("uses previousDeploy when running in Vercel CI with VERCEL_GIT_PREVIOUS_SHA", () => {
process.env.VERCEL = "1";
process.env.VERCEL_GIT_PREVIOUS_SHA = "mygitsha";
process.env.VERCEL_GIT_COMMIT_REF = "my-branch";
Expand All @@ -58,4 +79,21 @@ describe("getComparison()", () => {
}
`);
});

it("modifies output when running in Vercel CI with VERCEL_GIT_PREVIOUS_SHA but no VERCEL_GIT_COMMIT_REF", () => {
process.env.VERCEL = "1";
process.env.VERCEL_GIT_PREVIOUS_SHA = "mygitsha";
expect(getComparison({ workspace: "test-workspace" }))
.toMatchInlineSnapshot(`
Object {
"ref": "mygitsha",
"type": "previousDeploy",
}
`);
expect(mockConsole.log).toHaveBeenNthCalledWith(
1,
"≫ ",
'Found previous deployment ("mygitsha") for "test-workspace"'
);
});
});
6 changes: 3 additions & 3 deletions packages/turbo-ignore/__tests__/getTask.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { getTask } from "../src/getTask";
import { spyConsole, validateLogs } from "@turbo/test-utils";
import { getTask } from "../src/getTask";

describe("getWorkspace()", () => {
const mockConsole = spyConsole();
it("getTask defaults to build", async () => {
it("getTask defaults to build", () => {
expect(getTask({})).toEqual("build");
validateLogs(
['Using "build" as the task as it was unspecified'],
Expand All @@ -12,7 +12,7 @@ describe("getWorkspace()", () => {
);
});

it("getTask returns a quoted task if user-supplied", async () => {
it("getTask returns a quoted task if user-supplied", () => {
expect(
getTask({
task: "workspace#task",
Expand Down
2 changes: 1 addition & 1 deletion packages/turbo-ignore/__tests__/getWorkspace.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getWorkspace } from "../src/getWorkspace";
import { spyConsole, validateLogs } from "@turbo/test-utils";
import { getWorkspace } from "../src/getWorkspace";

describe("getWorkspace()", () => {
const mockConsole = spyConsole();
Expand Down
16 changes: 10 additions & 6 deletions packages/turbo-ignore/__tests__/ignore.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import child_process, { ChildProcess, ExecException } from "child_process";
import turboIgnore from "../src/ignore";
// eslint-disable-next-line camelcase
import child_process, {
type ChildProcess,
type ExecException,
} from "node:child_process";
import {
spyConsole,
spyExit,
SpyExit,
type SpyExit,
mockEnv,
validateLogs,
} from "@turbo/test-utils";
import { turboIgnore } from "../src/ignore";

function expectBuild(mockExit: SpyExit) {
expect(mockExit.exit).toHaveBeenCalledWith(1);
Expand All @@ -27,7 +31,7 @@ describe("turboIgnore()", () => {
.mockImplementation((command, options, callback) => {
if (callback) {
return callback(
"error" as unknown as ExecException,
{ message: "error details" } as unknown as ExecException,
"stdout",
"stderr"
) as unknown as ChildProcess;
Expand All @@ -45,7 +49,7 @@ describe("turboIgnore()", () => {
expect.anything()
);

validateLogs(["UNKNOWN_ERROR: error"], mockConsole.error, {
validateLogs(["UNKNOWN_ERROR: error details"], mockConsole.error, {
prefix: "≫ ",
});

Expand Down Expand Up @@ -250,7 +254,7 @@ describe("turboIgnore()", () => {
expect(mockConsole.log).toHaveBeenNthCalledWith(
4,
"≫ ",
'No previous deployments found for "test-app" on branch "my-branch".'
'No previous deployments found for "test-app" on branch "my-branch"'
);
expectBuild(mockExit);
});
Expand Down
1 change: 1 addition & 0 deletions packages/turbo-ignore/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@turbo/eslint-config": "workspace:*",
"@turbo/test-utils": "workspace:^0.0.0",
"@turbo/tsconfig": "workspace:*",
"@turbo/types": "workspace:*",
"@turbo/utils": "workspace:*",
"@types/jest": "^27.4.0",
"@types/node": "^18.17.2",
Expand Down
12 changes: 5 additions & 7 deletions packages/turbo-ignore/src/args.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pkg from "../package.json";
import { TurboIgnoreArgs } from "./types";
import type { TurboIgnoreArgs } from "./types";
import {
skipAllCommits,
forceAllCommits,
Expand Down Expand Up @@ -41,27 +41,25 @@ ${[...forceAllCommits, ...forceWorkspaceCommits({ workspace: "<workspace>" })]

// simple args parser because we don't want to pull in a dependency
// and we don't need many features
export default function parseArgs({
argv,
}: {
argv: Array<string>;
}): TurboIgnoreArgs {
export function parseArgs({ argv }: { argv: Array<string> }): TurboIgnoreArgs {
const args: TurboIgnoreArgs = { directory: process.cwd() };

// find all flags
const flags = new Set(
argv
.filter((args) => args.startsWith("-"))
.filter((arg) => arg.startsWith("-"))
.map((flag) => flag.replace(/-/g, ""))
);

// handle help flag and exit
if (flags.has("help") || flags.has("h")) {
// eslint-disable-next-line no-console
console.log(help);
process.exit(0);
}
// handle version flag and exit
if (flags.has("version") || flags.has("v")) {
// eslint-disable-next-line no-console
console.log(pkg.version);
process.exit(0);
}
Expand Down
Loading

0 comments on commit 529dfd7

Please sign in to comment.