Skip to content

Commit

Permalink
(chocolatey#2345) Add Pester tests to validate fix
Browse files Browse the repository at this point in the history
This adds a set of tests to validate that the spaces that may be passed
in by the package are honoured, while not introducing erroneous spaces.
  • Loading branch information
corbob committed May 7, 2024
1 parent d1dc928 commit a1b036a
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# This package purposely uses files that don't exists.
# As such, we need to continue on error.
$ErrorActionPreference = 'SilentlyContinue'
$toolsDir = "$(Split-Path -parent $MyInvocation.MyCommand.Definition)"
$fileLocation = "$toolsDir\ConsoleApp1"
$exeArgs = @{
packageName = $env:ChocolateyPackageName
silentArgs = "/exe "
fileType = 'exe'
file = "$fileLocation.exe"
}
$msiArgs = @{
packageName = $env:ChocolateyPackageName
silentArgs = "/qn "
# msiexec will exit with 1619 when the file doesn't exist.
validExitCodes = @(1619)
fileType = 'msi'
file = "$fileLocation.msi"
}
$msuArgs = @{
packageName = $env:ChocolateyPackageName
silentArgs = "/quiet "
# wusa will exit with 2 when the file doesn't exist.
validExitCodes = @(2)
fileType = 'msu'
file = "$fileLocation.msu"
}

# Since the exe doesn't exist, Install-ChocolateyInstallPackage is expected to
# throw an exception. We don't care if it throws, we care that it outputs the
# correct parameters.
try {
Install-ChocolateyInstallPackage @exeArgs
} catch {}
Install-ChocolateyInstallPackage @msiArgs
Install-ChocolateyInstallPackage @msuArgs

# Set the chocolateyInstallArgument environment variable to mimic Chocolatey
# setting this variable.
$env:chocolateyInstallArguments = '/norestart '
try {
Install-ChocolateyInstallPackage @exeArgs
} catch {}
Install-ChocolateyInstallPackage @msiArgs
Install-ChocolateyInstallPackage @msuArgs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Do not remove this test for UTF-8: if “Ω” doesn’t appear as greek uppercase omega letter enclosed in quotation marks, you should use an editor that supports UTF-8, not this one. -->
<package xmlns="http://schemas.microsoft.com/packaging/2015/06/nuspec.xsd">
<metadata>
<id>install-chocolateyinstallpackage-tests</id>
<authors>Chocolatey Software</authors>
<version>1.0</version>
<title>Install-ChocolateyInstallPackage tests</title>
<summary>Package for the testing of Install-ChocolateyInstallPackage</summary>
<description>Package for the testing of Install-ChocolateyInstallPackage. Used by Pester to ensure extra spaces are not added to command lines.</description>
</metadata>
<files>
<file src="chocolateyInstall.ps1" target="tools/chocolateyInstall.ps1" />
</files>
</package>
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
Describe 'Testing Install-ChocolateyInstallPackage' {
BeforeDiscovery {
# These tests are explicitly testing some scenarios that are not immediately
# obvious. The test package sets 'SilentArguments' to a flag with a trailing space.
# This is being tested both with and without Chocolatey adding additional
# arguments. The package also sets the ChocolateyAdditionalArguments
# environment variable to a string also with a trailing space.
# Using exe as an example, that means the arguments passed are expected to be `/exe ` and then `/exe /norestart ` (Note the extra spaces in both instances).
$ExpectedOutput = @(
@{
OutputWithoutAdditionalArguments = '\["C:\\.*\\lib\\install-chocolateyinstallpackage-tests\\tools\\ConsoleApp1.exe" /exe \]'
OutputWithAdditionalArguments = '\["C:\\.*\\lib\\install-chocolateyinstallpackage-tests\\tools\\ConsoleApp1.exe" /exe /norestart \]'
Type = 'exe'
}
@{
OutputWithoutAdditionalArguments = '\["C:\\Windows\\System32\\msiexec.exe" /i "C:\\.*\\lib\\install-chocolateyinstallpackage-tests\\tools\\ConsoleApp1.msi" /qn \]'
OutputWithAdditionalArguments = '\["C:\\Windows\\System32\\msiexec.exe" /i "C:\\.*\\lib\\install-chocolateyinstallpackage-tests\\tools\\ConsoleApp1.msi" /qn /norestart \]'
Type = 'msi'
}
@{
OutputWithoutAdditionalArguments = '\["C:\\Windows\\System32\\wusa.exe" "C:\\.*\\lib\\install-chocolateyinstallpackage-tests\\tools\\ConsoleApp1.msu" /quiet \]'
OutputWithAdditionalArguments = '\["C:\\Windows\\System32\\wusa.exe" "C:\\.*\\lib\\install-chocolateyinstallpackage-tests\\tools\\ConsoleApp1.msu" /quiet /norestart \]'
Type = 'msu'
}
)
}

BeforeAll {
Initialize-ChocolateyTestInstall
$PackageUnderTest = 'install-chocolateyinstallpackage-tests'
Restore-ChocolateyInstallSnapshot
$Output = Invoke-Choco install $PackageUnderTest --confirm --debug
}

AfterAll {
Remove-ChocolateyInstallSnapshot
}

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

It 'Output is accurate for installer type <Type>' -ForEach $ExpectedOutput {
$Output.String | Should -MatchExactly $OutputWithoutAdditionalArguments -Because ($Output.Lines | Select-String "ConsoleApp1.$Type")
$Output.String | Should -MatchExactly $OutputWithAdditionalArguments -Because ($Output.Lines | Select-String "ConsoleApp1.$Type")
}
}

0 comments on commit a1b036a

Please sign in to comment.