From fbdcd1466be7fdfa633226d1c07b3e07bc8aea05 Mon Sep 17 00:00:00 2001 From: Cory Knox Date: Mon, 23 Jan 2023 13:25:56 -0800 Subject: [PATCH] (#2738) Add tests for pushing to NuGet Repository Add tests for pushing to NuGet v2 and v3 repositories. Enhance the `New-ChocolateyTestPackage` function to accept an additional version part, and return the location of the built packages. --- .../commands/choco-push.Tests.ps1 | 81 +++++++++++++++++++ .../Chocolatey/New-ChocolateyTestPackage.ps1 | 13 ++- 2 files changed, 92 insertions(+), 2 deletions(-) diff --git a/tests/chocolatey-tests/commands/choco-push.Tests.ps1 b/tests/chocolatey-tests/commands/choco-push.Tests.ps1 index b20dc0cbd2..e8c246eb9d 100644 --- a/tests/chocolatey-tests/commands/choco-push.Tests.ps1 +++ b/tests/chocolatey-tests/commands/choco-push.Tests.ps1 @@ -130,3 +130,84 @@ Describe "choco push" -Tag Chocolatey, PushCommand, Broken -Skip:($null -eq $env # This needs to be the last test in this block, to ensure NuGet configurations aren't being created. Test-NuGetPaths } + +Describe 'choco push nuget <_> repository' -Tag Chocolatey, PushCommand -Skip:($null -eq $env:NUGET_API_KEY -or $null -eq $env:NUGET_PUSH_REPO) -ForEach @('v2', 'v3') { + BeforeDiscovery { + $TestCases = @( + @{ Wording = 'using config' ; UseConfig = $true } + @{ Wording = 'using command line parameters' ; UseConfig = $false } + ) + } + + BeforeAll { + $RepositoryEndpoint = switch ($_) { + 'v2' { '' } + 'v3' { 'index.json' } + } + + $ApiKey = $env:NUGET_API_KEY + $RepositoryToUse = $env:NUGET_PUSH_REPO + + Initialize-ChocolateyTestInstall + + New-ChocolateyInstallSnapshot + } + + AfterAll { + Remove-ChocolateyTestInstall + } + + Context "Pushing package successfully " -ForEach $TestCases { + BeforeAll { + $snapshotPath = New-ChocolateyInstallSnapshot + $PackageUnderTest = "chocolatey-dummy-package" + $TestPath = "$PSScriptRoot\testpackages" + $VersionUnderTest = '1.0.0' + $AddedVersion = "a$(((New-Guid) -split '-')[0])" + $NewChocolateyTestPackage = @{ + TestPath = $TestPath + Name = $PackageUnderTest + Version = $VersionUnderTest + AddedVersion = $AddedVersion + } + $PackagePath = New-ChocolateyTestPackage @NewChocolateyTestPackage + + if ($UseConfig) { + # TODO: These really should use the full parameter names + $null = Invoke-Choco apikey -s $RepositoryToUse$RepositoryEndpoint -k $ApiKey + # Ensure the key is null (should always be, but scoping can be wonky) + $KeyParameter = $null + } else { + # PowerShell requires this for reasons that only PowerShell knows + $KeyParameter = @("--api-key", $ApiKey) + } + + $Output = Invoke-Choco push $PackagePath --source $RepositoryToUse$RepositoryEndpoint @KeyParameter + $VerifyPackagesSplat = @( + "find" + "$PackageUnderTest" + "--pre" + "--source" + "$RepositoryToUse$RepositoryEndpoint" + "--api-key" + "$ApiKey" + "--version" + "$VersionUnderTest-$AddedVersion" + ) + $Packages = (Invoke-Choco @VerifyPackagesSplat --Limit-Output).Lines | ConvertFrom-ChocolateyOutput -Command List + } + + AfterAll { + $null = Invoke-Choco install nuget.commandline -y + & "$env:ChocolateyInstall/bin/nuget.exe" delete $PackageUnderTest "$VersionUnderTest-$AddedVersion" -Source $RepositoryToUse -ApiKey $ApiKey -NonInteractive + } + + It 'Exits with Success (0)' { + $Output.ExitCode | Should -Be 0 -Because $Output.String + } + + It 'Successfully pushed the package to the repository' { + $Packages | Should -Not -BeNullOrEmpty + } + } +} diff --git a/tests/helpers/common/Chocolatey/New-ChocolateyTestPackage.ps1 b/tests/helpers/common/Chocolatey/New-ChocolateyTestPackage.ps1 index 02f1b2f279..fead2f6e7e 100644 --- a/tests/helpers/common/Chocolatey/New-ChocolateyTestPackage.ps1 +++ b/tests/helpers/common/Chocolatey/New-ChocolateyTestPackage.ps1 @@ -20,11 +20,18 @@ # Location to store the built package(s) [ValidateNotNullOrEmpty()] - [string]$Destination = $env:CHOCOLATEY_TEST_PACKAGES_PATH + [string]$Destination = $env:CHOCOLATEY_TEST_PACKAGES_PATH, + + [Parameter()] + [string]$AddedVersion ) process { $NuspecFile = Get-Item "$TestPath\$Name\$Version\$Name.nuspec" + if ($AddedVersion) { + $Version = "$Version-$AddedVersion" + } + Write-Verbose "Building '$($NuspecFile.Count)' packages" foreach ($Package in $NuspecFile) { @@ -32,12 +39,14 @@ $ExpectedPackage = Join-Path $Destination "$Name.$Version.nupkg" if (-not (Test-Path $ExpectedPackage)) { - $BuildOutput = Invoke-Choco pack $Package.FullName --outputdirectory $Destination + $BuildOutput = Invoke-Choco pack $Package.FullName --outputdirectory $Destination --version $Version if ($BuildOutput.ExitCode -ne 0) { throw $BuildOutput.String } } + + $ExpectedPackage } } }