diff --git a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Utilities/PathHelperTests.cs b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Utilities/PathHelperTests.cs index 09220b0b9d..abaaf1f7c4 100644 --- a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Utilities/PathHelperTests.cs +++ b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Utilities/PathHelperTests.cs @@ -51,11 +51,11 @@ public void VerifyMultipleMatchingPathsReturnsAllPaths() var expected = new[] { - Path.Combine(temp.DirectoryPath, "src", "a", "packages.config").NormalizePathToUnix(), Path.Combine(temp.DirectoryPath, "src", "A", "packages.config").NormalizePathToUnix(), + Path.Combine(temp.DirectoryPath, "src", "a", "packages.config").NormalizePathToUnix(), }; - Assert.Equal(expected, resolvedPaths!); + AssertEx.Equal(expected, resolvedPaths!); } [LinuxOnlyFact] diff --git a/nuget/script/ci-test b/nuget/script/ci-test index 7684f82741..2c6687ed1b 100755 --- a/nuget/script/ci-test +++ b/nuget/script/ci-test @@ -2,6 +2,12 @@ set -e +# PowerShell unit tests +pushd ./updater +pwsh ./test.ps1 +popd + +# C# unit tests pushd ./helpers/lib/NuGetUpdater dotnet restore dotnet format --no-restore --exclude ../NuGet.Client --verify-no-changes -v diag @@ -10,5 +16,6 @@ dotnet test --configuration Release --no-restore --no-build --logger "console;ve dotnet test --configuration Release --no-restore --no-build --logger "console;verbosity=normal" --blame-hang-timeout 5m ./NuGetUpdater.Core.Test/NuGetUpdater.Core.Test.csproj popd +# Ruby unit tests bundle install bundle exec rspec spec diff --git a/nuget/updater/common.ps1 b/nuget/updater/common.ps1 new file mode 100755 index 0000000000..b72f11fb1f --- /dev/null +++ b/nuget/updater/common.ps1 @@ -0,0 +1,26 @@ +# Walk from each update directory to the root reporting all global.json files. +function Get-DirectoriesForSdkInstall([string] $repoRoot, [string[]]$updateDirectories) { + $repoRoot = Convert-Path $repoRoot + $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 + } + + $candidateDir = Split-Path -Parent $candidateDir + if ($null -eq $candidateDir -or ` + $candidateDir -eq $repoRootParent) { + break + } + } + } + } + + return ,$globalJsonPaths +} diff --git a/nuget/updater/main.ps1 b/nuget/updater/main.ps1 index c25197a2f5..bbb3299405 100644 --- a/nuget/updater/main.ps1 +++ b/nuget/updater/main.ps1 @@ -1,6 +1,8 @@ Set-StrictMode -version 2.0 $ErrorActionPreference = "Stop" +. $PSScriptRoot\common.ps1 + $updaterTool = "$env:DEPENDABOT_NATIVE_HELPERS_PATH/nuget/NuGetUpdater/NuGetUpdater.Cli" $jobString = Get-Content -Path $env:DEPENDABOT_JOB_PATH $job = (ConvertFrom-Json -InputObject $jobString).job @@ -39,27 +41,18 @@ function Install-Sdks { $candidateDirectories += $job.source.directories } - foreach ($candidateDirName in $candidateDirectories) { - $candidateFullPath = "$rootDir/$candidateDirName" - if (Test-Path $candidateFullPath) { - $candidateDir = Convert-Path $candidateFullPath - while ($true) { - $globalJsonPath = Join-Path $candidateDir "global.json" - if (Test-Path $globalJsonPath) { - $globalJson = Get-Content $globalJsonPath | ConvertFrom-Json - $sdkVersion = $globalJson.sdk.version - if (-Not ($sdkVersion -in $installedSdks)) { - $installedSdks += $sdkVersion - Write-Host "Installing SDK $sdkVersion as specified in $globalJsonPath" - & $env:DOTNET_INSTALL_SCRIPT_PATH --version $sdkVersion --install-dir $env:DOTNET_INSTALL_DIR - } - } + $globalJsonRelativePaths = Get-DirectoriesForSdkInstall ` + -repoRoot $rootDir ` + -updateDirectories $candidateDirectories - $candidateDir = Split-Path -Parent $candidateDir - if ($candidateDir -eq $rootDir) { - break - } - } + foreach ($globalJsonRelativePath in $globalJsonRelativePaths) { + $globalJsonPath = "$rootDir/$globalJsonRelativePath" + $globalJson = Get-Content $globalJsonPath | ConvertFrom-Json + $sdkVersion = $globalJson.sdk.version + if (-Not ($sdkVersion -in $installedSdks)) { + $installedSdks += $sdkVersion + Write-Host "Installing SDK $sdkVersion as specified in $globalJsonRelativePath" + & $env:DOTNET_INSTALL_SCRIPT_PATH --version $sdkVersion --install-dir $env:DOTNET_INSTALL_DIR } } diff --git a/nuget/updater/test-data/global-json-discovery-2-values/global.json b/nuget/updater/test-data/global-json-discovery-2-values/global.json new file mode 100644 index 0000000000..393f6c72b8 --- /dev/null +++ b/nuget/updater/test-data/global-json-discovery-2-values/global.json @@ -0,0 +1,3 @@ +{ + "comment": "content unimportant for test" +} \ No newline at end of file diff --git a/nuget/updater/test-data/global-json-discovery-2-values/src/global.json b/nuget/updater/test-data/global-json-discovery-2-values/src/global.json new file mode 100644 index 0000000000..393f6c72b8 --- /dev/null +++ b/nuget/updater/test-data/global-json-discovery-2-values/src/global.json @@ -0,0 +1,3 @@ +{ + "comment": "content unimportant for test" +} \ No newline at end of file diff --git a/nuget/updater/test-data/global-json-discovery-none/src/.gitignore b/nuget/updater/test-data/global-json-discovery-none/src/.gitignore new file mode 100644 index 0000000000..3ecd98852c --- /dev/null +++ b/nuget/updater/test-data/global-json-discovery-none/src/.gitignore @@ -0,0 +1 @@ +# empty to force directory creation diff --git a/nuget/updater/test-data/global-json-discovery-root-no-file/.gitignore b/nuget/updater/test-data/global-json-discovery-root-no-file/.gitignore new file mode 100644 index 0000000000..3ecd98852c --- /dev/null +++ b/nuget/updater/test-data/global-json-discovery-root-no-file/.gitignore @@ -0,0 +1 @@ +# empty to force directory creation diff --git a/nuget/updater/test-data/global-json-discovery-root-with-file/global.json b/nuget/updater/test-data/global-json-discovery-root-with-file/global.json new file mode 100644 index 0000000000..393f6c72b8 --- /dev/null +++ b/nuget/updater/test-data/global-json-discovery-root-with-file/global.json @@ -0,0 +1,3 @@ +{ + "comment": "content unimportant for test" +} \ No newline at end of file diff --git a/nuget/updater/test.ps1 b/nuget/updater/test.ps1 new file mode 100755 index 0000000000..3ef5f29956 --- /dev/null +++ b/nuget/updater/test.ps1 @@ -0,0 +1,54 @@ +Set-StrictMode -version 2.0 +$ErrorActionPreference = "Stop" + +. $PSScriptRoot\common.ps1 + +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])'" + } + } +} + +function Test-GlobalJsonDiscovery([string]$testDirectory, [string[]]$directories, [string[]]$expectedPaths) { + Write-Host "Test-GlobalJsonDiscovery in $testDirectory ... " -NoNewline + $testDirectoryFull = "$PSScriptRoot/test-data/$testDirectory" + $actualPaths = Get-DirectoriesForSdkInstall -repoRoot $testDirectoryFull -updateDirectories $directories + Assert-ArraysEqual -expected $expectedPaths -actual $actualPaths + Write-Host "OK" +} + +try { + # verify SDK updater directories + Test-GlobalJsonDiscovery ` + -testDirectory "global-json-discovery-root-no-file" ` + -directories @("/") ` + -expectedPaths @() + + Test-GlobalJsonDiscovery ` + -testDirectory "global-json-discovery-root-with-file" ` + -directories @("/") ` + -expectedPaths @("global.json") + + Test-GlobalJsonDiscovery ` + -testDirectory "global-json-discovery-none" ` + -directories @("src") ` + -expectedPaths @() + + Test-GlobalJsonDiscovery ` + -testDirectory "global-json-discovery-2-values" ` + -directories @("src") ` + -expectedPaths @("src/global.json", "global.json") +} +catch { + Write-Host $_ + Write-Host $_.Exception + Write-Host $_.ScriptStackTrace + exit 1 +}