-
Notifications
You must be signed in to change notification settings - Fork 2.6k
refactor(storage): better fs check #7164
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| import * as vscode from "vscode" | ||
|
|
||
| vi.mock("fs/promises", async () => { | ||
| const mod = await import("../../__mocks__/fs/promises") | ||
| return (mod as any).default ?? mod | ||
| }) | ||
|
|
||
| describe("getStorageBasePath - customStoragePath", () => { | ||
| const defaultPath = "/test/global-storage" | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks() | ||
| }) | ||
|
|
||
| it("returns the configured custom path when it is writable", async () => { | ||
| const customPath = "/test/storage/path" | ||
| vi.spyOn(vscode.workspace, "getConfiguration").mockReturnValue({ | ||
| get: vi.fn().mockReturnValue(customPath), | ||
| } as any) | ||
|
|
||
| const fsPromises = await import("fs/promises") | ||
| const { getStorageBasePath } = await import("../storage") | ||
|
|
||
| const result = await getStorageBasePath(defaultPath) | ||
|
|
||
| expect(result).toBe(customPath) | ||
| expect((fsPromises as any).mkdir).toHaveBeenCalledWith(customPath, { recursive: true }) | ||
| expect((fsPromises as any).access).toHaveBeenCalledWith(customPath, 7) // 7 = R_OK(4) | W_OK(2) | X_OK(1) | ||
| }) | ||
|
|
||
| it("falls back to default and shows an error when custom path is not writable", async () => { | ||
| const customPath = "/test/storage/unwritable" | ||
|
|
||
| vi.spyOn(vscode.workspace, "getConfiguration").mockReturnValue({ | ||
| get: vi.fn().mockReturnValue(customPath), | ||
| } as any) | ||
|
|
||
| const showErrorSpy = vi.spyOn(vscode.window, "showErrorMessage").mockResolvedValue(undefined as any) | ||
|
|
||
| const fsPromises = await import("fs/promises") | ||
| const { getStorageBasePath } = await import("../storage") | ||
|
|
||
| await (fsPromises as any).mkdir(customPath, { recursive: true }) | ||
|
|
||
| const accessMock = (fsPromises as any).access as ReturnType<typeof vi.fn> | ||
| accessMock.mockImplementationOnce(async (p: string) => { | ||
| if (p === customPath) { | ||
| const err: any = new Error("EACCES: permission denied") | ||
| err.code = "EACCES" | ||
| throw err | ||
| } | ||
| return Promise.resolve() | ||
| }) | ||
|
|
||
| const result = await getStorageBasePath(defaultPath) | ||
|
|
||
| expect(result).toBe(defaultPath) | ||
| expect(showErrorSpy).toHaveBeenCalledTimes(1) | ||
| const firstArg = showErrorSpy.mock.calls[0][0] | ||
| expect(typeof firstArg).toBe("string") | ||
| }) | ||
| it("returns the default path when customStoragePath is an empty string and does not touch fs", async () => { | ||
| vi.spyOn(vscode.workspace, "getConfiguration").mockReturnValue({ | ||
| get: vi.fn().mockReturnValue(""), | ||
| } as any) | ||
|
|
||
| const fsPromises = await import("fs/promises") | ||
| const { getStorageBasePath } = await import("../storage") | ||
|
|
||
| const result = await getStorageBasePath(defaultPath) | ||
|
|
||
| expect(result).toBe(defaultPath) | ||
| expect((fsPromises as any).mkdir).not.toHaveBeenCalled() | ||
| expect((fsPromises as any).access).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it("falls back to default when mkdir fails and does not attempt access", async () => { | ||
| const customPath = "/test/storage/failmkdir" | ||
|
|
||
| vi.spyOn(vscode.workspace, "getConfiguration").mockReturnValue({ | ||
| get: vi.fn().mockReturnValue(customPath), | ||
| } as any) | ||
|
|
||
| const showErrorSpy = vi.spyOn(vscode.window, "showErrorMessage").mockResolvedValue(undefined as any) | ||
|
|
||
| const fsPromises = await import("fs/promises") | ||
| const { getStorageBasePath } = await import("../storage") | ||
|
|
||
| const mkdirMock = (fsPromises as any).mkdir as ReturnType<typeof vi.fn> | ||
| mkdirMock.mockImplementationOnce(async (p: string) => { | ||
| if (p === customPath) { | ||
| const err: any = new Error("EACCES: permission denied") | ||
| err.code = "EACCES" | ||
| throw err | ||
| } | ||
| return Promise.resolve() | ||
| }) | ||
|
|
||
| const result = await getStorageBasePath(defaultPath) | ||
|
|
||
| expect(result).toBe(defaultPath) | ||
| expect((fsPromises as any).access).not.toHaveBeenCalled() | ||
| expect(showErrorSpy).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| it("passes the correct permission flags (R_OK | W_OK | X_OK) to fs.access", async () => { | ||
| const customPath = "/test/storage/path" | ||
| vi.spyOn(vscode.workspace, "getConfiguration").mockReturnValue({ | ||
| get: vi.fn().mockReturnValue(customPath), | ||
| } as any) | ||
|
|
||
| const fsPromises = await import("fs/promises") | ||
| const { getStorageBasePath } = await import("../storage") | ||
|
|
||
| await getStorageBasePath(defaultPath) | ||
|
|
||
| const constants = (fsPromises as any).constants | ||
| const expectedFlags = constants.R_OK | constants.W_OK | constants.X_OK | ||
|
|
||
| expect((fsPromises as any).access).toHaveBeenCalledWith(customPath, expectedFlags) | ||
| }) | ||
|
|
||
| it("falls back when directory is readable but not writable (partial permissions)", async () => { | ||
| const customPath = "/test/storage/readonly" | ||
| vi.spyOn(vscode.workspace, "getConfiguration").mockReturnValue({ | ||
| get: vi.fn().mockReturnValue(customPath), | ||
| } as any) | ||
|
|
||
| const showErrorSpy = vi.spyOn(vscode.window, "showErrorMessage").mockResolvedValue(undefined as any) | ||
|
|
||
| const fsPromises = await import("fs/promises") | ||
| const { getStorageBasePath } = await import("../storage") | ||
|
|
||
| const accessMock = (fsPromises as any).access as ReturnType<typeof vi.fn> | ||
| const constants = (fsPromises as any).constants | ||
| accessMock.mockImplementationOnce(async (p: string, mode?: number) => { | ||
| // Simulate readable (R_OK) but not writable/executable (W_OK | X_OK) | ||
| if (p === customPath && mode && mode & (constants.W_OK | constants.X_OK)) { | ||
| const err: any = new Error("EACCES: permission denied") | ||
| err.code = "EACCES" | ||
| throw err | ||
| } | ||
| return Promise.resolve() | ||
| }) | ||
|
|
||
| const result = await getStorageBasePath(defaultPath) | ||
|
|
||
| expect(result).toBe(defaultPath) | ||
| expect(showErrorSpy).toHaveBeenCalledTimes(1) | ||
| }) | ||
| }) | ||
elianiva marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.