Skip to content

Commit 5a89594

Browse files
committed
Cleanup dotnet test
1 parent 53b18f9 commit 5a89594

File tree

5 files changed

+38
-59
lines changed

5 files changed

+38
-59
lines changed

src/Cli/dotnet/Commands/Run/RunCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ static void ValidatePreconditions(ProjectInstance project)
355355

356356
static RunProperties ReadRunPropertiesFromProject(ProjectInstance project, string[] applicationArgs)
357357
{
358-
var runProperties = RunProperties.FromProjectAndApplicationArguments(project, applicationArgs, fallbackToTargetPath: false);
358+
var runProperties = RunProperties.FromProjectAndApplicationArguments(project, applicationArgs);
359359
if (string.IsNullOrEmpty(runProperties.RunCommand))
360360
{
361361
ThrowUnableToRunError(project);

src/Cli/dotnet/Commands/Run/RunProperties.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,9 @@ namespace Microsoft.DotNet.Cli.Commands.Run;
88

99
internal record RunProperties(string RunCommand, string? RunArguments, string? RunWorkingDirectory)
1010
{
11-
internal static RunProperties FromProjectAndApplicationArguments(ProjectInstance project, string[] applicationArgs, bool fallbackToTargetPath)
11+
internal static RunProperties FromProjectAndApplicationArguments(ProjectInstance project, string[] applicationArgs)
1212
{
1313
string runProgram = project.GetPropertyValue("RunCommand");
14-
if (fallbackToTargetPath &&
15-
(string.IsNullOrEmpty(runProgram) || !File.Exists(runProgram)))
16-
{
17-
// If we can't find the executable that runCommand is pointing to, we simply use TargetPath instead.
18-
// In this case, we discard everything related to "Run" (i.e, RunWorkingDirectory and RunArguments) and use only TargetPath
19-
runProgram = project.GetPropertyValue("TargetPath");
20-
return new(runProgram, null, null);
21-
}
22-
2314
string runArguments = project.GetPropertyValue("RunArguments");
2415
string runWorkingDirectory = project.GetPropertyValue("RunWorkingDirectory");
2516

src/Cli/dotnet/Commands/Test/SolutionAndProjectUtility.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,17 @@ public static IEnumerable<ParallelizableTestModuleGroupWithSequentialInnerModule
230230

231231
string targetFramework = project.GetPropertyValue(ProjectProperties.TargetFramework);
232232
RunProperties runProperties = GetRunProperties(project, loggers);
233+
if (string.IsNullOrEmpty(runProperties.RunCommand) || runProperties.RunCommand.HasExtension(CliConstants.DLLExtension))
234+
{
235+
throw new GracefulException(
236+
string.Format(
237+
CliCommandStrings.RunCommandExceptionUnableToRun,
238+
"dotnet test",
239+
"OutputType",
240+
project.GetPropertyValue("OutputType")));
241+
242+
}
243+
233244
string projectFullPath = project.GetPropertyValue(ProjectProperties.ProjectFullPath);
234245

235246
// TODO: Support --launch-profile and pass it here.
@@ -252,7 +263,7 @@ static RunProperties GetRunProperties(ProjectInstance project, ICollection<ILogg
252263
}
253264
}
254265

255-
return RunProperties.FromProjectAndApplicationArguments(project, [], fallbackToTargetPath: true);
266+
return RunProperties.FromProjectAndApplicationArguments(project, []);
256267
}
257268
}
258269

src/Cli/dotnet/Commands/Test/TestApplication.cs

Lines changed: 18 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using Microsoft.DotNet.Cli.Commands.Test.IPC;
99
using Microsoft.DotNet.Cli.Commands.Test.IPC.Models;
1010
using Microsoft.DotNet.Cli.Commands.Test.IPC.Serializers;
11-
using Microsoft.DotNet.Cli.Utils;
1211

1312
namespace Microsoft.DotNet.Cli.Commands.Test;
1413

@@ -54,12 +53,10 @@ public async Task<int> RunAsync(TestOptions testOptions)
5453

5554
private ProcessStartInfo CreateProcessStartInfo(TestOptions testOptions)
5655
{
57-
bool isDll = Module.RunProperties.RunCommand.HasExtension(CliConstants.DLLExtension);
58-
5956
var processStartInfo = new ProcessStartInfo
6057
{
61-
FileName = GetFileName(testOptions, isDll),
62-
Arguments = GetArguments(testOptions, isDll),
58+
FileName = Module.RunProperties.RunCommand,
59+
Arguments = GetArguments(testOptions),
6360
RedirectStandardOutput = true,
6461
RedirectStandardError = true
6562
};
@@ -87,23 +84,27 @@ private ProcessStartInfo CreateProcessStartInfo(TestOptions testOptions)
8784
return processStartInfo;
8885
}
8986

90-
private string GetFileName(TestOptions testOptions, bool isDll)
91-
=> isDll ? Environment.ProcessPath : Module.RunProperties.RunCommand;
92-
93-
private string GetArguments(TestOptions testOptions, bool isDll)
87+
private string GetArguments(TestOptions testOptions)
9488
{
95-
if (testOptions.HasFilterMode || !isDll || !IsArchitectureSpecified(testOptions))
89+
// Keep RunArguments first.
90+
// In the case of UseAppHost=false, RunArguments is set to `exec $(TargetPath)`:
91+
// https://github.com/dotnet/sdk/blob/333388c31d811701e3b6be74b5434359151424dc/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.targets#L1411
92+
// So, we keep that first always.
93+
StringBuilder builder = new(Module.RunProperties.RunArguments);
94+
95+
if (testOptions.IsHelp)
9696
{
97-
return BuildArgs(testOptions, isDll);
97+
builder.Append($" {TestingPlatformOptions.HelpOption.Name} ");
9898
}
9999

100-
// If we reach here, that means we have a test project that doesn't produce an executable.
101-
throw new InvalidOperationException($"A Microsoft.Testing.Platform test project should produce an executable. The file '{Module.RunProperties.RunCommand}' is dll.");
102-
}
100+
var args = _buildOptions.UnmatchedTokens;
101+
builder.Append(args.Count != 0
102+
? args.Aggregate((a, b) => $"{a} {b}")
103+
: string.Empty);
103104

104-
private static bool IsArchitectureSpecified(TestOptions testOptions)
105-
{
106-
return !string.IsNullOrEmpty(testOptions.Architecture);
105+
builder.Append($" {CliConstants.ServerOptionKey} {CliConstants.ServerOptionValue} {CliConstants.DotNetTestPipeOptionKey} {_pipeNameDescription.Name}");
106+
107+
return builder.ToString();
107108
}
108109

109110
private void WaitOnTestApplicationPipeConnectionLoop()
@@ -277,35 +278,6 @@ private bool ModulePathExists()
277278
return true;
278279
}
279280

