Skip to content

Commit

Permalink
Merge pull request #11071 from dependabot/dev/brettfo/nuget-unit-test…
Browse files Browse the repository at this point in the history
…-sdk-install

make directory crawling unit-testable
  • Loading branch information
randhircs authored Dec 9, 2024
2 parents fa9831f + 6a944b5 commit facb5d7
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
7 changes: 7 additions & 0 deletions nuget/script/ci-test
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
26 changes: 26 additions & 0 deletions nuget/updater/common.ps1
Original file line number Diff line number Diff line change
@@ -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
}
33 changes: 13 additions & 20 deletions nuget/updater/main.ps1
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"comment": "content unimportant for test"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"comment": "content unimportant for test"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# empty to force directory creation
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# empty to force directory creation
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"comment": "content unimportant for test"
}
54 changes: 54 additions & 0 deletions nuget/updater/test.ps1
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit facb5d7

Please sign in to comment.