-
Notifications
You must be signed in to change notification settings - Fork 34
Refactor storage.ts into testable modules and add unit tests #589
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
12 commits
Select commit
Hold shift + click to select a range
f3a23c6
Move path resolution from storage.ts
EhabY 1a4183d
Move CLI configuration from storage.ts
EhabY ae30ae8
Move memento and secrets managment from storage.ts
EhabY a2792c4
Split binary management logic from storage.ts
EhabY 2fe85c8
Remove `storage.ts` file entirely
EhabY 9117ed8
Add binaryManager test + update vitest
EhabY 1432979
Rebase fallout
EhabY 3c3cb5e
Mock VS Code fully instead of DI
EhabY 0b42207
Add unit tests for the split modules
EhabY ac418d1
Fix binary manager tests
EhabY 784bb1f
Review comments 1
EhabY 2c674e4
Review comments 2
EhabY 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,274 @@ | ||
import { vi } from "vitest"; | ||
import * as vscode from "vscode"; | ||
|
||
/** | ||
* Mock configuration provider that integrates with the vscode workspace configuration mock. | ||
* Use this to set configuration values that will be returned by vscode.workspace.getConfiguration(). | ||
*/ | ||
export class MockConfigurationProvider { | ||
private config = new Map<string, unknown>(); | ||
|
||
constructor() { | ||
this.setupVSCodeMock(); | ||
} | ||
|
||
/** | ||
* Set a configuration value that will be returned by vscode.workspace.getConfiguration().get() | ||
*/ | ||
set(key: string, value: unknown): void { | ||
this.config.set(key, value); | ||
} | ||
|
||
/** | ||
* Get a configuration value (for testing purposes) | ||
*/ | ||
get<T>(key: string): T | undefined; | ||
get<T>(key: string, defaultValue: T): T; | ||
get<T>(key: string, defaultValue?: T): T | undefined { | ||
const value = this.config.get(key); | ||
return value !== undefined ? (value as T) : defaultValue; | ||
} | ||
|
||
/** | ||
* Clear all configuration values | ||
*/ | ||
clear(): void { | ||
this.config.clear(); | ||
} | ||
|
||
/** | ||
* Setup the vscode.workspace.getConfiguration mock to return our values | ||
*/ | ||
private setupVSCodeMock(): void { | ||
vi.mocked(vscode.workspace.getConfiguration).mockImplementation( | ||
(section?: string) => { | ||
// Create a snapshot of the current config when getConfiguration is called | ||
const snapshot = new Map(this.config); | ||
const getFullKey = (part: string) => | ||
section ? `${section}.${part}` : part; | ||
|
||
return { | ||
get: vi.fn((key: string, defaultValue?: unknown) => { | ||
const value = snapshot.get(getFullKey(key)); | ||
return value !== undefined ? value : defaultValue; | ||
}), | ||
has: vi.fn((key: string) => { | ||
return snapshot.has(getFullKey(key)); | ||
}), | ||
inspect: vi.fn(), | ||
update: vi.fn((key: string, value: unknown) => { | ||
this.config.set(getFullKey(key), value); | ||
return Promise.resolve(); | ||
}), | ||
}; | ||
}, | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Mock progress reporter that integrates with vscode.window.withProgress. | ||
* Use this to control progress reporting behavior and cancellation in tests. | ||
*/ | ||
export class MockProgressReporter { | ||
private shouldCancel = false; | ||
private progressReports: Array<{ message?: string; increment?: number }> = []; | ||
|
||
constructor() { | ||
this.setupVSCodeMock(); | ||
} | ||
|
||
/** | ||
* Set whether the progress should be cancelled | ||
*/ | ||
setCancellation(cancel: boolean): void { | ||
this.shouldCancel = cancel; | ||
} | ||
|
||
/** | ||
* Get all progress reports that were made | ||
*/ | ||
getProgressReports(): Array<{ message?: string; increment?: number }> { | ||
return [...this.progressReports]; | ||
} | ||
|
||
/** | ||
* Clear all progress reports | ||
*/ | ||
clearProgressReports(): void { | ||
this.progressReports = []; | ||
} | ||
|
||
/** | ||
* Setup the vscode.window.withProgress mock | ||
*/ | ||
private setupVSCodeMock(): void { | ||
vi.mocked(vscode.window.withProgress).mockImplementation( | ||
async <T>( | ||
_options: vscode.ProgressOptions, | ||
task: ( | ||
progress: vscode.Progress<{ message?: string; increment?: number }>, | ||
token: vscode.CancellationToken, | ||
) => Thenable<T>, | ||
): Promise<T> => { | ||
const progress = { | ||
report: vi.fn((value: { message?: string; increment?: number }) => { | ||
this.progressReports.push(value); | ||
}), | ||
}; | ||
|
||
const cancellationToken: vscode.CancellationToken = { | ||
isCancellationRequested: this.shouldCancel, | ||
onCancellationRequested: vi.fn((listener: (x: unknown) => void) => { | ||
if (this.shouldCancel) { | ||
setTimeout(listener, 0); | ||
} | ||
return { dispose: vi.fn() }; | ||
}), | ||
}; | ||
|
||
return task(progress, cancellationToken); | ||
}, | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Mock user interaction that integrates with vscode.window message dialogs. | ||
* Use this to control user responses in tests. | ||
*/ | ||
export class MockUserInteraction { | ||
private responses = new Map<string, string | undefined>(); | ||
private externalUrls: string[] = []; | ||
|
||
constructor() { | ||
this.setupVSCodeMock(); | ||
} | ||
|
||
/** | ||
* Set a response for a specific message | ||
*/ | ||
setResponse(message: string, response: string | undefined): void { | ||
this.responses.set(message, response); | ||
} | ||
|
||
/** | ||
* Get all URLs that were opened externally | ||
*/ | ||
getExternalUrls(): string[] { | ||
return [...this.externalUrls]; | ||
} | ||
|
||
/** | ||
* Clear all external URLs | ||
*/ | ||
clearExternalUrls(): void { | ||
this.externalUrls = []; | ||
} | ||
|
||
/** | ||
* Clear all responses | ||
*/ | ||
clearResponses(): void { | ||
this.responses.clear(); | ||
} | ||
|
||
/** | ||
* Setup the vscode.window message dialog mocks | ||
*/ | ||
private setupVSCodeMock(): void { | ||
const getResponse = (message: string): string | undefined => { | ||
return this.responses.get(message); | ||
}; | ||
|
||
vi.mocked(vscode.window.showErrorMessage).mockImplementation( | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
(message: string): Thenable<any> => { | ||
const response = getResponse(message); | ||
return Promise.resolve(response); | ||
}, | ||
); | ||
|
||
vi.mocked(vscode.window.showWarningMessage).mockImplementation( | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
(message: string): Thenable<any> => { | ||
const response = getResponse(message); | ||
return Promise.resolve(response); | ||
}, | ||
); | ||
|
||
vi.mocked(vscode.window.showInformationMessage).mockImplementation( | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
(message: string): Thenable<any> => { | ||
const response = getResponse(message); | ||
return Promise.resolve(response); | ||
}, | ||
); | ||
|
||
vi.mocked(vscode.env.openExternal).mockImplementation( | ||
(target: vscode.Uri): Promise<boolean> => { | ||
this.externalUrls.push(target.toString()); | ||
return Promise.resolve(true); | ||
}, | ||
); | ||
} | ||
} | ||
|
||
// Simple in-memory implementation of Memento | ||
export class InMemoryMemento implements vscode.Memento { | ||
private storage = new Map<string, unknown>(); | ||
|
||
get<T>(key: string): T | undefined; | ||
get<T>(key: string, defaultValue: T): T; | ||
get<T>(key: string, defaultValue?: T): T | undefined { | ||
return this.storage.has(key) ? (this.storage.get(key) as T) : defaultValue; | ||
} | ||
|
||
async update(key: string, value: unknown): Promise<void> { | ||
if (value === undefined) { | ||
this.storage.delete(key); | ||
} else { | ||
this.storage.set(key, value); | ||
} | ||
return Promise.resolve(); | ||
} | ||
|
||
keys(): readonly string[] { | ||
return Array.from(this.storage.keys()); | ||
} | ||
} | ||
|
||
// Simple in-memory implementation of SecretStorage | ||
export class InMemorySecretStorage implements vscode.SecretStorage { | ||
private secrets = new Map<string, string>(); | ||
private isCorrupted = false; | ||
|
||
onDidChange: vscode.Event<vscode.SecretStorageChangeEvent> = () => ({ | ||
dispose: () => {}, | ||
}); | ||
|
||
async get(key: string): Promise<string | undefined> { | ||
if (this.isCorrupted) { | ||
return Promise.reject(new Error("Storage corrupted")); | ||
} | ||
return this.secrets.get(key); | ||
} | ||
|
||
async store(key: string, value: string): Promise<void> { | ||
if (this.isCorrupted) { | ||
return Promise.reject(new Error("Storage corrupted")); | ||
} | ||
this.secrets.set(key, value); | ||
} | ||
|
||
async delete(key: string): Promise<void> { | ||
if (this.isCorrupted) { | ||
return Promise.reject(new Error("Storage corrupted")); | ||
} | ||
this.secrets.delete(key); | ||
} | ||
|
||
corruptStorage(): void { | ||
this.isCorrupted = true; | ||
} | ||
} |
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.