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
62 changes: 51 additions & 11 deletions src/Cli/dotnet/commands/dotnet-test/TestCommandParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.CommandLine;
using Microsoft.DotNet.Tools.Test;
using Microsoft.Extensions.Configuration;
using LocalizableStrings = Microsoft.DotNet.Tools.Test.LocalizableStrings;

namespace Microsoft.DotNet.Cli
Expand Down Expand Up @@ -151,28 +152,67 @@ public static CliCommand GetCommand()
return Command;
}

private static bool IsTestingPlatformEnabled()
public static string GetTestRunnerName()
{
var testingPlatformEnabledEnvironmentVariable = Environment.GetEnvironmentVariable("DOTNET_CLI_TESTINGPLATFORM_ENABLE");
var isTestingPlatformEnabled = testingPlatformEnabledEnvironmentVariable == "1" || string.Equals(testingPlatformEnabledEnvironmentVariable, "true", StringComparison.OrdinalIgnoreCase);
return isTestingPlatformEnabled;
var builder = new ConfigurationBuilder();

string dotnetConfigPath = GetDotnetConfigPath(Environment.CurrentDirectory);

if (!File.Exists(dotnetConfigPath))
{
return CliConstants.VSTest;
}

builder.AddIniFile(dotnetConfigPath);

IConfigurationRoot config = builder.Build();
var testSection = config.GetSection("dotnet.test");

if (!testSection.Exists())
{
return CliConstants.VSTest;
}

string runnerNameSection = testSection["runner:name"];

if (string.IsNullOrEmpty(runnerNameSection))
{
return CliConstants.VSTest;
}

return runnerNameSection;
}

private static string? GetDotnetConfigPath(string? startDir)
{
string? directory = startDir;
while (directory != null)
{
string dotnetConfigPath = Path.Combine(directory, "dotnet.config");
if (File.Exists(dotnetConfigPath))
{
return dotnetConfigPath;
}

directory = Path.GetDirectoryName(directory);
}
return null;
}
Comment on lines +186 to 200
Copy link
Contributor

@KalleOlaviNiemitalo KalleOlaviNiemitalo Feb 13, 2025

Choose a reason for hiding this comment

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

On Windows, should this disallow reading dotnet.config from the root directory of a volume, like #33216 and #33282 disallow reading .config\dotnet-tools.json from there?

Copy link
Contributor

Choose a reason for hiding this comment

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

I suppose such a restriction is not necessary for security because the volume root directory on Windows typically has an NT AUTHORITY\Authenticated Users:(AD) access control entry. This ACE allows the user to create a subdirectory such as .config in the root directory, and the user can then get full control on the new subdirectory and create a dotnet-tools.json file in there. However, the ACE does not allow the user to create a file such as dotnet.config or global.json directly in the root directory. See FILE_ADD_SUBDIRECTORY in File Access Rights Constants.


private static CliCommand ConstructCommand()
{
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(string.Format(LocalizableStrings.CmdUnsupportedTestRunnerDescription, testRunnerName));
}

private static CliCommand GetTestingPlatformCliCommand()
Expand Down
2 changes: 2 additions & 0 deletions test/TestAssets/TestProjects/EmptyFolder/dotnet.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[dotnet.test:runner]
name= "MicrosoftTestingPlatform"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[dotnet.test:runner]
name= "MicrosoftTestingPlatform"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[dotnet.test:runner]
name= "MicrosoftTestingPlatform"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[dotnet.test:runner]
name= "MicrosoftTestingPlatform"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[dotnet.test:runner]
name= "MicrosoftTestingPlatform"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[dotnet.test:runner]
name= "MicrosoftTestingPlatform"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[dotnet.test:runner]
name= "MicrosoftTestingPlatform"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[dotnet.test:runner]
name= "MicrosoftTestingPlatform"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[dotnet.test:runner]
name= "MicrosoftTestingPlatform"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[dotnet.test:runner]
name= "MicrosoftTestingPlatform"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[dotnet.test:runner]
name= "MicrosoftTestingPlatform"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[dotnet.test:runner]
name= "MicrosoftTestingPlatform"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[dotnet.test:runner]
name= "MicrosoftTestingPlatform"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[dotnet.test:runner]
name= "MicrosoftTestingPlatform"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[dotnet.test:runner]
name= "MicrosoftTestingPlatform"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[dotnet.test:runner]
name= "MicrosoftTestingPlatform"
Loading