-
Notifications
You must be signed in to change notification settings - Fork 450
Add non-mocked test for symlink #1308
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f1c0f96
Add non-mocked test for symlink
coyotte508 1bb2f16
relative symlink
coyotte508 609660e
one more test
coyotte508 3bcf70d
comment out mocked tests
coyotte508 531ea58
add unit test for windows
coyotte508 7840b33
maybe I went too far
coyotte508 a384e10
use os-spcific dir instead of /tmp
coyotte508 6cfb738
maybe all tests work on windows
coyotte508 0f79780
increase timeout
coyotte508 9c99a87
remove internal loggin
coyotte508 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -1,63 +1,89 @@ | ||
import { describe, test, expect, vi, beforeEach } from "vitest"; | ||
import * as fs from "node:fs/promises"; | ||
/* eslint-disable @typescript-eslint/consistent-type-imports */ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { describe, expect, it, vi } from "vitest"; | ||
import { createSymlink } from "./symlink"; | ||
import * as path from "node:path"; | ||
import type { FileHandle } from "node:fs/promises"; | ||
|
||
vi.mock("node:fs/promises", () => ({ | ||
rm: vi.fn(), | ||
symlink: vi.fn(), | ||
rename: vi.fn(), | ||
copyFile: vi.fn(), | ||
mkdir: vi.fn(), | ||
mkdtemp: vi.fn(), | ||
open: vi.fn(), | ||
})); | ||
import { readFileSync, writeFileSync } from "node:fs"; | ||
import { lstat, rm } from "node:fs/promises"; | ||
import { tmpdir } from "node:os"; | ||
import { join } from "node:path"; | ||
|
||
let failSymlink = false; | ||
vi.mock("node:fs/promises", async (importOriginal) => ({ | ||
...(await importOriginal<typeof import("node:fs/promises")>()), | ||
symlink: async (...args: any[]) => { | ||
if (failSymlink) { | ||
failSymlink = false; | ||
throw new Error("Symlink not supported"); | ||
} | ||
|
||
vi.mock("node:os", () => ({ | ||
platform: vi.fn(), | ||
// @ts-expect-error - ignore | ||
return (await importOriginal<typeof import("node:fs/promises")>()).symlink(...args); | ||
}, | ||
})); | ||
|
||
describe("createSymlink", () => { | ||
const src = "/path/to/src"; | ||
const dst = "/path/to/dst"; | ||
describe("utils/symlink", () => { | ||
it("should create a symlink", async () => { | ||
writeFileSync(join(tmpdir(), "test.txt"), "hello world"); | ||
await createSymlink({ | ||
sourcePath: join(tmpdir(), "test.txt"), | ||
finalPath: join(tmpdir(), "test-symlink.txt"), | ||
}); | ||
|
||
beforeEach(() => { | ||
vi.resetAllMocks(); | ||
vi.mocked(fs.mkdtemp).mockImplementation(async (prefix) => `${prefix}/temp`); | ||
vi.mocked(fs.open).mockResolvedValue({ close: vi.fn() } as unknown as FileHandle); | ||
}); | ||
const stats = await lstat(join(tmpdir(), "test-symlink.txt")); | ||
expect(stats.isSymbolicLink()).toBe(process.platform !== "win32"); | ||
|
||
test("should remove existing destination", async () => { | ||
await createSymlink(dst, src); | ||
expect(fs.rm).toHaveBeenCalledWith(dst); | ||
// Test file content | ||
const content = readFileSync(join(tmpdir(), "test-symlink.txt"), "utf8"); | ||
expect(content).toBe("hello world"); | ||
|
||
// Cleanup | ||
await rm(join(tmpdir(), "test-symlink.txt")); | ||
await rm(join(tmpdir(), "test.txt")); | ||
}); | ||
|
||
describe("symlink not supported (Windows)", () => { | ||
beforeEach(() => { | ||
vi.mocked(fs.symlink).mockRejectedValue(new Error("Symlink not supported")); | ||
it("should work when symlinking twice", async () => { | ||
writeFileSync(join(tmpdir(), "test.txt"), "hello world"); | ||
writeFileSync(join(tmpdir(), "test2.txt"), "hello world2"); | ||
await createSymlink({ | ||
sourcePath: join(tmpdir(), "test.txt"), | ||
finalPath: join(tmpdir(), "test-symlink.txt"), | ||
}); | ||
|
||
test("should copyfile", async () => { | ||
await createSymlink(dst, src); | ||
expect(fs.copyFile).toHaveBeenCalledWith(path.resolve(dst), path.resolve(src)); | ||
await createSymlink({ | ||
sourcePath: join(tmpdir(), "test2.txt"), | ||
finalPath: join(tmpdir(), "test-symlink.txt"), | ||
}); | ||
|
||
test("should rename file if new_blob is true", async () => { | ||
await createSymlink(dst, src, true); | ||
expect(fs.rename).toHaveBeenCalledWith(path.resolve(dst), path.resolve(src)); | ||
}); | ||
const stats = await lstat(join(tmpdir(), "test-symlink.txt")); | ||
expect(stats.isSymbolicLink()).toBe(process.platform !== "win32"); | ||
|
||
// Test file content | ||
const content = readFileSync(join(tmpdir(), "test-symlink.txt"), "utf8"); | ||
expect(content).toBe("hello world2"); | ||
|
||
// Cleanup | ||
await rm(join(tmpdir(), "test-symlink.txt")); | ||
await rm(join(tmpdir(), "test.txt")); | ||
await rm(join(tmpdir(), "test2.txt")); | ||
}); | ||
|
||
describe("symlink supported", () => { | ||
test("should symlink", async () => { | ||
await createSymlink(dst, src); | ||
expect(fs.symlink).toHaveBeenCalledWith(path.resolve(dst), path.resolve(src)); | ||
}); | ||
it("should work when symlink doesn't work (windows)", async () => { | ||
writeFileSync(join(tmpdir(), "test.txt"), "hello world"); | ||
|
||
test("should symlink if new_blob is true", async () => { | ||
await createSymlink(dst, src, true); | ||
expect(fs.symlink).toHaveBeenCalledWith(path.resolve(dst), path.resolve(src)); | ||
failSymlink = true; | ||
await createSymlink({ | ||
sourcePath: join(tmpdir(), "test.txt"), | ||
finalPath: join(tmpdir(), "test-symlink.txt"), | ||
}); | ||
|
||
const stats = await lstat(join(tmpdir(), "test-symlink.txt")); | ||
expect(stats.isSymbolicLink()).toBe(false); | ||
|
||
// Test file content | ||
const content = readFileSync(join(tmpdir(), "test-symlink.txt"), "utf8"); | ||
expect(content).toBe("hello world"); | ||
|
||
// Cleanup | ||
await rm(join(tmpdir(), "test-symlink.txt")); | ||
await rm(join(tmpdir(), "test.txt")); | ||
}); | ||
}); |
This file contains hidden or 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 hidden or 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
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.
Not sure to get why we are changing the flag here
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.
It's to reset the function once it's called. (so it doesn't impact other tests)
Could be done more elegantly