2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
4
using System . Collections . Immutable ;
5
+ using System . Text . Json ;
6
+ using System . Text . RegularExpressions ;
5
7
6
8
namespace Microsoft . DotNet . Cli . New . IntegrationTests
7
9
{
@@ -27,6 +29,8 @@ private static readonly (string ProjectTemplateName, string[] Languages, bool Ru
27
29
( "nunit-playwright" , new [ ] { Languages . CSharp } , false , false ) ,
28
30
] ;
29
31
32
+ private static readonly string PackagesJsonPath = Path . Combine ( CodeBaseRoot , "test" , "component-governance" , "packages.json" ) ;
33
+
30
34
public DotnetNewTestTemplatesTests ( ITestOutputHelper log ) : base ( log )
31
35
{
32
36
_log = log ;
@@ -119,8 +123,12 @@ public void ItemTemplate_CanBeInstalledAndTestArePassing(string targetFramework,
119
123
// Therefore, in total we would have 2.
120
124
result . StdOut . Should ( ) . MatchRegex ( @"Passed:\s*2" ) ;
121
125
126
+ // After executing dotnet new and before cleaning up
127
+ RecordPackages ( outputDirectory ) ;
128
+
122
129
Directory . Delete ( outputDirectory , true ) ;
123
130
Directory . Delete ( workingDirectory , true ) ;
131
+
124
132
}
125
133
126
134
[ Theory ]
@@ -152,6 +160,9 @@ public void ProjectTemplate_CanBeInstalledAndTestsArePassing(string targetFramew
152
160
result . StdOut . Should ( ) . MatchRegex ( @"Passed:\s*1" ) ;
153
161
}
154
162
163
+ // After executing dotnet new and before cleaning up
164
+ RecordPackages ( outputDirectory ) ;
165
+
155
166
Directory . Delete ( outputDirectory , true ) ;
156
167
Directory . Delete ( workingDirectory , true ) ;
157
168
}
@@ -191,6 +202,9 @@ public void MSTestAndPlaywrightProjectTemplate_WithCoverageToolAndTestRunner_Can
191
202
result . StdOut . Should ( ) . MatchRegex ( @"Passed:\s*1" ) ;
192
203
}
193
204
205
+ // After executing dotnet new and before cleaning up
206
+ RecordPackages ( outputDirectory ) ;
207
+
194
208
Directory . Delete ( outputDirectory , true ) ;
195
209
Directory . Delete ( workingDirectory , true ) ;
196
210
}
@@ -204,6 +218,79 @@ private void AddItemToFsproj(string itemName, string outputDirectory, string pro
204
218
File . WriteAllLines ( fsproj , lines ) ;
205
219
}
206
220
221
+ private void RecordPackages ( string projectDirectory )
222
+ {
223
+ // Get all project files with a single directory search, then filter to specific types
224
+ var projectFiles = Directory . GetFiles ( projectDirectory , "*.*proj" )
225
+ . Where ( file => file . EndsWith ( ".csproj" , StringComparison . OrdinalIgnoreCase ) ||
226
+ file . EndsWith ( ".fsproj" , StringComparison . OrdinalIgnoreCase ) ||
227
+ file . EndsWith ( ".vbproj" , StringComparison . OrdinalIgnoreCase ) ) ;
228
+
229
+ Dictionary < string , string > packageVersions =
230
+ [ ] ;
231
+
232
+ // Load existing package versions if file exists
233
+ if ( File . Exists ( PackagesJsonPath ) )
234
+ {
235
+ try
236
+ {
237
+ packageVersions = JsonSerializer . Deserialize < Dictionary < string , string > > (
238
+ File . ReadAllText ( PackagesJsonPath ) ) ??
239
+ [ ] ;
240
+ }
241
+ catch ( Exception ex )
242
+ {
243
+ _log . WriteLine ( $ "Warning: Could not parse existing packages.json: { ex . Message } ") ;
244
+ }
245
+ }
246
+
247
+ // Extract package references from project files
248
+ foreach ( var projectFile in projectFiles )
249
+ {
250
+ string content = File . ReadAllText ( projectFile ) ;
251
+ var packageRefMatches = Regex . Matches (
252
+ content ,
253
+ @"<PackageReference\s+(?:Include=""([^""]+)""\s+Version=""([^""]+)""|Version=""([^""]+)""\s+Include=""([^""]+)"")" ,
254
+ RegexOptions . IgnoreCase ) ;
255
+
256
+ foreach ( Match match in packageRefMatches )
257
+ {
258
+ string packageId ;
259
+ string version ;
260
+
261
+ if ( ! string . IsNullOrEmpty ( match . Groups [ 1 ] . Value ) )
262
+ {
263
+ // Include first, then Version
264
+ packageId = match . Groups [ 1 ] . Value ;
265
+ version = match . Groups [ 2 ] . Value ;
266
+ }
267
+ else
268
+ {
269
+ // Version first, then Include
270
+ packageId = match . Groups [ 4 ] . Value ;
271
+ version = match . Groups [ 3 ] . Value ;
272
+ }
273
+
274
+ packageVersions [ packageId ] = version ;
275
+ }
276
+ }
277
+
278
+ // Ensure directory exists
279
+ if ( Path . GetDirectoryName ( PackagesJsonPath ) is string directoryPath )
280
+ {
281
+ Directory . CreateDirectory ( directoryPath ) ;
282
+ }
283
+ else
284
+ {
285
+ _log . WriteLine ( $ "Warning: Could not determine directory path for '{ PackagesJsonPath } '.") ;
286
+ }
287
+
288
+ // Write updated packages.json
289
+ File . WriteAllText (
290
+ PackagesJsonPath ,
291
+ JsonSerializer . Serialize ( packageVersions , new JsonSerializerOptions { WriteIndented = true } ) ) ;
292
+ }
293
+
207
294
private static string GenerateTestProjectName ( )
208
295
{
209
296
// Avoiding VB errors because root namespace must not start with number or contain dashes
0 commit comments