7
7
using System . IO ;
8
8
using System . Text ;
9
9
using Microsoft . AspNetCore . InternalTesting ;
10
+ using Microsoft . AspNetCore . Tools ;
10
11
using Microsoft . Extensions . Configuration . UserSecrets ;
11
12
using Microsoft . Extensions . Configuration . UserSecrets . Tests ;
12
13
using Microsoft . Extensions . Tools . Internal ;
@@ -64,7 +65,27 @@ public void Error_Project_DoesNotExist()
64
65
var secretManager = CreateProgram ( ) ;
65
66
66
67
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 ( ) ) ;
68
89
}
69
90
70
91
[ Fact ]
@@ -81,9 +102,10 @@ public void SupportsRelativePaths()
81
102
}
82
103
83
104
[ 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 )
87
109
{
88
110
var secrets = new KeyValuePair < string , string > [ ]
89
111
{
@@ -95,18 +117,22 @@ public void SetSecrets(bool fromCurrentDirectory)
95
117
new KeyValuePair < string , string > ( "--twoDashedKey" , "--twoDashedValue" )
96
118
} ;
97
119
98
- var projectPath = _fixture . GetTempSecretProject ( ) ;
120
+ var projectPath = fileBasedApp
121
+ ? _fixture . GetTempFileBasedApp ( out _ )
122
+ : _fixture . GetTempSecretProject ( ) ;
99
123
var dir = fromCurrentDirectory
100
124
? projectPath
101
125
: Path . GetTempPath ( ) ;
126
+ ReadOnlySpan < string > pathArgs = fromCurrentDirectory
127
+ ? [ ]
128
+ : ( fileBasedApp
129
+ ? [ "-f" , Path . Join ( projectPath , "app.cs" ) ]
130
+ : [ "- p ", projectPath] ) ;
102
131
var secretManager = new Program ( _console , dir ) ;
103
132
104
133
foreach ( var secret in secrets)
105
134
{
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" ] ) ;
110
136
}
111
137
112
138
foreach ( var keyValue in secrets)
@@ -117,10 +143,7 @@ public void SetSecrets(bool fromCurrentDirectory)
117
143
}
118
144
119
145
_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" ] ) ;
124
147
foreach ( var keyValue in secrets)
125
148
{
126
149
Assert. Contains (
@@ -132,21 +155,24 @@ public void SetSecrets(bool fromCurrentDirectory)
132
155
_console. ClearOutput ( ) ;
133
156
foreach ( var secret in secrets)
134
157
{
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" ] ) ;
139
159
}
140
160
141
161
// Verify secrets are removed.
142
162
_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" ] ) ;
147
164
Assert. Contains ( Resources . Error_No_Secrets_Found , _console . GetOutput ( ) ) ;
148
165
}
149
166
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
+
150
176
[ Fact ]
151
177
public void SetSecret_Update_Existing_Secret( )
152
178
{
@@ -268,16 +294,25 @@ public void List_Empty_Secrets_File()
268
294
}
269
295
270
296
[ 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 )
274
301
{
275
- var projectPath = _fixture . GetTempSecretProject ( ) ;
302
+ var projectPath = fileBasedApp
303
+ ? _fixture . GetTempFileBasedApp ( out _ )
304
+ : _fixture . GetTempSecretProject ( ) ;
276
305
277
306
var dir = fromCurrentDirectory
278
307
? projectPath
279
308
: Path . GetTempPath ( ) ;
280
309
310
+ ReadOnlySpan < string > pathArgs = fromCurrentDirectory
311
+ ? [ ]
312
+ : ( fileBasedApp
313
+ ? [ "-f" , Path . Join ( projectPath , "app.cs" ) ]
314
+ : [ "-p" , projectPath ] ) ;
315
+
281
316
var secretManager = new Program( _console , dir ) ;
282
317
283
318
var secrets = new KeyValuePair < string , string > [ ]
@@ -290,10 +325,7 @@ public void Clear_Secrets(bool fromCurrentDirectory)
290
325
291
326
foreach ( var secret in secrets)
292
327
{
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" ] ) ;
297
329
}
298
330
299
331
foreach ( var keyValue in secrets)
@@ -305,10 +337,7 @@ public void Clear_Secrets(bool fromCurrentDirectory)
305
337
306
338
// Verify secrets are persisted.
307
339
_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" ] ) ;
312
341
foreach ( var keyValue in secrets)
313
342
{
314
343
Assert. Contains (
@@ -318,11 +347,9 @@ public void Clear_Secrets(bool fromCurrentDirectory)
318
347
319
348
// Clear secrets.
320
349
_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" ] ) ;
323
351
324
- args = fromCurrentDirectory ? new string [ ] { "list" , "--verbose" } : new string [ ] { "list" , "-p" , projectPath , "--verbose" } ;
325
- secretManager . RunInternal ( args ) ;
352
+ secretManager. RunInternal ( [ "list" , .. pathArgs , "--verbose" ] ) ;
326
353
Assert. Contains ( Resources . Error_No_Secrets_Found , _console . GetOutput ( ) ) ;
327
354
}
328
355
0 commit comments