1
+ // Licensed to the .NET Foundation under one or more agreements.
2
+ // The .NET Foundation licenses this file to you under the MIT license.
3
+
4
+ using System . Xml ;
5
+ using Aspire . Cli . Packaging ;
6
+
7
+ namespace Aspire . Cli . Tests . Packaging ;
8
+
9
+ public class TemporaryNuGetConfigTests
10
+ {
11
+ [ Fact ]
12
+ public async Task CreateAsync_IncludesAllPackageSourceMappings ( )
13
+ {
14
+ // Arrange
15
+ var mappings = new PackageMapping [ ]
16
+ {
17
+ new ( "Aspire.*" , "https://example.com/feed1" ) ,
18
+ new ( PackageMapping . AllPackages , "https://example.com/feed2" ) , // "*" filter
19
+ new ( "Microsoft.*" , "https://example.com/feed1" )
20
+ } ;
21
+
22
+ // Act
23
+ using var tempConfig = await TemporaryNuGetConfig . CreateAsync ( mappings ) ;
24
+
25
+ // Assert
26
+ var configContent = await File . ReadAllTextAsync ( tempConfig . ConfigFile . FullName ) ;
27
+ var xmlDoc = new XmlDocument ( ) ;
28
+ xmlDoc . LoadXml ( configContent ) ;
29
+
30
+ // Verify that package source mappings section exists
31
+ var packageSourceMappingNode = xmlDoc . SelectSingleNode ( "//packageSourceMapping" ) ;
32
+ Assert . NotNull ( packageSourceMappingNode ) ;
33
+
34
+ // Verify all package sources are present
35
+ var packageSourceNodes = xmlDoc . SelectNodes ( "//packageSourceMapping/packageSource" ) ;
36
+ Assert . NotNull ( packageSourceNodes ) ;
37
+ Assert . Equal ( 2 , packageSourceNodes . Count ) ; // Two distinct sources
38
+
39
+ // Verify that the AllPackages mapping is included
40
+ var allPackagesMapping = xmlDoc . SelectSingleNode ( "//packageSourceMapping/packageSource[@key='https://example.com/feed2']/package[@pattern='*']" ) ;
41
+ Assert . NotNull ( allPackagesMapping ) ;
42
+
43
+ // Verify other specific mappings are also included
44
+ var aspireMapping = xmlDoc . SelectSingleNode ( "//packageSourceMapping/packageSource[@key='https://example.com/feed1']/package[@pattern='Aspire.*']" ) ;
45
+ Assert . NotNull ( aspireMapping ) ;
46
+
47
+ var microsoftMapping = xmlDoc . SelectSingleNode ( "//packageSourceMapping/packageSource[@key='https://example.com/feed1']/package[@pattern='Microsoft.*']" ) ;
48
+ Assert . NotNull ( microsoftMapping ) ;
49
+ }
50
+
51
+ [ Fact ]
52
+ public async Task CreateAsync_WithOnlyAllPackagesMappings_IncludesAllMappings ( )
53
+ {
54
+ // Arrange
55
+ var mappings = new PackageMapping [ ]
56
+ {
57
+ new ( PackageMapping . AllPackages , "https://feed1.example.com" ) ,
58
+ new ( PackageMapping . AllPackages , "https://feed2.example.com" )
59
+ } ;
60
+
61
+ // Act
62
+ using var tempConfig = await TemporaryNuGetConfig . CreateAsync ( mappings ) ;
63
+
64
+ // Assert
65
+ var configContent = await File . ReadAllTextAsync ( tempConfig . ConfigFile . FullName ) ;
66
+ var xmlDoc = new XmlDocument ( ) ;
67
+ xmlDoc . LoadXml ( configContent ) ;
68
+
69
+ // Verify that package source mappings section exists
70
+ var packageSourceMappingNode = xmlDoc . SelectSingleNode ( "//packageSourceMapping" ) ;
71
+ Assert . NotNull ( packageSourceMappingNode ) ;
72
+
73
+ // Verify all package sources are present
74
+ var packageSourceNodes = xmlDoc . SelectNodes ( "//packageSourceMapping/packageSource" ) ;
75
+ Assert . NotNull ( packageSourceNodes ) ;
76
+ Assert . Equal ( 2 , packageSourceNodes . Count ) ; // Two distinct sources
77
+
78
+ // Verify that both AllPackages mappings are included
79
+ var feed1Mapping = xmlDoc . SelectSingleNode ( "//packageSourceMapping/packageSource[@key='https://feed1.example.com']/package[@pattern='*']" ) ;
80
+ Assert . NotNull ( feed1Mapping ) ;
81
+
82
+ var feed2Mapping = xmlDoc . SelectSingleNode ( "//packageSourceMapping/packageSource[@key='https://feed2.example.com']/package[@pattern='*']" ) ;
83
+ Assert . NotNull ( feed2Mapping ) ;
84
+ }
85
+
86
+ [ Fact ]
87
+ public async Task CreateAsync_WithNoMappings_CreatesValidConfig ( )
88
+ {
89
+ // Arrange
90
+ var mappings = Array . Empty < PackageMapping > ( ) ;
91
+
92
+ // Act
93
+ using var tempConfig = await TemporaryNuGetConfig . CreateAsync ( mappings ) ;
94
+
95
+ // Assert
96
+ var configContent = await File . ReadAllTextAsync ( tempConfig . ConfigFile . FullName ) ;
97
+ var xmlDoc = new XmlDocument ( ) ;
98
+ xmlDoc . LoadXml ( configContent ) ;
99
+
100
+ // Verify basic structure exists
101
+ var configNode = xmlDoc . SelectSingleNode ( "//configuration" ) ;
102
+ Assert . NotNull ( configNode ) ;
103
+
104
+ var packageSourcesNode = xmlDoc . SelectSingleNode ( "//packageSources" ) ;
105
+ Assert . NotNull ( packageSourcesNode ) ;
106
+
107
+ // No package source mappings should exist when no mappings provided
108
+ var packageSourceMappingNode = xmlDoc . SelectSingleNode ( "//packageSourceMapping" ) ;
109
+ Assert . Null ( packageSourceMappingNode ) ;
110
+ }
111
+ }
0 commit comments