280-
private string BuildArgs(TestOptions testOptions, bool isDll)
281-
{
282-
StringBuilder builder = new();
283-
284-
if (isDll)
285-
{
286-
builder.Append($"exec {Module.RunProperties.RunCommand} ");
287-
}
288-
289-
AppendCommonArgs(builder, testOptions);
290-
291-
return builder.ToString();
292-
}
293-
294-
private void AppendCommonArgs(StringBuilder builder, TestOptions testOptions)
295-
{
296-
if (testOptions.IsHelp)
297-
{
298-
builder.Append($" {TestingPlatformOptions.HelpOption.Name} ");
299-
}
300-
301-
var args = _buildOptions.UnmatchedTokens;
302-
builder.Append(args.Count != 0
303-
? args.Aggregate((a, b) => $"{a} {b}")
304-
: string.Empty);
305-
306-
builder.Append($" {CliConstants.ServerOptionKey} {CliConstants.ServerOptionValue} {CliConstants.DotNetTestPipeOptionKey} {_pipeNameDescription.Name} {Module.RunProperties.RunArguments}");
307-
}
308-
309281
public void OnHandshakeMessage(HandshakeMessage handshakeMessage)
310282
{
311283
HandshakeReceived?.Invoke(this, new HandshakeArgs { Handshake = new Handshake(handshakeMessage.Properties) });

src/Cli/dotnet/Commands/Test/TestModulesFilterHandler.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.DotNet.Cli.Commands.Run;
88
using Microsoft.DotNet.Cli.Commands.Test.Terminal;
99
using Microsoft.DotNet.Cli.Extensions;
10+
using Microsoft.DotNet.Cli.Utils;
1011
using Microsoft.Extensions.FileSystemGlobbing;
1112

1213
namespace Microsoft.DotNet.Cli.Commands.Test;
@@ -49,7 +50,11 @@ public bool RunWithTestModulesFilter(ParseResult parseResult)
4950

5051
foreach (string testModule in testModulePaths)
5152
{
52-
var testApp = new ParallelizableTestModuleGroupWithSequentialInnerModules(new TestModule(new RunProperties(testModule, null, null), null, null, true, true, null));
53+
RunProperties runProperties = testModule.HasExtension(CliConstants.DLLExtension)
54+
? new RunProperties(new Muxer().MuxerPath, $@"exec ""{testModule}""", null)
55+
: new RunProperties(testModule, null, null);
56+
57+
var testApp = new ParallelizableTestModuleGroupWithSequentialInnerModules(new TestModule(runProperties, null, null, true, true, null));
5358
// Write the test application to the channel
5459
_actionQueue.Enqueue(testApp);
5560
}

0 commit comments

Comments
 (0)