Skip to content
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

feat: Add toggle command #385

Merged
merged 6 commits into from
Nov 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@
"command": "coverage-gutters.displayCoverage",
"title": "Coverage Gutters: Display Coverage"
},
{
"command": "coverage-gutters.toggleCoverage",
"title": "Coverage Gutters: Toggle Coverage"
},
anishkny marked this conversation as resolved.
Show resolved Hide resolved
{
"command": "coverage-gutters.watchCoverageAndVisibleEditors",
"title": "Coverage Gutters: Watch"
Expand Down Expand Up @@ -200,6 +204,11 @@
"key": "ctrl+shift+0",
"mac": "shift+cmd+0",
"win": "ctrl+shift+backspace"
},
{
"command": "coverage-gutters.toggleCoverage",
"key": "ctrl+shift+5",
"mac": "shift+cmd+5"
}
],
"menus": {
Expand Down
11 changes: 11 additions & 0 deletions src/coverage-system/coverageservice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class CoverageService {
private outputChannel: OutputChannel;
private filesLoader: FilesLoader;
private renderer: Renderer;
private isCoverageDisplayed = false;

private coverageParser: CoverageParser;
private coverageWatcher: FileSystemWatcher | undefined;
Expand Down Expand Up @@ -68,6 +69,15 @@ export class CoverageService {

public async displayForFile() {
await this.loadCacheAndProcess();
this.isCoverageDisplayed = true;
}

public async toggleCoverage() {
if (this.isCoverageDisplayed) {
this.removeCoverageForCurrentEditor();
} else {
await this.displayForFile();
}
}

public async watchWorkspace() {
Expand All @@ -82,6 +92,7 @@ export class CoverageService {
this.renderer.renderCoverage(new Map(), window.visibleTextEditors);
} finally {
this.statusBar.setLoading(false);
this.isCoverageDisplayed = false;
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export function activate(context: vscode.ExtensionContext) {
"coverage-gutters.displayCoverage",
gutters.displayCoverageForActiveFile.bind(gutters),
);
const toggle = vscode.commands.registerCommand(
"coverage-gutters.toggleCoverage",
gutters.toggleCoverageForActiveFile.bind(gutters),
);
const watch = vscode.commands.registerCommand(
"coverage-gutters.watchCoverageAndVisibleEditors",
gutters.watchCoverageAndVisibleEditors.bind(gutters),
Expand All @@ -40,6 +44,7 @@ export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(previewCoverageReport);
context.subscriptions.push(remove);
context.subscriptions.push(display);
context.subscriptions.push(toggle);
context.subscriptions.push(watch);
context.subscriptions.push(removeWatch);
context.subscriptions.push(gutters);
Expand Down
9 changes: 9 additions & 0 deletions src/extension/gutters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ export class Gutters {
}
}

public async toggleCoverageForActiveFile() {
try {
await this.coverageService.toggleCoverage();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
this.handleError("toggleCoverageForActiveFile", error);
}
}
anishkny marked this conversation as resolved.
Show resolved Hide resolved

public async watchCoverageAndVisibleEditors() {
try {
this.statusBar.toggle(true);
Expand Down
38 changes: 38 additions & 0 deletions test/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,44 @@ suite("Extension Tests", function() {
expect(cachedLines.none).to.have.lengthOf(3);
});

await vscode.commands.executeCommand("coverage-gutters.removeCoverage");
decorationSpy.restore();
});

test("Run toggle coverage on python test file x2 @integration", async () => {
// Set up the spies to allow for detecting proper code flows
const decorationSpy = sinon.spy(Renderer.prototype, "setDecorationsForEditor");
const removalSpy = sinon.spy(Renderer.prototype, "removeDecorationsForEditor");
const extension = await vscode.extensions.getExtension("ryanluker.vscode-coverage-gutters");
if (!extension) {
throw new Error("Could not load extension");
}

const testCoverage = await vscode.workspace.findFiles("**/bar/a.py", "**/node_modules/**");
const testDocument = await vscode.workspace.openTextDocument(testCoverage[0]);
await vscode.window.showTextDocument(testDocument);

// Toggle coverage on
await vscode.commands.executeCommand("coverage-gutters.toggleCoverage");
await checkCoverage(() => {
// Look for exact coverage on the file
const cachedLines: ICoverageLines = decorationSpy.getCall(0).args[1];
expect(cachedLines.full).to.have.lengthOf(3);
expect(cachedLines.none).to.have.lengthOf(3);
});

// Toggle coverage off
await vscode.commands.executeCommand("coverage-gutters.toggleCoverage");
// Check that renderSections was called with empty Map
await checkCoverage(() => {
// Check for remove coverage being called twice
const coverageRemovalCalls = removalSpy.getCalls();
expect(coverageRemovalCalls).to.have.length(2);
// Check for the coverage display being called once
const coverageAdditionCalls = decorationSpy.getCalls();
expect(coverageAdditionCalls).to.have.length(1);
});

decorationSpy.restore();
});

Expand Down