Skip to content

Commit

Permalink
Merge pull request #3161 from corbob/version-normalization-tests
Browse files Browse the repository at this point in the history
(#2994) Add Pester tests for version normalization
  • Loading branch information
gep13 authored May 16, 2023
2 parents 238bd2e + 323fa06 commit 329c7e5
Show file tree
Hide file tree
Showing 11 changed files with 227 additions and 60 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ code_drop
[Bb]in
obj
src/packages
/.vscode/
/.vscode/**
!/.vscode/settings.json
.vs

*.suo
Expand Down
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"[powershell]": {
"files.encoding": "utf8bom",
}
}
3 changes: 2 additions & 1 deletion Invoke-Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ else {
if (-not (Test-Path "$TestPath/packages") -or -not $SkipPackaging) {
$null = New-Item -Path "$TestPath/packages" -ItemType Directory -Force
# Get and pack packages
$nuspecs = Get-ChildItem -Path $PSScriptRoot/src/chocolatey.tests.integration, $PSScriptRoot/tests/packages -Recurse | Where-Object Name -Like '*.nuspec'
$nuspecs = Get-ChildItem -Path $PSScriptRoot/src/chocolatey.tests.integration, $PSScriptRoot/tests/packages -Recurse -Include *.nuspec
Get-ChildItem -Path $PSScriptRoot/tests/packages -Recurse -Include *.nupkg | Copy-Item -Destination "$TestPath/packages"

foreach ($file in $nuspecs) {
Write-Host "Packaging $file"
Expand Down
35 changes: 21 additions & 14 deletions tests/Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ Vagrant.configure("2") do |config|
config.vm.network :forwarded_port, guest: 3389, host: 14389, id: 'rdp', auto_correct: true

config.vm.provider "virtualbox" do |vb|
vb.gui = true
vb.memory = 4096
vb.cpus = 2
vb.gui = ENV['VM_GUI'] ? ENV['VM_GUI'].to_s.downcase == 'true' : true
vb.memory = ENV['VM_MEMORY'] ? ENV['VM_MEMORY'].to_i : 4096
vb.cpus = ENV['VM_CPU'] ? ENV['VM_CPU'].to_i : 2
vb.linked_clone = true
vb.customize ['modifyvm', :id, '--vram', '128']
end
Expand All @@ -37,29 +37,36 @@ Vagrant.configure("2") do |config|

config.vm.synced_folder '../', '/chocoRoot'

config.vm.provision "shell", inline: <<-SHELL
config.vm.provision "shell", name: "prep", inline: <<-SHELL
cscript c:/windows/system32/slmgr.vbs /rearm | Out-Null
Set-ExecutionPolicy Bypass -Scope Process -Force
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072
iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
iex (irm https://community.chocolatey.org/install.ps1)
Write-Host "We are going to install some prerequisites now. This could take some time"
Write-Host "($(Get-Date -Format "dddd MM/dd/yyyy HH:mm:ss K")) We are going to install some prerequisites now. This could take some time"
$null = choco install git pester visualstudio2022community visualstudio2022-workload-manageddesktop netfx-4.8-devpack -y
Import-Module $env:ChocolateyInstall/helpers/chocolateyProfile.psm1
Update-SessionEnvironment
Write-Host "Done installing software, now copying files"
Write-Host "($(Get-Date -Format "dddd MM/dd/yyyy HH:mm:ss K")) Done installing software, now copying files"
# If you try to build on macOS, then run this, there are attributes on some files that cause the build to fail inside of the vagrant environment.
$null = robocopy c:/chocoRoot c:/code/choco /mir /copy:dt
$null = robocopy c:/chocoRoot c:/code/choco /e /copy:dt
SHELL
config.vm.provision "shell", name: "build", inline: <<-SHELL
$null = robocopy c:/chocoRoot c:/code/choco /e /copy:dt /purge
Push-Location c:/code/choco
Write-Host "Files have been copied, beginning build process."
Write-Host "($(Get-Date -Format "dddd MM/dd/yyyy HH:mm:ss K")) Files have been copied, beginning build process."
$CakeOutput = ./build.bat 2>&1
if ($LastExitCode -ne 0) {
Set-Content c:/vagrant/buildOutput.log -Value $CakeOutput
Write-Host "The build has failed. Please see the buildOutput.log file for details"
exit $LastExitCode
Set-Content c:/vagrant/buildOutput.log -Value $CakeOutput
Write-Host "The build has failed. Please see the buildOutput.log file for details"
exit $LastExitCode
}
Write-Host "Build complete. Executing tests."
Write-Host "($(Get-Date -Format "dddd MM/dd/yyyy HH:mm:ss K")) Build complete."
SHELL
config.vm.provision "shell", name: "test", inline: <<-SHELL
Write-Host "($(Get-Date -Format "dddd MM/dd/yyyy HH:mm:ss K")) Starting Test Execution"
Push-Location c:/code/choco
# $env:TEST_KITCHEN = 1
$env:VM_RUNNING = 1
./Invoke-Tests.ps1
Expand Down
29 changes: 26 additions & 3 deletions tests/chocolatey-tests/commands/choco-info.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
Import-Module helpers/common-helpers

Describe "choco info" -Tag Chocolatey, InfoCommand {
Describe "choco info" -Tag Chocolatey, InfoCommand {
BeforeDiscovery {
$licensedProxyFixed = Test-PackageIsEqualOrHigher 'chocolatey.extension' 2.2.0-beta -AllowMissingPackage
}
Expand Down Expand Up @@ -197,6 +195,31 @@ Describe "choco info" -Tag Chocolatey, InfoCommand {
}
}

Context "Listing package information for non-normalized exact package version" -ForEach @(
@{ ExpectedPackageVersion = '1.0.0' ; SearchVersion = '1' }
@{ ExpectedPackageVersion = '1.0.0' ; SearchVersion = '1.0' }
@{ ExpectedPackageVersion = '1.0.0' ; SearchVersion = '1.0.0' }
@{ ExpectedPackageVersion = '4.0.1' ; SearchVersion = '4.0.1' }
@{ ExpectedPackageVersion = '1.0.0' ; SearchVersion = '01.0.0.0' }
@{ ExpectedPackageVersion = '4.0.1' ; SearchVersion = '004.0.01.0' }
@{ ExpectedPackageVersion = '4.0.1' ; SearchVersion = '0000004.00000.00001.0000' }
) {
BeforeAll {
Restore-ChocolateyInstallSnapshot
$PackageUnderTest = 'nonnormalizedversions'

$Output = Invoke-Choco info $PackageUnderTest --version $SearchVersion
}

It "Should exit with success (0)" {
$Output.ExitCode | Should -Be 0 -Because $Output.String
}

It "Should find and report the normalized package version" {
$Output.Lines | Should -Contain "$PackageUnderTest $ExpectedPackageVersion" -Because $Output.String
}
}

# This needs to be the last test in this block, to ensure NuGet configurations aren't being created.
Test-NuGetPaths
}
40 changes: 35 additions & 5 deletions tests/chocolatey-tests/commands/choco-install.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
Import-Module helpers/common-helpers

# https://github.com/chocolatey/choco/blob/master/src/chocolatey.tests.integration/scenarios/InstallScenarios.cs

Describe "choco install" -Tag Chocolatey, InstallCommand {
Describe "choco install" -Tag Chocolatey, InstallCommand {
BeforeDiscovery {
$isLicensed30OrMissingVersion = Test-PackageIsEqualOrHigher 'chocolatey.extension' '3.0.0-beta' -AllowMissingPackage
$licensedProxyFixed = Test-PackageIsEqualOrHigher 'chocolatey.extension' 2.2.0-beta -AllowMissingPackage
Expand Down Expand Up @@ -1778,6 +1774,40 @@ To install a local, or remote file, you may use:
}
}

Context "Installing a package with a non-normalized version number" -ForEach @(
@{ ExpectedPackageVersion = '1.0.0' ; SearchVersion = '1' ; NuspecVersion = '01.0.0.0'}
@{ ExpectedPackageVersion = '1.0.0' ; SearchVersion = '1.0' ; NuspecVersion = '01.0.0.0'}
@{ ExpectedPackageVersion = '1.0.0' ; SearchVersion = '1.0.0' ; NuspecVersion = '01.0.0.0' }
@{ ExpectedPackageVersion = '4.0.1' ; SearchVersion = '4.0.1' ; NuspecVersion = '004.0.01.0' }
@{ ExpectedPackageVersion = '1.0.0' ; SearchVersion = '01.0.0.0' ; NuspecVersion = '01.0.0.0' }
@{ ExpectedPackageVersion = '4.0.1' ; SearchVersion = '004.0.01.0' ; NuspecVersion = '004.0.01.0' }
@{ ExpectedPackageVersion = '4.0.1' ; SearchVersion = '0000004.00000.00001.0000' ; NuspecVersion = '004.0.01.0' }
) {
BeforeAll {
Restore-ChocolateyInstallSnapshot
$PackageUnderTest = 'nonnormalizedversions'
$Output = Invoke-Choco install $PackageUnderTest --Version $SearchVersion
}

It "Should exit with success (0)" {
$Output.ExitCode | Should -Be 0 -Because $Output.String
}

It "Should report successful installation" {
$Output.Lines | Should -Contain "$PackageUnderTest v$ExpectedPackageVersion" -Because $Output.String
$Output.Lines | Should -Contain 'Chocolatey installed 1/1 packages.' -Because $Output.String
}

It "Should have installed the correct files" {
$ExpectedNupkg = "${env:ChocolateyInstall}/lib/$PackageUnderTest/$PackageUnderTest.nupkg"
$ExpectedNupkg | Should -Exist -Because $Output.String
Expand-ZipArchive -Source $ExpectedNupkg -Destination "${env:TEMP}/$PackageUnderTest-expanded"
$NuspecContents = [xml](Get-Content "${env:TEMP}/$PackageUnderTest-expanded/$PackageUnderTest.nuspec")
Remove-Item -Path "${env:TEMP}/$PackageUnderTest-expanded" -Recurse -Force
$NuspecContents.package.metadata.version | Should -Be $NuspecVersion
}
}

# This needs to be the last test in this block, to ensure NuGet configurations aren't being created.
# Any tests after this block are expected to generate the configuration as they're explicitly using the NuGet CLI
Test-NuGetPaths
Expand Down
101 changes: 71 additions & 30 deletions tests/chocolatey-tests/commands/choco-pack.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
Import-Module helpers/common-helpers

$successPack = @('basic'; 'basic-dependencies'; "cdata"; "full")
# Required elements, that can also not be empty
$missingFailures = @('id'; 'version'; 'authors'; 'description')
# Elements that can not be set to an empty string, but are not required
$emptyFailures = @(
"projectUrl"
"projectSourceUrl"
"docsUrl"
"bugTrackerUrl"
"mailingListUrl"
"iconUrl"
"licenseUrl"
)
# Elements that will return an invalid failure (usually due to serialization)
$invalidFailures = @(
@{id = 'projectUrl'; message = "ERROR: CHCU0001: 'invalid project url' is not a valid URL for the projectUrl element in the package nuspec file." }
@{id = 'projectSourceUrl'; message = "ERROR: CHCU0001: 'invalid project source url' is not a valid URL for the projectSourceUrl element in the package nuspec file." }
@{id = 'docsUrl'; message = "ERROR: CHCU0001: 'invalid docs url' is not a valid URL for the docsUrl element in the package nuspec file." }
@{id = 'bugTrackerUrl'; message = "ERROR: CHCU0001: 'invalid bug tracker url' is not a valid URL for the bugTrackerUrl element in the package nuspec file." }
@{id = 'mailingListUrl'; message = "ERROR: CHCU0001: 'invalid mailing list url' is not a valid URL for the mailingListUrl element in the package nuspec file." }
@{id = 'iconUrl'; message = "ERROR: CHCU0001: 'invalid icon url' is not a valid URL for the iconUrl element in the package nuspec file." }
@{id = 'licenseUrl'; message = "ERROR: CHCU0001: 'invalid license url' is not a valid URL for the licenseUrl element in the package nuspec file." }
@{id = "version"; message = "ERROR: CHCU0001: 'INVALID' is not a valid version string in the package nuspec file." }
@{id = "no-content"; message = "Cannot create a package that has no dependencies nor content." } # This is a message from NuGet.Client, we may want to take ownership of it eventually.
@{id = "id"; message = "The package ID 'invalid id' contains invalid characters. Examples of valid package IDs include 'MyPackage' and 'MyPackage.Sample'." } # This is a message from NuGet.Client, we may want to take ownership of it eventually.
@{id = "requirelicenseacceptance"; message = "ERROR: CHCR0002: Enabling license acceptance requires a license url." }
)


Describe "choco pack" -Tag Chocolatey, PackCommand {
BeforeDiscovery {
$successPack = @('basic'; 'basic-dependencies'; "cdata"; "full")
# Required elements, that can also not be empty
$missingFailures = @('id'; 'version'; 'authors'; 'description')
# Elements that can not be set to an empty string, but are not required
$emptyFailures = @(
"projectUrl"
"projectSourceUrl"
"docsUrl"
"bugTrackerUrl"
"mailingListUrl"
"iconUrl"
"licenseUrl"
)
# Elements that will return an invalid failure (usually due to serialization)
$invalidFailures = @(
@{id = 'projectUrl'; message = "ERROR: CHCU0001: 'invalid project url' is not a valid URL for the projectUrl element in the package nuspec file." }
@{id = 'projectSourceUrl'; message = "ERROR: CHCU0001: 'invalid project source url' is not a valid URL for the projectSourceUrl element in the package nuspec file." }
@{id = 'docsUrl'; message = "ERROR: CHCU0001: 'invalid docs url' is not a valid URL for the docsUrl element in the package nuspec file." }
@{id = 'bugTrackerUrl'; message = "ERROR: CHCU0001: 'invalid bug tracker url' is not a valid URL for the bugTrackerUrl element in the package nuspec file." }
@{id = 'mailingListUrl'; message = "ERROR: CHCU0001: 'invalid mailing list url' is not a valid URL for the mailingListUrl element in the package nuspec file." }
@{id = 'iconUrl'; message = "ERROR: CHCU0001: 'invalid icon url' is not a valid URL for the iconUrl element in the package nuspec file." }
@{id = 'licenseUrl'; message = "ERROR: CHCU0001: 'invalid license url' is not a valid URL for the licenseUrl element in the package nuspec file." }
@{id = "version"; message = "ERROR: CHCU0001: 'INVALID' is not a valid version string in the package nuspec file." }
@{id = "no-content"; message = "Cannot create a package that has no dependencies nor content." } # This is a message from NuGet.Client, we may want to take ownership of it eventually.
@{id = "id"; message = "The package ID 'invalid id' contains invalid characters. Examples of valid package IDs include 'MyPackage' and 'MyPackage.Sample'." } # This is a message from NuGet.Client, we may want to take ownership of it eventually.
@{id = "requirelicenseacceptance"; message = "ERROR: CHCR0002: Enabling license acceptance requires a license url." }
)
}

BeforeAll {
Remove-NuGetPaths
$testPackageLocation = "$(Get-TempDirectory)ChocolateyTests\packages"
Expand Down Expand Up @@ -584,6 +585,46 @@ Describe "choco pack" -Tag Chocolatey, PackCommand {
}
}

Context 'Packing a package with non-normalized versions generates normalized versions' -ForEach @(
@{ ExpectedPackageVersion = '0.1.0' ; ProvidedVersion = '0.1.0.0' }
@{ ExpectedPackageVersion = '1.2.3.4' ; ProvidedVersion = '01.02.03.04' }
@{ ExpectedPackageVersion = '1.2.4' ; ProvidedVersion = '01.02.04' }
@{ ExpectedPackageVersion = '1.2.0' ; ProvidedVersion = '01.02' }
@{ ExpectedPackageVersion = '1.2.3' ; ProvidedVersion = '0001.0002.0003' }
@{ ExpectedPackageVersion = '2.0.0' ; ProvidedVersion = '02' }
) {
BeforeAll {
Restore-ChocolateyInstallSnapshot
$PackageUnderTest = 'nonnormalizedversions'
Push-Location (New-Item "$(Get-TempDirectory)/$(New-Guid)" -ItemType Directory)
$null = Invoke-Choco new $PackageUnderTest --version $ProvidedVersion
$SourceNuspec = "$PWD/$PackageUnderTest/$PackageUnderTest.nuspec"
$Output = Invoke-Choco pack $SourceNuspec
}

AfterAll {
Pop-Location
}

It "Should exit with success (0)" {
$Output.ExitCode | Should -Be 0 -Because $Output.String
}

It "Should report successful installation" {
$Output.Lines | Should -Contain "Successfully created package '$PWD\$PackageUnderTest.$ExpectedPackageVersion.nupkg'" -Because $Output.String
}

It "Should have generated the correct files" {
$ExpectedNupkg = "$PWD/$PackageUnderTest.$ExpectedPackageVersion.nupkg"
$ExpectedNupkg | Should -Exist -Because $Output.String
Expand-ZipArchive -Source $ExpectedNupkg -Destination "$PWD/$PackageUnderTest-expanded"
$SourceNuspecContents = [xml](Get-Content $SourceNuspec)
$PackedNuspecContents = [xml](Get-Content "$PWD/$PackageUnderTest-expanded/$PackageUnderTest.nuspec")
$SourceNuspecContents.package.metadata.version | Should -Be $ProvidedVersion
$PackedNuspecContents.package.metadata.version | Should -Be $ExpectedPackageVersion
}
}

# This needs to be the last test in this block, to ensure NuGet configurations aren't being created.
Test-NuGetPaths
}
31 changes: 29 additions & 2 deletions tests/chocolatey-tests/commands/choco-uninstall.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
Import-Module helpers/common-helpers

Describe "choco uninstall" -Tag Chocolatey, UninstallCommand {
BeforeAll {
Remove-NuGetPaths
Expand Down Expand Up @@ -421,6 +419,35 @@ Describe "choco uninstall" -Tag Chocolatey, UninstallCommand {
}
}

Context "Uninstalling a package with a non-normalized version number" -ForEach @(
@{ ExpectedPackageVersion = '1.0.0' ; SearchVersion = '01.0.0.0' }
@{ ExpectedPackageVersion = '4.0.1' ; SearchVersion = '004.0.01.0' }
) {
BeforeAll {
Restore-ChocolateyInstallSnapshot
$PackageUnderTest = 'nonnormalizedversions'
$null = Invoke-Choco install $PackageUnderTest --Version $SearchVersion
$Output = Invoke-Choco uninstall $PackageUnderTest
}

It "Should exit with success (0)" {
$Output.ExitCode | Should -Be 0 -Because $Output.String
}

It "Should report successful uninstallation" {
$Output.Lines | Should -Contain "$PackageUnderTest v$ExpectedPackageVersion" -Because $Output.String
$Output.Lines | Should -Contain 'Chocolatey uninstalled 1/1 packages.' -Because $Output.String
}

It "Should have removed any <Directory> directory" -ForEach @(
@{ Directory = 'lib' }
@{ Directory = 'lib-bkp' }
) {
$InstallDirectory = "${env:ChocolateyInstall}/$Directory/$PackageUnderTest/"
$InstallDirectory | Should -Not -Exist -Because $Output.String
}
}

# This needs to be the last test in this block, to ensure NuGet configurations aren't being created.
Test-NuGetPaths
}
Loading

0 comments on commit 329c7e5

Please sign in to comment.