-
-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add validation for readOptions with zod #826
Merged
JoshuaKGoldberg
merged 21 commits into
JoshuaKGoldberg:main
from
nowyDEV:validate-options-zod
Sep 18, 2023
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
3927eb1
fininsh implementation
nowyDEV 01db7a2
fix type issues
nowyDEV 273d5b9
refactor
nowyDEV e96f283
add error handling
nowyDEV 99e2c0a
fix knip check
nowyDEV 8624778
refactor
nowyDEV f2f3a9b
add test for zod validation
nowyDEV dabba56
fix spelling
nowyDEV 6e9bdc7
add test case for successful run
nowyDEV 8b790d4
fix tests
nowyDEV 3e7c57c
fix tests v2
nowyDEV 8ad3aa6
address review comments
nowyDEV 9a71569
Merge branch 'main' into validate-options-zod
nowyDEV 926715f
cleanup added package
nowyDEV abef694
Merge branch 'main' into validate-options-zod
nowyDEV 78aa8a3
Merge branch 'main' into validate-options-zod
JoshuaKGoldberg b6ea877
fix error display inside prompt
nowyDEV 763c1a9
use assertion instead of runtime check
nowyDEV 1334977
extract runners to separate variable
nowyDEV cd18fe1
Merge branch 'main' into validate-options-zod
nowyDEV bcb68aa
fix test
nowyDEV File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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,92 @@ | ||
import { describe, expect, it, vi } from "vitest"; | ||
import z from "zod"; | ||
|
||
import { readOptions } from "./readOptions.js"; | ||
|
||
const emptyOptions = { | ||
author: undefined, | ||
base: undefined, | ||
createRepository: undefined, | ||
description: undefined, | ||
email: undefined, | ||
excludeCompliance: undefined, | ||
excludeContributors: undefined, | ||
excludeLintJson: undefined, | ||
excludeLintKnip: undefined, | ||
excludeLintMd: undefined, | ||
excludeLintPackageJson: undefined, | ||
excludeLintPackages: undefined, | ||
excludeLintPerfectionist: undefined, | ||
excludeLintSpelling: undefined, | ||
excludeLintYml: undefined, | ||
excludeReleases: undefined, | ||
excludeRenovate: undefined, | ||
excludeTests: undefined, | ||
funding: undefined, | ||
owner: undefined, | ||
repository: undefined, | ||
skipGitHubApi: false, | ||
skipInstall: false, | ||
skipRemoval: false, | ||
skipRestore: undefined, | ||
skipUninstall: false, | ||
title: undefined, | ||
}; | ||
|
||
const mockOptions = { | ||
base: "prompt", | ||
github: "mock.git", | ||
repository: "mock.repository", | ||
}; | ||
|
||
vi.mock("./getPrefillOrPromptedOption.js", () => ({ | ||
getPrefillOrPromptedOption() { | ||
return () => "mock"; | ||
}, | ||
})); | ||
|
||
vi.mock("./ensureRepositoryExists.js", () => ({ | ||
ensureRepositoryExists() { | ||
return { | ||
github: mockOptions.github, | ||
repository: mockOptions.repository, | ||
}; | ||
}, | ||
})); | ||
|
||
vi.mock("../../shared/cli/spinners.ts", () => ({ | ||
withSpinner() { | ||
return () => ({}); | ||
}, | ||
})); | ||
|
||
vi.mock("./augmentOptionsWithExcludes.js", () => ({ | ||
augmentOptionsWithExcludes() { | ||
return { ...emptyOptions, ...mockOptions }; | ||
}, | ||
})); | ||
|
||
describe("readOptions", () => { | ||
it("cancels the function when --email is invalid", async () => { | ||
const validationResult = z | ||
.object({ email: z.string().email() }) | ||
.safeParse({ email: "wrongEmail" }); | ||
|
||
expect(await readOptions(["--email", "wrongEmail"])).toStrictEqual({ | ||
cancelled: true, | ||
options: { ...emptyOptions, email: "wrongEmail" }, | ||
zodError: (validationResult as z.SafeParseError<{ email: string }>).error, | ||
}); | ||
}); | ||
|
||
it("successfully runs the function when --base is valid", async () => { | ||
expect(await readOptions(["--base", mockOptions.base])).toStrictEqual({ | ||
cancelled: false, | ||
github: mockOptions.github, | ||
options: { | ||
...emptyOptions, | ||
...mockOptions, | ||
}, | ||
}); | ||
}); | ||
}); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Praise] 💯 love it! No notes!