Skip to content

Commit

Permalink
feat(deleteAppMaps.ts): add closeEditorByUri function after deleting …
Browse files Browse the repository at this point in the history
…appmaps

feat(contextMenu.ts): update deleteAppMap command to handle absence of item object
feat(deleteAppMap.test.ts): add new integration test for deleteAppMap
fix(closeEditorByUri.test.ts): fix test to include input conditions
feat(deleteAppMaps.ts): add clear method to AppMapCollection after closing editors
feat(appmapCollection.ts): introduce clear method in AppMapCollection interface
feat(appmapCollectionFile.ts): implement clear method for AppMapCollectionFile class
  • Loading branch information
moxw authored and ahtrotta committed Sep 27, 2023
1 parent 3c7d437 commit d77083d
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 6 deletions.
9 changes: 8 additions & 1 deletion src/lib/deleteAppMaps.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as vscode from 'vscode';
import deleteAppMap from './deleteAppMap';
import closeEditorByUri from './closeEditorByUri';
import { promisify } from 'util';
import { glob } from 'glob';
import AppMapCollection from '../services/appmapCollection';
Expand All @@ -14,7 +15,13 @@ export default async function deleteAppMaps(
vscode.Uri.file(path)
);

await Promise.all(appmapFiles.map((uri) => deleteAppMap(uri, appMapCollection)));
await Promise.all(
appmapFiles.map((uri) => {
deleteAppMap(uri, appMapCollection);
closeEditorByUri(uri);
})
);
appMapCollection.clear();

return appmapFiles.length;
}
2 changes: 2 additions & 0 deletions src/services/appmapCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ export default interface AppMapCollection {
allAppMapsForWorkspaceFolder(workspaceFolder: WorkspaceFolder): AppMapLoader[];

has(uri: vscode.Uri): boolean;

clear(): void;
}
6 changes: 6 additions & 0 deletions src/services/appmapCollectionFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,10 @@ export default class AppMapCollectionFile implements AppMapCollection, AppMapsSe
public has(uri: vscode.Uri): boolean {
return this.loaders.has(uri.fsPath);
}

public clear(): void {
console.debug('Clearing AppMap collection from tree', this.loaders.size);
this.loaders.clear();
this.emitUpdated(undefined);
}
}
17 changes: 15 additions & 2 deletions src/tree/contextMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,21 @@ export default class ContextMenu {
);
context.subscriptions.push(
vscode.commands.registerCommand('appmap.context.deleteAppMap', async (item: AppMapLoader) => {
await deleteAppMap(item.descriptor.resourceUri, appmaps);
await closeEditorByUri(item.descriptor.resourceUri);
let uri: vscode.Uri;
if (!item) {
const { activeTab } = vscode.window.tabGroups.activeTabGroup;
if (!activeTab) {
vscode.window.showErrorMessage('No active editor.');
return;
}

uri = (activeTab.input as vscode.TabInputCustom).uri;
} else {
uri = item.descriptor.resourceUri;
}

await deleteAppMap(uri, appmaps);
await closeEditorByUri(uri);
})
);
context.subscriptions.push(
Expand Down
40 changes: 40 additions & 0 deletions test/integration/command/deleteAppMap.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import * as vscode from 'vscode';
import { assert } from 'chai';
import { join } from 'path';
import * as sinon from 'sinon';
import { initializeWorkspace, ProjectA, waitForExtension } from '../util';

describe('deleteAppMap test', function () {
let sandbox: sinon.SinonSandbox;

const appmapFilePath = join(
ProjectA,
'tmp/appmap/minitest',
'Microposts_controller_can_get_microposts_as_JSON.appmap.json'
);

beforeEach(() => (sandbox = sinon.createSandbox()));
beforeEach(initializeWorkspace);
beforeEach(async () => await waitForExtension());

afterEach(initializeWorkspace);
afterEach(() => sandbox.restore());

it('deletes appmap with matching URI', async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const mockUri: vscode.Uri = { path: appmapFilePath } as any;
await vscode.workspace.openTextDocument(mockUri);
await vscode.window.showTextDocument(vscode.Uri.file(mockUri.path));
await vscode.commands.executeCommand('appmap.context.deleteAppMap');

const tabs = vscode.window.tabGroups.all.map((tg) => tg.tabs).flat();
const index = tabs.findIndex(
(tab) =>
(tab.input instanceof vscode.TabInputCustom ||
tab.input instanceof vscode.TabInputText ||
tab.input instanceof vscode.TabInputNotebook) &&
tab.input.uri.path === mockUri.path
);
assert.isTrue(index === -1);
});
});
7 changes: 4 additions & 3 deletions test/integration/lib/closeEditorByUri.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ describe('closeEditorByUri Tests', function () {
const tabs = vscode.window.tabGroups.all.map((tg) => tg.tabs).flat();
const index = tabs.findIndex(
(tab) =>
tab.input instanceof vscode.TabInputCustom ||
tab.input instanceof vscode.TabInputText ||
(tab.input instanceof vscode.TabInputNotebook && tab.input.uri.path === mockUri.path)
(tab.input instanceof vscode.TabInputCustom ||
tab.input instanceof vscode.TabInputText ||
tab.input instanceof vscode.TabInputNotebook) &&
tab.input.uri.path === mockUri.path
);

assert.isTrue(index === -1);
Expand Down

0 comments on commit d77083d

Please sign in to comment.