Skip to content
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

fix: corrected readOptions using provided values #1084

Merged
merged 2 commits into from
Dec 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions src/shared/options/getPrefillOrPromptedOption.test.ts
Original file line number Diff line number Diff line change
@@ -16,23 +16,23 @@ describe("getPrefillOrPromptedValue", () => {
it("returns the placeholder when auto is true and it exists", async () => {
const value = "Test Value";

const actual = await getPrefillOrPromptedOption(
"field",
true,
"Input message.",
vi.fn().mockResolvedValue(value),
);
const actual = await getPrefillOrPromptedOption({
auto: true,
getDefaultValue: vi.fn().mockResolvedValue(value),
message: "Input message.",
name: "field",
});

expect(actual).toEqual({ error: undefined, value });
});

it("returns an error when auto is true and no placeholder exists", async () => {
const actual = await getPrefillOrPromptedOption(
"field",
true,
"Input message.",
vi.fn().mockResolvedValue(undefined),
);
const actual = await getPrefillOrPromptedOption({
auto: true,
getDefaultValue: vi.fn().mockResolvedValue(undefined),
message: "Input message.",
name: "field",
});

expect(actual).toEqual({
error: "Could not infer a default value for field.",
@@ -43,7 +43,7 @@ describe("getPrefillOrPromptedValue", () => {
it("provides no placeholder when one is not provided and auto is false", async () => {
const message = "Test message";

await getPrefillOrPromptedOption("Input message.", false, message);
await getPrefillOrPromptedOption({ auto: false, message, name: "field" });

expect(mockText).toHaveBeenCalledWith({
message,
@@ -56,12 +56,12 @@ describe("getPrefillOrPromptedValue", () => {
const message = "Test message";
const placeholder = "Test placeholder";

const actual = await getPrefillOrPromptedOption(
"field",
false,
const actual = await getPrefillOrPromptedOption({
auto: false,
getDefaultValue: vi.fn().mockResolvedValue(placeholder),
message,
vi.fn().mockResolvedValue(placeholder),
);
name: "field",
});

expect(actual).toEqual({
error: undefined,
@@ -73,7 +73,7 @@ describe("getPrefillOrPromptedValue", () => {
it("validates entered text when it's not blank and auto is false", async () => {
const message = "Test message";

await getPrefillOrPromptedOption("Input message.", false, message);
await getPrefillOrPromptedOption({ auto: false, message, name: "field" });

const { validate } = (mockText.mock.calls[0] as [Required<TextOptions>])[0];

@@ -83,7 +83,7 @@ describe("getPrefillOrPromptedValue", () => {
it("invalidates entered text when it's blank and auto is false", async () => {
const message = "";

await getPrefillOrPromptedOption("Input message.", false, message);
await getPrefillOrPromptedOption({ auto: false, message, name: "field" });

const { validate } = (mockText.mock.calls[0] as [Required<TextOptions>])[0];

19 changes: 13 additions & 6 deletions src/shared/options/getPrefillOrPromptedOption.ts
Original file line number Diff line number Diff line change
@@ -2,12 +2,19 @@ import * as prompts from "@clack/prompts";

import { filterPromptCancel } from "../prompts.js";

export async function getPrefillOrPromptedOption(
name: string,
auto: boolean,
message: string,
getDefaultValue?: () => Promise<string | undefined>,
) {
export interface GetPrefillOrPromptedOptionOptions {
auto: boolean;
getDefaultValue?: () => Promise<string | undefined>;
message: string;
name: string;
}

export async function getPrefillOrPromptedOption({
auto,
getDefaultValue,
message,
name,
}: GetPrefillOrPromptedOptionOptions) {
const defaultValue = await getDefaultValue?.();

if (auto || defaultValue) {
184 changes: 180 additions & 4 deletions src/shared/options/readOptions.test.ts
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ import { describe, expect, it, vi } from "vitest";
import z from "zod";

import { Options } from "../types.js";
import { GetPrefillOrPromptedOptionOptions } from "./getPrefillOrPromptedOption.js";
import { optionsSchemaShape } from "./optionsSchema.js";
import { readOptions } from "./readOptions.js";

@@ -60,6 +61,14 @@ vi.mock("../cli/spinners.ts", () => ({
},
}));

const mockReadPackageData = vi.fn();

vi.mock("../packages.js", () => ({
get readPackageData() {
return mockReadPackageData;
},
}));

const mockAugmentOptionsWithExcludes = vi.fn();

vi.mock("./augmentOptionsWithExcludes.js", () => ({
@@ -113,10 +122,11 @@ vi.mock("./createOptionDefaults/index.js", () => ({
},
}));

const mockReadPackageData = vi.fn();
vi.mock("../packages.js", () => ({
get readPackageData() {
return mockReadPackageData;
const mockLogInferredOptions = vi.fn();

vi.mock("./logInferredOptions.js", () => ({
get logInferredOptions() {
return mockLogInferredOptions;
},
}));

@@ -595,4 +605,170 @@ describe("readOptions", () => {
},
});
});

it("uses values when provided on the CLI", async () => {
mockReadPackageData.mockImplementationOnce(() => ({}));
mockGetPrefillOrPromptedOption.mockImplementation(
async ({ getDefaultValue }: GetPrefillOrPromptedOptionOptions) => ({
value: (await getDefaultValue?.()) ?? "mock",
}),
);

const description = "Test description.";
const owner = "TestOwner";
const repository = "test-repository";
const title = "Test Title";

expect(
await readOptions(
[
"--offline",
"--description",
description,
"--owner",
owner,
"--repository",
repository,
"--title",
title,
],
"migrate",
),
).toStrictEqual({
cancelled: false,
github: mockOptions.github,
options: {
...emptyOptions,
...mockOptions,
access: "public",
base: "minimum",
description,
directory: repository,
email: {
github: "mock",
npm: "mock",
},
guide: undefined,
logo: undefined,
mode: "migrate",
offline: true,
owner,
skipAllContributorsApi: true,
skipGitHubApi: true,
title,
},
});

expect(mockLogInferredOptions).not.toHaveBeenCalled();
});

it("uses and logs values when provided on the CLI with auto", async () => {
mockReadPackageData.mockImplementationOnce(() => ({}));
mockGetPrefillOrPromptedOption.mockImplementation(
async ({ getDefaultValue }: GetPrefillOrPromptedOptionOptions) => ({
value: (await getDefaultValue?.()) ?? "mock",
}),
);

const description = "Test description.";
const owner = "TestOwner";
const repository = "test-repository";
const title = "Test Title";

expect(
await readOptions(
[
"--auto",
"--offline",
"--description",
description,
"--owner",
owner,
"--repository",
repository,
"--title",
title,
],
"migrate",
),
).toStrictEqual({
cancelled: false,
github: mockOptions.github,
options: {
...emptyOptions,
...mockOptions,
access: "public",
auto: true,
base: "minimum",
description,
directory: repository,
email: {
github: "mock",
npm: "mock",
},
guide: undefined,
logo: undefined,
mode: "migrate",
offline: true,
owner,
skipAllContributorsApi: true,
skipGitHubApi: true,
title,
},
});

expect(mockLogInferredOptions.mock.calls).toMatchInlineSnapshot(`
[
[
{
"access": "public",
"author": undefined,
"auto": true,
"base": "minimum",
"bin": undefined,
"description": "Test description.",
"directory": "test-repository",
"email": {
"github": "mock",
"npm": "mock",
},
"excludeAllContributors": undefined,
"excludeCompliance": undefined,
"excludeLintDeprecation": undefined,
"excludeLintESLint": undefined,
"excludeLintJSDoc": undefined,
"excludeLintJson": undefined,
"excludeLintKnip": undefined,
"excludeLintMd": undefined,
"excludeLintPackageJson": undefined,
"excludeLintPackages": undefined,
"excludeLintPerfectionist": undefined,
"excludeLintRegex": undefined,
"excludeLintSpelling": undefined,
"excludeLintStrict": undefined,
"excludeLintYml": undefined,
"excludeReleases": undefined,
"excludeRenovate": undefined,
"excludeTests": undefined,
"funding": undefined,
"github": "mock.git",
"guide": undefined,
"logo": undefined,
"mode": "migrate",
"offline": true,
"owner": "TestOwner",
"preserveGeneratedFrom": false,
"repository": "mock.repository",
"skipAllContributorsApi": true,
"skipGitHubApi": true,
"skipInstall": undefined,
"skipRemoval": undefined,
"skipRestore": undefined,
"skipUninstall": undefined,
"title": "Test Title",
},
],
]
`);
});
});
Loading