Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ This is equivalent to deleting project.assets.json.</value>
<value>Include packages with symbols in addition to regular packages in output directory.</value>
</data>
<data name="CmdListTestsDescription" xml:space="preserve">
<value>List the discovered tests instead of running the tests.</value>
<value>List the discovered tests instead of running the tests. Optionally accepts a format: 'text' (default) for human-readable output or 'json' for machine-readable output.</value>
</data>
<data name="CmdLockedModeOptionDescription" xml:space="preserve">
<value>Don't allow updating project lock file.</value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,16 @@ public sealed class MicrosoftTestingPlatform : TestCommandDefinition, ICustomHel

public const string ListTestsOptionName = "--list-tests";

public readonly Option<string> ListTestsOption = new(ListTestsOptionName)
public const string ListTestsFormatText = "text";

public const string ListTestsFormatJson = "json";

public readonly Option<string> ListTestsOption = new Option<string>(ListTestsOptionName)
{
Description = CommandDefinitionStrings.CmdListTestsDescription,
Arity = ArgumentArity.Zero
};
HelpName = $"{ListTestsFormatText}|{ListTestsFormatJson}",
Arity = ArgumentArity.ZeroOrOne
}.AcceptOnlyFromAmong(ListTestsFormatText, ListTestsFormatJson);

public readonly Option<bool> NoLaunchProfileOption = new("--no-launch-profile")
{
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public int Run(ParseResult parseResult, bool isHelp)
var testOptions = new TestOptions(
IsHelp: isHelp,
IsDiscovery: parseResult.HasOption(definition.ListTestsOption),
ListTestsFormat: GetListTestsFormat(parseResult, definition),
EnvironmentVariables: parseResult.GetValue(definition.EnvOption) ?? ImmutableDictionary<string, string>.Empty);

var output = InitializeOutput(degreeOfParallelism, parseResult, testOptions);
Expand Down Expand Up @@ -110,6 +111,16 @@ public int Run(ParseResult parseResult, bool isHelp)
}
}

private static TestListFormat GetListTestsFormat(ParseResult parseResult, TestCommandDefinition.MicrosoftTestingPlatform definition)
{
// '--list-tests' has ZeroOrOne arity. A bare '--list-tests' (no value) defaults to text.
// The accepted values are constrained to 'text'/'json' by the option definition.
string? value = parseResult.GetValue(definition.ListTestsOption);
return string.Equals(value, TestCommandDefinition.MicrosoftTestingPlatform.ListTestsFormatJson, StringComparison.Ordinal)
? TestListFormat.Json
: TestListFormat.Text;
}

private static TerminalTestReporter InitializeOutput(int degreeOfParallelism, ParseResult parseResult, TestOptions testOptions)
{
var definition = (TestCommandDefinition.MicrosoftTestingPlatform)parseResult.CommandResult.Command;
Expand All @@ -119,6 +130,16 @@ private static TerminalTestReporter InitializeOutput(int degreeOfParallelism, Pa
var noProgress = parseResult.HasOption(definition.NoProgressOption);
var noAnsi = parseResult.HasOption(definition.NoAnsiOption);

// When emitting machine-readable JSON discovery output, stdout must contain only the JSON
// document. Force off ANSI, progress rendering and the per-assembly "Discovering tests from..."
// banners so nothing else is interleaved with the JSON.
bool isJsonDiscovery = testOptions.IsDiscovery && testOptions.ListTestsFormat == TestListFormat.Json;
if (isJsonDiscovery)
{
noProgress = true;
noAnsi = true;
}

// TODO: Replace this with proper CI detection that we already have in telemetry. https://github.com/microsoft/testfx/issues/5533#issuecomment-2838893327
bool inCI = string.Equals(Environment.GetEnvironmentVariable("TF_BUILD"), "true", StringComparison.OrdinalIgnoreCase) || string.Equals(Environment.GetEnvironmentVariable("GITHUB_ACTIONS"), "true", StringComparison.OrdinalIgnoreCase);

Expand All @@ -142,9 +163,10 @@ private static TerminalTestReporter InitializeOutput(int degreeOfParallelism, Pa
ShowProgress = !noProgress,
ShowActiveTests = !noProgress && ansiMode == AnsiMode.AnsiIfPossible,
AnsiMode = ansiMode,
ShowAssembly = true,
ShowAssemblyStartAndComplete = true,
ShowAssembly = !isJsonDiscovery,
ShowAssemblyStartAndComplete = !isJsonDiscovery,
MinimumExpectedTests = parseResult.GetValue(definition.MinimumExpectedTestsOption),
ListTestsFormat = testOptions.ListTestsFormat,
});

// Ctrl+C handling is wired in Run() through CtrlCCancellationManager so that
Expand Down
15 changes: 14 additions & 1 deletion src/Cli/dotnet/Commands/Test/MTP/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,20 @@

namespace Microsoft.DotNet.Cli.Commands.Test;

internal record TestOptions(bool IsHelp, bool IsDiscovery, IReadOnlyDictionary<string, string> EnvironmentVariables);
internal enum TestListFormat
{
/// <summary>
/// Human-readable discovery output (the default when '--list-tests' is passed without a value).
/// </summary>
Text,

/// <summary>
/// Machine-readable JSON discovery output ('--list-tests json').
/// </summary>
Json,
}

internal record TestOptions(bool IsHelp, bool IsDiscovery, TestListFormat ListTestsFormat, IReadOnlyDictionary<string, string> EnvironmentVariables);

internal record PathOptions(string? ProjectOrSolutionPath, string? SolutionPath, string? TestModules, string? ResultsDirectoryPath, string? ConfigFilePath, string? DiagnosticOutputDirectoryPath);

Expand Down
Loading
Loading