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
  • Loading branch information
moxw committed Sep 21, 2023
1 parent 428151c commit 08d569d
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 6 deletions.
8 changes: 7 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,12 @@ 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);
})
);

return appmapFiles.length;
}
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 08d569d

Please sign in to comment.