Skip to content

Commit

Permalink
Test getInstanceFromResource
Browse files Browse the repository at this point in the history
Part of #126387

Co-Authored-By: Megan Rogge <merogge@microsoft.com>
  • Loading branch information
Tyriar and meganrogge committed Jul 21, 2021
1 parent 954686a commit 9fee021
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 57 deletions.
13 changes: 2 additions & 11 deletions src/vs/workbench/contrib/terminal/browser/terminalEditorService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { IRemoteTerminalService, ITerminalEditorService, ITerminalInstance, ITer
import { TerminalEditor } from 'vs/workbench/contrib/terminal/browser/terminalEditor';
import { TerminalEditorInput } from 'vs/workbench/contrib/terminal/browser/terminalEditorInput';
import { DeserializedTerminalEditorInput } from 'vs/workbench/contrib/terminal/browser/terminalEditorSerializer';
import { parseTerminalUri } from 'vs/workbench/contrib/terminal/browser/terminalUri';
import { getInstanceFromResource, parseTerminalUri } from 'vs/workbench/contrib/terminal/browser/terminalUri';
import { ILocalTerminalService, IOffProcessTerminalService } from 'vs/workbench/contrib/terminal/common/terminal';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
Expand Down Expand Up @@ -219,16 +219,7 @@ export class TerminalEditorService extends Disposable implements ITerminalEditor
}

getInstanceFromResource(resource: URI | undefined): ITerminalInstance | undefined {
if (URI.isUri(resource)) {
// note that the uri and and instance id might
// not match this window
for (const instance of this.instances) {
if (instance.resource.path === resource.path) {
return instance;
}
}
}
return undefined;
return getInstanceFromResource(this.instances, resource);
}

splitInstance(instanceToSplit: ITerminalInstance, shellLaunchConfig: IShellLaunchConfig = {}): ITerminalInstance {
Expand Down
12 changes: 2 additions & 10 deletions src/vs/workbench/contrib/terminal/browser/terminalGroupService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { IShellLaunchConfig, TerminalSettingId } from 'vs/platform/terminal/comm
import { IViewDescriptorService, IViewsService, ViewContainerLocation } from 'vs/workbench/common/views';
import { ITerminalFindHost, ITerminalGroup, ITerminalGroupService, ITerminalInstance } from 'vs/workbench/contrib/terminal/browser/terminal';
import { TerminalGroup } from 'vs/workbench/contrib/terminal/browser/terminalGroup';
import { getInstanceFromResource } from 'vs/workbench/contrib/terminal/browser/terminalUri';
import { TerminalViewPane } from 'vs/workbench/contrib/terminal/browser/terminalView';
import { TERMINAL_VIEW_ID } from 'vs/workbench/contrib/terminal/common/terminal';
import { TerminalContextKeys } from 'vs/workbench/contrib/terminal/common/terminalContextKey';
Expand Down Expand Up @@ -177,16 +178,7 @@ export class TerminalGroupService extends Disposable implements ITerminalGroupSe
}

getInstanceFromResource(resource: URI | undefined): ITerminalInstance | undefined {
if (URI.isUri(resource)) {
// note that the uri and and instance id might
// not match this window
for (const instance of this.instances) {
if (instance.resource.path === resource.path) {
return instance;
}
}
}
return undefined;
return getInstanceFromResource(this.instances, resource);
}

