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 FluentAssertions ;
5
+ using Microsoft . Build . Framework ;
6
+ using Microsoft . Build . Utilities ;
7
+ using Xunit ;
8
+
9
+ namespace Microsoft . NET . Build . Tasks . UnitTests
10
+ {
11
+ public class GivenASelectRuntimeIdentifierSpecificItems
12
+ {
13
+ [ Fact ]
14
+ public void ItSelectsCompatibleItems ( )
15
+ {
16
+ // Arrange
17
+ var testRuntimeGraphPath = CreateTestRuntimeGraph ( ) ;
18
+ var items = new [ ]
19
+ {
20
+ CreateTaskItem ( "Item1" , "linux-x64" ) ,
21
+ CreateTaskItem ( "Item2" , "win-x64" ) ,
22
+ CreateTaskItem ( "Item3" , "linux" ) ,
23
+ CreateTaskItem ( "Item4" , "ubuntu.18.04-x64" )
24
+ } ;
25
+
26
+ var task = new SelectRuntimeIdentifierSpecificItems ( )
27
+ {
28
+ TargetRuntimeIdentifier = "ubuntu.18.04-x64" ,
29
+ Items = items ,
30
+ RuntimeIdentifierGraphPath = testRuntimeGraphPath ,
31
+ BuildEngine = new MockBuildEngine ( )
32
+ } ;
33
+
34
+ // Act
35
+ bool result = task . Execute ( ) ;
36
+
37
+ // Assert
38
+ result . Should ( ) . BeTrue ( ) ;
39
+ task . SelectedItems . Should ( ) . HaveCount ( 3 ) ; // linux-x64, linux, ubuntu.18.04-x64 should be compatible
40
+ task . SelectedItems . Should ( ) . Contain ( i => i . ItemSpec == "Item1" ) ; // linux-x64
41
+ task . SelectedItems . Should ( ) . Contain ( i => i . ItemSpec == "Item3" ) ; // linux
42
+ task . SelectedItems . Should ( ) . Contain ( i => i . ItemSpec == "Item4" ) ; // ubuntu.18.04-x64
43
+ task . SelectedItems . Should ( ) . NotContain ( i => i . ItemSpec == "Item2" ) ; // win-x64
44
+ }
45
+
46
+ [ Fact ]
47
+ public void ItSelectsItemsWithExactMatch ( )
48
+ {
49
+ // Arrange
50
+ var testRuntimeGraphPath = CreateTestRuntimeGraph ( ) ;
51
+ var items = new [ ]
52
+ {
53
+ CreateTaskItem ( "Item1" , "win-x64" ) ,
54
+ CreateTaskItem ( "Item2" , "linux-x64" )
55
+ } ;
56
+
57
+ var task = new SelectRuntimeIdentifierSpecificItems ( )
58
+ {
59
+ TargetRuntimeIdentifier = "win-x64" ,
60
+ Items = items ,
61
+ RuntimeIdentifierGraphPath = testRuntimeGraphPath ,
62
+ BuildEngine = new MockBuildEngine ( )
63
+ } ;
64
+
65
+ // Act
66
+ bool result = task . Execute ( ) ;
67
+
68
+ // Assert
69
+ result . Should ( ) . BeTrue ( ) ;
70
+ task . SelectedItems . Should ( ) . HaveCount ( 1 ) ;
71
+ task . SelectedItems [ 0 ] . ItemSpec . Should ( ) . Be ( "Item1" ) ;
72
+ }
73
+
74
+ [ Fact ]
75
+ public void ItSkipsItemsWithoutRuntimeIdentifierMetadata ( )
76
+ {
77
+ // Arrange
78
+ var testRuntimeGraphPath = CreateTestRuntimeGraph ( ) ;
79
+ var items = new [ ]
80
+ {
81
+ CreateTaskItem ( "Item1" , "linux-x64" ) ,
82
+ CreateTaskItem ( "Item2" , null ) , // No runtime identifier
83
+ CreateTaskItem ( "Item3" , "" ) // Empty runtime identifier
84
+ } ;
85
+
86
+ var task = new SelectRuntimeIdentifierSpecificItems ( )
87
+ {
88
+ TargetRuntimeIdentifier = "linux-x64" ,
89
+ Items = items ,
90
+ RuntimeIdentifierGraphPath = testRuntimeGraphPath ,
91
+ BuildEngine = new MockBuildEngine ( )
92
+ } ;
93
+
94
+ // Act
95
+ bool result = task . Execute ( ) ;
96
+
97
+ // Assert
98
+ result . Should ( ) . BeTrue ( ) ;
99
+ task . SelectedItems . Should ( ) . HaveCount ( 1 ) ;
100
+ task . SelectedItems [ 0 ] . ItemSpec . Should ( ) . Be ( "Item1" ) ;
101
+ }
102
+
103
+ [ Fact ]
104
+ public void ItUsesCustomRuntimeIdentifierMetadata ( )
105
+ {
106
+ // Arrange
107
+ var testRuntimeGraphPath = CreateTestRuntimeGraph ( ) ;
108
+ var item = new TaskItem ( "Item1" ) ;
109
+ item . SetMetadata ( "CustomRID" , "linux-x64" ) ;
110
+
111
+ var task = new SelectRuntimeIdentifierSpecificItems ( )
112
+ {
113
+ TargetRuntimeIdentifier = "ubuntu.18.04-x64" ,
114
+ Items = new [ ] { item } ,
115
+ RuntimeIdentifierItemMetadata = "CustomRID" ,
116
+ RuntimeIdentifierGraphPath = testRuntimeGraphPath ,
117
+ BuildEngine = new MockBuildEngine ( )
118
+ } ;
119
+
120
+ // Act
121
+ bool result = task . Execute ( ) ;
122
+
123
+ // Assert
124
+ result . Should ( ) . BeTrue ( ) ;
125
+ task . SelectedItems . Should ( ) . HaveCount ( 1 ) ;
126
+ task . SelectedItems [ 0 ] . ItemSpec . Should ( ) . Be ( "Item1" ) ;
127
+ }
128
+
129
+ [ Fact ]
130
+ public void ItReturnsEmptyArrayWhenNoItemsProvided ( )
131
+ {
132
+ // Arrange
133
+ var testRuntimeGraphPath = CreateTestRuntimeGraph ( ) ;
134
+
135
+ var task = new SelectRuntimeIdentifierSpecificItems ( )
136
+ {
137
+ TargetRuntimeIdentifier = "linux-x64" ,
138
+ Items = new ITaskItem [ 0 ] ,
139
+ RuntimeIdentifierGraphPath = testRuntimeGraphPath ,
140
+ BuildEngine = new MockBuildEngine ( )
141
+ } ;
142
+
143
+ // Act
144
+ bool result = task . Execute ( ) ;
145
+
146
+ // Assert
147
+ result . Should ( ) . BeTrue ( ) ;
148
+ task . SelectedItems . Should ( ) . BeEmpty ( ) ;
149
+ }
150
+
151
+ private static TaskItem CreateTaskItem ( string itemSpec , string ? runtimeIdentifier )
152
+ {
153
+ var item = new TaskItem ( itemSpec ) ;
154
+ if ( ! string . IsNullOrEmpty ( runtimeIdentifier ) )
155
+ {
156
+ item . SetMetadata ( "RuntimeIdentifier" , runtimeIdentifier ) ;
157
+ }
158
+ return item ;
159
+ }
160
+
161
+ private static string CreateTestRuntimeGraph ( )
162
+ {
163
+ // Create a minimal runtime graph for testing
164
+ var runtimeGraph = @"{
165
+ ""runtimes"": {
166
+ ""linux"": {},
167
+ ""linux-x64"": {
168
+ ""#import"": [""linux""]
169
+ },
170
+ ""ubuntu"": {
171
+ ""#import"": [""linux""]
172
+ },
173
+ ""ubuntu.18.04"": {
174
+ ""#import"": [""ubuntu""]
175
+ },
176
+ ""ubuntu.18.04-x64"": {
177
+ ""#import"": [""ubuntu.18.04"", ""linux-x64""]
178
+ },
179
+ ""win"": {},
180
+ ""win-x64"": {
181
+ ""#import"": [""win""]
182
+ }
183
+ }
184
+ }" ;
185
+
186
+ var tempFile = Path . GetTempFileName ( ) ;
187
+ File . WriteAllText ( tempFile , runtimeGraph ) ;
188
+ return tempFile ;
189
+ }
190
+ }
191
+ }
0 commit comments