(GH-61) Validate exit code when installing package#168
(GH-61) Validate exit code when installing package#168orck-adrouin wants to merge 1 commit intochocolatey:developmentfrom
Conversation
Changes cChocoPackagesInstall to validate choco's exit code. Handles the standard exit codes as documented in Chocolatey's documentation. Fixes issue chocolatey#61 Supersedes chocolatey#103
| $p.Dispose() | ||
|
|
||
| #Set $LASTEXITCODE variable. | ||
| powershell.exe -NoLogo -NoProfile -Noninteractive "exit $exitcode" |
There was a problem hiding this comment.
Is there a reason to instantiate a powershell.exe instance instead of just setting $LastExitCode directly?
There was a problem hiding this comment.
This PR is basically a "port" of the old PR #103 from 2017 to the latest code base so I cannot tell you exactly why they did it that way.
The only reason I can see to do it that way is to ensure that the real $LastExitCode is set. Setting $LastExitCode directly would create a variable named $LastExitCode which is scoped to the function Invoke-ChocoProcess and it would not set the global $LastExitCode variable.
You can reproduce the behavior with this code
function Test {
$LASTEXITCODE = 3 # this will create a variable scoped to the function and will not impact its parent scope
# you can uncomment this line to set the global $LastExitCode
# powershell.exe -NoLogo -NoProfile -Noninteractive "exit 3"
}
#fake a lastexitcode
powershell.exe -NoLogo -NoProfile -Noninteractive "exit 123"
write-Host "Value before Test function: $LASTEXITCODE" # will print 123 for the exit code (set by the previous line)
Test
write-Host "Value after Test function: $LASTEXITCODE" # value stays at 123 because the exit code variable in the Test function is scope to that function| } | ||
| } | ||
|
|
||
| Context -Name "Package cannot be found" -Fixture { |
There was a problem hiding this comment.
All of these tests added will likely require adjustments when #163 is merged due to the upgrade to Pester 5. (Not suggesting anything needs to be done around this at this particular moment. Just to keep in mind.)
| } | ||
| } | ||
|
|
||
| function Invoke-ChocoProcess { |
There was a problem hiding this comment.
Is there any particular reason to use this method to create the Chocolatey process instead of & or Start-Process? Or even the Invoke-Expression that was originally being used?
There was a problem hiding this comment.
From what I remember from 13 months ago, I had some issues getting a reliable last exit code with Invoke-Expression so I used to the code from the very old and possibly abandoned PR #103 which address the same issue.
Description
Changes cChocoPackagesInstall to validate choco's exit code. Handles the standard exit codes as documented in Chocolatey's documentation.
This PR is based on pullrequest #103 that has not seen any updates in the last 2 years. Instead of rebasing the PR I cloned the cChoco repo and I copied over (and improved) the changes from the original author.
Related Issue
Fixes #61
Motivation and Context
It allows a DSC to report a failure when Chocolatey fails to install a package.
How Has This Been Tested?
I used the DSC that was provided in issue #61 to simulate an installation failure. Unfortunately, I was not able to find Chocolatey packages to test the different exit codes outlined in this document: https://docs.chocolatey.org/en-us/choco/commands/install#exit-codes .
Types of changes
Checklist: