Skip to content
This repository has been archived by the owner on Feb 19, 2019. It is now read-only.

write to the correctly scoped chocolateyErrored variable #658

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/chocolatey.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ if ($forceX86) {

$env:chocolateyPackageParameters = $packageParameters

$chocolateyErrored = $false
$script:chocolateyErrored = $false
$badPackages = ''

#todo: This does not catch package names that come later
Expand All @@ -193,7 +193,7 @@ foreach ($packageName in $packageNames) {
}
}
catch {
$chocolateyErrored = $true
$script:chocolateyErrored = $true
Write-Host "$($_.Exception.Message)" -BackgroundColor $ErrorColor -ForegroundColor White ;
if ($badPackages -ne '') { $badPackages += ', '}
$badPackages += "$packageName"
Expand All @@ -208,7 +208,7 @@ if ($badPackages -ne '') {
Write-Host "Command `'$command`' failed (sometimes this indicates a partial failure). Additional info/packages: $badpackages" -BackgroundColor $ErrorColor -ForegroundColor White
}

if ($chocolateyErrored) {
if ($script:chocolateyErrored) {
Write-Debug "Exiting with non-zero exit code."
exit 1
}
Expand Down
4 changes: 2 additions & 2 deletions src/functions/Chocolatey-NuGet.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ Write-Debug "Installing packages to `"$nugetLibPath`"."
Write-Host " "
Write-Host "$installedPackageName v$installedPackageVersion" -ForegroundColor $Note -BackgroundColor Black

if ([System.IO.Directory]::Exists($packageFolder)) {
if (Test-Path $packageFolder) {
try {
Delete-ExistingErrorLog $installedPackageName
Run-ChocolateyPS1 $packageFolder $installedPackageName "install" $installerArguments
Expand All @@ -90,7 +90,7 @@ Write-Debug "Installing packages to `"$nugetLibPath`"."
Write-Error "Package `'$installedPackageName v$installedPackageVersion`' did not install successfully: $($_.Exception.Message)"
if ($badPackages -ne '') { $badPackages += ', '}
$badPackages += "$packageName"
$chocolateyErrored = $true
$script:chocolateyErrored = $true
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions tests/unit/Chocolatey-NuGet.tests.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
$here = Split-Path -Parent $MyInvocation.MyCommand.Definition
$common = Join-Path (Split-Path -Parent $here) '_Common.ps1'
. $common
$chocolateyErrored = $false

Describe "Chocolatey-NuGet" {
Context "under normal circumstances" {
Expand All @@ -17,6 +18,22 @@ Describe "Chocolatey-NuGet" {
}
}

Context "chocolateyPS1 throws an error" {
$nugetLibPath = "c:\fake-lib"
Mock Update-SessionEnvironment
Mock Run-NuGet {"Successfully installed 'somepackage 1.0.0'"} -Verifiable -ParameterFilter {$packageName -eq 'somepackage'}
Mock Run-ChocolateyPS1 { throw "big bad error" }
Mock Test-Path { $true } -ParameterFilter {$path -eq 'c:\fake-lib\somepackage.1.0.0'}
Mock Move-BadInstall
Mock Write-Error

Chocolatey-NuGet 'somepackage'

It "should set chocolateyErrored to true" {
$chocolateyErrored | should be $true
}
}

Context "with packageName 'all'" {
Mock Update-SessionEnvironment
Mock Chocolatey-InstallAll {} -Verifiable -ParameterFilter {$source -eq 'source'}
Expand Down