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

dotnet test output with color #641

Merged
merged 20 commits into from
Mar 31, 2017
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
2 changes: 1 addition & 1 deletion NuGet.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<clear />
<add key="dotnet-core" value="https://dotnet.myget.org/F/dotnet-core/api/v3/index.json" />
<add key="api.nuget.org" value="https://api.nuget.org/v3/index.json" />
<add key="vstest" value="https://dotnet.myget.org/F/vstest/auth/967ad631-d24c-4c0e-bece-922de3064956/api/v3/index.json" />
<add key="vstest" value="https://dotnet.myget.org/F/vstest/api/v3/index.json" />
<add key="dotnet.myget.org roslyn-tools" value="https://dotnet.myget.org/F/roslyn-tools/api/v3/index.json" />
</packageSources>
<activePackageSource>
Expand Down
2 changes: 1 addition & 1 deletion netci.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def branch = GithubBranchName
def testString = """call test.cmd -c ${configuration}"""
def platformtestString = """call test.cmd -c ${configuration} -p platformtests"""
def smoketestString = """call test.cmd -c ${configuration} -p smoke"""
def acceptancetestString = """call test.cmd -c ${configuration} -p AcceptanceTests"""
def acceptancetestString = """call test.cmd -c ${configuration} -p AcceptanceTests -v"""

