Skip to content

Commit

Permalink
Adds e2e tests for CI env and --force flag
Browse files Browse the repository at this point in the history
  • Loading branch information
ajkpersonal committed Nov 27, 2024
1 parent ac96a58 commit cb03371
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 15 deletions.
31 changes: 21 additions & 10 deletions packages/cli/cli/src/__test__/checkOutputDirectory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean>);
};
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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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();
Expand Down
9 changes: 4 additions & 5 deletions packages/cli/cli/src/__test__/checkOutputDirectoryCI.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CliContext>;

beforeEach(() => {
originalEnv = process.env;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
});

0 comments on commit cb03371

Please sign in to comment.