Skip to content

Commit 8707eea

Browse files
corbobgep13
authored andcommitted
(tests) Add non-normalized version Pester tests
Add Pester tests to ensure that non-normalized version numbers work, but are normalized where they're expected to be.
1 parent f6433ad commit 8707eea

File tree

5 files changed

+190
-24
lines changed

5 files changed

+190
-24
lines changed

tests/chocolatey-tests/commands/choco-info.Tests.ps1

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Describe "choco info" -Tag Chocolatey, InfoCommand {
1+
Describe "choco info" -Tag Chocolatey, InfoCommand {
22
BeforeDiscovery {
33
$licensedProxyFixed = Test-PackageIsEqualOrHigher 'chocolatey.extension' 2.2.0-beta -AllowMissingPackage
44
}
@@ -195,6 +195,29 @@ Describe "choco info" -Tag Chocolatey, InfoCommand {
195195
}
196196
}
197197

198+
Context "Listing package information for non-normalized exact package version" -ForEach @(
199+
@{ ExpectedPackageVersion = '1.0.0' ; SearchVersion = '1.0.0' }
200+
@{ ExpectedPackageVersion = '4.0.1' ; SearchVersion = '4.0.1' }
201+
@{ ExpectedPackageVersion = '1.0.0' ; SearchVersion = '01.0.0.0' }
202+
@{ ExpectedPackageVersion = '4.0.1' ; SearchVersion = '004.0.01.0' }
203+
@{ ExpectedPackageVersion = '4.0.1' ; SearchVersion = '0000004.00000.00001.0000' }
204+
) {
205+
BeforeAll {
206+
Restore-ChocolateyInstallSnapshot
207+
$PackageUnderTest = 'nonnormalizedversions'
208+
209+
$Output = Invoke-Choco info $PackageUnderTest --version $SearchVersion
210+
}
211+
212+
It "Should exit with success (0)" {
213+
$Output.ExitCode | Should -Be 0 -Because $Output.String
214+
}
215+
216+
It "Should find and report the normalized package version" {
217+
$Output.Lines | Should -Contain "$PackageUnderTest $ExpectedPackageVersion" -Because $Output.String
218+
}
219+
}
220+
198221
# This needs to be the last test in this block, to ensure NuGet configurations aren't being created.
199222
Test-NuGetPaths
200223
}

tests/chocolatey-tests/commands/choco-install.Tests.ps1

+37
Original file line numberDiff line numberDiff line change
@@ -1774,6 +1774,43 @@ To install a local, or remote file, you may use:
17741774
}
17751775
}
17761776

1777+
Context "Installing a package with a non-normalized version number" -ForEach @(
1778+
@{ ExpectedPackageVersion = '1.0.0' ; SearchVersion = '1.0.0' ; NuspecVersion = '01.0.0.0' }
1779+
@{ ExpectedPackageVersion = '4.0.1' ; SearchVersion = '4.0.1' ; NuspecVersion = '004.0.01.0' }
1780+
@{ ExpectedPackageVersion = '1.0.0' ; SearchVersion = '01.0.0.0' ; NuspecVersion = '01.0.0.0' }
1781+
@{ ExpectedPackageVersion = '4.0.1' ; SearchVersion = '004.0.01.0' ; NuspecVersion = '004.0.01.0' }
1782+
@{ ExpectedPackageVersion = '4.0.1' ; SearchVersion = '0000004.00000.00001.0000' ; NuspecVersion = '004.0.01.0' }
1783+
) {
1784+
BeforeAll {
1785+
Restore-ChocolateyInstallSnapshot
1786+
Push-Location (New-Item "$(Get-TempDirectory)/$(New-Guid)" -ItemType Directory)
1787+
$PackageUnderTest = 'nonnormalizedversions'
1788+
$Output = Invoke-Choco install $PackageUnderTest --Version $SearchVersion
1789+
}
1790+
1791+
AfterAll {
1792+
Pop-Location
1793+
}
1794+
1795+
It "Should exit with success (0)" {
1796+
$Output.ExitCode | Should -Be 0 -Because $Output.String
1797+
}
1798+
1799+
It "Should report successful installation" {
1800+
$Output.Lines | Should -Contain "$PackageUnderTest v$ExpectedPackageVersion" -Because $Output.String
1801+
$Output.Lines | Should -Contain 'Chocolatey installed 1/1 packages.' -Because $Output.String
1802+
}
1803+
1804+
It "Should have installed the correct files" {
1805+
$ExpectedNupkg = "${env:ChocolateyInstall}/lib/$PackageUnderTest/$PackageUnderTest.nupkg"
1806+
$ExpectedNupkg | Should -Exist -Because $Output.String
1807+
Expand-ZipArchive -Source $ExpectedNupkg -Destination "${env:TEMP}/$PackageUnderTest-expanded"
1808+
$NuspecContents = [xml](Get-Content "${env:TEMP}/$PackageUnderTest-expanded/$PackageUnderTest.nuspec")
1809+
Remove-Item -Path "${env:TEMP}/$PackageUnderTest-expanded" -Recurse -Force
1810+
$NuspecContents.package.metadata.version | Should -Be $NuspecVersion
1811+
}
1812+
}
1813+
17771814
# This needs to be the last test in this block, to ensure NuGet configurations aren't being created.
17781815
# Any tests after this block are expected to generate the configuration as they're explicitly using the NuGet CLI
17791816
Test-NuGetPaths

