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 ability to use custom runsettings for tests #1710

Merged
merged 5 commits into from
Mar 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 4 additions & 4 deletions src/OmniSharp.DotNetTest/DebugSessionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ public void EndSession()
}
}

public Task<DebugTestGetStartInfoResponse> DebugGetStartInfoAsync(string methodName, string testFrameworkName, string targetFrameworkVersion, CancellationToken cancellationToken)
=> DebugGetStartInfoAsync(new string[] { methodName }, testFrameworkName, targetFrameworkVersion, cancellationToken);
public Task<DebugTestGetStartInfoResponse> DebugGetStartInfoAsync(string methodName, string runSettings, string testFrameworkName, string targetFrameworkVersion, CancellationToken cancellationToken)
=> DebugGetStartInfoAsync(new string[] { methodName }, runSettings, testFrameworkName, targetFrameworkVersion, cancellationToken);

public Task<DebugTestGetStartInfoResponse> DebugGetStartInfoAsync(string[] methodNames, string testFrameworkName, string targetFrameworkVersion, CancellationToken cancellationToken)
public Task<DebugTestGetStartInfoResponse> DebugGetStartInfoAsync(string[] methodNames, string runSettings, string testFrameworkName, string targetFrameworkVersion, CancellationToken cancellationToken)
{
VerifySession(isStarted: true);

return _testManager.DebugGetStartInfoAsync(methodNames, testFrameworkName, targetFrameworkVersion, cancellationToken);
return _testManager.DebugGetStartInfoAsync(methodNames, runSettings, testFrameworkName, targetFrameworkVersion, cancellationToken);
}

