Skip to content

Commit

Permalink
(chocolatey#3461) Add pester tests for slow dependency
Browse files Browse the repository at this point in the history
This commit adds pester tests and files required for the pester tests.
The tests include ensuring that installing a package that contains a
dependency tree that spans lots of versions does not take unnecessarily
long. As well as duplicating an integration test into the pester tests
to provide us with some belt and suspenders to help us catch issues at
both the integration and the pester test levels.
  • Loading branch information
corbob committed Aug 6, 2024
1 parent f3e5623 commit 56893a2
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2946,6 +2946,10 @@ public void Should_not_have_warning_package_result()
}
}

/// <summary>
/// This test suite tests upgrading isdependency from 1.0.0 while hasdependency is pinned to a version that does not allow
/// for isdependency to upgrade to the latest available in the source.
/// </summary>
public class When_upgrading_a_dependency_with_parent_being_pinned_and_depends_on_a_range_less_than_upgrade_version : ScenariosBase
{
public override void Context()
Expand Down
4 changes: 4 additions & 0 deletions tests/helpers/common-helpers.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ $script:LicenseType = $null
$script:chocolateyTestLocation = $null
$script:originalChocolateyInstall = $env:ChocolateyInstall

# Set the Progress Preference to SilentlyContinue. We do not need progress bars in our tests.
# And they are known to greatly hinder performance on Windows PowerShell.
$global:ProgressPreference = 'SilentlyContinue'

Get-ChildItem -Path $PSScriptRoot\common -Filter *.ps1 -Recurse | ForEach-Object { . $_.FullName }

# Prepare information that will be useful for troubleshooting.
Expand Down
24 changes: 24 additions & 0 deletions tests/pester-tests/commands/choco-install.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2046,6 +2046,30 @@ To install a local, or remote file, you may use:
}
}

Context 'Installing package with large number of dependency versions' {
BeforeAll {
Restore-ChocolateyInstallSnapshot -SetWorkDir

# demo-projects is a zip file that contains 5 packages with over 2000 versions between them.
# They can be freshly generated by running the GeneratePackages.ps1 script inside of it with
# PowerShell 7+. This zip file is placed in the repository as it is expected to not change,
# and makes this test simpler. If this zip file requires regular changes, we would want to
# remove it from the repository and store it elsewhere that we can retrieve it during CI testing.
Expand-Archive $PSScriptRoot/demo-projects.zip -DestinationPath $PWD
$Output = Invoke-Choco install package-a -s 'a,b,c' --confirm
}

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

It "Completes quickly" {
# 30 seconds was chosen for this timeout because the benchmark of Chocolatey was that this would take about 15 seconds.
# Prior to the change that this is testing, this execution would be measured in minutes with the fastest being 7 minutes.
$Output.Duration | Should -BeLessOrEqual '00:00:30' -Because $Output.String
}
}

# 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
41 changes: 41 additions & 0 deletions tests/pester-tests/commands/choco-upgrade.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,47 @@ To upgrade a local, or remote file, you may use:
}
}

# This test is taken from UpgradeScenarios.cs from the integration test suite:
# When_upgrading_a_dependency_with_parent_being_pinned_and_depends_on_a_range_less_than_upgrade_version
# Further details about this test can be found there.
Context 'Upgrading a dependency with pinned parent and depends on a range less than upgrade version' {
BeforeAll {
$DependentPackageName = 'isdependency'
Restore-ChocolateyInstallSnapshot
$Setup = @(
Invoke-Choco install $DependentPackageName --version 1.0.0 --confirm
Invoke-Choco install hasdependency --pin --version 1.0.0 --confirm
)
$Output = Invoke-Choco upgrade $DependentPackageName --confirm
$Packages = (Invoke-Choco list -r).Lines | ConvertFrom-ChocolateyOutput -Command List
}

AfterAll {
Remove-ChocolateyInstallSnapshot
}

It "Exits with Success (0)" {
$Output.ExitCode | Should -Be 0 -Because (@($Setup.String; $Output.String) -join [System.Environment]::NewLine)
}

It "Should report upgraded successfully" {
$Output.Lines | Should -Contain "Chocolatey upgraded 1/1 packages." -Because (@($Setup.String; $Output.String) -join [System.Environment]::NewLine)
}

It "Should report package conflicts" {
$message = "[NuGet] One or more unresolved package dependency constraints detected in the Chocolatey lib folder. All dependency constraints must be resolved to add or update packages. If these packages are being updated this message may be ignored, if not the following error(s) may be blocking the current package operation: 'hasdependency 1.0.0 constraint: isdependency (>= 1.0.0 && < 2.0.0)'"
$Output.Lines | Should -Contain $message -Because (@($Setup.String; $Output.String) -join [System.Environment]::NewLine)
}

It "Should upgrade the dependent package to the highest version in the range" {
($Packages | Where-Object Name -eq $DependentPackageName).Version | Should -Be '1.1.0' -Because (@($Setup.String; $Output.String) -join [System.Environment]::NewLine)
}

It "Should not upgrade the exact version dependency package" {
($Packages | Where-Object Name -eq 'isexactversiondependency').Version | Should -Be '1.0.0' -Because (@($Setup.String; $Output.String) -join [System.Environment]::NewLine)
}
}

# This needs to be (almost) 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
Binary file added tests/pester-tests/commands/demo-projects.zip
Binary file not shown.

0 comments on commit 56893a2

Please sign in to comment.