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
Changes from 2 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
36 changes: 26 additions & 10 deletions src/Cli/dotnet/commands/dotnet-test/TestCommandParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.CommandLine;
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 @@ -159,28 +161,42 @@ public static CliCommand GetCommand()
return Command;
}

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

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

if (!string.IsNullOrEmpty(globalJsonPath))
{
JsonNode globalJson = JsonObject.Parse(File.ReadAllText(globalJsonPath));

if (globalJson is not null)
{
var testSdkSection = globalJson["test-sdk"];
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks for "test-sdk" but the example in the PR description has "test-sdks".

if (testSdkSection is not null && testSdkSection["name"] is not null)
{
return testSdkSection["name"].AsValue().GetValue<string>();
}
}
}
return defaultTestingSdk;
}

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

if (isTestingPlatformEnabled)
if (testingSdkName.Equals("vstest", StringComparison.OrdinalIgnoreCase))
{
return GetTestingPlatformCliCommand();
return GetVSTestCliCommand();
}
else
else if (testingSdkName.Equals("testingplatform", StringComparison.OrdinalIgnoreCase))
{
return GetVSTestCliCommand();
return GetTestingPlatformCliCommand();
}

throw new InvalidOperationException($"Testing sdk not supported: {testingSdkName}");
Expand Down
Loading