Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

LeaDevelop
Copy link
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
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
}

…ipt for cases when chocolatey is not installed
@LeaDevelop LeaDevelop marked this pull request as ready for review October 28, 2024 20:54
…ipt for cases when chocolatey is not installed
…ent-for-error-action' into (chocolatey#1039)-minor-improvement-for-error-action
@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
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 😓

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
1 participant