Skip to content
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
17 changes: 16 additions & 1 deletion src/Cli/dotnet/Commands/Test/MTP/MSBuildUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,25 @@ public static BuildOptions GetBuildOptions(ParseResult parseResult)

LoggerUtility.SeparateBinLogArguments(parseResult.UnmatchedTokens, out var binLogArgs, out var otherArgs);

// Terminal logger arguments (e.g. --tl:off, -terminalLogger:auto, -tlp:default=true)
// should be forwarded to MSBuild during the build phase rather than being passed to
// the test application as it doesn't recognize them. See https://github.com/dotnet/sdk/issues/52229.
var terminalLoggerArgs = new List<string>();
for (int i = otherArgs.Count - 1; i >= 0; i--)
{
if (LoggerUtility.IsTerminalLoggerArgument(otherArgs[i]))
{
terminalLoggerArgs.Add(otherArgs[i]);
otherArgs.RemoveAt(i);
}
}
terminalLoggerArgs.Reverse();

var (positionalProjectOrSolution, positionalTestModules) = GetPositionalArguments(otherArgs);

var msbuildArgs = parseResult.OptionValuesToBeForwarded(definition)
.Concat(binLogArgs);
.Concat(binLogArgs)
.Concat(terminalLoggerArgs);

string? resultsDirectory = parseResult.GetValue(definition.ResultsDirectoryOption);
if (resultsDirectory is not null)
Expand Down
43 changes: 43 additions & 0 deletions src/Cli/dotnet/LoggerUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,49 @@ internal static bool IsBinLogArgument(string arg)
|| arg.StartsWith("--binaryLogger:", comp) || arg.Equals("--binaryLogger", comp)
|| arg.StartsWith("-bl:", comp) || arg.Equals("-bl", comp);
}

private static readonly string[] s_terminalLoggerArgumentNames =
Comment thread
Evangelink marked this conversation as resolved.
Resolved
[
"tl",
"terminallogger",
"ll",
"livelogger",
"tlp",
"terminalloggerparameters",
];

private static readonly string[] s_argumentPrefixes = ["--", "-", "/"];

/// <summary>
/// Determines whether the given argument is an MSBuild terminal logger argument
/// (e.g. <c>-tl[:value]</c>, <c>--terminalLogger[:value]</c>, <c>-ll[:value]</c>,
/// <c>--livelogger[:value]</c>, <c>-tlp:...</c>, or <c>--terminalLoggerParameters:...</c>).
/// </summary>
internal static bool IsTerminalLoggerArgument(string arg)
Comment thread
Evangelink marked this conversation as resolved.
Resolved
{
const StringComparison comp = StringComparison.OrdinalIgnoreCase;
foreach (var prefix in s_argumentPrefixes)
{
if (!arg.StartsWith(prefix, comp))
{
continue;
}

var nameAndValue = arg.AsSpan(prefix.Length);
int colonIndex = nameAndValue.IndexOf(':');
var name = colonIndex < 0 ? nameAndValue : nameAndValue[..colonIndex];

foreach (var knownName in s_terminalLoggerArgumentNames)
{
if (name.Equals(knownName.AsSpan(), comp))
{
return true;
}
}
}

return false;
}
}

/// <summary>
Expand Down
107 changes: 107 additions & 0 deletions test/dotnet.Tests/LoggerUtilityTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.DotNet.Cli;
using Microsoft.DotNet.Cli.Commands.Test;

