Skip to content

(#1039) Improved flow for cases when chocolatey is not installed#1092

Merged
corbob merged 1 commit intochocolatey:masterfrom
LeaDevelop:(#1039)-minor-improvement-for-error-action
Jan 26, 2026
Merged

(#1039) Improved flow for cases when chocolatey is not installed#1092
corbob merged 1 commit intochocolatey:masterfrom
LeaDevelop:(#1039)-minor-improvement-for-error-action

Conversation

@LeaDevelop
Copy link
Copy Markdown
Contributor

@LeaDevelop LeaDevelop commented Oct 28, 2024

Description Of Changes

The change has following affects:

  • Proper error propagation
  • Error messages are captured in logs
  • Failed state is correctly reported
  • Error handling mechanisms work as expected
  • Doesn't break the automation tool's PowerShell session

Motivation and Context

With this correction script will stop and not attempt to install the chocolatey server.
Automated approach will recognize error and for manual options user is able to spot error and logs as PowerShell's window remains open.

Testing

  • I have previewed these changes using the Docker Container or another method before submitting this pull request.

Change Types Made

  • Minor documentation fix (typos etc.).
  • Major documentation change (refactoring, reformatting or adding documentation to existing page).
    • It's a minor script improvement, wasn't sure where to put it so I chose this option
  • New documentation page added.
  • The change I have made should have a video added, and I have raised an issue for this.
    • Issue #

Change Checklist

  • Requires a change to menu structure (top or left-hand side)/
  • Menu structure has been updated

Related Issue

Fixes #1039

@LeaDevelop
Copy link
Copy Markdown
Contributor Author

Test

Describe 'Test case when Chocolatey is not installed' {
    BeforeEach {
        # Function we're testing
        function Test-ChocolateyAvailability {
            if ($null -eq (Get-Command -Name 'choco.exe' -ErrorAction SilentlyContinue)) {
                Write-Error "Chocolatey not installed. Cannot install standard packages." -ErrorAction Stop
            }
            return $true
        }

        # Mock Get-Command to simulate Chocolatey not being installed
        Mock Get-Command { return $null } -ParameterFilter { $Name -eq 'choco.exe' }
    }

    It 'Throws an error when Chocolatey is not found' {
        { Test-ChocolateyAvailability } | Should -Throw
    }

    It 'Throws the correct error message' {
        $expectedMessage = "Chocolatey not installed. Cannot install standard packages."
        { Test-ChocolateyAvailability } | Should -Throw $expectedMessage
    }

    It 'Should call Get-Command with correct parameters' {
        try {
            Test-ChocolateyAvailability
        } catch {
            # Ignore the expected error
        }
        Should -Invoke Get-Command -Times 1 -ParameterFilter { 
            $Name -eq 'choco.exe' -and 
            $ErrorAction -eq 'SilentlyContinue' 
        }
    }
}

Test result - Detailed Pester Test Results - Chocolatey Availability Tests

Test Summary

  • Total Tests: 3
  • Passed: ✅ 3
  • Failed: ❌ 0
  • Skipped: ⚠️ 0
  • Duration: 00:00:00.0820122

Test Environment

  • PowerShell Version: 7.4.5
  • Pester Version: 5.6.1
  • OS: Microsoft Windows NT 10.0.19045.0

Test Configuration

Mock Get-Command returns: null
ParameterFilter: Name -eq 'choco.exe'
ErrorAction: SilentlyContinue

Detailed Test Results

1️⃣ Test: Throws an error when Chocolatey is not found

  • Result: ✅ Passed
  • Duration: 00:00:00.0122264
  • Test Block:
{ Test-ChocolateyAvailability } | Should -Throw

2️⃣ Test: Throws the correct error message

  • Result: ✅ Passed
  • Duration: 00:00:00.0055697
  • Test Block:
$expectedMessage = 'Chocolatey not installed. Cannot install standard packages.'
{ Test-ChocolateyAvailability } | Should -Throw $expectedMessage

3️⃣ Test: Should call Get-Command with correct parameters

  • Result: ✅ Passed
  • Duration: 00:00:00.0421638
  • Test Block:
Should -Invoke Get-Command -Times 1 -ParameterFilter { $Name -eq 'choco.exe' -and $ErrorAction -eq 'SilentlyContinue' }

Function Being Tested

function Test-ChocolateyAvailability {
    if ($null -eq (Get-Command -Name 'choco.exe' -ErrorAction SilentlyContinue)) {
        Write-Error "Chocolatey not installed. Cannot install standard packages." -ErrorAction Stop
    }
    return $true
}

@LeaDevelop LeaDevelop marked this pull request as ready for review October 28, 2024 20:54
@LeaDevelop LeaDevelop changed the title (#1028) Improved flow for cases when chocolatey is not installed (#1039) Improved flow for cases when chocolatey is not installed Oct 28, 2024
@LeaDevelop
Copy link
Copy Markdown
Contributor Author

@pauby I took liberty to just prep this one. I had more than 2 commits, cos I forgot to update issue's ID number in commit message 😓

…ipt for cases when chocolatey is not installed
@corbob corbob force-pushed the (#1039)-minor-improvement-for-error-action branch from b4c2ea5 to 9d25a01 Compare January 26, 2026 18:38
@corbob
Copy link
Copy Markdown
Member

corbob commented Jan 26, 2026

pauby I took liberty to just prep this one. I had more than 2 commits, cos I forgot to update issue's ID number in commit message 😓

@LeaDevelop Thank you for this contribution. I took the liberty of rebasing it all off of the current master branch and dropping from it the commits with the wrong message. As well as the commit for the chocolatey.server page which no longer has these changes to it.

If you're interested, I'm more than happy to expand on what I did to change the commit messages.

Copy link
Copy Markdown
Member

@corbob corbob left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@corbob corbob merged commit 55e78c4 into chocolatey:master Jan 26, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PowerShell consol is terminated if choco.exe isn't found

2 participants