diff --git a/packages/cli/cli/src/__test__/checkOutputDirectory.test.ts b/packages/cli/cli/src/__test__/checkOutputDirectory.test.ts index 66dfd102232..d90d072ac17 100644 --- a/packages/cli/cli/src/__test__/checkOutputDirectory.test.ts +++ b/packages/cli/cli/src/__test__/checkOutputDirectory.test.ts @@ -4,24 +4,35 @@ import tmp from "tmp-promise"; import { checkOutputDirectory } from "../commands/generate/checkOutputDirectory"; import { getOutputDirectories } from "../persistence/getOutputDirectories"; import { storeOutputDirectories } from "../persistence/storeOutputDirectories"; -import { describe, it, expect, beforeEach, vi, Mock } from "vitest"; +import { describe, it, expect, beforeEach, vi, Mock, afterEach } from "vitest"; +import { CliContext } from "../cli-context/CliContext"; describe("checkOutputDirectory", () => { let mockCliContext: { - confirmPrompt: Mock; + confirmPrompt: Mock & ((message: string, defaultValue?: boolean) => Promise); }; + let originalEnv: NodeJS.ProcessEnv; beforeEach(() => { mockCliContext = { - confirmPrompt: vi.fn() + confirmPrompt: vi.fn().mockImplementation(async () => true) }; + originalEnv = process.env; + process.env = { + ...process.env, + CI: "false" + }; + }); + + afterEach(() => { + process.env = originalEnv; }); it("doesn't prompt if directory doesn't exist", async () => { const tmpDir = await tmp.dir(); const nonExistentPath = join(AbsoluteFilePath.of(tmpDir.path), RelativeFilePath.of("non-existent")); - const result = await checkOutputDirectory(nonExistentPath, mockCliContext as any, false); + const result = await checkOutputDirectory(nonExistentPath, mockCliContext as unknown as CliContext, false); expect(result).toEqual({ shouldProceed: true @@ -34,7 +45,7 @@ describe("checkOutputDirectory", () => { const emptyDir = join(AbsoluteFilePath.of(tmpDir.path), RelativeFilePath.of("empty")); await mkdir(emptyDir); - const result = await checkOutputDirectory(emptyDir, mockCliContext as any, false); + const result = await checkOutputDirectory(emptyDir, mockCliContext as unknown as CliContext, false); expect(result).toEqual({ shouldProceed: true @@ -50,7 +61,7 @@ describe("checkOutputDirectory", () => { mockCliContext.confirmPrompt.mockResolvedValueOnce(true); - const result = await checkOutputDirectory(dirWithFiles, mockCliContext as any, false); + const result = await checkOutputDirectory(dirWithFiles, mockCliContext as unknown as CliContext, false); expect(result).toEqual({ shouldProceed: true @@ -67,7 +78,7 @@ describe("checkOutputDirectory", () => { // Add to safelist await storeOutputDirectories([safelistedDir]); - const result = await checkOutputDirectory(safelistedDir, mockCliContext as any, false); + const result = await checkOutputDirectory(safelistedDir, mockCliContext as unknown as CliContext, false); expect(result).toEqual({ shouldProceed: true @@ -83,7 +94,7 @@ describe("checkOutputDirectory", () => { mockCliContext.confirmPrompt.mockResolvedValueOnce(true); - const result = await checkOutputDirectory(dirToSafelist, mockCliContext as any, false); + const result = await checkOutputDirectory(dirToSafelist, mockCliContext as unknown as CliContext, false); expect(result).toEqual({ shouldProceed: true @@ -102,7 +113,7 @@ describe("checkOutputDirectory", () => { mockCliContext.confirmPrompt.mockResolvedValueOnce(false); // overwrite prompt - const result = await checkOutputDirectory(dirWithFiles, mockCliContext as any, false); + const result = await checkOutputDirectory(dirWithFiles, mockCliContext as unknown as CliContext, false); expect(result).toEqual({ shouldProceed: false @@ -116,7 +127,7 @@ describe("checkOutputDirectory", () => { await mkdir(dirWithFiles); await writeFile(join(dirWithFiles, RelativeFilePath.of("test.txt")), "test"); - const result = await checkOutputDirectory(dirWithFiles, mockCliContext as any, true); + const result = await checkOutputDirectory(dirWithFiles, mockCliContext as unknown as CliContext, true); expect(result).toEqual({ shouldProceed: true }); expect(mockCliContext.confirmPrompt).not.toHaveBeenCalled(); diff --git a/packages/cli/cli/src/__test__/checkOutputDirectoryCI.test.ts b/packages/cli/cli/src/__test__/checkOutputDirectoryCI.test.ts index 44e4572d47e..076d252958b 100644 --- a/packages/cli/cli/src/__test__/checkOutputDirectoryCI.test.ts +++ b/packages/cli/cli/src/__test__/checkOutputDirectoryCI.test.ts @@ -2,13 +2,12 @@ import { AbsoluteFilePath, join, RelativeFilePath } from "@fern-api/fs-utils"; import { mkdir, writeFile } from "fs/promises"; import tmp from "tmp-promise"; import { checkOutputDirectory } from "../commands/generate/checkOutputDirectory"; -import { describe, it, expect, beforeEach, afterEach, vi, Mock } from "vitest"; +import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; +import { CliContext } from "../cli-context/CliContext"; describe("checkOutputDirectory in CI", () => { let originalEnv: NodeJS.ProcessEnv; - let mockCliContext: { - confirmPrompt: Mock; - }; + let mockCliContext: Partial; beforeEach(() => { originalEnv = process.env; @@ -32,7 +31,7 @@ describe("checkOutputDirectory in CI", () => { await mkdir(dirWithFiles); await writeFile(join(dirWithFiles, RelativeFilePath.of("test.txt")), "test"); - const result = await checkOutputDirectory(dirWithFiles, mockCliContext as any, false); + const result = await checkOutputDirectory(dirWithFiles, mockCliContext as CliContext, false); expect(result).toEqual({ shouldProceed: true diff --git a/packages/cli/ete-tests/src/tests/generate/output-directory-prompts.test.ts b/packages/cli/ete-tests/src/tests/generate/output-directory-prompts.test.ts new file mode 100644 index 00000000000..77f6dff8c36 --- /dev/null +++ b/packages/cli/ete-tests/src/tests/generate/output-directory-prompts.test.ts @@ -0,0 +1,43 @@ +import { AbsoluteFilePath, join, RelativeFilePath } from "@fern-api/fs-utils"; +import { cp, mkdir } from "fs/promises"; +import tmp from "tmp-promise"; +import { runFernCli } from "../../utils/runFernCli"; +import stripAnsi from "strip-ansi"; +import { init } from "../init/init"; +import { Options } from "execa"; + +const envWithCI = { + CI: "true" +}; + +const envWithoutCI = { + CI: "false" +}; + +describe("output directory prompts", () => { + it("doesn't show prompts for CI environment", async () => { + const pathOfDirectory = await init(); + + const { stdout } = await runFernCli(["generate", "--local", "--keepDocker"], { + cwd: pathOfDirectory, + env: envWithCI + }); + + const cleanOutput = stripAnsi(stdout).trim(); + expect(cleanOutput).not.toContain("contains existing files"); + expect(cleanOutput).not.toContain("Would you like to save this"); + }, 180_000); + + it("doesn't show prompts with the --force flag", async () => { + const pathOfDirectory = await init(); + + const { stdout } = await runFernCli(["generate", "--local", "--keepDocker", "--force"], { + cwd: pathOfDirectory, + env: envWithoutCI + }); + + const cleanOutput = stripAnsi(stdout).trim(); + expect(cleanOutput).not.toContain("contains existing files"); + expect(cleanOutput).not.toContain("Would you like to save this"); + }, 180_000); +});