findNext(): void {
Expand Down
13 changes: 2 additions & 11 deletions src/vs/workbench/contrib/terminal/browser/terminalService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import { TerminalConfigHelper } from 'vs/workbench/contrib/terminal/browser/term
import { TerminalEditor } from 'vs/workbench/contrib/terminal/browser/terminalEditor';
import { getColorClass, getUriClasses } from 'vs/workbench/contrib/terminal/browser/terminalIcon';
import { configureTerminalProfileIcon } from 'vs/workbench/contrib/terminal/browser/terminalIcons';
import { getTerminalUri, parseTerminalUri } from 'vs/workbench/contrib/terminal/browser/terminalUri';
import { getInstanceFromResource, getTerminalUri, parseTerminalUri } from 'vs/workbench/contrib/terminal/browser/terminalUri';
import { TerminalViewPane } from 'vs/workbench/contrib/terminal/browser/terminalView';
import { ILocalTerminalService, IOffProcessTerminalService, IRemoteTerminalAttachTarget, IStartExtensionTerminalRequest, ITerminalConfigHelper, ITerminalProcessExtHostProxy, ITerminalProfileContribution, TERMINAL_VIEW_ID } from 'vs/workbench/contrib/terminal/common/terminal';
import { TerminalContextKeys } from 'vs/workbench/contrib/terminal/common/terminalContextKey';
Expand Down Expand Up @@ -606,16 +606,7 @@ export class TerminalService implements ITerminalService {
}

getInstanceFromResource(resource: URI | undefined): ITerminalInstance | undefined {
if (URI.isUri(resource)) {
// note that the uri and and instance id might
// not match this window
for (const instance of this.instances) {
if (instance.resource.path === resource.path) {
return instance;
}
}
}
return undefined;
return getInstanceFromResource(this.instances, resource);
}

isAttachedToTerminal(remoteTerm: IRemoteTerminalAttachTarget): boolean {
Expand Down
14 changes: 14 additions & 0 deletions src/vs/workbench/contrib/terminal/browser/terminalUri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { DataTransfers } from 'vs/base/browser/dnd';
import { Schemas } from 'vs/base/common/network';
import { URI } from 'vs/base/common/uri';
import { ITerminalInstance } from 'vs/workbench/contrib/terminal/browser/terminal';

export function parseTerminalUri(resource: URI): ITerminalIdentifier {
const [, workspaceId, instanceId] = resource.path.split('/');
Expand Down Expand Up @@ -44,3 +45,16 @@ export function getTerminalResourcesFromDragEvent(event: IPartialDragEvent): URI
}
return undefined;
}

export function getInstanceFromResource<T extends Pick<ITerminalInstance, 'resource'>>(instances: T[], resource: URI | undefined): T | undefined {
if (resource) {
for (const instance of instances) {
// Note that the URI's workspace and instance id might not originally be from this window
// Don't bother checking the scheme and assume instances only contains terminals
if (instance.resource.path === resource.path) {
return instance;
}
}
}
return undefined;
}
84 changes: 59 additions & 25 deletions src/vs/workbench/contrib/terminal/test/browser/terminalUri.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { deepStrictEqual } from 'assert';
import { getTerminalResourcesFromDragEvent, IPartialDragEvent } from 'vs/workbench/contrib/terminal/browser/terminalUri';
import { deepStrictEqual, strictEqual } from 'assert';
import { getInstanceFromResource, getTerminalResourcesFromDragEvent, getTerminalUri, IPartialDragEvent } from 'vs/workbench/contrib/terminal/browser/terminalUri';

function fakeDragEvent(data: string): IPartialDragEvent {
return {
Expand All @@ -16,29 +16,63 @@ function fakeDragEvent(data: string): IPartialDragEvent {
};
}

suite('getTerminalResourcesFromDragEvent', () => {
test('should give undefined when no terminal resources is in event', () => {
deepStrictEqual(
getTerminalResourcesFromDragEvent(fakeDragEvent(''))?.map(e => e.toString()),
undefined
);
suite('terminalUri', () => {
suite('getTerminalResourcesFromDragEvent', () => {
test('should give undefined when no terminal resources is in event', () => {
deepStrictEqual(
getTerminalResourcesFromDragEvent(fakeDragEvent(''))?.map(e => e.toString()),
undefined
);
});
test('should give undefined when an empty terminal resources array is in event', () => {
deepStrictEqual(
getTerminalResourcesFromDragEvent(fakeDragEvent('[]'))?.map(e => e.toString()),
undefined
);
});
test('should return terminal resource when event contains one', () => {
deepStrictEqual(
getTerminalResourcesFromDragEvent(fakeDragEvent('["vscode-terminal:/1626874386474/3"]'))?.map(e => e.toString()),
['vscode-terminal:/1626874386474/3']
);
});
test('should return multiple terminal resources when event contains multiple', () => {
deepStrictEqual(
getTerminalResourcesFromDragEvent(fakeDragEvent('["vscode-terminal:/foo/1","vscode-terminal:/bar/2"]'))?.map(e => e.toString()),
['vscode-terminal:/foo/1', 'vscode-terminal:/bar/2']
);
});
});
test('should give undefined when an empty terminal resources array is in event', () => {
deepStrictEqual(
getTerminalResourcesFromDragEvent(fakeDragEvent('[]'))?.map(e => e.toString()),
undefined
);
});
test('should return terminal resource when event contains one', () => {
deepStrictEqual(
getTerminalResourcesFromDragEvent(fakeDragEvent('["vscode-terminal:/1626874386474/3"]'))?.map(e => e.toString()),
['vscode-terminal:/1626874386474/3']
);
});
test('should return multiple terminal resources when event contains multiple', () => {
deepStrictEqual(
getTerminalResourcesFromDragEvent(fakeDragEvent('["vscode-terminal:/foo/1","vscode-terminal:/bar/2"]'))?.map(e => e.toString()),
['vscode-terminal:/foo/1', 'vscode-terminal:/bar/2']
);
suite('getInstanceFromResource', () => {
test('should return undefined if there is no match', () => {
strictEqual(
getInstanceFromResource([
{ resource: getTerminalUri('workspace', 2, 'title') }
], getTerminalUri('workspace', 1)),
undefined
);
});
test('should return a result if there is a match', () => {
const instance = { resource: getTerminalUri('workspace', 2, 'title') };
strictEqual(
getInstanceFromResource([
{ resource: getTerminalUri('workspace', 1, 'title') },
instance,
{ resource: getTerminalUri('workspace', 3, 'title') }
], getTerminalUri('workspace', 2)),
instance
);
});
test('should ignore the fragment', () => {
const instance = { resource: getTerminalUri('workspace', 2, 'title') };
strictEqual(
getInstanceFromResource([
{ resource: getTerminalUri('workspace', 1, 'title') },
instance,
{ resource: getTerminalUri('workspace', 3, 'title') }
], getTerminalUri('workspace', 2, 'does not match!')),
instance
);
});
});
});

0 comments on commit 9fee021

Please sign in to comment.