77using System . IO ;
88using System . Text ;
99using Microsoft . AspNetCore . InternalTesting ;
10+ using Microsoft . AspNetCore . Tools ;
1011using Microsoft . Extensions . Configuration . UserSecrets ;
1112using Microsoft . Extensions . Configuration . UserSecrets . Tests ;
1213using Microsoft . Extensions . Tools . Internal ;
@@ -64,7 +65,27 @@ public void Error_Project_DoesNotExist()
6465 var secretManager = CreateProgram ( ) ;
6566
6667 secretManager . RunInternal ( "list" , "--project" , projectPath ) ;
67- Assert . Contains ( Resources . FormatError_ProjectPath_NotFound ( projectPath ) , _console . GetOutput ( ) ) ;
68+ Assert . Contains ( SecretsHelpersResources . FormatError_File_NotFound ( projectPath ) , _console . GetOutput ( ) ) ;
69+ }
70+
71+ [ Fact ]
72+ public void Error_ProjectAndFileOptions ( )
73+ {
74+ var projectPath = Path . Combine ( _fixture . GetTempSecretProject ( ) , "does_not_exist" , "TestProject.csproj" ) ;
75+ var secretManager = CreateProgram ( ) ;
76+
77+ secretManager . RunInternal ( "list" , "--project" , projectPath , "--file" , projectPath ) ;
78+ Assert . Contains ( Resources . Error_ProjectAndFileOptions , _console . GetOutput ( ) ) ;
79+ }
80+
81+ [ Fact ]
82+ public void Error_InitFile ( )
83+ {
84+ var dir = _fixture . CreateFileBasedApp ( null ) ;
85+ var secretManager = CreateProgram ( ) ;
86+
87+ secretManager . RunInternal ( "init" , "--file" , Path . Combine ( dir , "app.cs" ) ) ;
88+ Assert . Contains ( Resources . Error_InitNotSupportedForFileBasedApps , _console . GetOutput ( ) ) ;
6889 }
6990
7091 [ Fact ]
@@ -81,9 +102,10 @@ public void SupportsRelativePaths()
81102 }
82103
83104 [ Theory ]
84- [ InlineData ( true ) ]
85- [ InlineData ( false ) ]
86- public void SetSecrets ( bool fromCurrentDirectory )
105+ [ InlineData ( false , true ) ]
106+ [ InlineData ( false , false ) ]
107+ [ InlineData ( true , false ) ]
108+ public void SetSecrets ( bool fromCurrentDirectory , bool fileBasedApp )
87109 {
88110 var secrets = new KeyValuePair < string , string > [ ]
89111 {
@@ -95,18 +117,22 @@ public void SetSecrets(bool fromCurrentDirectory)
95117 new KeyValuePair < string , string > ( "--twoDashedKey" , "--twoDashedValue" )
96118 } ;
97119
98- var projectPath = _fixture . GetTempSecretProject ( ) ;
120+ var projectPath = fileBasedApp
121+ ? _fixture . GetTempFileBasedApp ( out _ )
122+ : _fixture . GetTempSecretProject ( ) ;
99123 var dir = fromCurrentDirectory
100124 ? projectPath
101125 : Path . GetTempPath ( ) ;
126+ ReadOnlySpan < string > pathArgs = fromCurrentDirectory
127+ ? [ ]
128+ : ( fileBasedApp
129+ ? [ "-f" , Path . Join ( projectPath , "app.cs" ) ]
130+ : [ "- p ", projectPath] ) ;
102131 var secretManager = new Program ( _console , dir ) ;
103132
104133 foreach ( var secret in secrets)
105134 {
106- var parameters = fromCurrentDirectory ?
107- new string [ ] { "set" , secret . Key , secret . Value , "--verbose" } :
108- new string [ ] { "set" , secret . Key , secret . Value , "-p" , projectPath , "--verbose" } ;
109- secretManager . RunInternal ( parameters ) ;
135+ secretManager. RunInternal ( [ "set" , secret . Key , secret . Value , .. pathArgs , "--verbose" ] ) ;
110136 }
111137
112138 foreach ( var keyValue in secrets)
@@ -117,10 +143,7 @@ public void SetSecrets(bool fromCurrentDirectory)
117143 }
118144
119145 _console. ClearOutput ( ) ;
120- var args = fromCurrentDirectory
121- ? new string [ ] { "list" , "--verbose" }
122- : new string [ ] { "list" , "-p" , projectPath , "--verbose" } ;
123- secretManager . RunInternal ( args ) ;
146+ secretManager. RunInternal ( [ "list" , .. pathArgs , "--verbose" ] ) ;
124147 foreach ( var keyValue in secrets)
125148 {
126149 Assert. Contains (
@@ -132,21 +155,24 @@ public void SetSecrets(bool fromCurrentDirectory)
132155 _console. ClearOutput ( ) ;
133156 foreach ( var secret in secrets)
134157 {
135- var parameters = fromCurrentDirectory ?
136- new string [ ] { "remove" , secret . Key , "--verbose" } :
137- new string [ ] { "remove" , secret . Key , "-p" , projectPath , "--verbose" } ;
138- secretManager . RunInternal ( parameters ) ;
158+ secretManager. RunInternal ( [ "remove" , secret . Key , .. pathArgs , "--verbose" ] ) ;
139159 }
140160
141161 // Verify secrets are removed.
142162 _console. ClearOutput ( ) ;
143- args = fromCurrentDirectory
144- ? new string [ ] { "list" , "--verbose" }
145- : new string [ ] { "list" , "-p" , projectPath , "--verbose" } ;
146- secretManager . RunInternal ( args ) ;
163+ secretManager. RunInternal ( [ "list" , .. pathArgs , "--verbose" ] ) ;
147164 Assert. Contains ( Resources . Error_No_Secrets_Found , _console . GetOutput ( ) ) ;
148165 }
149166
167+ [ Fact ]
168+ public void SetSecrets_FileBasedAppInCurrentDirectory( )
169+ {
170+ var directoryPath = _fixture. GetTempFileBasedApp ( out _ ) ;
171+ var secretManager = new Program( _console , directoryPath ) ;
172+ secretManager. RunInternal ( "set" , "key1" , "value1" , "--verbose" ) ;
173+ Assert. Contains ( SecretsHelpersResources . FormatError_NoProjectsFound ( directoryPath ) , _console . GetOutput ( ) ) ;
174+ }
175+
150176 [ Fact ]
151177 public void SetSecret_Update_Existing_Secret( )
152178 {
@@ -268,16 +294,25 @@ public void List_Empty_Secrets_File()
268294 }
269295
270296 [ Theory ]
271- [ InlineData ( true ) ]
272- [ InlineData ( false ) ]
273- public void Clear_Secrets ( bool fromCurrentDirectory )
297+ [ InlineData ( false , true ) ]
298+ [ InlineData ( false , false ) ]
299+ [ InlineData ( true , false ) ]
300+ public void Clear_Secrets( bool fromCurrentDirectory , bool fileBasedApp )
274301 {
275- var projectPath = _fixture . GetTempSecretProject ( ) ;
302+ var projectPath = fileBasedApp
303+ ? _fixture . GetTempFileBasedApp ( out _ )
304+ : _fixture . GetTempSecretProject ( ) ;
276305
277306 var dir = fromCurrentDirectory
278307 ? projectPath
279308 : Path . GetTempPath ( ) ;
280309
310+ ReadOnlySpan < string > pathArgs = fromCurrentDirectory
311+ ? [ ]
312+ : ( fileBasedApp
313+ ? [ "-f" , Path . Join ( projectPath , "app.cs" ) ]
314+ : [ "-p" , projectPath ] ) ;
315+
281316 var secretManager = new Program( _console , dir ) ;
282317
283318 var secrets = new KeyValuePair < string , string > [ ]
@@ -290,10 +325,7 @@ public void Clear_Secrets(bool fromCurrentDirectory)
290325
291326 foreach ( var secret in secrets)
292327 {
293- var parameters = fromCurrentDirectory ?
294- new string [ ] { "set" , secret . Key , secret . Value , "--verbose" } :
295- new string [ ] { "set" , secret . Key , secret . Value , "-p" , projectPath , "--verbose" } ;
296- secretManager . RunInternal ( parameters ) ;
328+ secretManager. RunInternal ( [ "set" , secret . Key , secret . Value , .. pathArgs , "--verbose" ] ) ;
297329 }
298330
299331 foreach ( var keyValue in secrets)
@@ -305,10 +337,7 @@ public void Clear_Secrets(bool fromCurrentDirectory)
305337
306338 // Verify secrets are persisted.
307339 _console. ClearOutput ( ) ;
308- var args = fromCurrentDirectory ?
309- new string [ ] { "list" , "--verbose" } :
310- new string [ ] { "list" , "-p" , projectPath , "--verbose" } ;
311- secretManager . RunInternal ( args ) ;
340+ secretManager. RunInternal ( [ "list" , .. pathArgs , "--verbose" ] ) ;
312341 foreach ( var keyValue in secrets)
313342 {
314343 Assert. Contains (
@@ -318,11 +347,9 @@ public void Clear_Secrets(bool fromCurrentDirectory)
318347
319348 // Clear secrets.
320349 _console. ClearOutput ( ) ;
321- args = fromCurrentDirectory ? new string [ ] { "clear" , "--verbose" } : new string [ ] { "clear" , "-p" , projectPath , "--verbose" } ;
322- secretManager . RunInternal ( args ) ;
350+ secretManager. RunInternal ( [ "clear" , .. pathArgs , "--verbose" ] ) ;
323351
324- args = fromCurrentDirectory ? new string [ ] { "list" , "--verbose" } : new string [ ] { "list" , "-p" , projectPath , "--verbose" } ;
325- secretManager . RunInternal ( args ) ;
352+ secretManager. RunInternal ( [ "list" , .. pathArgs , "--verbose" ] ) ;
326353 Assert. Contains ( Resources . Error_No_Secrets_Found , _console . GetOutput ( ) ) ;
327354 }
328355
0 commit comments