-
Notifications
You must be signed in to change notification settings - Fork 2.6k
fix: eliminate getStorageBasePath race condition and vscode popup #7174
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import { describe, it, expect, beforeEach, afterEach, vi } from "vitest" | ||
| import * as fs from "fs/promises" | ||
| import * as path from "path" | ||
| import * as os from "os" | ||
| import { getStorageBasePath } from "../storage" | ||
|
|
||
| // Mock VSCode | ||
| vi.mock("vscode", () => ({ | ||
| workspace: { | ||
| getConfiguration: vi.fn(), | ||
| }, | ||
| window: { | ||
| showErrorMessage: vi.fn(), | ||
| }, | ||
| })) | ||
|
|
||
| // Mock i18n | ||
| vi.mock("../../i18n", () => ({ | ||
| t: vi.fn((key: string) => key), | ||
| })) | ||
|
|
||
| describe("getStorageBasePath", () => { | ||
| let tempDir: string | ||
|
|
||
| beforeEach(async () => { | ||
| tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "storage-test-")) | ||
| vi.clearAllMocks() | ||
| }) | ||
|
|
||
| afterEach(async () => { | ||
| await fs.rm(tempDir, { recursive: true, force: true }) | ||
| vi.restoreAllMocks() | ||
| }) | ||
|
|
||
| it("should handle concurrent storage validations without race conditions", async () => { | ||
| const customPath = path.join(tempDir, "custom-storage") | ||
| const defaultPath = path.join(tempDir, "default-storage") | ||
|
|
||
| // Mock VSCode configuration to return custom path | ||
| const mockConfig = { | ||
| get: vi.fn().mockReturnValue(customPath), | ||
| has: vi.fn().mockReturnValue(true), | ||
| inspect: vi.fn(), | ||
| update: vi.fn(), | ||
| } as any | ||
|
|
||
| const vscode = await import("vscode") | ||
| vi.mocked(vscode.workspace.getConfiguration).mockReturnValue(mockConfig) | ||
|
|
||
| // Create the custom storage directory | ||
| await fs.mkdir(customPath, { recursive: true }) | ||
|
|
||
| // Run multiple concurrent storage validations | ||
| const concurrentCalls = Array(10) | ||
| .fill(null) | ||
| .map(() => getStorageBasePath(defaultPath)) | ||
|
|
||
| // All should succeed and return the custom path | ||
| const results = await Promise.all(concurrentCalls) | ||
|
|
||
| // Verify all calls succeeded | ||
| expect(results).toHaveLength(10) | ||
| results.forEach((result) => { | ||
| expect(result).toBe(customPath) | ||
| }) | ||
|
|
||
| // Verify no leftover test files (all should be cleaned up with unique names) | ||
| const dirContents = await fs.readdir(customPath) | ||
| const testFiles = dirContents.filter((file) => file.startsWith(".write_test")) | ||
| expect(testFiles).toHaveLength(0) | ||
| }) | ||
| }) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,8 +32,9 @@ export async function getStorageBasePath(defaultPath: string): Promise<string> { | |
| // Ensure custom path exists | ||
| await fs.mkdir(customStoragePath, { recursive: true }) | ||
|
|
||
| // Test if path is writable | ||
| const testFile = path.join(customStoragePath, ".write_test") | ||
| // Test if path is writable (use unique filename to avoid race conditions) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great fix! Consider making the comment slightly more detailed to explain why we need the unique suffix, e.g.: |
||
| const uniqueSuffix = `${Date.now()}-${Math.random().toString(36).slice(2, 11)}` | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The unique suffix generation pattern works well. As a minor enhancement for future consideration, this could be extracted to a small utility function if similar unique ID generation is needed elsewhere in the codebase. But for a single use case, the current implementation is perfectly fine. |
||
| const testFile = path.join(customStoragePath, `.write_test_${uniqueSuffix}`) | ||
| await fs.writeFile(testFile, "test") | ||
| await fs.rm(testFile) | ||
|
|
||
|
|
||
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.
Excellent test coverage for the race condition scenario! For future enhancements, consider adding test cases for:
These would provide even more comprehensive coverage of edge cases.