forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add the proposed Port Attributes API for python testing #22245
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
199960c
use vscode proposed API to register testing ports
eleanorjboyd 2174aeb
add test for service registry
eleanorjboyd 1b8a774
remove mistaken addition
eleanorjboyd ba6c4e5
remove unneeded assert
eleanorjboyd e043c72
add specific port assignment
eleanorjboyd 9752fca
add fix for get port
eleanorjboyd 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,50 @@ | ||
/* eslint-disable class-methods-use-this */ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import { | ||
CancellationToken, | ||
Disposable, | ||
PortAttributes, | ||
PortAttributesProvider, | ||
PortAutoForwardAction, | ||
ProviderResult, | ||
workspace, | ||
} from 'vscode'; | ||
|
||
class TestPortAttributesProvider implements PortAttributesProvider { | ||
private knownPorts: number[] = []; | ||
|
||
public providePortAttributes( | ||
attributes: { port: number; pid?: number; commandLine?: string }, | ||
_token: CancellationToken, | ||
): ProviderResult<PortAttributes> { | ||
if (this.knownPorts.includes(attributes.port)) { | ||
return new PortAttributes(PortAutoForwardAction.Ignore); | ||
} | ||
return undefined; | ||
} | ||
|
||
public setPortAttribute(port: number): void { | ||
this.knownPorts.push(port); | ||
} | ||
|
||
public resetPortAttribute(port: number): void { | ||
this.knownPorts = this.knownPorts.filter((p) => p !== port); | ||
} | ||
} | ||
|
||
let provider: TestPortAttributesProvider | undefined; | ||
|
||
export function registerTestPortAttributesProvider(disposables: Disposable[]): void { | ||
provider = new TestPortAttributesProvider(); | ||
disposables.push(workspace.registerPortAttributesProvider({}, provider)); | ||
} | ||
|
||
export function setPortAttribute(port: number): void { | ||
provider?.setPortAttribute(port); | ||
} | ||
|
||
export function resetPortAttribute(port: number): void { | ||
provider?.resetPortAttribute(port); | ||
} |
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
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,69 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
'use strict'; | ||
|
||
import { instance, mock, verify } from 'ts-mockito'; | ||
import { registerTypes } from '../../client/activation/serviceRegistry'; | ||
import { IExtensionActivationService } from '../../client/activation/types'; | ||
import { ServiceManager } from '../../client/ioc/serviceManager'; | ||
import { IServiceManager } from '../../client/ioc/types'; | ||
import { TestConfigSettingsService } from '../../client/testing/common/configSettingService'; | ||
import { DebugLauncher } from '../../client/testing/common/debugLauncher'; | ||
import { TestRunner } from '../../client/testing/common/runner'; | ||
import { UnitTestSocketServer } from '../../client/testing/common/socketServer'; | ||
import { TestsHelper } from '../../client/testing/common/testUtils'; | ||
import { | ||
ITestDebugLauncher, | ||
ITestsHelper, | ||
IUnitTestSocketServer, | ||
ITestRunner, | ||
ITestConfigurationService, | ||
ITestConfigSettingsService, | ||
ITestConfigurationManagerFactory, | ||
} from '../../client/testing/common/types'; | ||
import { UnitTestConfigurationService } from '../../client/testing/configuration'; | ||
import { TestConfigurationManagerFactory } from '../../client/testing/configurationFactory'; | ||
import { TestingService, UnitTestManagementService } from '../../client/testing/main'; | ||
import { ITestingService } from '../../client/testing/types'; | ||
|
||
suite('Testing_Service_Registry', () => { | ||
let serviceManager: IServiceManager; | ||
|
||
setup(() => { | ||
serviceManager = mock(ServiceManager); | ||
}); | ||
test('Ensure services are registered', async () => { | ||
registerTypes(instance(serviceManager)); | ||
|
||
verify(serviceManager.addSingleton<ITestDebugLauncher>(ITestDebugLauncher, DebugLauncher)).once(); | ||
verify(serviceManager.add<ITestsHelper>(ITestsHelper, TestsHelper)).once(); | ||
verify(serviceManager.add<IUnitTestSocketServer>(IUnitTestSocketServer, UnitTestSocketServer)).once(); | ||
verify(serviceManager.add<ITestRunner>(ITestRunner, TestRunner)).once(); | ||
verify( | ||
serviceManager.addSingleton<ITestConfigurationService>( | ||
ITestConfigurationService, | ||
UnitTestConfigurationService, | ||
), | ||
).once(); | ||
verify(serviceManager.addSingleton<ITestingService>(ITestingService, TestingService)).once(); | ||
verify( | ||
serviceManager.addSingleton<ITestConfigSettingsService>( | ||
ITestConfigSettingsService, | ||
TestConfigSettingsService, | ||
), | ||
).once(); | ||
verify( | ||
serviceManager.addSingleton<ITestConfigurationManagerFactory>( | ||
ITestConfigurationManagerFactory, | ||
TestConfigurationManagerFactory, | ||
), | ||
).once(); | ||
verify( | ||
serviceManager.addSingleton<IExtensionActivationService>( | ||
IExtensionActivationService, | ||
UnitTestManagementService, | ||
), | ||
).once(); | ||
}); | ||
}); |
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,105 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
declare module 'vscode' { | ||
// https://github.com/microsoft/vscode/issues/115616 @alexr00 | ||
|
||
/** | ||
* The action that should be taken when a port is discovered through automatic port forwarding discovery. | ||
*/ | ||
export enum PortAutoForwardAction { | ||
/** | ||
* Notify the user that the port is being forwarded. This is the default action. | ||
*/ | ||
Notify = 1, | ||
/** | ||
* Once the port is forwarded, open the user's web browser to the forwarded port. | ||
*/ | ||
OpenBrowser = 2, | ||
/** | ||
* Once the port is forwarded, open the preview browser to the forwarded port. | ||
*/ | ||
OpenPreview = 3, | ||
/** | ||
* Forward the port silently. | ||
*/ | ||
Silent = 4, | ||
/** | ||
* Do not forward the port. | ||
*/ | ||
Ignore = 5, | ||
} | ||
|
||
/** | ||
* The attributes that a forwarded port can have. | ||
*/ | ||
export class PortAttributes { | ||
/** | ||
* The action to be taken when this port is detected for auto forwarding. | ||
*/ | ||
autoForwardAction: PortAutoForwardAction; | ||
|
||
/** | ||
* Creates a new PortAttributes object | ||
* @param port the port number | ||
* @param autoForwardAction the action to take when this port is detected | ||
*/ | ||
constructor(autoForwardAction: PortAutoForwardAction); | ||
} | ||
|
||
/** | ||
* A provider of port attributes. Port attributes are used to determine what action should be taken when a port is discovered. | ||
*/ | ||
export interface PortAttributesProvider { | ||
/** | ||
* Provides attributes for the given port. For ports that your extension doesn't know about, simply | ||
* return undefined. For example, if `providePortAttributes` is called with ports 3000 but your | ||
* extension doesn't know anything about 3000 you should return undefined. | ||
* @param port The port number of the port that attributes are being requested for. | ||
* @param pid The pid of the process that is listening on the port. If the pid is unknown, undefined will be passed. | ||
* @param commandLine The command line of the process that is listening on the port. If the command line is unknown, undefined will be passed. | ||
* @param token A cancellation token that indicates the result is no longer needed. | ||
*/ | ||
providePortAttributes( | ||
attributes: { port: number; pid?: number; commandLine?: string }, | ||
token: CancellationToken, | ||
): ProviderResult<PortAttributes>; | ||
} | ||
|
||
/** | ||
* A selector that will be used to filter which {@link PortAttributesProvider} should be called for each port. | ||
*/ | ||
export interface PortAttributesSelector { | ||
/** | ||
* Specifying a port range will cause your provider to only be called for ports within the range. | ||
* The start is inclusive and the end is exclusive. | ||
*/ | ||
portRange?: [number, number] | number; | ||
|
||
/** | ||
* Specifying a command pattern will cause your provider to only be called for processes whose command line matches the pattern. | ||
*/ | ||
commandPattern?: RegExp; | ||
} | ||
|
||
export namespace workspace { | ||
/** | ||
* If your extension listens on ports, consider registering a PortAttributesProvider to provide information | ||
* about the ports. For example, a debug extension may know about debug ports in it's debuggee. By providing | ||
* this information with a PortAttributesProvider the extension can tell the editor that these ports should be | ||
* ignored, since they don't need to be user facing. | ||
* | ||
* The results of the PortAttributesProvider are merged with the user setting `remote.portsAttributes`. If the values conflict, the user setting takes precedence. | ||
* | ||
* @param portSelector It is best practice to specify a port selector to avoid unnecessary calls to your provider. | ||
* If you don't specify a port selector your provider will be called for every port, which will result in slower port forwarding for the user. | ||
* @param provider The {@link PortAttributesProvider PortAttributesProvider}. | ||
*/ | ||
export function registerPortAttributesProvider( | ||
portSelector: PortAttributesSelector, | ||
provider: PortAttributesProvider, | ||
): Disposable; | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Passing
{}
is still not ideal for the selector. This means that your attributes provider will be called for each auto forwarded port. Is there really nocommandPattern
that you can specify that will at least filter out some ports?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking of requiring that at least one value of the selector is set to a meaningful value and logging an error if it's not set properly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@karthiknadig, thoughts on this? I am not sure about the specific of the
commandPattern
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The port is created in the extension host, so the process itself is whatever process that extension host is running in. So we could specify the process id of the extension host.