Skip to content

Commit 1eb697b

Browse files
authored
Read dotnet.config file to use MTP or VSTest in dotnet test (#46717)
2 parents f39664b + dd191b5 commit 1eb697b

File tree

17 files changed

+83
-11
lines changed

17 files changed

+83
-11
lines changed

src/Cli/dotnet/commands/dotnet-test/TestCommandParser.cs

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.CommandLine;
55
using Microsoft.DotNet.Tools.Test;
6+
using Microsoft.Extensions.Configuration;
67
using LocalizableStrings = Microsoft.DotNet.Tools.Test.LocalizableStrings;
78

89
namespace Microsoft.DotNet.Cli
@@ -151,28 +152,67 @@ public static CliCommand GetCommand()
151152
return Command;
152153
}
153154

154-
private static bool IsTestingPlatformEnabled()
155+
public static string GetTestRunnerName()
155156
{
156-
var testingPlatformEnabledEnvironmentVariable = Environment.GetEnvironmentVariable("DOTNET_CLI_TESTINGPLATFORM_ENABLE");
157-
var isTestingPlatformEnabled = testingPlatformEnabledEnvironmentVariable == "1" || string.Equals(testingPlatformEnabledEnvironmentVariable, "true", StringComparison.OrdinalIgnoreCase);
158-
return isTestingPlatformEnabled;
157+
var builder = new ConfigurationBuilder();
158+
159+
string dotnetConfigPath = GetDotnetConfigPath(Environment.CurrentDirectory);
160+
161+
if (!File.Exists(dotnetConfigPath))
162+
{
163+
return CliConstants.VSTest;
164+
}
165+
166+
builder.AddIniFile(dotnetConfigPath);
167+
168+
IConfigurationRoot config = builder.Build();
169+
var testSection = config.GetSection("dotnet.test");
170+
171+
if (!testSection.Exists())
172+
{
173+
return CliConstants.VSTest;
174+
}
175+
176+
string runnerNameSection = testSection["runner:name"];
177+
178+
if (string.IsNullOrEmpty(runnerNameSection))
179+
{
180+
return CliConstants.VSTest;
181+
}
182+
183+
return runnerNameSection;
184+
}
185+
186+
private static string? GetDotnetConfigPath(string? startDir)
187+
{
188+
string? directory = startDir;
189+
while (directory != null)
190+
{
191+
string dotnetConfigPath = Path.Combine(directory, "dotnet.config");
192+
if (File.Exists(dotnetConfigPath))
193+
{
194+
return dotnetConfigPath;
195+
}
196+
197+
directory = Path.GetDirectoryName(directory);
198+
}
199+
return null;
159200
}
160201

161202
private static CliCommand ConstructCommand()
162203
{
163-
bool isTestingPlatformEnabled = IsTestingPlatformEnabled();
164-
string testingSdkName = isTestingPlatformEnabled ? "testingplatform" : "vstest";
204+
string testRunnerName = GetTestRunnerName();
165205

166-
if (isTestingPlatformEnabled)
206+
if (testRunnerName.Equals(CliConstants.VSTest, StringComparison.OrdinalIgnoreCase))
167207
{
168-
return GetTestingPlatformCliCommand();
208+
return GetVSTestCliCommand();
169209
}
170-
else
210+
else if (testRunnerName.Equals(CliConstants.MicrosoftTestingPlatform, StringComparison.OrdinalIgnoreCase))
171211
{
172-
return GetVSTestCliCommand();
212+
return GetTestingPlatformCliCommand();
173213
}
174214

175-
throw new InvalidOperationException($"Testing sdk not supported: {testingSdkName}");
215+
throw new InvalidOperationException(string.Format(LocalizableStrings.CmdUnsupportedTestRunnerDescription, testRunnerName));
176216
}
177217

178218
private static CliCommand GetTestingPlatformCliCommand()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[dotnet.test:runner]
2+
name= "MicrosoftTestingPlatform"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[dotnet.test:runner]
2+
name= "MicrosoftTestingPlatform"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[dotnet.test:runner]
2+
name= "MicrosoftTestingPlatform"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[dotnet.test:runner]
2+
name= "MicrosoftTestingPlatform"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[dotnet.test:runner]
2+
name= "MicrosoftTestingPlatform"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[dotnet.test:runner]
2+
name= "MicrosoftTestingPlatform"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[dotnet.test:runner]
2+
name= "MicrosoftTestingPlatform"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[dotnet.test:runner]
2+
name= "MicrosoftTestingPlatform"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[dotnet.test:runner]
2+
name= "MicrosoftTestingPlatform"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[dotnet.test:runner]
2+
name= "MicrosoftTestingPlatform"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[dotnet.test:runner]
2+
name= "MicrosoftTestingPlatform"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[dotnet.test:runner]
2+
name= "MicrosoftTestingPlatform"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[dotnet.test:runner]
2+
name= "MicrosoftTestingPlatform"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[dotnet.test:runner]
2+
name= "MicrosoftTestingPlatform"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[dotnet.test:runner]
2+
name= "MicrosoftTestingPlatform"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[dotnet.test:runner]
2+
name= "MicrosoftTestingPlatform"

0 commit comments

Comments
 (0)