// Create a new job for windows build
def newJob = job(newJobName) {
Expand Down
8 changes: 6 additions & 2 deletions scripts/build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ $env:TP_TOOLS_DIR = Join-Path $env:TP_ROOT_DIR "tools"
$env:TP_PACKAGES_DIR = Join-Path $env:TP_ROOT_DIR "packages"
$env:TP_OUT_DIR = Join-Path $env:TP_ROOT_DIR "artifacts"
$env:TP_PACKAGE_PROJ_DIR = Join-Path $env:TP_ROOT_DIR "src\package"
$env:TP_SRC_DIR = Join-Path $env:TP_ROOT_DIR "src"

#
# Dotnet configuration
Expand Down Expand Up @@ -539,7 +538,7 @@ function Build-SpecificProjects
$dotnetPath = Get-DotNetPath

# Get projects to build.
Get-ChildItem -Recurse -Path $env:TP_SRC_DIR -Include *.csproj | ForEach-Object {
Get-ChildItem -Recurse -Path $env:TP_ROOT_DIR -Include *.csproj | ForEach-Object {
foreach ($ProjectNamePattern in $ProjectNamePatterns) {
if($_.FullName -match $ProjectNamePattern) {
$ProjectsToBuild += ,"$_"
Expand All @@ -560,6 +559,11 @@ function Build-SpecificProjects
$output = & $dotnetPath build $ProjectToBuild
PrintAndExit-OnError $output

if (-Not ($ProjectToBuild.FullName -contains "$($env:TP_ROOT_DIR)$([IO.Path]::DirectorySeparatorChar)src")) {
# Don't copy artifacts for non src folders.
continue;
}

# Copy artifacts
$ProjectDir = [System.IO.Path]::GetDirectoryName($ProjectToBuild)
foreach($FrameworkAndOutDir in $FrameworksAndOutDirs) {
Expand Down
10 changes: 6 additions & 4 deletions scripts/test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -262,12 +262,12 @@ function Invoke-Test

if($fx -eq $TPT_TargetFrameworkFullCLR) {

Write-Verbose "$vstestConsolePath $testContainer /platform:$testArchitecture /framework:$testFrameWork /testAdapterPath:$testAdapterPath /logger:`"trx;LogFileName=$trxLogFileName`""
& $vstestConsolePath $testContainer /platform:$testArchitecture /framework:$testFrameWork /testAdapterPath:"$testAdapterPath" /logger:"trx;LogFileName=$trxLogFileName"
Write-Verbose "$vstestConsolePath $testContainer /platform:$testArchitecture /framework:$testFrameWork /testAdapterPath:$testAdapterPath /logger:`"trx;LogFileName=$trxLogFileName`" $ConsoleLogger"
& $vstestConsolePath $testContainer /platform:$testArchitecture /framework:$testFrameWork /testAdapterPath:"$testAdapterPath" /logger:"trx;LogFileName=$trxLogFileName" $ConsoleLogger
} else {

Write-Verbose "$dotNetPath $vstestConsolePath $testContainer /platform:$testArchitecture /framework:$testFrameWork /testAdapterPath:$testAdapterPath /logger:`"trx;LogFileName=$trxLogFileName`""
& $dotNetPath $vstestConsolePath $testContainer /platform:$testArchitecture /framework:$testFrameWork /testAdapterPath:"$testAdapterPath" /logger:"trx;LogFileName=$trxLogFileName"
Write-Verbose "$dotNetPath $vstestConsolePath $testContainer /platform:$testArchitecture /framework:$testFrameWork /testAdapterPath:$testAdapterPath /logger:`"trx;LogFileName=$trxLogFileName`" $ConsoleLogger"
& $dotNetPath $vstestConsolePath $testContainer /platform:$testArchitecture /framework:$testFrameWork /testAdapterPath:"$testAdapterPath" /logger:"trx;LogFileName=$trxLogFileName" $ConsoleLogger
}

Reset-TestEnvironment
Expand Down Expand Up @@ -334,6 +334,8 @@ Get-ChildItem env: | Where-Object -FilterScript { $_.Name.StartsWith("TP_") } |
Write-Log "Test run configuration: "
Get-Variable | Where-Object -FilterScript { $_.Name.StartsWith("TPT_") } | Format-Table

$ConsoleLogger = if ($VerbosePreference -eq "Continue") {'/logger:"console;verbosity=normal"'} else {'/logger:"console;verbosity=minimal"'}

Invoke-Test

Write-Log "Test complete. {$(Get-ElapsedTime($timer))}"
Expand Down
8 changes: 0 additions & 8 deletions src/Microsoft.TestPlatform.Build/Tasks/VSTestForwardingApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,15 @@ public int Execute()
FileName = hostExe,
Arguments = string.Join(" ", this.allArgs),
UseShellExecute = false,
CreateNoWindow = true,
Copy link
Contributor

Choose a reason for hiding this comment

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

What are the scenarios we have tried?

  • dotnet test in cmd, powershell, bash
  • dotnet test from Process.CreateProcess of any app
  • Mimic how vstest.console is called from VSTS task or a jenkins/travis or any CI system

We should ensure that CI tasks that run as services (and thus not interactive) don't face any issues with this approach.

Copy link
Member

Choose a reason for hiding this comment

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

Also try "dotnet watch test" and make sure the color flows all the way through.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@codito dotnet test working fine for above three scenarios. No color is displaying for dotnet test on VSTS task (same behavior as dotnet build).

@shanselman Yes, dotnet watch test displaying colored output.

RedirectStandardError = true,
RedirectStandardOutput = true
};

Tracing.Trace("VSTest: Starting vstest.console...");
Tracing.Trace("VSTest: Arguments: " + processInfo.FileName + " " + processInfo.Arguments);

using (var activeProcess = new Process { StartInfo = processInfo })
{
activeProcess.OutputDataReceived += (sender, args) => Console.WriteLine(args.Data);
Copy link
Contributor

Choose a reason for hiding this comment

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

Good change 👍

activeProcess.ErrorDataReceived += (sender, args) => Console.WriteLine(args.Data);

activeProcess.Start();
this.activeProcessId = activeProcess.Id;
activeProcess.BeginOutputReadLine();
activeProcess.BeginErrorReadLine();

activeProcess.WaitForExit();
Tracing.Trace("VSTest: Exit code: " + activeProcess.ExitCode);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Microsoft.VisualStudio.TestPlatform.Utilities
/// <summary>
/// Utility Methods for sending output to IOutput.
/// </summary>
public static class OutputUtilities
public static class OutputExtensions
{
private const string DefaultFormat = "{0}";

Expand All @@ -24,10 +24,10 @@ public static class OutputUtilities
/// <param name="args">Arguments to format into the format string.</param>
public static void Error(this IOutput output, string format, params object[] args)
{
using (new ConsoleColorHelper(ConsoleColor.Red))
SetColorForAction(ConsoleColor.Red, () =>
{
Output(output, OutputLevel.Error, DefaultFormat, format, args);
}
});
}

/// <summary>
Expand All @@ -38,10 +38,10 @@ public static void Error(this IOutput output, string format, params object[] arg
/// <param name="args">Arguments to format into the format string.</param>
public static void Warning(this IOutput output, string format, params object[] args)
{
using (new ConsoleColorHelper(ConsoleColor.Yellow))
SetColorForAction(ConsoleColor.Yellow, () =>
{
Output(output, OutputLevel.Warning, DefaultFormat, format, args);
}
});
}

/// <summary>
Expand All @@ -52,7 +52,22 @@ public static void Warning(this IOutput output, string format, params object[] a
/// <param name="args">Arguments to format into the format string.</param>
public static void Information(this IOutput output, string format, params object[] args)
{
Output(output, OutputLevel.Information, DefaultFormat, format, args);
Information(output, Console.ForegroundColor, format, args);
}

/// <summary>
/// Output a informational message.
/// </summary>
/// <param name="output">Output instance the method is being invoked with.</param>
/// <param name="format">Format string for the informational message.</param>
/// <param name="foregroundColor">Color in which text prints.</param>
/// <param name="args">Arguments to format into the format string.</param>
public static void Information(this IOutput output, ConsoleColor foregroundColor, string format, params object[] args)
{
SetColorForAction(foregroundColor, () =>
{
Output(output, OutputLevel.Information, DefaultFormat, format, args);
});
}

/// <summary>
Expand Down Expand Up @@ -99,5 +114,24 @@ private static string Format(string messageTypeFormat, string format, params obj

return string.Format(CultureInfo.CurrentCulture, messageTypeFormat, message);
}

private static void SetColorForAction(ConsoleColor foregroundColor, Action action)
{
if (action == null)
{
return;
}

var previousForegroundColor = Console.ForegroundColor;
try
{
Console.ForegroundColor = foregroundColor;
action.Invoke();
}
finally
{
Console.ForegroundColor = previousForegroundColor;
}
}
}
}
7 changes: 4 additions & 3 deletions src/Microsoft.TestPlatform.Extensions.TrxLogger/TrxLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace Microsoft.VisualStudio.TestPlatform.Extensions.TrxLogger
using Microsoft.TestPlatform.Extensions.TrxLogger.XML;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;
using Microsoft.VisualStudio.TestPlatform.Utilities;

using ObjectModel.Logging;

Expand Down Expand Up @@ -404,7 +405,7 @@ internal virtual void PopulateTrxFile(string trxFileName, XmlElement rootElement
{
var overwriteWarningMsg = string.Format(CultureInfo.CurrentCulture,
TrxLoggerResources.TrxLoggerResultsFileOverwriteWarning, trxFileName);
Console.WriteLine(overwriteWarningMsg);
ConsoleOutput.Instance.Warning(overwriteWarningMsg);
EqtTrace.Warning(overwriteWarningMsg);
}

Expand All @@ -413,12 +414,12 @@ internal virtual void PopulateTrxFile(string trxFileName, XmlElement rootElement
rootElement.OwnerDocument.Save(fs);
}
String resultsFileMessage = String.Format(CultureInfo.CurrentCulture, TrxLoggerResources.TrxLoggerResultsFile, trxFileName);
Console.WriteLine(resultsFileMessage);
ConsoleOutput.Instance.Information(resultsFileMessage);
EqtTrace.Info(resultsFileMessage);
}
catch (System.UnauthorizedAccessException fileWriteException)
{
Console.WriteLine(fileWriteException.Message);
ConsoleOutput.Instance.Error(fileWriteException.Message);
}
}

Expand Down
Loading