public async Task<DebugTestLaunchResponse> DebugLaunchAsync(int targetProcessId)
Expand Down
1 change: 1 addition & 0 deletions src/OmniSharp.DotNetTest/Models/BaseTestClassRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace OmniSharp.DotNetTest.Models
public class BaseTestClassRequest : Request
{
public string[] MethodNames { get; set; }
public string RunSettings { get; set; }
public string TestFrameworkName { get; set; }

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace OmniSharp.DotNetTest.Models
public class DebugTestGetStartInfoRequest : Request
{
public string MethodName { get; set; }
public string RunSettings { get; set; }
public string TestFrameworkName { get; set; }
/// <summary>
/// e.g. .NETCoreApp, Version=2.0
Expand Down
1 change: 1 addition & 0 deletions src/OmniSharp.DotNetTest/Models/GetTestStartInfoRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace OmniSharp.DotNetTest.Models
public class GetTestStartInfoRequest : Request
{
public string MethodName { get; set; }
public string RunSettings { get; set; }
public string TestFrameworkName { get; set; }
/// <summary>
/// e.g. .NETCoreApp, Version=2.0
Expand Down
1 change: 1 addition & 0 deletions src/OmniSharp.DotNetTest/Models/RunTestRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace OmniSharp.DotNetTest.Models
public class RunTestRequest : Request
{
public string MethodName { get; set; }
public string RunSettings { get; set; }
public string TestFrameworkName { get; set; }
/// <summary>
/// e.g. .NETCoreApp, Version=2.0
Expand Down
2 changes: 1 addition & 1 deletion src/OmniSharp.DotNetTest/Services/DebugTestClassService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public async Task<DebugTestGetStartInfoResponse> Handle(DebugTestClassGetStartIn
var testManager = CreateTestManager(request.FileName);
_debugSessionManager.StartSession(testManager);

return await _debugSessionManager.DebugGetStartInfoAsync(request.MethodNames, request.TestFrameworkName, request.TargetFrameworkVersion, CancellationToken.None);
return await _debugSessionManager.DebugGetStartInfoAsync(request.MethodNames, request.RunSettings, request.TestFrameworkName, request.TargetFrameworkVersion, CancellationToken.None);
}
}
}
2 changes: 1 addition & 1 deletion src/OmniSharp.DotNetTest/Services/DebugTestService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public Task<DebugTestGetStartInfoResponse> Handle(DebugTestGetStartInfoRequest r
{
//only if the test manager connected successfully, shall we proceed with the request
_debugSessionManager.StartSession(testManager);
return _debugSessionManager.DebugGetStartInfoAsync(request.MethodName, request.TestFrameworkName, request.TargetFrameworkVersion, CancellationToken.None);
return _debugSessionManager.DebugGetStartInfoAsync(request.MethodName, request.RunSettings, request.TestFrameworkName, request.TargetFrameworkVersion, CancellationToken.None);
}

throw new InvalidOperationException("The debugger could not be started");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public GetTestStartInfoService(OmniSharpWorkspace workspace, IDotNetCliService d

protected override GetTestStartInfoResponse HandleRequest(GetTestStartInfoRequest request, TestManager testManager)
{
return testManager.GetTestStartInfo(request.MethodName, request.TestFrameworkName, request.TargetFrameworkVersion);
return testManager.GetTestStartInfo(request.MethodName, request.RunSettings, request.TestFrameworkName, request.TargetFrameworkVersion);
}
}
}
2 changes: 1 addition & 1 deletion src/OmniSharp.DotNetTest/Services/RunTestService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protected override RunTestResponse HandleRequest(RunTestRequest request, TestMan
{
if (testManager.IsConnected)
{
return testManager.RunTest(request.MethodName, request.TestFrameworkName, request.TargetFrameworkVersion);
return testManager.RunTest(request.MethodName, request.RunSettings, request.TestFrameworkName, request.TargetFrameworkVersion);
}

var response = new RunTestResponse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protected override RunTestResponse HandleRequest(RunTestsInClassRequest request,
{
if (testManager.IsConnected)
{
return testManager.RunTest(request.MethodNames, request.TestFrameworkName, request.TargetFrameworkVersion);
return testManager.RunTest(request.MethodNames, request.RunSettings, request.TestFrameworkName, request.TargetFrameworkVersion);
}

var response = new RunTestResponse
Expand Down
10 changes: 5 additions & 5 deletions src/OmniSharp.DotNetTest/TestManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,18 @@ public static TestManager Create(Project project, IDotNetCliService dotNetCli, I
protected abstract string GetCliTestArguments(int port, int parentProcessId);
protected abstract void VersionCheck();

public abstract RunTestResponse RunTest(string methodName, string testFrameworkName, string targetFrameworkVersion);
public abstract RunTestResponse RunTest(string methodName, string runSettings, string testFrameworkName, string targetFrameworkVersion);

public virtual RunTestResponse RunTest(string[] methodNames, string testFrameworkName, string targetFrameworkVersion)
public virtual RunTestResponse RunTest(string[] methodNames, string runSettings, string testFrameworkName, string targetFrameworkVersion)
{
throw new NotImplementedException();
}

public abstract GetTestStartInfoResponse GetTestStartInfo(string methodName, string testFrameworkName, string targetFrameworkVersion);
public abstract GetTestStartInfoResponse GetTestStartInfo(string methodName, string runSettings, string testFrameworkName, string targetFrameworkVersion);

public abstract Task<DebugTestGetStartInfoResponse> DebugGetStartInfoAsync(string methodName, string testFrameworkName, string targetFrameworkVersion, CancellationToken cancellationToken);
public abstract Task<DebugTestGetStartInfoResponse> DebugGetStartInfoAsync(string methodName, string runSettings, string testFrameworkName, string targetFrameworkVersion, CancellationToken cancellationToken);

public virtual Task<DebugTestGetStartInfoResponse> DebugGetStartInfoAsync(string[] methodNames, string testFrameworkName, string targetFrameworkVersion, CancellationToken cancellationToken)
public virtual Task<DebugTestGetStartInfoResponse> DebugGetStartInfoAsync(string[] methodNames, string runSettings, string testFrameworkName, string targetFrameworkVersion, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
Expand Down
58 changes: 37 additions & 21 deletions src/OmniSharp.DotNetTest/VSTestManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,24 @@ public VSTestManager(Project project, string workingDirectory, IDotNetCliService
{
}

private object GetDefaultRunSettings(string targetFrameworkVersion)
private object LoadRunSettingsOrDefault(string runSettingsPath, string targetFrameworkVersion)
{
if (runSettingsPath != null)
{
try
{
return File.ReadAllText(runSettingsPath);
}
catch (FileNotFoundException)
{
EmitTestMessage(TestMessageLevel.Warning, $"RunSettings file {runSettingsPath} not found. Continuing with default settings...");
}
catch (Exception e)
{
EmitTestMessage(TestMessageLevel.Warning, $"There was an error loading runsettings at {runSettingsPath}: {e}. Continuing with default settings...");
}
}

if (!string.IsNullOrWhiteSpace(targetFrameworkVersion))
{
return $@"
Expand Down Expand Up @@ -101,18 +117,18 @@ private static void VerifyTestFramework(string testFrameworkName)
}
}

public override GetTestStartInfoResponse GetTestStartInfo(string methodName, string testFrameworkName, string targetFrameworkVersion)
public override GetTestStartInfoResponse GetTestStartInfo(string methodName, string runSettings, string testFrameworkName, string targetFrameworkVersion)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

normally we do not break the public APIs but this particular package is no published on nuget, so it's OK

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though these functions are public, TestManager and VSTestManager are actually internal classes, so there probably isn't any worry.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was more concerned about the changes to the over-the-wire interface. At least the stdio version of the server seems to treat arguments as optional, in which case I think everything remains compatible.

{
VerifyTestFramework(testFrameworkName);

var testCases = DiscoverTests(new string[] { methodName }, targetFrameworkVersion);
var testCases = DiscoverTests(new string[] { methodName }, runSettings, targetFrameworkVersion);

SendMessage(MessageType.GetTestRunnerProcessStartInfoForRunSelected,
new
{
TestCases = testCases,
DebuggingEnabled = true,
RunSettings = GetDefaultRunSettings(targetFrameworkVersion)
RunSettings = LoadRunSettingsOrDefault(runSettings, targetFrameworkVersion)
});

var message = ReadMessage();
Expand All @@ -126,21 +142,21 @@ public override GetTestStartInfoResponse GetTestStartInfo(string methodName, str
};
}

public override async Task<DebugTestGetStartInfoResponse> DebugGetStartInfoAsync(string methodName, string testFrameworkName, string targetFrameworkVersion, CancellationToken cancellationToken)
=> await DebugGetStartInfoAsync(new string[] { methodName }, testFrameworkName, targetFrameworkVersion, cancellationToken);
public override async Task<DebugTestGetStartInfoResponse> DebugGetStartInfoAsync(string methodName, string runSettings, string testFrameworkName, string targetFrameworkVersion, CancellationToken cancellationToken)
=> await DebugGetStartInfoAsync(new string[] { methodName }, runSettings, testFrameworkName, targetFrameworkVersion, cancellationToken);

public override async Task<DebugTestGetStartInfoResponse> DebugGetStartInfoAsync(string[] methodNames, string testFrameworkName, string targetFrameworkVersion, CancellationToken cancellationToken)
public override async Task<DebugTestGetStartInfoResponse> DebugGetStartInfoAsync(string[] methodNames, string runSettings, string testFrameworkName, string targetFrameworkVersion, CancellationToken cancellationToken)
{
VerifyTestFramework(testFrameworkName);

var testCases = await DiscoverTestsAsync(methodNames, targetFrameworkVersion, cancellationToken);
var testCases = await DiscoverTestsAsync(methodNames, runSettings, targetFrameworkVersion, cancellationToken);

SendMessage(MessageType.GetTestRunnerProcessStartInfoForRunSelected,
new
{
TestCases = testCases,
DebuggingEnabled = true,
RunSettings = GetDefaultRunSettings(targetFrameworkVersion)
RunSettings = LoadRunSettingsOrDefault(runSettings, targetFrameworkVersion)
});

var message = await ReadMessageAsync(cancellationToken);
Expand Down Expand Up @@ -186,14 +202,14 @@ public override async Task DebugLaunchAsync(CancellationToken cancellationToken)
}
}

public override RunTestResponse RunTest(string methodName, string testFrameworkName, string targetFrameworkVersion)
=> RunTest(new string[] { methodName }, testFrameworkName, targetFrameworkVersion);
public override RunTestResponse RunTest(string methodName, string runSettings, string testFrameworkName, string targetFrameworkVersion)
=> RunTest(new string[] { methodName }, runSettings, testFrameworkName, targetFrameworkVersion);

public override RunTestResponse RunTest(string[] methodNames, string testFrameworkName, string targetFrameworkVersion)
public override RunTestResponse RunTest(string[] methodNames, string runSettings, string testFrameworkName, string targetFrameworkVersion)
{
VerifyTestFramework(testFrameworkName);

var testCases = DiscoverTests(methodNames, targetFrameworkVersion);
var testCases = DiscoverTests(methodNames, runSettings, targetFrameworkVersion);

var testResults = new List<TestResult>();

Expand All @@ -204,7 +220,7 @@ public override RunTestResponse RunTest(string[] methodNames, string testFramewo
new
{
TestCases = testCases,
RunSettings = GetDefaultRunSettings(targetFrameworkVersion)
RunSettings = LoadRunSettingsOrDefault(runSettings, targetFrameworkVersion)
});

var done = false;
Expand Down Expand Up @@ -244,10 +260,10 @@ public override RunTestResponse RunTest(string[] methodNames, string testFramewo
ErrorMessage = testResult.ErrorMessage,
ErrorStackTrace = testResult.ErrorStackTrace,
StandardOutput = testResult.Messages
.Where(message => message.Category == TestResultMessage.StandardOutCategory)
.Select(message => message.Text).ToArray(),
.Where(message => message.Category == TestResultMessage.StandardOutCategory)
.Select(message => message.Text).ToArray(),
StandardError = testResult.Messages.Where(message => message.Category == TestResultMessage.StandardErrorCategory)
.Select(message => message.Text).ToArray()
.Select(message => message.Text).ToArray()
});

return new RunTestResponse
Expand All @@ -257,7 +273,7 @@ public override RunTestResponse RunTest(string[] methodNames, string testFramewo
};
}

private async Task<TestCase[]> DiscoverTestsAsync(string[] methodNames, string targetFrameworkVersion, CancellationToken cancellationToken)
private async Task<TestCase[]> DiscoverTestsAsync(string[] methodNames, string runSettings, string targetFrameworkVersion, CancellationToken cancellationToken)
{
SendMessage(MessageType.StartDiscovery,
new
Expand All @@ -266,7 +282,7 @@ private async Task<TestCase[]> DiscoverTestsAsync(string[] methodNames, string t
{
Project.OutputFilePath
},
RunSettings = GetDefaultRunSettings(targetFrameworkVersion)
RunSettings = LoadRunSettingsOrDefault(runSettings, targetFrameworkVersion)
});

var testCases = new List<TestCase>();
Expand Down Expand Up @@ -322,9 +338,9 @@ bool isInRequestedMethods(TestCase testCase)
};
}

private TestCase[] DiscoverTests(string[] methodNames, string targetFrameworkVersion)
private TestCase[] DiscoverTests(string[] methodNames, string runSettings, string targetFrameworkVersion)
{
return DiscoverTestsAsync(methodNames, targetFrameworkVersion, CancellationToken.None).Result;
return DiscoverTestsAsync(methodNames, runSettings, targetFrameworkVersion, CancellationToken.None).Result;
}
}
}
8 changes: 8 additions & 0 deletions test-assets/test-projects/MSTestProject/TestProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace Main.Test
[TestClass]
public class MainTest
{
public TestContext TestContext { get; set; }

[TestMethod]
public void Test()
{
Expand Down Expand Up @@ -42,6 +44,12 @@ public void CheckStandardOutput()
Assert.AreEqual(a,b);
}

[TestMethod]
public void CheckRunSettings()
{
Assert.AreEqual(TestContext.Properties["TestRunSetting"].ToString(), "CorrectValue");
}

private void UtilityFunction()
{

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<RunSettings>
<TestRunParameters>
<Parameter name="TestRunSetting" value="CorrectValue" />
</TestRunParameters>
</RunSettings>
7 changes: 6 additions & 1 deletion tests/OmniSharp.DotNetTest.Tests/AbstractRunTestFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal RunTestService GetRequestHandler(OmniSharpTestHost host)
return host.GetRequestHandler<RunTestService>(OmniSharpEndpoints.V2.RunTest);
}

protected async Task<RunTestResponse> RunDotNetTestAsync(string projectName, string methodName, string testFramework, bool shouldPass, string targetFrameworkVersion = null, bool expectResults = true)
protected async Task<RunTestResponse> RunDotNetTestAsync(string projectName, string methodName, string testFramework, bool shouldPass, string targetFrameworkVersion = null, bool expectResults = true, bool useRunSettings = false)
{
using (var testProject = await TestAssets.Instance.GetTestProjectAsync(projectName))
using (var host = CreateOmniSharpHost(testProject.Directory, null, DotNetCliVersion))
Expand All @@ -35,6 +35,11 @@ protected async Task<RunTestResponse> RunDotNetTestAsync(string projectName, str
TargetFrameworkVersion = targetFrameworkVersion
};

if (useRunSettings)
{
request.RunSettings = Path.Combine(testProject.Directory, "TestRunSettings.runsettings");
}

var response = await service.Handle(request);

if (expectResults)
Expand Down
26 changes: 26 additions & 0 deletions tests/OmniSharp.DotNetTest.Tests/RunTestFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,5 +223,31 @@ public async Task RunMSTestStandardOutputIsReturned()
Assert.Single(response.Results);
Assert.NotEmpty(response.Results[0].StandardOutput);
}

[Fact]
public async Task RunMSTestWithRunSettings()
{
await RunDotNetTestAsync(
MSTestProject,
methodName: "Main.Test.MainTest.CheckRunSettings",
testFramework: "mstest",
shouldPass: true,
useRunSettings: true);
}

[Fact]
public async Task RunMSTestWithoutRunSettings()
{
var response = await RunDotNetTestAsync(
MSTestProject,
methodName: "Main.Test.MainTest.CheckRunSettings",
testFramework: "mstest",
shouldPass: false,
useRunSettings: false);

Assert.Single(response.Results);
Assert.NotEmpty(response.Results[0].ErrorMessage);
Assert.NotEmpty(response.Results[0].ErrorStackTrace);
}
}
}