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

Add support for passing a runsettings path to the server #6265

Merged
merged 5 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion omnisharptest/omnisharpUnitTests/fakes/fakeOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export function getEmptyOptions(): Options {
excludePaths: [],
defaultSolution: '',
unitTestDebuggingOptions: {},
runSettingsPath: '',
},
{
useModernNet: false,
Expand All @@ -40,7 +41,6 @@ export function getEmptyOptions(): Options {
sdkPath: '',
sdkVersion: '',
sdkIncludePrereleases: false,
testRunSettings: '',
dotNetCliPaths: [],
useFormatting: false,
showReferencesCodeLens: false,
Expand Down
6 changes: 2 additions & 4 deletions omnisharptest/omnisharpUnitTests/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ suite('Options tests', () => {
options.omnisharpOptions.enableImportCompletion.should.equal(false);
options.omnisharpOptions.enableAsyncCompletion.should.equal(false);
options.omnisharpOptions.analyzeOpenDocumentsOnly.should.equal(true);
options.omnisharpOptions.testRunSettings.should.equal('');
options.commonOptions.runSettingsPath.should.equal('');
});

test('Verify return no excluded paths when files.exclude empty', () => {
Expand Down Expand Up @@ -128,8 +128,6 @@ suite('Options tests', () => {

const options = Options.Read(vscode);

options.omnisharpOptions.testRunSettings.should.equal(
'some_valid_path\\some_valid_runsettings_files.runsettings'
);
options.commonOptions.runSettingsPath.should.equal('some_valid_path\\some_valid_runsettings_files.runsettings');
});
});
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1081,10 +1081,6 @@
"default": false,
"description": "(EXPERIMENTAL) Enables support for resolving completion edits asynchronously. This can speed up time to show the completion list, particularly override and partial method completion lists, at the cost of slight delays after inserting a completion item. Most completion items will have no noticeable impact with this feature, but typing immediately after inserting an override or partial method completion, before the insert is completed, can have unpredictable results."
},
"omnisharp.testRunSettings": {
"type": "string",
"description": "Path to the .runsettings file which should be used when running unit tests."
},
"omnisharp.dotNetCliPaths": {
"type": "array",
"items": {
Expand Down Expand Up @@ -1358,6 +1354,10 @@
"description": "%configuration.dotnet.symbolSearch.searchReferenceAssemblies%",
"order": 80
},
"dotnet.unitTests.runSettingsPath": {
"type": "string",
"description": "Path to the .runsettings file which should be used when running unit tests."
dibarbet marked this conversation as resolved.
Show resolved Hide resolved
},
"dotnet.unitTestDebuggingOptions": {
"type": "object",
"description": "%configuration.dotnet.unitTestDebuggingOptions%",
Expand Down
2 changes: 1 addition & 1 deletion src/features/dotnetTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ export default class TestManager extends AbstractProvider {
}

private _getRunSettings(filename: string): string | undefined {
const testSettingsPath = this.optionProvider.GetLatestOptions().omnisharpOptions.testRunSettings;
const testSettingsPath = this.optionProvider.GetLatestOptions().commonOptions.runSettingsPath;
if (testSettingsPath.length === 0) {
return undefined;
}
Expand Down
2 changes: 1 addition & 1 deletion src/lsptoolshost/roslynLanguageServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@ export async function activateRoslynLanguageServer(

registerRazorCommands(context, _languageServer);

registerUnitTestingCommands(context, _languageServer, dotnetTestChannel);
registerUnitTestingCommands(context, _languageServer, dotnetTestChannel, optionProvider);

// Register any needed debugger components that need to communicate with the language server.
registerDebugger(context, _languageServer, platformInfo, optionProvider, _channel);
Expand Down
5 changes: 5 additions & 0 deletions src/lsptoolshost/roslynProtocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ export interface RunTestsParams extends lsp.WorkDoneProgressParams, lsp.PartialR
* Whether the request should attempt to call back to the client to attach a debugger before running the tests.
*/
attachDebugger: boolean;

/**
* The absolute path to a .runsettings file to configure the test run.
*/
runSettingsPath?: string;
}

export interface TestProgress {
Expand Down
59 changes: 51 additions & 8 deletions src/lsptoolshost/unitTesting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,30 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import * as fs from 'fs';
import * as path from 'path';
import * as vscode from 'vscode';
import * as languageClient from 'vscode-languageclient/node';
import { RoslynLanguageServer } from './roslynLanguageServer';
import { RunTestsParams, RunTestsPartialResult, RunTestsRequest } from './roslynProtocol';
import OptionProvider from '../shared/observers/optionProvider';

export function registerUnitTestingCommands(
context: vscode.ExtensionContext,
languageServer: RoslynLanguageServer,
dotnetTestChannel: vscode.OutputChannel
dotnetTestChannel: vscode.OutputChannel,
optionProvider: OptionProvider
) {
context.subscriptions.push(
vscode.commands.registerCommand('dotnet.test.run', async (request) =>
runTests(request, languageServer, dotnetTestChannel)
runTests(request, languageServer, dotnetTestChannel, optionProvider)
)
);
context.subscriptions.push(
vscode.commands.registerTextEditorCommand(
'dotnet.test.runTestsInContext',
async (textEditor: vscode.TextEditor) => {
return runTestsInContext(false, textEditor, languageServer, dotnetTestChannel);
return runTestsInContext(false, textEditor, languageServer, dotnetTestChannel, optionProvider);
}
)
);
Expand All @@ -31,7 +35,7 @@ export function registerUnitTestingCommands(
vscode.commands.registerTextEditorCommand(
'dotnet.test.debugTestsInContext',
async (textEditor: vscode.TextEditor) => {
return runTestsInContext(true, textEditor, languageServer, dotnetTestChannel);
return runTestsInContext(true, textEditor, languageServer, dotnetTestChannel, optionProvider);
}
)
);
Expand All @@ -41,21 +45,29 @@ async function runTestsInContext(
debug: boolean,
textEditor: vscode.TextEditor,
languageServer: RoslynLanguageServer,
dotnetTestChannel: vscode.OutputChannel
dotnetTestChannel: vscode.OutputChannel,
optionProvider: OptionProvider
) {
const contextRange: languageClient.Range = { start: textEditor.selection.active, end: textEditor.selection.active };
const textDocument: languageClient.TextDocumentIdentifier = { uri: textEditor.document.fileName };
const request: RunTestsParams = { textDocument: textDocument, range: contextRange, attachDebugger: debug };
await runTests(request, languageServer, dotnetTestChannel);
const request: RunTestsParams = {
textDocument: textDocument,
range: contextRange,
attachDebugger: debug,
};
await runTests(request, languageServer, dotnetTestChannel, optionProvider);
}

let _testRunInProgress = false;

async function runTests(
request: RunTestsParams,
languageServer: RoslynLanguageServer,
dotnetTestChannel: vscode.OutputChannel
dotnetTestChannel: vscode.OutputChannel,
optionProvider: OptionProvider
) {
request.runSettingsPath = getRunSettings(request.textDocument.uri, optionProvider, dotnetTestChannel);

if (_testRunInProgress) {
vscode.window.showErrorMessage('Test run already in progress');
return;
Expand Down Expand Up @@ -125,3 +137,34 @@ async function runTests(
() => (_testRunInProgress = false)
);
}

function getRunSettings(
documentUri: string,
optionProvider: OptionProvider,
dotnetTestChannel: vscode.OutputChannel
): string | undefined {
const runSettingsPathOption = optionProvider.GetLatestOptions().commonOptions.runSettingsPath;
if (runSettingsPathOption.length === 0) {
return undefined;
}

let absolutePath = runSettingsPathOption;
if (!path.isAbsolute(runSettingsPathOption)) {
// Path is relative to the workspace. Create absolute path.
const workspaceFolder = vscode.workspace.getWorkspaceFolder(vscode.Uri.parse(documentUri));
if (workspaceFolder === undefined) {
dotnetTestChannel.appendLine(
`Warning: Unable to find workspace folder for ${documentUri}, cannot resolve run settings path ${runSettingsPathOption}.`
);
return undefined;
}
absolutePath = path.join(workspaceFolder.uri.fsPath, runSettingsPathOption);
}

if (!fs.existsSync(absolutePath)) {
dotnetTestChannel.appendLine(`Warning: Unable to find run settings file at ${absolutePath}.`);
return undefined;
}

return absolutePath;
}
4 changes: 4 additions & 0 deletions src/shared/migrateOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ export const migrateOptions = [
omnisharpOption: 'csharp.unitTestDebuggingOptions',
roslynOption: 'dotnet.unitTestDebuggingOptions',
},
{
omnisharpOption: 'omnisharp.testRunSettings',
roslynOption: 'dotnet.unitTests.runSettingsPath',
},
];

export async function MigrateOptions(vscode: vscode): Promise<void> {
Expand Down
11 changes: 8 additions & 3 deletions src/shared/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ export class Options {
{},
'csharp.unitTestDebuggingOptions'
);
const runSettingsPath = Options.readOption<string>(
config,
'dotnet.unitTests.runSettingsPath',
'',
'omnisharp.testRunSettings'
);

// Omnisharp Server Options

Expand Down Expand Up @@ -179,7 +185,6 @@ export class Options {
'omnisharp.enableMsBuildLoadProjectsOnDemand',
false
);
const testRunSettings = Options.readOption<string>(config, 'omnisharp.testRunSettings', '');
const dotNetCliPaths = Options.readOption<string[]>(config, 'omnisharp.dotNetCliPaths', []);

const useFormatting = Options.readOption<boolean>(config, 'csharp.format.enable', true);
Expand Down Expand Up @@ -312,6 +317,7 @@ export class Options {
excludePaths: excludePaths,
defaultSolution: defaultSolution,
unitTestDebuggingOptions: unitTestDebuggingOptions,
runSettingsPath: runSettingsPath,
},
{
useModernNet: useModernNet,
Expand All @@ -337,7 +343,6 @@ export class Options {
sdkPath: sdkPath,
sdkVersion: sdkVersion,
sdkIncludePrereleases: sdkIncludePrereleases,
testRunSettings: testRunSettings,
dotNetCliPaths: dotNetCliPaths,
useFormatting: useFormatting,
showReferencesCodeLens: showReferencesCodeLens,
Expand Down Expand Up @@ -425,6 +430,7 @@ export interface CommonOptions {
/** The default solution; this has been normalized to a full file path from the workspace folder it was configured in, or the string "disable" if that has been disabled */
defaultSolution: string;
unitTestDebuggingOptions: object;
runSettingsPath: string;
}

export const CommonOptionsThatTriggerReload: ReadonlyArray<keyof CommonOptions> = [
Expand Down Expand Up @@ -458,7 +464,6 @@ export interface OmnisharpServerOptions {
sdkPath: string;
sdkVersion: string;
sdkIncludePrereleases: boolean;
testRunSettings: string;
dotNetCliPaths: string[];
useFormatting: boolean;
showReferencesCodeLens: boolean;
Expand Down
Loading