Skip to content

Commit a23ef31

Browse files
authored
[automated] Merge branch 'release/10.0.1xx' => 'main' (#50470)
2 parents 5f91c54 + 85d7d5f commit a23ef31

26 files changed

+170
-126
lines changed

.vsts-ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ trigger:
55
branches:
66
include:
77
- main
8-
- release/10.0.1xx-*
8+
- release/10.0.1*
99
- internal/release/*
1010
- exp/*
1111

src/Cli/dotnet/Commands/CliCommandStrings.resx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2634,12 +2634,12 @@ Proceed?</value>
26342634
<data name="TestCommandUseProject" xml:space="preserve">
26352635
<value>Specifying a project for 'dotnet test' should be via '--project'.</value>
26362636
</data>
2637-
<data name="TestCommandUseDirectory" xml:space="preserve">
2638-
<value>Specifying a directory for 'dotnet test' should be via '--directory'.</value>
2639-
</data>
26402637
<data name="TestCommandUseTestModules" xml:space="preserve">
26412638
<value>Specifying dlls or executables for 'dotnet test' should be via '--test-modules'.</value>
26422639
</data>
2640+
<data name="TestCommandUseDirectoryWithSwitch" xml:space="preserve">
2641+
<value>Specifying a directory for 'dotnet test' should be via '--project' or '--solution'.</value>
2642+
</data>
26432643
<data name="PackCmdVersionDescription" xml:space="preserve">
26442644
<value>The version of the package to create</value>
26452645
</data>

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

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,22 @@ public bool RunMSBuild()
3232
if (!string.IsNullOrEmpty(pathOptions.ProjectPath))
3333
{
3434
path = PathUtility.GetFullPath(pathOptions.ProjectPath);
35-
msBuildExitCode = RunBuild(path, isSolution: false);
35+
36+
msBuildExitCode = Directory.Exists(path)
37+
? RunBuild(path, expectProject: true)
38+
: RunBuild(path, isSolution: false);
3639
}
3740
else if (!string.IsNullOrEmpty(pathOptions.SolutionPath))
3841
{
3942
path = PathUtility.GetFullPath(pathOptions.SolutionPath);
40-
msBuildExitCode = RunBuild(path, isSolution: true);
43+
44+
msBuildExitCode = Directory.Exists(path)
45+
? RunBuild(path, expectSolution: true)
46+
: RunBuild(path, isSolution: true);
4147
}
4248
else
4349
{
44-
path = PathUtility.GetFullPath(pathOptions.DirectoryPath ?? Directory.GetCurrentDirectory());
50+
path = PathUtility.GetFullPath(Directory.GetCurrentDirectory());
4551
msBuildExitCode = RunBuild(path);
4652
}
4753

@@ -54,9 +60,27 @@ public bool RunMSBuild()
5460
return true;
5561
}
5662

57-
private int RunBuild(string directoryPath)
63+
private int RunBuild(string directoryPath, bool expectProject = false, bool expectSolution = false)
5864
{
59-
(bool solutionOrProjectFileFound, string message) = SolutionAndProjectUtility.TryGetProjectOrSolutionFilePath(directoryPath, out string projectOrSolutionFilePath, out bool isSolution);
65+
bool solutionOrProjectFileFound;
66+
string message;
67+
string projectOrSolutionFilePath;
68+
bool isSolution;
69+
70+
if (expectProject)
71+
{
72+
(solutionOrProjectFileFound, message) = SolutionAndProjectUtility.TryGetProjectFilePath(directoryPath, out projectOrSolutionFilePath);
73+
isSolution = false;
74+
}
75+
else if (expectSolution)
76+
{
77+
(solutionOrProjectFileFound, message) = SolutionAndProjectUtility.TryGetSolutionFilePath(directoryPath, out projectOrSolutionFilePath);
78+
isSolution = true;
79+
}
80+
else
81+
{
82+
(solutionOrProjectFileFound, message) = SolutionAndProjectUtility.TryGetProjectOrSolutionFilePath(directoryPath, out projectOrSolutionFilePath, out isSolution);
83+
}
6084

6185
if (!solutionOrProjectFileFound)
6286
{

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ public static BuildOptions GetBuildOptions(ParseResult parseResult, int degreeOf
8787
PathOptions pathOptions = new(
8888
parseResult.GetValue(TestingPlatformOptions.ProjectOption),
8989
parseResult.GetValue(TestingPlatformOptions.SolutionOption),
90-
parseResult.GetValue(TestingPlatformOptions.DirectoryOption),
9190
resultsDirectory,
9291
configFile,
9392
diagnosticOutputDirectory);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Microsoft.DotNet.Cli.Commands.Test;
55

66
internal record TestOptions(bool HasFilterMode, bool IsHelp);
77

8-
internal record PathOptions(string? ProjectPath, string? SolutionPath, string? DirectoryPath, string? ResultsDirectoryPath, string? ConfigFilePath, string? DiagnosticOutputDirectoryPath);
8+
internal record PathOptions(string? ProjectPath, string? SolutionPath, string? ResultsDirectoryPath, string? ConfigFilePath, string? DiagnosticOutputDirectoryPath);
99

1010
internal record BuildOptions(
1111
PathOptions PathOptions,
@@ -14,6 +14,6 @@ internal record BuildOptions(
1414
Utils.VerbosityOptions? Verbosity,
1515
bool NoLaunchProfile,
1616
bool NoLaunchProfileArguments,
17-
int DegreeOfParallelism,
17+
int DegreeOfParallelism,
1818
List<string> UnmatchedTokens,
1919
IEnumerable<string> MSBuildArgs);

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,56 @@ public static (bool SolutionOrProjectFileFound, string Message) TryGetProjectOrS
114114
return (true, string.Empty);
115115
}
116116

117+
public static (bool ProjectFileFound, string Message) TryGetProjectFilePath(string directory, out string projectFilePath)
118+
{
119+
projectFilePath = string.Empty;
120+
121+
if (!Directory.Exists(directory))
122+
{
123+
return (false, string.Format(CliCommandStrings.CmdNonExistentDirectoryErrorDescription, directory));
124+
}
125+
126+
var actualProjectFiles = GetProjectFilePaths(directory);
127+
128+
if (actualProjectFiles.Length == 0)
129+
{
130+
return (false, string.Format(CliStrings.CouldNotFindAnyProjectInDirectory, directory));
131+
}
132+
133+
if (actualProjectFiles.Length == 1)
134+
{
135+
projectFilePath = actualProjectFiles[0];
136+
return (true, string.Empty);
137+
}
138+
139+
return (false, string.Format(CliStrings.MoreThanOneProjectInDirectory, directory));
140+
}
141+
142+
public static (bool SolutionFileFound, string Message) TryGetSolutionFilePath(string directory, out string solutionFilePath)
143+
{
144+
solutionFilePath = string.Empty;
145+
146+
if (!Directory.Exists(directory))
147+
{
148+
return (false, string.Format(CliCommandStrings.CmdNonExistentDirectoryErrorDescription, directory));
149+
}
150+
151+
var actualSolutionFiles = GetSolutionFilePaths(directory);
152+
153+
if (actualSolutionFiles.Length == 0)
154+
{
155+
return (false, string.Format(CliStrings.SolutionDoesNotExist, directory + Path.DirectorySeparatorChar));
156+
}
157+
158+
if (actualSolutionFiles.Length > 1)
159+
{
160+
return (false, string.Format(CliStrings.MoreThanOneSolutionInDirectory, directory + Path.DirectorySeparatorChar));
161+
}
162+
163+
solutionFilePath = actualSolutionFiles[0];
164+
return (true, string.Empty);
165+
}
166+
117167
private static string[] GetSolutionFilePaths(string directory) => [
118168
.. Directory.GetFiles(directory, CliConstants.SolutionExtensionPattern, SearchOption.TopDirectoryOnly),
119169
.. Directory.GetFiles(directory, CliConstants.SolutionXExtensionPattern, SearchOption.TopDirectoryOnly)

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.CommandLine;
5-
using System.Diagnostics;
65
using Microsoft.DotNet.Cli.Extensions;
76
using Microsoft.Extensions.Configuration;
87

@@ -232,7 +231,6 @@ private static Command GetTestingPlatformCliCommand()
232231
command.SetAction(parseResult => command.Run(parseResult));
233232
command.Options.Add(TestingPlatformOptions.ProjectOption);
234233
command.Options.Add(TestingPlatformOptions.SolutionOption);
235-
command.Options.Add(TestingPlatformOptions.DirectoryOption);
236234
command.Options.Add(TestingPlatformOptions.TestModulesFilterOption);
237235
command.Options.Add(TestingPlatformOptions.TestModulesRootDirectoryOption);
238236
command.Options.Add(TestingPlatformOptions.ResultsDirectoryOption);

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@
66
using System.CommandLine;
77
using Microsoft.DotNet.Cli.Commands.Test.Terminal;
88
using Microsoft.DotNet.Cli.Extensions;
9-
using Microsoft.DotNet.Cli.Utils;
109
using Microsoft.TemplateEngine.Cli.Commands;
11-
using Microsoft.TemplateEngine.Cli.Help;
1210

1311
namespace Microsoft.DotNet.Cli.Commands.Test;
1412

15-
internal partial class TestingPlatformCommand : System.CommandLine.Command, ICustomHelp
13+
internal partial class TestingPlatformCommand : Command, ICustomHelp
1614
{
1715
private MSBuildHandler _msBuildHandler;
1816
private TerminalTestReporter _output;
@@ -47,7 +45,7 @@ private int RunInternal(ParseResult parseResult)
4745
{
4846
ValidationUtility.ValidateMutuallyExclusiveOptions(parseResult);
4947
ValidationUtility.ValidateSolutionOrProjectOrDirectoryOrModulesArePassedCorrectly(parseResult);
50-
48+
5149
PrepareEnvironment(parseResult, out TestOptions testOptions, out int degreeOfParallelism);
5250

5351
InitializeOutput(degreeOfParallelism, parseResult, testOptions.IsHelp);

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#nullable disable
55

66
using System.CommandLine;
7-
using Microsoft.DotNet.Cli.Extensions;
87

98
namespace Microsoft.DotNet.Cli.Commands.Test;
109

@@ -24,13 +23,6 @@ internal static class TestingPlatformOptions
2423
Arity = ArgumentArity.ExactlyOne
2524
};
2625

27-
public static readonly Option<string> DirectoryOption = new("--directory")
28-
{
29-
Description = CliCommandStrings.CmdDirectoryDescription,
30-
HelpName = CliCommandStrings.CmdDirectoryPathName,
31-
Arity = ArgumentArity.ExactlyOne
32-
};
33-
3426
public static readonly Option<string> TestModulesFilterOption = new("--test-modules")
3527
{
3628
Description = CliCommandStrings.CmdTestModulesDescription,

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

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ static void ValidatePathOptions(ParseResult parseResult)
2323
if (parseResult.HasOption(TestingPlatformOptions.TestModulesFilterOption))
2424
count++;
2525

26-
if (parseResult.HasOption(TestingPlatformOptions.DirectoryOption))
27-
count++;
28-
2926
if (parseResult.HasOption(TestingPlatformOptions.SolutionOption))
3027
count++;
3128

@@ -60,18 +57,12 @@ public static bool ValidateBuildPathOptions(BuildOptions buildPathOptions, Termi
6057

6158
if (!string.IsNullOrEmpty(pathOptions.ProjectPath))
6259
{
63-
return ValidateProjectFilePath(pathOptions.ProjectPath, output);
60+
return ValidateProjectPath(pathOptions.ProjectPath, output);
6461
}
6562

6663
if (!string.IsNullOrEmpty(pathOptions.SolutionPath))
6764
{
68-
return ValidateSolutionFilePath(pathOptions.SolutionPath, output);
69-
}
70-
71-
if (!string.IsNullOrEmpty(pathOptions.DirectoryPath) && !Directory.Exists(pathOptions.DirectoryPath))
72-
{
73-
output.WriteMessage(string.Format(CliCommandStrings.CmdNonExistentDirectoryErrorDescription, pathOptions.DirectoryPath));
74-
return false;
65+
return ValidateSolutionPath(pathOptions.SolutionPath, output);
7566
}
7667

7768
return true;
@@ -105,39 +96,53 @@ public static void ValidateSolutionOrProjectOrDirectoryOrModulesArePassedCorrect
10596
{
10697
throw new GracefulException(CliCommandStrings.TestCommandUseProject);
10798
}
108-
else if (Directory.Exists(token))
109-
{
110-
throw new GracefulException(CliCommandStrings.TestCommandUseDirectory);
111-
}
11299
else if ((token.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) ||
113100
token.EndsWith(".exe", StringComparison.OrdinalIgnoreCase)) &&
114101
File.Exists(token))
115102
{
116103
throw new GracefulException(CliCommandStrings.TestCommandUseTestModules);
117104
}
105+
else if (Directory.Exists(token))
106+
{
107+
throw new GracefulException(CliCommandStrings.TestCommandUseDirectoryWithSwitch);
108+
}
118109
}
119110
}
120111

121-
private static bool ValidateSolutionFilePath(string filePath, TerminalTestReporter output)
112+
private static bool ValidateSolutionPath(string path, TerminalTestReporter output)
122113
{
123-
if (!CliConstants.SolutionExtensions.Contains(Path.GetExtension(filePath)))
114+
// If it's a directory, just check if it exists
115+
if (Directory.Exists(path))
124116
{
125-
output.WriteMessage(string.Format(CliCommandStrings.CmdInvalidSolutionFileExtensionErrorDescription, filePath));
117+
return true;
118+
}
119+
120+
// If it's not a directory, validate as a file path
121+
if (!CliConstants.SolutionExtensions.Contains(Path.GetExtension(path)))
122+
{
123+
output.WriteMessage(string.Format(CliCommandStrings.CmdInvalidSolutionFileExtensionErrorDescription, path));
126124
return false;
127125
}
128126

129-
return ValidateFilePathExists(filePath, output);
127+
return ValidateFilePathExists(path, output);
130128
}
131129

132-
private static bool ValidateProjectFilePath(string filePath, TerminalTestReporter output)
130+
private static bool ValidateProjectPath(string path, TerminalTestReporter output)
133131
{
134-
if (!Path.GetExtension(filePath).EndsWith("proj", StringComparison.OrdinalIgnoreCase))
132+
// If it's a directory, just check if it exists
133+
if (Directory.Exists(path))
134+
{
135+
return true;
136+
}
137+
138+
// If it's not a directory, validate as a file path
139+
if (!Path.GetExtension(path).EndsWith("proj", StringComparison.OrdinalIgnoreCase))
135140
{
136-
output.WriteMessage(string.Format(CliCommandStrings.CmdInvalidProjectFileExtensionErrorDescription, filePath));
141+
output.WriteMessage(string.Format(CliCommandStrings.CmdInvalidProjectFileExtensionErrorDescription, path));
137142
return false;
138143
}
139144

140-
return ValidateFilePathExists(filePath, output);
145+
return ValidateFilePathExists(path, output);
141146
}
142147

143148
private static bool ValidateFilePathExists(string filePath, TerminalTestReporter output)

0 commit comments

Comments
 (0)