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

Fix dotnet test bugs #833

Merged
merged 8 commits into from
Apr 27, 2017
15 changes: 7 additions & 8 deletions src/OmniSharp.DotNetTest/Legacy/LegacyTestManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.CodeAnalysis;
using Microsoft.Extensions.Logging;
using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;
using OmniSharp.DotNetTest.Models;
using OmniSharp.DotNetTest.Models.Events;
using OmniSharp.DotNetTest.TestFrameworks;
Expand Down Expand Up @@ -84,17 +85,15 @@ public override RunTestResponse RunTest(string methodName, string testFrameworkN

testProcess.OutputDataReceived += (_, e) =>
{
EventEmitter.Emit(TestMessageEvent.Id,
new TestMessageEvent
{
MessageLevel = "info",
Message = e.Data ?? string.Empty
});

EmitTestMessage(TestMessageLevel.Informational, e.Data ?? string.Empty);
output.AppendLine(e.Data);
};

testProcess.ErrorDataReceived += (_, e) => error.AppendLine(e.Data);
testProcess.ErrorDataReceived += (_, e) =>
{
EmitTestMessage(TestMessageLevel.Error, e.Data ?? string.Empty);
error.AppendLine(e.Data);
};

testProcess.BeginOutputReadLine();
testProcess.BeginErrorReadLine();
Expand Down
17 changes: 17 additions & 0 deletions src/OmniSharp.DotNetTest/TestManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
using Microsoft.Extensions.Logging;
using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities;
using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;
using OmniSharp.DotNetTest.Legacy;
using OmniSharp.DotNetTest.Models;
using OmniSharp.DotNetTest.Models.Events;
using OmniSharp.Eventing;
using OmniSharp.Services;
using OmniSharp.Utilities;
Expand Down Expand Up @@ -179,6 +181,21 @@ private static int FindFreePort()
}
}

protected void EmitTestMessage(TestMessageLevel messageLevel, string message)
{
EventEmitter.Emit(TestMessageEvent.Id,
new TestMessageEvent
{
MessageLevel = messageLevel.ToString().ToLowerInvariant(),
Message = message
});
}

protected void EmitTestMessage(TestMessagePayload testMessage)
{
EmitTestMessage(testMessage.MessageLevel, testMessage.Message);
}

