Skip to content

Commit 8265479

Browse files
azure-sdkbenbp
andauthored
Fix bug where imported matrix parameter duplicates are not overrided (#17126)
Co-authored-by: Ben Broderick Phillips <[email protected]>
1 parent 6b559bf commit 8265479

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

eng/common/scripts/job-matrix/job-matrix-functions.ps1

+12-11
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function GenerateMatrix(
3232
[Array]$filters = @(),
3333
[Array]$nonSparseParameters = @()
3434
) {
35-
$orderedMatrix, $importedMatrix = ProcessImport $config.orderedMatrix $selectFromMatrixType
35+
$orderedMatrix, $importedMatrix, $importedDisplayNamesLookup = ProcessImport $config.orderedMatrix $selectFromMatrixType
3636
if ($selectFromMatrixType -eq "sparse") {
3737
[Array]$matrix = GenerateSparseMatrix $orderedMatrix $config.displayNamesLookup $nonSparseParameters
3838
} elseif ($selectFromMatrixType -eq "all") {
@@ -44,7 +44,7 @@ function GenerateMatrix(
4444
# Combine with imported after matrix generation, since a sparse selection should result in a full combination of the
4545
# top level and imported sparse matrices (as opposed to a sparse selection of both matrices).
4646
if ($importedMatrix) {
47-
[Array]$matrix = CombineMatrices $matrix $importedMatrix
47+
[Array]$matrix = CombineMatrices $matrix $importedMatrix $importedDisplayNamesLookup
4848
}
4949

5050
if ($config.exclude) {
@@ -199,19 +199,19 @@ function ProcessIncludes([MatrixConfig]$config, [Array]$matrix)
199199
function ProcessImport([System.Collections.Specialized.OrderedDictionary]$matrix, [String]$selection)
200200
{
201201
if (!$matrix -or !$matrix.Contains($IMPORT_KEYWORD)) {
202-
return $matrix
202+
return $matrix, @(), @{}
203203
}
204204

205205
$importPath = $matrix[$IMPORT_KEYWORD]
206206
$matrix.Remove($IMPORT_KEYWORD)
207207

208-
$matrixConfig = GetMatrixConfigFromJson (Get-Content $importPath)
209-
$importedMatrix = GenerateMatrix $matrixConfig $selection
208+
$importedMatrixConfig = GetMatrixConfigFromJson (Get-Content $importPath)
209+
$importedMatrix = GenerateMatrix $importedMatrixConfig $selection
210210

211-
return $matrix, $importedMatrix
211+
return $matrix, $importedMatrix, $importedMatrixConfig.displayNamesLookup
212212
}
213213

214-
function CombineMatrices([Array]$matrix1, [Array]$matrix2)
214+
function CombineMatrices([Array]$matrix1, [Array]$matrix2, [Hashtable]$displayNamesLookup = @{})
215215
{
216216
$combined = @()
217217
if (!$matrix1) {
@@ -223,21 +223,22 @@ function CombineMatrices([Array]$matrix1, [Array]$matrix2)
223223

224224
foreach ($entry1 in $matrix1) {
225225
foreach ($entry2 in $matrix2) {
226+
$entry2name = @()
226227
$newEntry = @{
227228
name = $entry1.name
228229
parameters = CloneOrderedDictionary $entry1.parameters
229230
}
230231
foreach($param in $entry2.parameters.GetEnumerator()) {
231-
if (!$newEntry.Contains($param.Name)) {
232+
if (!$newEntry.parameters.Contains($param.Name)) {
232233
$newEntry.parameters[$param.Name] = $param.Value
234+
$entry2name += CreateDisplayName $param.Value $displayNamesLookup
233235
} else {
234236
Write-Warning "Skipping duplicate parameter `"$($param.Name)`" when combining matrix."
235237
}
236238
}
237239

238240
# The maximum allowed matrix name length is 100 characters
239-
$entry2.name = $entry2.name.TrimStart("job_")
240-
$newEntry.name = $newEntry.name, $entry2.name -join "_"
241+
$newEntry.name = @($newEntry.name, ($entry2name -join "_")) -join "_"
241242
if ($newEntry.name.Length -gt 100) {
242243
$newEntry.name = $newEntry.name[0..99] -join ""
243244
}
@@ -305,7 +306,7 @@ function GenerateSparseMatrix(
305306

306307
if ($nonSparse) {
307308
[Array]$allOfMatrix = GenerateFullMatrix $nonSparse $displayNamesLookup
308-
return CombineMatrices $allOfMatrix $sparseMatrix
309+
return CombineMatrices $allOfMatrix $sparseMatrix $displayNamesLookup
309310
}
310311

311312
return $sparseMatrix

eng/common/scripts/job-matrix/tests/job-matrix-functions.modification.tests.ps1

+21
Original file line numberDiff line numberDiff line change
@@ -216,4 +216,25 @@ Describe "Platform Matrix Import" -Tag "import" {
216216
$matrix.Length | Should -Be 7
217217
CompareMatrices $matrix $expected
218218
}
219+
220+
It "Should generate a sparse matrix with an imported a sparse matrix" {
221+
$matrixJson = @'
222+
{
223+
"matrix": {
224+
"$IMPORT": "./test-import-matrix.json",
225+
"Foo": [ "fooOverride1", "fooOverride2" ],
226+
}
227+
}
228+
'@
229+
230+
$importConfig = GetMatrixConfigFromJson $matrixJson
231+
$matrix = GenerateMatrix $importConfig "sparse"
232+
233+
$matrix[0].parameters["Foo"] | Should -Be "fooOverride1"
234+
$matrix[0].name | Should -Be "fooOverride1_bar1"
235+
$matrix[3].parameters["Foo"] | Should -Be "fooOverride2"
236+
$matrix[3].name | Should -Be "fooOverride2_bar1"
237+
$matrix[5].parameters["Foo"] | Should -Be "fooOverride2"
238+
$matrix[5].name | Should -Be "fooOverride2_importedBaz"
239+
}
219240
}

0 commit comments

Comments
 (0)