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

Read global.json to decide to run dotnet test in vstest or testing platform mode #45418

Closed
Closed
Show file tree
Hide file tree
Changes from 7 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
5 changes: 5 additions & 0 deletions src/Cli/dotnet/commands/dotnet-test/CliConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ internal static class CliConstants

public const string MSBuildExeName = "MSBuild.dll";
public const string ParametersSeparator = "--";

public const string VSTest = "VSTest";
public const string MicrosoftTestingPlatform = "MicrosoftTestingPlatform";

public const string TestSectionKey = "test";
}

internal static class TestStates
Expand Down
49 changes: 36 additions & 13 deletions src/Cli/dotnet/commands/dotnet-test/TestCommandParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.CommandLine;
using System.Text.Json;
using System.Text.Json.Nodes;
using Microsoft.DotNet.Tools.Test;
using Microsoft.NET.Sdk.WorkloadManifestReader;
using LocalizableStrings = Microsoft.DotNet.Tools.Test.LocalizableStrings;

namespace Microsoft.DotNet.Cli
Expand Down Expand Up @@ -152,38 +155,58 @@ private static CliOption<string> CreateBlameHangDumpOption()

private static readonly CliCommand Command = ConstructCommand();



public static CliCommand GetCommand()
{
return Command;
}

private static bool IsTestingPlatformEnabled()
private static string GetTestRunnerName()
{
var testingPlatformEnabledEnvironmentVariable = Environment.GetEnvironmentVariable("DOTNET_CLI_TESTINGPLATFORM_ENABLE");
var isTestingPlatformEnabled = testingPlatformEnabledEnvironmentVariable == "1" || string.Equals(testingPlatformEnabledEnvironmentVariable, "true", StringComparison.OrdinalIgnoreCase);
return isTestingPlatformEnabled;
string defaultTestingSdk = CliConstants.VSTest;

string? globalJsonPath = SdkDirectoryWorkloadManifestProvider.GetGlobalJsonPath(Environment.CurrentDirectory);

if (!string.IsNullOrEmpty(globalJsonPath))
{
JsonNode globalJson = JsonObject.Parse(File.ReadAllText(globalJsonPath));
JsonNode? testSection = globalJson[CliConstants.TestSectionKey];

if (testSection is null)
{
return defaultTestingSdk;
}

JsonSerializerOptions JsonSerializerOptions = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
};
var testSettings = JsonSerializer.Deserialize<TestSettings>(testSection, JsonSerializerOptions);

if (testSettings?.Runner?.Name is not null)
{
return testSettings.Runner.Name;
}
}
return defaultTestingSdk;
}

private static CliCommand ConstructCommand()
{
#if RELEASE
return GetVSTestCliCommand();
#else
bool isTestingPlatformEnabled = IsTestingPlatformEnabled();
string testingSdkName = isTestingPlatformEnabled ? "testingplatform" : "vstest";
string testRunnerName = GetTestRunnerName();

if (isTestingPlatformEnabled)
if (testRunnerName.Equals(CliConstants.VSTest, StringComparison.OrdinalIgnoreCase))
{
return GetTestingPlatformCliCommand();
return GetVSTestCliCommand();
}
else
else if (testRunnerName.Equals(CliConstants.MicrosoftTestingPlatform, StringComparison.OrdinalIgnoreCase))
{
return GetVSTestCliCommand();
return GetTestingPlatformCliCommand();
}

throw new InvalidOperationException($"Testing sdk not supported: {testingSdkName}");
throw new InvalidOperationException($"Test runner not supported: {testRunnerName}");
#endif
}

Expand Down
8 changes: 8 additions & 0 deletions src/Cli/dotnet/commands/dotnet-test/TestSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.DotNet.Cli
{
internal sealed record TestSettings(Runner? Runner);
internal sealed record Runner(string Name);
}
Loading