protected Message ReadMessage()
{
var rawMessage = _reader.ReadString();
Expand Down
50 changes: 31 additions & 19 deletions src/OmniSharp.DotNetTest/VSTestManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;
using OmniSharp.DotNetTest.Models;
using OmniSharp.DotNetTest.Models.Events;
using OmniSharp.DotNetTest.TestFrameworks;
using OmniSharp.Eventing;
using OmniSharp.Services;
Expand Down Expand Up @@ -47,9 +47,24 @@ protected override bool PrepareToConnect()
{
// The project must be built before we can test.
var process = DotNetCli.Start("build", WorkingDirectory);

process.OutputDataReceived += (_, e) =>
{
EmitTestMessage(TestMessageLevel.Informational, e.Data ?? string.Empty);
};

process.ErrorDataReceived += (_, e) =>
{
EmitTestMessage(TestMessageLevel.Error, e.Data ?? string.Empty);
};

process.BeginOutputReadLine();
process.BeginErrorReadLine();

process.WaitForExit();

return File.Exists(Project.OutputFilePath);
return process.ExitCode == 0
&& File.Exists(Project.OutputFilePath);
}

private static void VerifyTestFramework(string testFrameworkName)
Expand Down Expand Up @@ -131,14 +146,7 @@ public override async Task DebugLaunchAsync(CancellationToken cancellationToken)
switch (message.MessageType)
{
case MessageType.TestMessage:
var testMessage = message.DeserializePayload<TestMessagePayload>();
EventEmitter.Emit(TestMessageEvent.Id,
new TestMessageEvent
{
MessageLevel = testMessage.MessageLevel.ToString().ToLowerInvariant(),
Message = testMessage.Message
});

EmitTestMessage(message.DeserializePayload<TestMessagePayload>());
break;

case MessageType.ExecutionComplete:
Expand Down Expand Up @@ -174,14 +182,7 @@ public override RunTestResponse RunTest(string methodName, string testFrameworkN
switch (message.MessageType)
{
case MessageType.TestMessage:
var testMessage = message.DeserializePayload<TestMessagePayload>();
EventEmitter.Emit(TestMessageEvent.Id,
new TestMessageEvent
{
MessageLevel = testMessage.MessageLevel.ToString().ToLowerInvariant(),
Message = testMessage.Message
});

EmitTestMessage(message.DeserializePayload<TestMessagePayload>());
break;

case MessageType.TestRunStatsChange:
Expand Down Expand Up @@ -240,12 +241,23 @@ private async Task<TestCase[]> DiscoverTestsAsync(string methodName, Cancellatio
switch (message.MessageType)
{
case MessageType.TestMessage:
EmitTestMessage(message.DeserializePayload<TestMessagePayload>());
break;

case MessageType.TestCasesFound:
foreach (var testCase in message.DeserializePayload<TestCase[]>())
{
if (testCase.DisplayName.StartsWith(methodName))
var testName = testCase.FullyQualifiedName;

var testNameEnd = testName.IndexOf('(');
if (testNameEnd >= 0)
{
testName = testName.Substring(0, testNameEnd);
}

testName = testName.Trim();

if (testName.Equals(methodName, StringComparison.Ordinal))
{
testCases.Add(testCase);
}
Expand Down
18 changes: 18 additions & 0 deletions test-assets/test-projects/XunitTestProject/TestProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,23 @@ private void UtilityFunction()
{

}

[Fact(DisplayName = "My Test Name")]
public void UsesDisplayName()
{
Assert.True(true);
}

[Fact]
public void TestWithSimilarName()
{
Assert.True(true);
}

[Fact]
public void TestWithSimilarNameFooBar()
{
Assert.True(true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace OmniSharp.DotNetTest.Tests
{
internal abstract class AbstractGetTestStartInfoFacts : AbstractSingleRequestHandlerTestFixture<GetTestStartInfoService>
public abstract class AbstractGetTestStartInfoFacts : AbstractTestFixture
{
protected const string LegacyXunitTestProject = "LegacyXunitTestProject";
protected const string LegacyNunitTestProject = "LegacyNunitTestProject";
Expand All @@ -21,7 +21,11 @@ protected AbstractGetTestStartInfoFacts(ITestOutputHelper output)
{
}

protected override string EndpointName { get; } = OmniSharpEndpoints.V2.GetTestStartInfo;
internal GetTestStartInfoService GetRequestHandler(OmniSharpTestHost host)
{
return host.GetRequestHandler<GetTestStartInfoService>(OmniSharpEndpoints.V2.GetTestStartInfo);
}


public abstract bool UseLegacyDotNetCli { get; }

Expand All @@ -46,6 +50,5 @@ protected async Task GetDotNetTestStartInfoAsync(string projectName, string meth
Assert.Equal(dotNetCli.DotNetPath, response.Executable);
}
}

}
}
16 changes: 13 additions & 3 deletions tests/OmniSharp.DotNetTest.Tests/AbstractRunTestFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace OmniSharp.DotNetTest.Tests
{
internal abstract class AbstractRunTestFacts : AbstractSingleRequestHandlerTestFixture<RunTestService>
public abstract class AbstractRunTestFacts : AbstractTestFixture
{
protected const string LegacyXunitTestProject = "LegacyXunitTestProject";
protected const string LegacyNunitTestProject = "LegacyNunitTestProject";
Expand All @@ -20,11 +20,14 @@ public AbstractRunTestFacts(ITestOutputHelper testOutput)
{
}

protected override string EndpointName { get; } = OmniSharpEndpoints.V2.RunTest;
internal RunTestService GetRequestHandler(OmniSharpTestHost host)
{
return host.GetRequestHandler<RunTestService>(OmniSharpEndpoints.V2.RunTest);
}

public abstract bool UseLegacyDotNetCli { get; }

protected async Task RunDotNetTestAsync(string projectName, string methodName, string testFramework, bool shouldPass)
protected async Task<RunTestResponse> RunDotNetTestAsync(string projectName, string methodName, string testFramework, bool shouldPass, bool expectResults = true)
{
using (var testProject = await TestAssets.Instance.GetTestProjectAsync(projectName))
using (var host = CreateOmniSharpHost(testProject.Directory, useLegacyDotNetCli: UseLegacyDotNetCli))
Expand All @@ -40,6 +43,11 @@ protected async Task RunDotNetTestAsync(string projectName, string methodName, s

var response = await service.Handle(request);

if (expectResults)
{
Assert.True(response.Results?.Length > 0, "Expected test to return results.");
}

if (shouldPass)
{
Assert.True(response.Pass, "Expected test to pass but it failed");
Expand All @@ -48,6 +56,8 @@ protected async Task RunDotNetTestAsync(string projectName, string methodName, s
{
Assert.False(response.Pass, "Expected test to fail but it passed");
}

return response;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/OmniSharp.DotNetTest.Tests/GetTestStartInfoFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace OmniSharp.DotNetTest.Tests
{
internal class GetTestStartInfoFacts : AbstractGetTestStartInfoFacts
public class GetTestStartInfoFacts : AbstractGetTestStartInfoFacts
{
public GetTestStartInfoFacts(ITestOutputHelper output) : base(output)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace OmniSharp.DotNetTest.Tests
{
internal class LegacyGetTestStartInfoFacts : AbstractGetTestStartInfoFacts
public class LegacyGetTestStartInfoFacts : AbstractGetTestStartInfoFacts
{
public LegacyGetTestStartInfoFacts(ITestOutputHelper testOutput) : base(testOutput)
{
Expand Down
2 changes: 1 addition & 1 deletion tests/OmniSharp.DotNetTest.Tests/LegacyRunTestFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace OmniSharp.DotNetTest.Tests
/// <summary>
/// Tests for legacy 'dotnet test' support for project.json support.
/// </summary>
internal class LegacyRunTestFacts : AbstractRunTestFacts
public class LegacyRunTestFacts : AbstractRunTestFacts
{
public LegacyRunTestFacts(ITestOutputHelper output)
: base(output)
Expand Down
24 changes: 23 additions & 1 deletion tests/OmniSharp.DotNetTest.Tests/RunTestFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace OmniSharp.DotNetTest.Tests
{
internal class RunTestFacts : AbstractRunTestFacts
public class RunTestFacts : AbstractRunTestFacts
{
public RunTestFacts(ITestOutputHelper testOutput)
: base(testOutput)
Expand Down Expand Up @@ -43,6 +43,28 @@ await RunDotNetTestAsync(
shouldPass: true);
}

[Fact]
public async Task RunXunitTestWithDisplayName()
{
await RunDotNetTestAsync(
XunitTestProject,
methodName: "Main.Test.MainTest.UsesDisplayName",
testFramework: "xunit",
shouldPass: true);
}

[Fact]
public async Task RunXunitTestWithSimilarName()
{
var response = await RunDotNetTestAsync(
XunitTestProject,
methodName: "Main.Test.MainTest.TestWithSimilarName",
testFramework: "xunit",
shouldPass: true);

Assert.Equal(1, response.Results.Length);
}

// NUnit does not work with .NET CLI RTM yet. https://github.com/nunit/dotnet-test-nunit/issues/108
// When it does, the NUnitTestProject should be updated and the tests below re-enabled.

Expand Down