From f5ebc99fbc49f4faddd643a1ac38fbe2868195f9 Mon Sep 17 00:00:00 2001 From: "Brett V. Forsgren" Date: Wed, 22 Jan 2025 12:02:26 -0700 Subject: [PATCH] improve sdk detection with wildcards and dirs that don't exist --- nuget/updater/common.ps1 | 32 ++++++++++++------- .../.gitignore | 0 .../src/global.json | 5 +++ nuget/updater/test.ps1 | 28 +++++++++++----- 4 files changed, 45 insertions(+), 20 deletions(-) create mode 100644 nuget/updater/test-data/global-json-discovery-recursive-wildcard/.gitignore create mode 100644 nuget/updater/test-data/global-json-discovery-recursive-wildcard/src/global.json diff --git a/nuget/updater/common.ps1 b/nuget/updater/common.ps1 index 7ed642b1dd..d5112c6ed0 100755 --- a/nuget/updater/common.ps1 +++ b/nuget/updater/common.ps1 @@ -30,19 +30,27 @@ function Get-GlobalJsonForSdkInstall([string] $repoRoot, [string[]] $updateDirec $repoRootParent = Split-Path -Parent $repoRoot $globalJsonPaths = @() foreach ($updateDirectory in $updateDirectories) { - $candidateDir = Convert-Path "$repoRoot/$updateDirectory" - if (Test-Path $candidateDir) { - while ($true) { - $globalJsonPath = Join-Path $candidateDir "global.json" - if (Test-Path $globalJsonPath) { - $repoRelativeGlobalJsonPath = [System.IO.Path]::GetRelativePath($repoRoot, $globalJsonPath).Replace("\", "/") - $globalJsonPaths += $repoRelativeGlobalJsonPath - } + if (-not (Test-Path "$repoRoot/$updateDirectory")) { + # directory doesn't exist + continue + } + + # $updateDirectory might be a recursive wildcard like "/**"; this takes care of that + $candidateDirs = Convert-Path "$repoRoot/$updateDirectory" + foreach ($candidateDir in $candidateDirs) { + if (Test-Path $candidateDir -PathType Container) { + while ($true) { + $globalJsonPath = Join-Path $candidateDir "global.json" + if (Test-Path $globalJsonPath) { + $repoRelativeGlobalJsonPath = [System.IO.Path]::GetRelativePath($repoRoot, $globalJsonPath).Replace("\", "/") + $globalJsonPaths += $repoRelativeGlobalJsonPath + } - $candidateDir = Split-Path -Parent $candidateDir - if ($null -eq $candidateDir -or ` - $candidateDir -eq $repoRootParent) { - break + $candidateDir = Split-Path -Parent $candidateDir + if ($null -eq $candidateDir -or ` + $candidateDir -eq $repoRootParent) { + break + } } } } diff --git a/nuget/updater/test-data/global-json-discovery-recursive-wildcard/.gitignore b/nuget/updater/test-data/global-json-discovery-recursive-wildcard/.gitignore new file mode 100644 index 0000000000..e69de29bb2 diff --git a/nuget/updater/test-data/global-json-discovery-recursive-wildcard/src/global.json b/nuget/updater/test-data/global-json-discovery-recursive-wildcard/src/global.json new file mode 100644 index 0000000000..0f4c71898b --- /dev/null +++ b/nuget/updater/test-data/global-json-discovery-recursive-wildcard/src/global.json @@ -0,0 +1,5 @@ +{ + "sdk": { + "version": "1.2.3" + } +} \ No newline at end of file diff --git a/nuget/updater/test.ps1 b/nuget/updater/test.ps1 index e08a08c3f1..3456b1acd7 100755 --- a/nuget/updater/test.ps1 +++ b/nuget/updater/test.ps1 @@ -6,13 +6,8 @@ $ErrorActionPreference = "Stop" function Assert-ArraysEqual([string[]]$expected, [string[]]$actual) { $expectedText = $expected -join ", " $actualText = $actual -join ", " - if ($expected.Length -ne $actual.Length) { - throw "Expected array length $($expected.Length) but was $($actual.Length). Values: [$expectedText] vs [$actualText]" - } - for ($i = 0; $i -lt $expected.Length; $i++) { - if ($expected[$i] -ne $actual[$i]) { - throw "Expected array element at index $i to be '$($expected[$i])' but was '$($actual[$i])'" - } + if ($expectedText -ne $actualText) { + throw "Expected array values '$expectedText' but was '$actualText'" } } @@ -25,7 +20,6 @@ function Test-GlobalJsonVersions([string] $testDirectory, [string[]] $directorie } try { - # verify SDK updater directories Test-GlobalJsonVersions ` -testDirectory "global-json-discovery-root-no-file" ` -directories @("/") ` @@ -55,6 +49,24 @@ try { -directories @("/src") ` -installedSdks @("8.0.404", "9.0.101") ` -expectedSdksToInstall @("1.2.3") + + Test-GlobalJsonVersions ` + -testDirectory "global-json-discovery-recursive-wildcard" ` + -directories @("/**") ` + -installedSdks @("8.0.404", "9.0.101") ` + -expectedSdksToInstall @("1.2.3") + + Test-GlobalJsonVersions ` + -testDirectory "global-json-discovery-recursive-wildcard" ` + -directories @("/src/**/*") ` + -installedSdks @("8.0.404", "9.0.101") ` + -expectedSdksToInstall @() + + Test-GlobalJsonVersions ` + -testDirectory "global-json-discovery-none" ` + -directories @("/dir-that-does-not-exist") ` + -installedSdks @("8.0.404", "9.0.101") ` + -expectedSdksToInstall @() } catch { Write-Host $_