Skip to content

Commit 9081d8f

Browse files
Update test template tests (#49554)
1 parent 60557ee commit 9081d8f

File tree

2 files changed

+119
-19
lines changed

2 files changed

+119
-19
lines changed

test/TestPackages/cgmanifest.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"$schema": "https://json.schemastore.org/component-detection-manifest.json",
3+
"version": 1,
4+
"registrations": []
5+
}

test/dotnet-new.IntegrationTests/DotnetNewTestTemplatesTests.cs

Lines changed: 114 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Collections.Immutable;
55
using System.Text.Json;
6+
using System.Text.Json.Serialization;
67
using System.Text.RegularExpressions;
78

89
namespace Microsoft.DotNet.Cli.New.IntegrationTests
@@ -29,7 +30,7 @@ private static readonly (string ProjectTemplateName, string[] Languages, bool Ru
2930
("nunit-playwright", new[] { Languages.CSharp }, false, false),
3031
];
3132

32-
private static readonly string PackagesJsonPath = Path.Combine(CodeBaseRoot, "test", "component-governance", "packages.json");
33+
private static readonly string PackagesJsonPath = Path.Combine(CodeBaseRoot, "test", "TestPackages", "cgmanifest.json");
3334

3435
public DotnetNewTestTemplatesTests(ITestOutputHelper log) : base(log)
3536
{
@@ -226,23 +227,32 @@ private void RecordPackages(string projectDirectory)
226227
file.EndsWith(".fsproj", StringComparison.OrdinalIgnoreCase) ||
227228
file.EndsWith(".vbproj", StringComparison.OrdinalIgnoreCase));
228229

229-
Dictionary<string, string> packageVersions =
230-
[];
230+
// Load existing component detection manifest or create new one
231+
ComponentDetectionManifest manifest;
231232

232-
// Load existing package versions if file exists
233233
if (File.Exists(PackagesJsonPath))
234234
{
235235
try
236236
{
237-
packageVersions = JsonSerializer.Deserialize<Dictionary<string, string>>(
238-
File.ReadAllText(PackagesJsonPath)) ??
239-
[];
237+
string jsonContent = File.ReadAllText(PackagesJsonPath);
238+
manifest = JsonSerializer.Deserialize<ComponentDetectionManifest>(jsonContent) ??
239+
CreateNewManifest();
240240
}
241241
catch (Exception ex)
242242
{
243-
_log.WriteLine($"Warning: Could not parse existing packages.json: {ex.Message}");
243+
_log.WriteLine($"Warning: Could not parse existing component detection manifest: {ex.Message}");
244+
// Don't create a new manifest when we can't parse the existing one
245+
// This prevents overwriting the existing file with an empty manifest
246+
return;
244247
}
245248
}
249+
else
250+
{
251+
manifest = CreateNewManifest();
252+
}
253+
254+
// Keep track of whether we added anything new
255+
bool updatedManifest = false;
246256

247257
// Extract package references from project files
248258
foreach (var projectFile in projectFiles)
@@ -271,24 +281,109 @@ private void RecordPackages(string projectDirectory)
271281
version = match.Groups[3].Value;
272282
}
273283

274-
packageVersions[packageId] = version;
284+
// Find existing registration for this package or null if not found
285+
var existingRegistration = manifest.Registrations?.FirstOrDefault(r =>
286+
r.Component != null &&
287+
r.Component.Nuget != null &&
288+
string.Equals(r.Component.Nuget.Name, packageId, StringComparison.OrdinalIgnoreCase));
289+
290+
if (existingRegistration == null)
291+
{
292+
// Add new package if it doesn't exist
293+
manifest.Registrations?.Add(new Registration
294+
{
295+
Component = new Component
296+
{
297+
Type = "nuget",
298+
Nuget = new NugetComponent
299+
{
300+
Name = packageId,
301+
Version = version
302+
}
303+
}
304+
});
305+
updatedManifest = true;
306+
}
307+
else if (existingRegistration.Component?.Nuget?.Version != version)
308+
{
309+
// Update version if it's different from the existing one
310+
existingRegistration.Component?.Nuget?.Version = version;
311+
updatedManifest = true;
312+
}
275313
}
276314
}
277315

278-
// Ensure directory exists
279-
if (Path.GetDirectoryName(PackagesJsonPath) is string directoryPath)
316+
// Only write the file if we actually added something new
317+
if (updatedManifest)
280318
{
281-
Directory.CreateDirectory(directoryPath);
319+
// Ensure directory exists
320+
if (Path.GetDirectoryName(PackagesJsonPath) is string directoryPath)
321+
{
322+
Directory.CreateDirectory(directoryPath);
323+
}
324+
else
325+
{
326+
_log.WriteLine($"Warning: Could not determine directory path for '{PackagesJsonPath}'.");
327+
return;
328+
}
329+
330+
// Write updated manifest
331+
File.WriteAllText(
332+
PackagesJsonPath,
333+
JsonSerializer.Serialize(manifest, new JsonSerializerOptions
334+
{
335+
WriteIndented = true,
336+
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
337+
}));
282338
}
283-
else
339+
}
340+
341+
private ComponentDetectionManifest CreateNewManifest()
342+
{
343+
return new ComponentDetectionManifest
284344
{
285-
_log.WriteLine($"Warning: Could not determine directory path for '{PackagesJsonPath}'.");
286-
}
345+
Schema = "https://json.schemastore.org/component-detection-manifest.json",
346+
Version = 1,
347+
Registrations =
348+
[]
349+
};
350+
}
351+
352+
// Classes to model the component detection manifest
353+
private class ComponentDetectionManifest
354+
{
355+
[JsonPropertyName("$schema")]
356+
public string? Schema { get; set; }
357+
358+
[JsonPropertyName("version")]
359+
public int Version { get; set; }
360+
361+
[JsonPropertyName("registrations")]
362+
public List<Registration>? Registrations { get; set; }
363+
}
364+
365+
private class Registration
366+
{
367+
[JsonPropertyName("component")]
368+
public Component? Component { get; set; }
369+
}
370+
371+
private class Component
372+
{
373+
[JsonPropertyName("type")]
374+
public string? Type { get; set; }
375+
376+
[JsonPropertyName("nuget")]
377+
public NugetComponent? Nuget { get; set; }
378+
}
379+
380+
private class NugetComponent
381+
{
382+
[JsonPropertyName("name")]
383+
public string? Name { get; set; }
287384

288-
// Write updated packages.json
289-
File.WriteAllText(
290-
PackagesJsonPath,
291-
JsonSerializer.Serialize(packageVersions, new JsonSerializerOptions { WriteIndented = true }));
385+
[JsonPropertyName("version")]
386+
public string? Version { get; set; }
292387
}
293388

294389
private static string GenerateTestProjectName()

0 commit comments

Comments
 (0)