22// The .NET Foundation licenses this file to you under the MIT license.
33
44using System . CommandLine ;
5+ using System . Diagnostics . CodeAnalysis ;
56using Microsoft . DotNet . Cli . Extensions ;
67using Microsoft . DotNet . Cli . Utils ;
78
@@ -23,7 +24,7 @@ static void ValidatePathOptions(ParseResult parseResult)
2324 if ( parseResult . HasOption ( MicrosoftTestingPlatformOptions . SolutionOption ) )
2425 count ++ ;
2526
26- if ( parseResult . HasOption ( MicrosoftTestingPlatformOptions . ProjectOption ) )
27+ if ( parseResult . HasOption ( MicrosoftTestingPlatformOptions . ProjectOrSolutionOption ) )
2728 count ++ ;
2829
2930 if ( count > 1 )
@@ -48,18 +49,46 @@ static void ValidateOptionsIrrelevantToModulesFilter(ParseResult parseResult)
4849 }
4950 }
5051
51- public static bool ValidateBuildPathOptions ( BuildOptions buildPathOptions )
52+ public static bool ValidateBuildPathOptions ( PathOptions pathOptions , [ NotNullWhen ( true ) ] out string ? projectOrSolutionFilePath , out bool isSolution )
5253 {
53- PathOptions pathOptions = buildPathOptions . PathOptions ;
54-
55- if ( ! string . IsNullOrEmpty ( pathOptions . ProjectPath ) )
54+ if ( ! string . IsNullOrEmpty ( pathOptions . ProjectOrSolutionPath ) )
5655 {
57- return ValidateProjectPath ( pathOptions . ProjectPath ) ;
56+ return ValidateProjectOrSolutionPath ( pathOptions . ProjectOrSolutionPath , out projectOrSolutionFilePath , out isSolution ) ;
5857 }
5958
6059 if ( ! string . IsNullOrEmpty ( pathOptions . SolutionPath ) )
6160 {
62- return ValidateSolutionPath ( pathOptions . SolutionPath ) ;
61+ isSolution = true ;
62+ return ValidateSolutionPath ( pathOptions . SolutionPath , out projectOrSolutionFilePath ) ;
63+ }
64+
65+ return TryGetProjectOrSolutionFromDirectory ( Directory . GetCurrentDirectory ( ) , onlyConsiderSolutions : false , out projectOrSolutionFilePath , out isSolution ) ;
66+ }
67+
68+ private static bool TryGetProjectOrSolutionFromDirectory (
69+ string directory ,
70+ bool onlyConsiderSolutions ,
71+ [ NotNullWhen ( true ) ] out string ? projectOrSolutionFilePath ,
72+ out bool isSolution )
73+ {
74+ bool foundSolutionOrProjectInDirectory ;
75+ string ? message ;
76+ if ( onlyConsiderSolutions )
77+ {
78+ isSolution = true ;
79+ ( foundSolutionOrProjectInDirectory , message ) = SolutionAndProjectUtility . TryGetSolutionFilePath ( directory , out projectOrSolutionFilePath ) ;
80+ }
81+ else
82+ {
83+ ( foundSolutionOrProjectInDirectory , message ) = SolutionAndProjectUtility . TryGetProjectOrSolutionFilePath ( directory , out projectOrSolutionFilePath , out isSolution ) ;
84+ }
85+
86+ if ( ! foundSolutionOrProjectInDirectory )
87+ {
88+ Reporter . Error . WriteLine ( message ) ;
89+ projectOrSolutionFilePath = null ;
90+ isSolution = false ;
91+ return false ;
6392 }
6493
6594 return true ;
@@ -106,40 +135,47 @@ public static void ValidateSolutionOrProjectOrDirectoryOrModulesArePassedCorrect
106135 }
107136 }
108137
109- private static bool ValidateSolutionPath ( string path )
138+ private static bool ValidateSolutionPath ( string solutionFileOrDirectory , [ NotNullWhen ( true ) ] out string ? solutionFile )
110139 {
111140 // If it's a directory, just check if it exists
112- if ( Directory . Exists ( path ) )
141+ if ( Directory . Exists ( solutionFileOrDirectory ) )
113142 {
114- return true ;
143+ return TryGetProjectOrSolutionFromDirectory ( solutionFileOrDirectory , onlyConsiderSolutions : true , out solutionFile , out _ ) ;
115144 }
116145
146+ solutionFile = solutionFileOrDirectory ;
147+
117148 // If it's not a directory, validate as a file path
118- if ( ! CliConstants . SolutionExtensions . Contains ( Path . GetExtension ( path ) ) )
149+ if ( ! CliConstants . SolutionExtensions . Contains ( Path . GetExtension ( solutionFileOrDirectory ) ) )
119150 {
120- Reporter . Error . WriteLine ( string . Format ( CliCommandStrings . CmdInvalidSolutionFileExtensionErrorDescription , path ) ) ;
151+ Reporter . Error . WriteLine ( string . Format ( CliCommandStrings . CmdInvalidSolutionFileExtensionErrorDescription , solutionFileOrDirectory ) ) ;
121152 return false ;
122153 }
123154
124- return ValidateFilePathExists ( path ) ;
155+ return ValidateFilePathExists ( solutionFile ) ;
125156 }
126157
127- private static bool ValidateProjectPath ( string path )
158+ private static bool ValidateProjectOrSolutionPath ( string projectOrSolutionFileOrDirectory , [ NotNullWhen ( true ) ] out string ? projectOrSolutionFile , out bool isSolution )
128159 {
129160 // If it's a directory, just check if it exists
130- if ( Directory . Exists ( path ) )
161+ if ( Directory . Exists ( projectOrSolutionFileOrDirectory ) )
131162 {
132- return true ;
163+ return TryGetProjectOrSolutionFromDirectory ( projectOrSolutionFileOrDirectory , onlyConsiderSolutions : false , out projectOrSolutionFile , out isSolution ) ;
133164 }
134165
166+ var extension = Path . GetExtension ( projectOrSolutionFileOrDirectory ) ;
167+ isSolution = CliConstants . SolutionExtensions . Contains ( extension ) ;
168+ projectOrSolutionFile = projectOrSolutionFileOrDirectory ;
135169 // If it's not a directory, validate as a file path
136- if ( ! Path . GetExtension ( path ) . EndsWith ( "proj" , StringComparison . OrdinalIgnoreCase ) )
170+ if ( ! isSolution && ! extension . EndsWith ( "proj" , StringComparison . OrdinalIgnoreCase ) )
137171 {
138- Reporter . Error . WriteLine ( string . Format ( CliCommandStrings . CmdInvalidProjectFileExtensionErrorDescription , path ) ) ;
172+ projectOrSolutionFile = null ;
173+ isSolution = false ;
174+ Reporter . Error . WriteLine ( string . Format ( CliCommandStrings . CmdInvalidProjectFileExtensionErrorDescription , projectOrSolutionFileOrDirectory ) ) ;
139175 return false ;
140176 }
141177
142- return ValidateFilePathExists ( path ) ;
178+ return ValidateFilePathExists ( projectOrSolutionFileOrDirectory ) ;
143179 }
144180
145181 private static bool ValidateFilePathExists ( string filePath )
0 commit comments