Skip to content

Commit

Permalink
Console logger splits path using directory and alt directory separato…
Browse files Browse the repository at this point in the history
…rs (#3923)
  • Loading branch information
Evangelink authored Aug 22, 2022
1 parent a1c9f79 commit c0f8e0e
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 11 deletions.
10 changes: 5 additions & 5 deletions src/vstest.console/Internal/ConsoleLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;

Expand Down Expand Up @@ -161,7 +162,6 @@ protected static IOutput? Output
/// </summary>
private ConcurrentDictionary<Guid, MinimalTestResult>? LeafTestResults { get; set; }


#region ITestLoggerWithParameters

/// <summary>
Expand Down Expand Up @@ -678,8 +678,8 @@ private void TestRunCompleteHandler(object? sender, TestRunCompleteEventArgs e)
Output.WriteLine(string.Empty, OutputLevel.Information);

// Printing Run-level Attachments
var runLevelAttachementCount = e.AttachmentSets == null ? 0 : e.AttachmentSets.Sum(attachmentSet => attachmentSet.Attachments.Count);
if (runLevelAttachementCount > 0)
var runLevelAttachmentsCount = e.AttachmentSets == null ? 0 : e.AttachmentSets.Sum(attachmentSet => attachmentSet.Attachments.Count);
if (runLevelAttachmentsCount > 0)
{
// If ARTIFACTS_POSTPROCESSING is disabled
if (_featureFlag.IsSet(FeatureFlag.DISABLE_ARTIFACTS_POSTPROCESSING) ||
Expand All @@ -689,7 +689,7 @@ private void TestRunCompleteHandler(object? sender, TestRunCompleteEventArgs e)
CommandLineOptions.Instance.TestSessionCorrelationId is null)
{
Output.Information(false, CommandLineResources.AttachmentsBanner);
TPDebug.Assert(e.AttachmentSets != null, "e.AttachmentSets should not be null when runLevelAttachementCount > 0.");
TPDebug.Assert(e.AttachmentSets != null, "e.AttachmentSets should not be null when runLevelAttachmentsCount > 0.");
foreach (var attachmentSet in e.AttachmentSets)
{
foreach (var uriDataAttachment in attachmentSet.Attachments)
Expand Down Expand Up @@ -765,7 +765,7 @@ private void TestRunCompleteHandler(object? sender, TestRunCompleteEventArgs e)
: $"({_targetFramework})";

var duration = GetFormattedDurationString(sourceSummary.Duration);
var sourceName = sd.Key.Split('\\').Last();
var sourceName = Path.GetFileName(sd.Key);

var outputLine = string.Format(CultureInfo.CurrentCulture, CommandLineResources.TestRunSummary,
resultString,
Expand Down
10 changes: 4 additions & 6 deletions test/Microsoft.TestPlatform.TestUtilities/IntegrationTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ public class IntegrationTestBase
private readonly string _xUnitTestAdapterRelativePath = @"xunit.runner.visualstudio\{0}\build\_common".Replace('\\', Path.DirectorySeparatorChar);
private readonly string _chutzpahTestAdapterRelativePath = @"chutzpah\{0}\tools".Replace('\\', Path.DirectorySeparatorChar);

protected static readonly bool IsWindows = Environment.OSVersion.Platform.ToString().StartsWith("Win");

public enum UnitTestFramework
{
NUnit, XUnit, MSTest, CPP, Chutzpah
Expand Down Expand Up @@ -626,7 +624,7 @@ public virtual string GetConsoleRunnerPath()
}
else if (IsNetCoreRunner())
{
var executablePath = IsWindows ? @"dotnet\dotnet.exe" : @"dotnet-linux/dotnet";
var executablePath = OSUtils.IsWindows ? @"dotnet\dotnet.exe" : @"dotnet-linux/dotnet";
consoleRunnerPath = Path.Combine(_testEnvironment.ToolsDirectory, executablePath);
}
else
Expand Down Expand Up @@ -678,7 +676,7 @@ public IVsTestConsoleWrapper GetVsTestConsoleWrapper(Dictionary<string, string?>
var consoleRunnerPath = IsNetCoreRunner()
? GetDotnetRunnerPath()
: GetConsoleRunnerPath();
var executablePath = IsWindows ? @"dotnet\dotnet.exe" : @"dotnet-linux/dotnet";
var executablePath = OSUtils.IsWindows ? @"dotnet\dotnet.exe" : @"dotnet-linux/dotnet";
var dotnetPath = Path.Combine(_testEnvironment.ToolsDirectory, executablePath);

if (!File.Exists(dotnetPath))
Expand Down Expand Up @@ -770,7 +768,7 @@ private void ExecutePatchedDotnet(string command, string args, out string stdOut

environmentVariables["DOTNET_MULTILEVEL_LOOKUP"] = "0";

var executablePath = IsWindows ? @"dotnet\dotnet.exe" : @"dotnet-linux/dotnet";
var executablePath = OSUtils.IsWindows ? @"dotnet\dotnet.exe" : @"dotnet-linux/dotnet";
var patchedDotnetPath = Path.Combine(_testEnvironment.TestArtifactsDirectory, executablePath);
ExecuteApplication(patchedDotnetPath, string.Join(" ", command, args), out stdOut, out stdError, out exitCode, environmentVariables);
}
Expand Down Expand Up @@ -954,7 +952,7 @@ protected static string GetDownloadedDotnetMuxerFromTools(string architecture)
architecture == "X86" ?
"dotnet_x86" :
$"dotnet",
$"dotnet{(IsWindows ? ".exe" : "")}");
$"dotnet{(OSUtils.IsWindows ? ".exe" : "")}");

Assert.IsTrue(File.Exists(path));

Expand Down
9 changes: 9 additions & 0 deletions test/Microsoft.TestPlatform.TestUtilities/OSUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Microsoft.TestPlatform.TestUtilities;

public static class OSUtils
{
public static bool IsWindows { get; } = System.Environment.OSVersion.Platform.ToString().StartsWith("Win");
}
48 changes: 48 additions & 0 deletions test/vstest.console.UnitTests/Internal/ConsoleLoggerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using Microsoft.VisualStudio.TestPlatform.Utilities;
using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.TestPlatform.TestUtilities;

using Moq;

Expand Down Expand Up @@ -826,6 +827,53 @@ public void TestResultHandlerForTestResultWithDurationLessThanOneMsShouldPrintDu
_mockOutput.Verify(o => o.WriteLine("TestName [< 1 ms]", OutputLevel.Information), Times.Once());
}

[TestMethod]
public void TestRunCompleteHandlerCorrectlySplitPathsForSourceName()
{
// Arrange
var loggerEvents = new InternalTestLoggerEvents(TestSessionMessageLogger.Instance);
loggerEvents.EnableEvents();
var parameters = new Dictionary<string, string?>
{
{ "verbosity", "minimal" }
};
_consoleLogger.Initialize(loggerEvents, parameters);

// Linux-like path
loggerEvents.RaiseTestResult(new(new(new("FQN1", new Uri("some://uri"), "/home/MyApp1/Tests/MyApp1.Tests/MyApp1.Tests.dll"))));
// Double forward slashes path
loggerEvents.RaiseTestResult(new(new(new("FQN2", new Uri("some://uri"), "/home//MyApp2//Tests//MyApp2.Tests//MyApp2.Tests.dll"))));
// Backslashes path
loggerEvents.RaiseTestResult(new(new(new("FQN3", new Uri("some://uri"), @"C:\MyApp3/Tests/MyApp3.Tests\MyApp3.Tests.dll"))));
// Multiple Backslashes path
loggerEvents.RaiseTestResult(new(new(new("FQN4", new Uri("some://uri"), "C:\\\\MyApp4\\\\Tests\\\\MyApp4.Tests\\\\MyApp4.Tests.dll"))));
// Mix backslashes and forward slashes path
loggerEvents.RaiseTestResult(new(new(new("FQN5", new Uri("some://uri"), "C:\\MyApp5/Tests\\\\MyApp5.Tests///MyApp5.Tests.dll"))));
// UNC path
loggerEvents.RaiseTestResult(new(new(new("FQN6", new Uri("some://uri"), @"\\MyApp6\Tests\MyApp6.Tests\MyApp6.Tests.dll"))));

// Act
loggerEvents.CompleteTestRun(null, false, false, null, null, null, new TimeSpan(1, 0, 0, 0));

// Assert
VerifyCall("MyApp1.Tests.dll");
VerifyCall("MyApp2.Tests.dll");
// On Linux and MAC we don't support backslash for path so source name will contain backslashes.
VerifyCall(OSUtils.IsWindows ? "MyApp3.Tests.dll" : "MyApp3.Tests\\MyApp3.Tests.dll");
// On Linux and MAC we don't support backslash for path so source name will contain backslashes.
VerifyCall(OSUtils.IsWindows ? "MyApp4.Tests.dll" : @"C:\\MyApp4\\Tests\\MyApp4.Tests\\MyApp4.Tests.dll");
VerifyCall("MyApp5.Tests.dll");
VerifyCall(OSUtils.IsWindows ? "MyApp6.Tests.dll" : @"\\MyApp6\Tests\MyApp6.Tests\MyApp6.Tests.dll");

// Local functions
void VerifyCall(string testName)
=> _mockOutput.Verify(
o => o.WriteLine(
string.Format(CultureInfo.CurrentCulture, CommandLineResources.TestRunSummaryAssemblyAndFramework, testName, ""),
OutputLevel.Information),
Times.Once());
}

[TestMethod]
public void TestRunCompleteHandlerShouldWriteToConsoleIfTestsPass()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<ProjectReference Include="..\..\src\Microsoft.TestPlatform.PlatformAbstractions\Microsoft.TestPlatform.PlatformAbstractions.csproj">
<FromP2P>true</FromP2P>
</ProjectReference>
<ProjectReference Include="..\Microsoft.TestPlatform.TestUtilities\Microsoft.TestPlatform.TestUtilities.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="$(FluentAssertionsVersion)" />
Expand Down

0 comments on commit c0f8e0e

Please sign in to comment.