namespace dotnet.Tests
{
public class LoggerUtilityTests
{
[Theory]
[InlineData("-tl")]
[InlineData("--tl")]
[InlineData("/tl")]
[InlineData("-tl:off")]
[InlineData("--tl:off")]
[InlineData("/tl:on")]
[InlineData("-TL:Off")]
[InlineData("-terminallogger")]
[InlineData("--terminalLogger")]
[InlineData("/terminallogger")]
[InlineData("-terminallogger:auto")]
[InlineData("--TerminalLogger:on")]
[InlineData("-ll")]
[InlineData("--ll:off")]
[InlineData("/ll")]
[InlineData("-livelogger")]
[InlineData("--livelogger:off")]
[InlineData("-tlp:default=true")]
[InlineData("--tlp:default=auto")]
[InlineData("/tlp:DISABLENODEDISPLAY")]
[InlineData("-terminalloggerparameters:default=true")]
[InlineData("--terminalLoggerParameters:default=true")]
public void IsTerminalLoggerArgument_RecognizesTerminalLoggerArguments(string arg)
{
LoggerUtility.IsTerminalLoggerArgument(arg).Should().BeTrue();
}

[Theory]
[InlineData("--no-build")]
[InlineData("-bl")]
[InlineData("--binaryLogger")]
[InlineData("-bl:foo.binlog")]
[InlineData("-tlapropertythatstartslikethis")]
[InlineData("--tlpwithnocolon")]
[InlineData("--terminallogger-something")]
[InlineData("-llextra")]
[InlineData("foo.csproj")]
[InlineData("")]
public void IsTerminalLoggerArgument_RejectsNonTerminalLoggerArguments(string arg)
{
LoggerUtility.IsTerminalLoggerArgument(arg).Should().BeFalse();
}

[Theory]
[InlineData("--tl:off")]
[InlineData("--terminalLogger:auto")]
[InlineData("--tlp:default=true")]
[InlineData("/tl:off")]
[InlineData("/terminalLogger:auto")]
[InlineData("/tlp:default=true")]
public void GetBuildOptions_ForwardsTerminalLoggerArgsToMSBuild_NotToTestApplication(string terminalLoggerArg)
{
// Parse a `dotnet test` command line that includes a terminal logger argument
// against the MTP test command definition. The argument is unknown to the parser
// and lands in UnmatchedTokens.
var mtpCommand = new TestCommandDefinition.MicrosoftTestingPlatform();
var parseResult = mtpCommand.Parse(["--no-build", terminalLoggerArg]);

var buildOptions = MSBuildUtility.GetBuildOptions(parseResult);

buildOptions.MSBuildArgs.Should().Contain(terminalLoggerArg,
"terminal logger arguments must be forwarded to the underlying MSBuild build invocation (https://github.com/dotnet/sdk/issues/52229).");
buildOptions.TestApplicationArguments.Should().NotContain(terminalLoggerArg,
"terminal logger arguments must not be passed to the test application, which doesn't recognize them.");
}

[Fact]
public void GetBuildOptions_LeavesUnknownArgumentsAsTestApplicationArguments()
{
var mtpCommand = new TestCommandDefinition.MicrosoftTestingPlatform();
var parseResult = mtpCommand.Parse(["--no-build", "--my-test-arg"]);

var buildOptions = MSBuildUtility.GetBuildOptions(parseResult);

buildOptions.TestApplicationArguments.Should().Contain("--my-test-arg");
buildOptions.MSBuildArgs.Should().NotContain("--my-test-arg");
}

[Fact]
public void GetBuildOptions_ExtractsTerminalLoggerArgs_BeforePositionalArgumentDetection()
{
// Regression test: verify that interspersing terminal logger args with positional arguments
// (project paths, test modules) and test application arguments doesn't break positional detection
// or argument routing. See https://github.com/dotnet/sdk/issues/52229.
var mtpCommand = new TestCommandDefinition.MicrosoftTestingPlatform();
var parseResult = mtpCommand.Parse(["--no-build", "--tl:off", "--my-test-arg", "--tlp:default=true"]);

var buildOptions = MSBuildUtility.GetBuildOptions(parseResult);

buildOptions.MSBuildArgs.Should().Contain("--tl:off").And.Contain("--tlp:default=true");
buildOptions.MSBuildArgs.Should().NotContain("--my-test-arg");
buildOptions.TestApplicationArguments.Should().Contain("--my-test-arg");
buildOptions.TestApplicationArguments.Should().NotContain("--tl:off").And.NotContain("--tlp:default=true");
}
}
}
Loading