tests/chocolatey-tests/commands/choco-pack.Tests.ps1

+61-22
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
1-
1+
22
Describe "choco pack" -Tag Chocolatey, PackCommand {
33
BeforeDiscovery {
44
$successPack = @('basic'; 'basic-dependencies'; "cdata"; "full")
55
# Required elements, that can also not be empty
66
$missingFailures = @('id'; 'version'; 'authors'; 'description')
7-
# Elements that can not be set to an empty string, but are not required
8-
$emptyFailures = @(
9-
"projectUrl"
10-
"projectSourceUrl"
11-
"docsUrl"
12-
"bugTrackerUrl"
13-
"mailingListUrl"
14-
"iconUrl"
15-
"licenseUrl"
16-
)
17-
# Elements that will return an invalid failure (usually due to serialization)
18-
$invalidFailures = @(
19-
@{id = 'projectUrl'; message = "ERROR: CHCU0001: 'invalid project url' is not a valid URL for the projectUrl element in the package nuspec file." }
20-
@{id = 'projectSourceUrl'; message = "ERROR: CHCU0001: 'invalid project source url' is not a valid URL for the projectSourceUrl element in the package nuspec file." }
21-
@{id = 'docsUrl'; message = "ERROR: CHCU0001: 'invalid docs url' is not a valid URL for the docsUrl element in the package nuspec file." }
22-
@{id = 'bugTrackerUrl'; message = "ERROR: CHCU0001: 'invalid bug tracker url' is not a valid URL for the bugTrackerUrl element in the package nuspec file." }
23-
@{id = 'mailingListUrl'; message = "ERROR: CHCU0001: 'invalid mailing list url' is not a valid URL for the mailingListUrl element in the package nuspec file." }
24-
@{id = 'iconUrl'; message = "ERROR: CHCU0001: 'invalid icon url' is not a valid URL for the iconUrl element in the package nuspec file." }
25-
@{id = 'licenseUrl'; message = "ERROR: CHCU0001: 'invalid license url' is not a valid URL for the licenseUrl element in the package nuspec file." }
26-
@{id = "version"; message = "ERROR: CHCU0001: 'INVALID' is not a valid version string in the package nuspec file." }
27-
@{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.
7+
# Elements that can not be set to an empty string, but are not required
8+
$emptyFailures = @(
9+
"projectUrl"
10+
"projectSourceUrl"
11+
"docsUrl"
12+
"bugTrackerUrl"
13+
"mailingListUrl"
14+
"iconUrl"
15+
"licenseUrl"
16+
)
17+
# Elements that will return an invalid failure (usually due to serialization)
18+
$invalidFailures = @(
19+
@{id = 'projectUrl'; message = "ERROR: CHCU0001: 'invalid project url' is not a valid URL for the projectUrl element in the package nuspec file." }
20+
@{id = 'projectSourceUrl'; message = "ERROR: CHCU0001: 'invalid project source url' is not a valid URL for the projectSourceUrl element in the package nuspec file." }
21+
@{id = 'docsUrl'; message = "ERROR: CHCU0001: 'invalid docs url' is not a valid URL for the docsUrl element in the package nuspec file." }
22+
@{id = 'bugTrackerUrl'; message = "ERROR: CHCU0001: 'invalid bug tracker url' is not a valid URL for the bugTrackerUrl element in the package nuspec file." }
23+
@{id = 'mailingListUrl'; message = "ERROR: CHCU0001: 'invalid mailing list url' is not a valid URL for the mailingListUrl element in the package nuspec file." }
24+
@{id = 'iconUrl'; message = "ERROR: CHCU0001: 'invalid icon url' is not a valid URL for the iconUrl element in the package nuspec file." }
25+
@{id = 'licenseUrl'; message = "ERROR: CHCU0001: 'invalid license url' is not a valid URL for the licenseUrl element in the package nuspec file." }
26+
@{id = "version"; message = "ERROR: CHCU0001: 'INVALID' is not a valid version string in the package nuspec file." }
27+
@{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.
2828
@{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.
2929
@{id = "requirelicenseacceptance"; message = "ERROR: CHCR0002: Enabling license acceptance requires a license url." }
3030
)
@@ -585,6 +585,45 @@ $invalidFailures = @(
585585
}
586586
}
587587

588+
Context 'Packing a package with non-normalized versions generates normalized versions' -ForEach @(
589+
@{ ExpectedPackageVersion = '0.1.0' ; ProvidedVersion = '0.1.0.0' }
590+
@{ ExpectedPackageVersion = '1.2.3.4' ; ProvidedVersion = '01.02.03.04' }
591+
@{ ExpectedPackageVersion = '1.2.4' ; ProvidedVersion = '01.02.04' }
592+
@{ ExpectedPackageVersion = '1.2.0' ; ProvidedVersion = '01.02' }
593+
@{ ExpectedPackageVersion = '1.2.3' ; ProvidedVersion = '0001.0002.0003' }
594+
) {
595+
BeforeAll {
596+
Restore-ChocolateyInstallSnapshot
597+
$PackageUnderTest = 'nonnormalizedversions'
598+
Push-Location (New-Item "$(Get-TempDirectory)/$(New-Guid)" -ItemType Directory)
599+
$null = Invoke-Choco new $PackageUnderTest --version $ProvidedVersion
600+
$SourceNuspec = "$PWD/$PackageUnderTest/$PackageUnderTest.nuspec"
601+
$Output = Invoke-Choco pack $SourceNuspec
602+
}
603+
604+
AfterAll {
605+
Pop-Location
606+
}
607+
608+
It "Should exit with success (0)" {
609+
$Output.ExitCode | Should -Be 0 -Because $Output.String
610+
}
611+
612+
It "Should report successful installation" {
613+
$Output.Lines | Should -Contain "Successfully created package '$PWD\$PackageUnderTest.$ExpectedPackageVersion.nupkg'" -Because $Output.String
614+
}
615+
616+
It "Should have generated the correct files" {
617+
$ExpectedNupkg = "$PWD/$PackageUnderTest.$ExpectedPackageVersion.nupkg"
618+
$ExpectedNupkg | Should -Exist -Because $Output.String
619+
Expand-ZipArchive -Source $ExpectedNupkg -Destination "$PWD/$PackageUnderTest-expanded"
620+
$SourceNuspecContents = [xml](Get-Content $SourceNuspec)
621+
$PackedNuspecContents = [xml](Get-Content "$PWD/$PackageUnderTest-expanded/$PackageUnderTest.nuspec")
622+
$SourceNuspecContents.package.metadata.version | Should -Be $ProvidedVersion
623+
$PackedNuspecContents.package.metadata.version | Should -Be $ExpectedPackageVersion
624+
}
625+
}
626+
588627
# This needs to be the last test in this block, to ensure NuGet configurations aren't being created.
589628
Test-NuGetPaths
590629
}

tests/chocolatey-tests/commands/choco-uninstall.Tests.ps1

+33
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,39 @@ Describe "choco uninstall" -Tag Chocolatey, UninstallCommand {
419419
}
420420
}
421421

422+
Context "Uninstalling a package with a non-normalized version number" -ForEach @(
423+
@{ ExpectedPackageVersion = '1.0.0' ; SearchVersion = '01.0.0.0' }
424+
@{ ExpectedPackageVersion = '4.0.1' ; SearchVersion = '004.0.01.0' }
425+
) {
426+
BeforeAll {
427+
Restore-ChocolateyInstallSnapshot
428+
$PackageUnderTest = 'nonnormalizedversions'
429+
$null = Invoke-Choco install $PackageUnderTest --Version $SearchVersion
430+
$Output = Invoke-Choco uninstall $PackageUnderTest
431+
}
432+
433+
AfterAll {
434+
Pop-Location
435+
}
436+
437+
It "Should exit with success (0)" {
438+
$Output.ExitCode | Should -Be 0 -Because $Output.String
439+
}
440+
441+
It "Should report successful uninstallation" {
442+
$Output.Lines | Should -Contain "$PackageUnderTest v$ExpectedPackageVersion" -Because $Output.String
443+
$Output.Lines | Should -Contain 'Chocolatey uninstalled 1/1 packages.' -Because $Output.String
444+
}
445+
446+
It "Should have removed any <Directory> directory" -ForEach @(
447+
@{ Directory = 'lib' }
448+
@{ Directory = 'lib-bkp' }
449+
) {
450+
$InstallDirectory = "${env:ChocolateyInstall}/$Directory/$PackageUnderTest/"
451+
$InstallDirectory | Should -Not -Exist -Because $Output.String
452+
}
453+
}
454+
422455
# This needs to be the last test in this block, to ensure NuGet configurations aren't being created.
423456
Test-NuGetPaths
424457
}

tests/chocolatey-tests/commands/choco-upgrade.Tests.ps1

+35-1
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ To upgrade a local, or remote file, you may use:
344344
$PackageUnderTest = "hasinnoinstaller"
345345
$PackageVersion = '6.2.0.3'
346346

347-
# We are purposely using the --reduce-nupkg-only option (both here and in the next call to Invoke-Choco), to make the
347+
# We are purposely using the --reduce-nupkg-only option (both here and in the next call to Invoke-Choco), to make the
348348
# test as close to default operation, when running both in the context of OSS and CLE. It was found during testing, that
349349
# the Package Optimizer would remove the application installer, which is the file that is being locked during the test,
350350
# which means then that the test doesn't actually test what we want it to. We are locking the exe here instead of say a
@@ -521,6 +521,40 @@ To upgrade a local, or remote file, you may use:
521521
}
522522
}
523523

