-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests and force flag for directory overwrite fix
- Loading branch information
1 parent
d7209f4
commit ac96a58
Showing
6 changed files
with
194 additions
and
22 deletions.
There are no files selected for viewing
124 changes: 124 additions & 0 deletions
124
packages/cli/cli/src/__test__/checkOutputDirectory.test.ts
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,124 @@ | ||
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 { getOutputDirectories } from "../persistence/getOutputDirectories"; | ||
import { storeOutputDirectories } from "../persistence/storeOutputDirectories"; | ||
import { describe, it, expect, beforeEach, vi, Mock } from "vitest"; | ||
|
||
describe("checkOutputDirectory", () => { | ||
let mockCliContext: { | ||
confirmPrompt: Mock; | ||
}; | ||
|
||
beforeEach(() => { | ||
mockCliContext = { | ||
confirmPrompt: vi.fn() | ||
}; | ||
}); | ||
|
||
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); | ||
|
||
expect(result).toEqual({ | ||
shouldProceed: true | ||
}); | ||
expect(mockCliContext.confirmPrompt).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("doesn't prompt if directory is empty", async () => { | ||
const tmpDir = await tmp.dir(); | ||
const emptyDir = join(AbsoluteFilePath.of(tmpDir.path), RelativeFilePath.of("empty")); | ||
await mkdir(emptyDir); | ||
|
||
const result = await checkOutputDirectory(emptyDir, mockCliContext as any, false); | ||
|
||
expect(result).toEqual({ | ||
shouldProceed: true | ||
}); | ||
expect(mockCliContext.confirmPrompt).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("prompts for confirmation if directory has files and not in safelist", async () => { | ||
const tmpDir = await tmp.dir(); | ||
const dirWithFiles = join(AbsoluteFilePath.of(tmpDir.path), RelativeFilePath.of("with-files")); | ||
await mkdir(dirWithFiles); | ||
await writeFile(join(dirWithFiles, RelativeFilePath.of("test.txt")), "test"); | ||
|
||
mockCliContext.confirmPrompt.mockResolvedValueOnce(true); | ||
|
||
const result = await checkOutputDirectory(dirWithFiles, mockCliContext as any, false); | ||
|
||
expect(result).toEqual({ | ||
shouldProceed: true | ||
}); | ||
expect(mockCliContext.confirmPrompt).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it("doesn't prompt if directory is in safelist", async () => { | ||
const tmpDir = await tmp.dir(); | ||
const safelistedDir = join(AbsoluteFilePath.of(tmpDir.path), RelativeFilePath.of("safelisted")); | ||
await mkdir(safelistedDir); | ||
await writeFile(join(safelistedDir, RelativeFilePath.of("test.txt")), "test"); | ||
|
||
// Add to safelist | ||
await storeOutputDirectories([safelistedDir]); | ||
|
||
const result = await checkOutputDirectory(safelistedDir, mockCliContext as any, false); | ||
|
||
expect(result).toEqual({ | ||
shouldProceed: true | ||
}); | ||
expect(mockCliContext.confirmPrompt).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("saves directory to safelist when requested", async () => { | ||
const tmpDir = await tmp.dir(); | ||
const dirToSafelist = join(AbsoluteFilePath.of(tmpDir.path), RelativeFilePath.of("to-safelist")); | ||
await mkdir(dirToSafelist); | ||
await writeFile(join(dirToSafelist, RelativeFilePath.of("test.txt")), "test"); | ||
|
||
mockCliContext.confirmPrompt.mockResolvedValueOnce(true); | ||
|
||
const result = await checkOutputDirectory(dirToSafelist, mockCliContext as any, false); | ||
|
||
expect(result).toEqual({ | ||
shouldProceed: true | ||
}); | ||
|
||
// Verify directory was added to safelist | ||
const savedDirectories = await getOutputDirectories(); | ||
expect(savedDirectories).toContain(dirToSafelist); | ||
}); | ||
|
||
it("doesn't proceed if user declines overwrite", async () => { | ||
const tmpDir = await tmp.dir(); | ||
const dirWithFiles = join(AbsoluteFilePath.of(tmpDir.path), RelativeFilePath.of("with-files")); | ||
await mkdir(dirWithFiles); | ||
await writeFile(join(dirWithFiles, RelativeFilePath.of("test.txt")), "test"); | ||
|
||
mockCliContext.confirmPrompt.mockResolvedValueOnce(false); // overwrite prompt | ||
|
||
const result = await checkOutputDirectory(dirWithFiles, mockCliContext as any, false); | ||
|
||
expect(result).toEqual({ | ||
shouldProceed: false | ||
}); | ||
expect(mockCliContext.confirmPrompt).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it("doesn't prompt if force is true", async () => { | ||
const tmpDir = await tmp.dir(); | ||
const dirWithFiles = join(AbsoluteFilePath.of(tmpDir.path), RelativeFilePath.of("with-files")); | ||
await mkdir(dirWithFiles); | ||
await writeFile(join(dirWithFiles, RelativeFilePath.of("test.txt")), "test"); | ||
|
||
const result = await checkOutputDirectory(dirWithFiles, mockCliContext as any, true); | ||
|
||
expect(result).toEqual({ shouldProceed: true }); | ||
expect(mockCliContext.confirmPrompt).not.toHaveBeenCalled(); | ||
}); | ||
}); |
42 changes: 42 additions & 0 deletions
42
packages/cli/cli/src/__test__/checkOutputDirectoryCI.test.ts
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,42 @@ | ||
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"; | ||
|
||
describe("checkOutputDirectory in CI", () => { | ||
let originalEnv: NodeJS.ProcessEnv; | ||
let mockCliContext: { | ||
confirmPrompt: Mock; | ||
}; | ||
|
||
beforeEach(() => { | ||
originalEnv = process.env; | ||
process.env = { | ||
...process.env, | ||
CI: "true" | ||
}; | ||
|
||
mockCliContext = { | ||
confirmPrompt: vi.fn() | ||
}; | ||
}); | ||
|
||
afterEach(() => { | ||
process.env = originalEnv; | ||
}); | ||
|
||
it("doesn't prompt in CI environment even with files present", async () => { | ||
const tmpDir = await tmp.dir(); | ||
const dirWithFiles = join(AbsoluteFilePath.of(tmpDir.path), RelativeFilePath.of("with-files")); | ||
await mkdir(dirWithFiles); | ||
await writeFile(join(dirWithFiles, RelativeFilePath.of("test.txt")), "test"); | ||
|
||
const result = await checkOutputDirectory(dirWithFiles, mockCliContext as any, false); | ||
|
||
expect(result).toEqual({ | ||
shouldProceed: true | ||
}); | ||
expect(mockCliContext.confirmPrompt).not.toHaveBeenCalled(); | ||
}); | ||
}); |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.