Skip to content

Commit

Permalink
(chocolatey#2738) Add tests for pushing to NuGet Repository
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
corbob committed Jan 23, 2023
1 parent 153b297 commit fbdcd14
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 2 deletions.
81 changes: 81 additions & 0 deletions tests/chocolatey-tests/commands/choco-push.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 <Wording>" -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
}
}
}
13 changes: 11 additions & 2 deletions tests/helpers/common/Chocolatey/New-ChocolateyTestPackage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,33 @@

# 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) {
Write-Verbose "Building '$($Package.Name)' with '$($Package.FullName)'"
$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
}
}
}

0 comments on commit fbdcd14

Please sign in to comment.