Skip to content

Commit

Permalink
Merge pull request #3113 from blami/fix-2345
Browse files Browse the repository at this point in the history
(#2345) Avoid any trailing spaces after silentArgs
  • Loading branch information
gep13 authored May 23, 2024
2 parents a741814 + e3bd4e7 commit 872fe03
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 18 deletions.
4 changes: 2 additions & 2 deletions Invoke-Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Requires -Module @{ ModuleName = 'pester'; ModuleVersion = '5.3.1' }
#Requires -Module @{ ModuleName = 'pester'; ModuleVersion = '5.3.1' }
#Requires -RunAsAdministrator
<#
.SYNOPSIS
Expand Down Expand Up @@ -50,7 +50,7 @@ else {
if (-not (Test-Path "$TestPath/packages") -or -not $SkipPackaging) {
$null = New-Item -Path "$TestPath/packages" -ItemType Directory -Force
# Get and pack packages
$nuspecs = Get-ChildItem -Path $PSScriptRoot/src/chocolatey.tests.integration, $PSScriptRoot/tests/packages -Recurse -Include *.nuspec
$nuspecs = Get-ChildItem -Path $PSScriptRoot/src/chocolatey.tests.integration, $PSScriptRoot/tests/packages -Recurse -Include *.nuspec | Where-Object FullName -notmatch 'bin'
Get-ChildItem -Path $PSScriptRoot/tests/packages -Recurse -Include *.nupkg | Copy-Item -Destination "$TestPath/packages"

foreach ($file in $nuspecs) {
Expand Down
1 change: 1 addition & 0 deletions nuspec/chocolatey/chocolatey/tools/chocolateysetup.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ function Remove-ShimWithAuthenticodeSignature {
$possibleSignatures = @(
'RealDimensions Software, LLC'
'Chocolatey Software, Inc\.'
'Chocolatey Software, Inc'
)

$possibleSignatures | ForEach-Object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,45 +336,47 @@ Pro / Business supports a single, ubiquitous install directory option.
$msiArgs = "/i `"$fileFullPath`""
$msiArgs = if ($overrideArguments) {
Write-Host "Overriding package arguments with '$additionalInstallArgs' (replacing '$silentArgs')"
"$msiArgs $additionalInstallArgs"
(($msiArgs, $additionalInstallArgs) | Where-Object { $_ }) -join " "
}
else {
"$msiArgs $silentArgs $additionalInstallArgs"
(($msiArgs, $silentArgs, $additionalInstallArgs) | Where-Object { $_ }) -join " "
}

$env:ChocolateyExitCode = Start-ChocolateyProcessAsAdmin "$msiArgs" "$($env:SystemRoot)\System32\msiexec.exe" -validExitCodes $validExitCodes -workingDirectory $workingDirectory
}

if ($fileType -like 'msp') {
$msiArgs = '/update "{0}"' -f $fileFullPath
if ($overrideArguments) {
$msiArgs = if ($overrideArguments) {
Write-Host "Overriding package arguments with '$additionalInstallArgs' (replacing '$silentArgs')";
$msiArgs = "$msiArgs $additionalInstallArgs";
(($msiArgs, $additionalInstallArgs) | Where-Object { $_ }) -join " "
}
else {
$msiArgs = "$msiArgs $silentArgs $additionalInstallArgs";
(($msiArgs, $silentArgs, $additionalInstallArgs) | Where-Object { $_ }) -join " "
}

$env:ChocolateyExitCode = Start-ChocolateyProcessAsAdmin "$msiArgs" "$($env:SystemRoot)\System32\msiexec.exe" -validExitCodes $validExitCodes -workingDirectory $workingDirectory
}

if ($fileType -like 'exe') {
if ($overrideArguments) {
$exeArgs = if ($overrideArguments) {
Write-Host "Overriding package arguments with '$additionalInstallArgs' (replacing '$silentArgs')";
$env:ChocolateyExitCode = Start-ChocolateyProcessAsAdmin "$additionalInstallArgs" $fileFullPath -validExitCodes $validExitCodes -workingDirectory $workingDirectory
$additionalInstallArgs
}
else {
$env:ChocolateyExitCode = Start-ChocolateyProcessAsAdmin "$silentArgs $additionalInstallArgs" $fileFullPath -validExitCodes $validExitCodes -workingDirectory $workingDirectory
(($silentArgs, $additionalInstallArgs) | Where-Object { $_ }) -join " "
}
$env:ChocolateyExitCode = Start-ChocolateyProcessAsAdmin "$exeArgs" $fileFullPath -validExitCodes $validExitCodes -workingDirectory $workingDirectory
}

if ($fileType -like 'msu') {
if ($overrideArguments) {
$msuArgs = "`"$fileFullPath`""
$msuArgs = if ($overrideArguments) {
Write-Host "Overriding package arguments with '$additionalInstallArgs' (replacing '$silentArgs')";
$msuArgs = "`"$fileFullPath`" $additionalInstallArgs"
(($msuArgs, $additionalInstallArgs) | Where-Object { $_ }) -join " "
}
else {
$msuArgs = "`"$fileFullPath`" $silentArgs $additionalInstallArgs"
(($msuArgs, $silentArgs, $additionalInstallArgs) | Where-Object { $_ }) -join " "
}
$env:ChocolateyExitCode = Start-ChocolateyProcessAsAdmin "$msuArgs" "$($env:SystemRoot)\System32\wusa.exe" -validExitCodes $validExitCodes -workingDirectory $workingDirectory
}
Expand Down
7 changes: 6 additions & 1 deletion tests/Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

Vagrant.configure("2") do |config|
config.vm.box = "StefanScherer/windows_2022"
# Set Hostname to prevent error messages. https://github.com/hashicorp/vagrant/issues/12644
config.vm.hostname = 'chocolatey-tests'
config.vm.boot_timeout = 600

# windows
Expand Down Expand Up @@ -54,7 +56,7 @@ Vagrant.configure("2") do |config|
$null = robocopy c:/chocoRoot c:/code/choco /e /copy:dt /purge
Push-Location c:/code/choco
Write-Host "($(Get-Date -Format "dddd MM/dd/yyyy HH:mm:ss K")) Files have been copied, beginning build process."
$CakeOutput = ./build.bat 2>&1
$CakeOutput = ./build.bat --verbosity=diagnostic --target=CI --testExecutionType=none --shouldRunalyze=false --shouldRunNuGet=false 2>&1
if ($LastExitCode -ne 0) {
Set-Content c:/vagrant/buildOutput.log -Value $CakeOutput
Expand All @@ -69,6 +71,9 @@ Vagrant.configure("2") do |config|
Remove-Item "$env:TEMP/chocolateyTests/packages" -Recurse -Force -ErrorAction SilentlyContinue
SHELL
config.vm.provision "shell", name: "test", inline: <<-SHELL
# Copy changed files.
$null = robocopy c:/chocoRoot c:/code/choco /e /copy:dt
# Purge any tests files that have been removed.
$null = robocopy c:/chocoRoot/tests c:/code/choco/tests /e /copy:dt /purge
Write-Host "($(Get-Date -Format "dddd MM/dd/yyyy HH:mm:ss K")) Starting Test Execution"
Push-Location c:/code/choco
Expand Down
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>
12 changes: 9 additions & 3 deletions tests/pester-tests/chocolatey.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,20 @@ Describe "Ensuring Chocolatey is correctly installed" -Tag Environment, Chocolat
# This is skipped when not run in CI because it requires signed executables.
Context "File signing (<_.FullName>)" -ForEach @($PowerShellFiles; $ExecutableFiles; $StrongNamingKeyFiles) -Skip:((-not $env:TEST_KITCHEN) -or (-not (Test-ChocolateyVersionEqualOrHigherThan "1.0.0"))) {
BeforeAll {
# Due to changes in the signing setup, the certificate used to sign PS1 files and the Chocolatey CLI executable MIGHT be different. This ensures that the both certificates are trusted.
$FileUnderTest = $_
$SignerCert = (Get-AuthenticodeSignature (Get-ChocoPath)).SignerCertificate
$Ps1Cert = (Get-AuthenticodeSignature (Join-Path (Split-Path (Split-Path (Get-ChocoPath))) 'helpers/chocolateyScriptRunner.ps1')).SignerCertificate
$ExeCert = (Get-AuthenticodeSignature (Get-ChocoPath)).SignerCertificate
$Cert = "$PWD\cert.cer"
# Write out the certificate
[IO.File]::WriteAllBytes($Cert, $SignerCert.export([security.cryptography.x509certificates.x509contenttype]::cert))
# Write out the exe certificate
[IO.File]::WriteAllBytes($Cert, $ExeCert.export([security.cryptography.x509certificates.x509contenttype]::cert))
# Trust the certificate
Import-Certificate -FilePath $Cert -CertStoreLocation 'Cert:\CurrentUser\TrustedPublisher\'
Remove-Item -Path $Cert -Force -ErrorAction Ignore
# Write out the ps1 certificate
[IO.File]::WriteAllBytes($Cert, $Ps1Cert.export([security.cryptography.x509certificates.x509contenttype]::cert))
# Trust the certificate
Import-Certificate -FilePath $Cert -CertStoreLocation 'Cert:\CurrentUser\TrustedPublisher\'
}

AfterAll {
Expand Down
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")
}
}
2 changes: 1 addition & 1 deletion tests/pester-tests/commands/choco-upgrade.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ To upgrade a local, or remote file, you may use:
}
}

Context 'Upgrading a package where parent only contain pre-releases' -Tag Testing {
Context 'Upgrading a package where parent only contain pre-releases' {
BeforeAll {
Restore-ChocolateyInstallSnapshot

Expand Down

0 comments on commit 872fe03

Please # to comment.