Skip to content

Commit

Permalink
Improve type and null handling
Browse files Browse the repository at this point in the history
  • Loading branch information
benbp committed Feb 14, 2024
1 parent b94e9e9 commit 5b9cb4a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -591,26 +591,34 @@ Describe "Platform Matrix Environment Variables" -Tag "UnitTest", "envvar" {
"matrix": {
"foo": "bar",
"envReference": ["env:TestMatrixEnvReference", "env:TestMatrixEnvReference2", "noref"]
}
},
"include": [
{
"foo": "bar",
"envReference": "env:TestMatrixEnvReference"
}
]
}
'@

[System.Environment]::SetEnvironmentVariable("TestMatrixEnvReference", "")
[array]$matrix = GenerateMatrix (GetMatrixConfigFromJson $matrixJson) "sparse"
$matrix.Length | Should -Be 3
$matrix.Length | Should -Be 4
$matrix[0].name | Should -Be "bar"
$matrix[0].parameters.envReference | Should -Be ""

[System.Environment]::SetEnvironmentVariable("TestMatrixEnvReference", "replaced")
[System.Environment]::SetEnvironmentVariable("TestMatrixEnvReference2", "replaced2")
[array]$replacedMatrix = GenerateMatrix (GetMatrixConfigFromJson $matrixJson) "sparse"
$replacedMatrix.Length | Should -Be 3
$replacedMatrix.Length | Should -Be 4
$replacedMatrix[0].name | Should -Be "bar_replaced"
$replacedMatrix[0].parameters.envReference | Should -Be "replaced"
$replacedMatrix[1].name | Should -Be "bar_replaced2"
$replacedMatrix[1].parameters.envReference | Should -Be "replaced2"
$replacedMatrix[2].name | Should -Be "bar_noref"
$replacedMatrix[2].parameters.envReference | Should -Be "noref"
$replacedMatrix[3].name | Should -Be "bar_replaced"
$replacedMatrix[3].parameters.envReference | Should -Be "replaced"
}

It "Should support filter/replace with variable reference syntax" {
Expand Down
10 changes: 8 additions & 2 deletions eng/common/scripts/job-matrix/job-matrix-functions.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class MatrixParameter {
$displayName = $this.Value.ToString()
}

if ($displayNamesLookup.ContainsKey($displayName)) {
if ($displayNamesLookup -and $displayNamesLookup.ContainsKey($displayName)) {
$displayName = $displayNamesLookup[$displayName]
}

Expand Down Expand Up @@ -339,6 +339,9 @@ function ProcessReplace {

foreach ($element in $matrix) {
$replacement = [MatrixParameter[]]@()
if (!$element -or $element.Count -eq 0) {
continue
}

foreach ($perm in $element._permutation) {
$replace = $perm
Expand Down Expand Up @@ -372,11 +375,14 @@ function ProcessEnvironmentVariableReferences([array]$matrix, $displayNamesLooku

foreach ($element in $matrix) {
$updated = [MatrixParameter[]]@()
if (!$element -or $element.Count -eq 0) {
continue
}

foreach ($perm in $element._permutation) {
# Iterate nested permutations or run once for singular values (int, string, bool)
foreach ($flattened in $perm.Flatten()) {
if ($flattened.Value?.GetType() -eq "".GetType() -and $flattened.Value.StartsWith("env:")) {
if ($flattened.Value -is [string] -and $flattened.Value.StartsWith("env:")) {
$envKey = $flattened.Value.Replace("env:", "")
$value = [System.Environment]::GetEnvironmentVariable($envKey) ?? ""
if (!$value) {
Expand Down

0 comments on commit 5b9cb4a

Please sign in to comment.