Skip to content

Commit

Permalink
WIP - Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
corbob committed Aug 12, 2024
1 parent 2a7c584 commit 1731d3e
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function Get-ChocolateyInstalledPackages {
(Invoke-Choco list -r).Lines | ConvertFrom-ChocolateyOutput -Command List
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<tags>hasfailingnesteddependency admin</tags>
<dependencies>
<dependency id="dependencyfailure" version="1.0.0" />
<dependency id="hasdependency" version="1.0.0" />
<dependency id="downgradesdependency" version="2.0.0" />
</dependencies>
</metadata>
<files>
Expand Down
67 changes: 58 additions & 9 deletions tests/pester-tests/commands/choco-install.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
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 @@ -2070,7 +2070,7 @@ To install a local, or remote file, you may use:
}
}

Context 'Installing a package with argument (<Argument>) should (<AllowsDowngrade>) downgrade an existing package dependency.' -Tag Downgrade -ForEach @(
Context 'Installing a package with argument (<Argument>) should (<AllowsDowngrade>) downgrade an existing package dependency.' -Tag Downgrade,testing -ForEach @(
@{
Argument = '--force'
AllowsDowngrade = $true
Expand Down Expand Up @@ -2114,13 +2114,42 @@ To install a local, or remote file, you may use:
$Output.Lines | Should -Contain -Not:($AllowsDowngrade) "A newer version of $($DependentPackage.Name) (v$($DependentPackage.Version)) is already installed." -Because $Output.String
$Output.Lines | Should -Contain -Not:($AllowsDowngrade) 'Use --allow-downgrade or --force to attempt to install older versions.' -Because $Output.String
}

It "Should have the expected packages" {
$Packages = Get-ChocolateyInstalledPackages
$Packages | Out-String | Write-Host
if ($AllowsDowngrade) {
$Packages | Where { $_.Name -eq $DependentPackage.Name -and $_.Version -eq '1.0.0' } | Should -Not -BeNullOrEmpty -Because "Packages: $Packages $($Output.String)"
}
else {
$Packages | Where { $_.Name -eq $DependentPackage.Name -and $_.Version -eq $DependentPackage.Version } | Should -Not -BeNullOrEmpty -Because "Packages: $Packages $($Output.String)"
}
}
}

Context 'Installing a package with a failing nested dependency should not install the requested package' -Tag testing {
Context 'Installing a package (<PackageName>) with a failing nested dependency should not install the requested package' -Tag testing -ForEach @(
@{
PackageName = 'hasfailingnesteddependency'
}
@{
PackageName = 'packages.config'
}
) {
BeforeAll {
Restore-ChocolateyInstallSnapshot
$DependentPackage = @{
Name = 'isdependency'
Version = '2.1.0'
}

$Output = Invoke-Choco install hasfailingnesteddependency --confirm
Restore-ChocolateyInstallSnapshot -SetWorkingDirectory

$Setup = Invoke-Choco install $DependentPackage.Name --version $DependentPackage.Version --confirm

if ($PackageName -eq 'packages.config') {
Copy-Item "$PSScriptRoot/failingnested.packages.config" './packages.config'
}
$Output = Invoke-Choco install $PackageName --confirm
$Packages = Get-ChocolateyInstalledPackages
}

It "Exits correctly (15608)" {
Expand All @@ -2129,13 +2158,33 @@ To install a local, or remote file, you may use:
}

It "Reports that it fails" {
$Output.Lines | Should -Contain '- hasdependency v2.1.0' -Because $Output.String
$Output.Lines | Should -Contain '- isdependency v2.1.0' -Because $Output.String
$Output.Lines | Should -Contain '- isexactversiondependency v2.0.0' -Because $Output.String
# $Output.Lines | Should -Contain '- dependencyfailure - Failed to install dependencyfailure a previous dependency failed.' -Because $Output.String
# $Output.Lines | Should -Contain '- hasfailingnesteddependency - Failed to install hasfailingnesteddependency a previous dependency failed.' -Because $Output.String
# $Output.Lines | Should -Contain '- downgradesdependency - Failed to install downgradesdependency a previous dependency failed.' -Because $Output.String
$Output.Lines | Should -Contain "- failingdependency (exited 15608) - Error while running '$($env:ChocolateyInstall)\lib\failingdependency\tools\chocolateyinstall.ps1'." -Because $Output.String
$Output.Lines | Should -Contain 'Chocolatey installed 3/6 packages. 3 packages failed.' -Because $Output.String
# $Output.Lines | Should -Contain 'Chocolatey installed 3/6 packages. 3 packages failed.' -Because $Output.String
}

It "Should have the expected packages" {
$ExpectedPackages = @(
[pscustomobject]@{
Name = 'isdependency'
Version = '2.1.0'
}
)
$UnexpectedPackages = @(
'dependencyfailure'
'hasfailingnesteddependency'
'downgradesdependency'
)

foreach ($package in $ExpectedPackages) {
$Packages | Where { $_.Name -eq $package.Name -and $_.Version -eq $package.Version } | Should -Not -BeNullOrEmpty -Because "Package: $package $($Output.String)"
}

foreach ($package in $UnexpectedPackages) {
$Packages.Name | Should -Not -Contain $package -Because $Output.String
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions tests/pester-tests/commands/failingnested.packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="hasfailingnesteddependency" />
<package id="hasnesteddependency" />
</packages>

0 comments on commit 1731d3e

Please sign in to comment.