forked from vercel/turborepo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(turbo-ignore): args and refactor (vercel#2671)
- Loading branch information
Showing
26 changed files
with
921 additions
and
212 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ target/ | |
tests_output/ | ||
vendor/ | ||
/out/ | ||
coverage/ | ||
|
||
*.tsbuildinfo | ||
.eslintcache | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"name": "test-app", | ||
"private": true, | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "vercel" | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/turbo-ignore/__fixtures__/invalid-app/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"private": true, | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "vercel" | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import parseArgs, { help } from "../src/args"; | ||
import pkg from "../package.json"; | ||
import { spyConsole, spyExit } from "./test-utils"; | ||
|
||
describe("parseArgs()", () => { | ||
const mockConsole = spyConsole(); | ||
const mockExit = spyExit(); | ||
|
||
it("does not throw with no args", async () => { | ||
const result = parseArgs({ argv: [] }); | ||
expect(result.workspace).toBe(undefined); | ||
expect(result.fallback).toBe(undefined); | ||
}); | ||
|
||
it("outputs help text (--help)", async () => { | ||
parseArgs({ argv: ["--help"] }); | ||
expect(mockExit.exit).toHaveBeenCalledWith(0); | ||
expect(mockConsole.log).toHaveBeenCalledWith(help); | ||
}); | ||
|
||
it("outputs help text (-h)", async () => { | ||
parseArgs({ argv: ["-h"] }); | ||
expect(mockExit.exit).toHaveBeenCalledWith(0); | ||
expect(mockConsole.log).toHaveBeenCalledWith(help); | ||
}); | ||
|
||
it("outputs version text (--version)", async () => { | ||
parseArgs({ argv: ["--version"] }); | ||
expect(mockExit.exit).toHaveBeenCalledWith(0); | ||
expect(mockConsole.log).toHaveBeenCalledWith(pkg.version); | ||
}); | ||
|
||
it("outputs version text (-v)", async () => { | ||
parseArgs({ argv: ["-v"] }); | ||
expect(mockExit.exit).toHaveBeenCalledWith(0); | ||
expect(mockConsole.log).toHaveBeenCalledWith(pkg.version); | ||
}); | ||
|
||
it("correctly finds workspace", async () => { | ||
const result = parseArgs({ argv: ["this-workspace"] }); | ||
expect(result.workspace).toBe("this-workspace"); | ||
expect(result.fallback).toBe(undefined); | ||
expect(mockExit.exit).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
it("correctly finds fallback", async () => { | ||
const result = parseArgs({ argv: ["--fallback=false"] }); | ||
expect(result.workspace).toBe(undefined); | ||
expect(result.fallback).toBe("false"); | ||
expect(mockExit.exit).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
it("uses default fallback if incorrectly specified", async () => { | ||
const result = parseArgs({ argv: ["--fallback"] }); | ||
expect(result.workspace).toBe(undefined); | ||
expect(result.fallback).toBe(undefined); | ||
expect(mockExit.exit).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
it("correctly finds fallback and workspace", async () => { | ||
const result = parseArgs({ | ||
argv: ["this-workspace", "--fallback=false"], | ||
}); | ||
expect(result.workspace).toBe("this-workspace"); | ||
expect(result.fallback).toBe("false"); | ||
expect(mockExit.exit).toHaveBeenCalledTimes(0); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { getComparison } from "../src/getComparison"; | ||
import { spyConsole, validateLogs } from "./test-utils"; | ||
|
||
describe("getComparison()", () => { | ||
const mockConsole = spyConsole(); | ||
it("uses headRelative comparison when not running Vercel CI", async () => { | ||
expect(getComparison({ workspace: "test-workspace" })) | ||
.toMatchInlineSnapshot(` | ||
Object { | ||
"ref": "HEAD^", | ||
"type": "headRelative", | ||
} | ||
`); | ||
}); | ||
|
||
it("returns null when running in Vercel CI with no VERCEL_GIT_PREVIOUS_SHA and fallback disabled", async () => { | ||
process.env.VERCEL = "1"; | ||
process.env.VERCEL_GIT_COMMIT_REF = "my-branch"; | ||
expect( | ||
getComparison({ workspace: "test-workspace", fallback: "false" }) | ||
).toBeNull(); | ||
expect(mockConsole.log).toHaveBeenCalledWith( | ||
"≫ ", | ||
'no previous deployments found for "test-workspace" on "my-branch".' | ||
); | ||
}); | ||
|
||
it("uses default fallback when running in Vercel CI with no VERCEL_GIT_PREVIOUS_SHA", async () => { | ||
process.env.VERCEL = "1"; | ||
process.env.VERCEL_GIT_COMMIT_REF = "my-branch"; | ||
expect(getComparison({ workspace: "test-workspace" })) | ||
.toMatchInlineSnapshot(` | ||
Object { | ||
"ref": "HEAD^", | ||
"type": "customFallback", | ||
} | ||
`); | ||
|
||
validateLogs( | ||
[ | ||
'no previous deployments found for "test-workspace" on "my-branch".', | ||
"falling back to HEAD^", | ||
], | ||
mockConsole.log | ||
); | ||
}); | ||
|
||
it("uses custom fallback when running in Vercel CI with no VERCEL_GIT_PREVIOUS_SHA", async () => { | ||
process.env.VERCEL = "1"; | ||
process.env.VERCEL_GIT_COMMIT_REF = "my-branch"; | ||
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" on "my-branch".' | ||
); | ||
expect(mockConsole.log).toHaveBeenNthCalledWith( | ||
2, | ||
"≫ ", | ||
"falling back to HEAD^2" | ||
); | ||
}); | ||
|
||
it("uses previousDeploy when running in Vercel CI with VERCEL_GIT_PREVIOUS_SHA", async () => { | ||
process.env.VERCEL = "1"; | ||
process.env.VERCEL_GIT_PREVIOUS_SHA = "mygitsha"; | ||
process.env.VERCEL_GIT_COMMIT_REF = "my-branch"; | ||
expect(getComparison({ workspace: "test-workspace" })) | ||
.toMatchInlineSnapshot(` | ||
Object { | ||
"ref": "mygitsha", | ||
"type": "previousDeploy", | ||
} | ||
`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { getWorkspace } from "../src/getWorkspace"; | ||
import { spyConsole, validateLogs } from "./test-utils"; | ||
|
||
describe("getWorkspace()", () => { | ||
const mockConsole = spyConsole(); | ||
it("getWorkspace returns workspace from arg", async () => { | ||
expect( | ||
getWorkspace({ | ||
workspace: "test-workspace", | ||
}) | ||
).toEqual("test-workspace"); | ||
validateLogs( | ||
['using "test-workspace" as workspace from arguments'], | ||
mockConsole.log | ||
); | ||
}); | ||
|
||
it("getWorkspace returns workspace from package.json", async () => { | ||
expect( | ||
getWorkspace({ | ||
directory: "./__fixtures__/app", | ||
}) | ||
).toEqual("test-app"); | ||
expect(mockConsole.log).toHaveBeenCalledWith( | ||
"≫ ", | ||
'inferred "test-app" as workspace from "package.json"' | ||
); | ||
}); | ||
|
||
it("getWorkspace used current directory if not specified", async () => { | ||
expect(getWorkspace({})).toEqual("turbo-ignore"); | ||
expect(mockConsole.log).toHaveBeenCalledWith( | ||
"≫ ", | ||
'inferred "turbo-ignore" as workspace from "package.json"' | ||
); | ||
}); | ||
|
||
it("getWorkspace returns null when no arg is provided and package.json is missing name field", async () => { | ||
expect( | ||
getWorkspace({ | ||
directory: "./__fixtures__/invalid-app", | ||
}) | ||
).toEqual(null); | ||
expect(mockConsole.error).toHaveBeenCalledWith( | ||
"≫ ", | ||
'"__fixtures__/invalid-app/package.json" is missing the "name" field (required).' | ||
); | ||
}); | ||
|
||
it("getWorkspace returns null when no arg is provided and package.json can be found", async () => { | ||
expect( | ||
getWorkspace({ | ||
directory: "./__fixtures__/no-app", | ||
}) | ||
).toEqual(null); | ||
expect(mockConsole.error).toHaveBeenCalledWith( | ||
"≫ ", | ||
'"__fixtures__/no-app/package.json" could not be found. turbo-ignore inferencing failed' | ||
); | ||
}); | ||
}); |
Oops, something went wrong.