524+
Context "Upgrading a package with a non-normalized version number" {
525+
BeforeAll {
526+
Restore-ChocolateyInstallSnapshot
527+
Push-Location (New-Item "$(Get-TempDirectory)/$(New-Guid)" -ItemType Directory)
528+
$PackageUnderTest = 'nonnormalizedversions'
529+
$VersionUnderTest = '004.0.01.0'
530+
$ExpectedPackageVersion = '4.0.1'
531+
$null = Invoke-Choco install $PackageUnderTest --version 1.0.0
532+
$Output = Invoke-Choco upgrade $PackageUnderTest
533+
}
534+
535+
AfterAll {
536+
Pop-Location
537+
}
538+
539+
It "Should exit with success (0)" {
540+
$Output.ExitCode | Should -Be 0 -Because $Output.String
541+
}
542+
543+
It "Should report successful upgrade" {
544+
$Output.Lines | Should -Contain "$PackageUnderTest v$ExpectedPackageVersion" -Because $Output.String
545+
$Output.Lines | Should -Contain 'Chocolatey upgraded 1/1 packages.' -Because $Output.String
546+
}
547+
548+
It "Should have upgraded the correct files" {
549+
$ExpectedNupkg = "${env:ChocolateyInstall}/lib/$PackageUnderTest/$PackageUnderTest.nupkg"
550+
$ExpectedNupkg | Should -Exist -Because $Output.String
551+
Expand-ZipArchive -Source $ExpectedNupkg -Destination "$PWD/$PackageUnderTest-expanded"
552+
$NuspecContents = [xml](Get-Content "$PWD/$PackageUnderTest-expanded/$PackageUnderTest.nuspec")
553+
Remove-Item -Path "$PWD/$PackageUnderTest-expanded" -Recurse -Force
554+
$NuspecContents.package.metadata.version | Should -Be $VersionUnderTest
555+
}
556+
}
557+
524558
# This needs to be (almost) the last test in this block, to ensure NuGet configurations aren't being created.
525559
# Any tests after this block are expected to generate the configuration as they're explicitly using the NuGet CLI
526560
Test-NuGetPaths

0 commit comments

Comments
 (0)