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 6, 2024
1 parent e485943 commit 27b8874
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# 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 "
validExitCodes = @(2,1619)
fileType = 'exe'
file = "$fileLocation.exe"
}
$msiArgs = @{
packageName = $env:ChocolateyPackageName
silentArgs = "/qn "
validExitCodes = @(2,1619)
fileType = 'msi'
file = "$fileLocation.msi"
}
$msuArgs = @{
packageName = $env:ChocolateyPackageName
silentArgs = "/quiet "
validExitCodes = @(2,1619)
fileType = 'msu'
file = "$fileLocation.msu"
}


try {
Install-ChocolateyInstallPackage @exeArgs
} catch {}
Install-ChocolateyInstallPackage @msiArgs
Install-ChocolateyInstallPackage @msuArgs
$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,41 @@
Describe 'Testing Install-ChocolateyInstallPackage' -Tag Testing {
BeforeDiscovery {
$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 27b8874

Please sign in to comment.