Skip to content

Instantly share code, notes, and snippets.

@mwallner
Created October 19, 2019 09:12
Show Gist options
  • Save mwallner/e6249ccba22967df051de6f152cfd79b to your computer and use it in GitHub Desktop.
Save mwallner/e6249ccba22967df051de6f152cfd79b to your computer and use it in GitHub Desktop.
Boxstarter-Chocolatey-Param-Troubleshooting

I modified function 'choco' in Chocolatey.ps1 to include some additional logging:

function choco {
<#
.SYNOPSIS
Intercepts Chocolatey call to check for reboots

#>
    param(
        [string]$command,
        [string[]]$packageNames=@('')
    )
    Write-BoxstarterMessage "'choco' wrapper: command: '$command' packageNames: $($packageNames | % { "$($_); " })" -Verbose
    Write-BoxstarterMessage "'choco' wrapper: PSBoundParameters: $($PSBoundParameters.Keys | % { "$($_) = $($PSBoundParameters[$_]); " })" -Verbose
    Write-BoxstarterMessage "'choco' wrapper: args: $($args | % { "$($_); " })" -Verbose
    chocolatey @PSBoundParameters @args
}

I created an additional unit-test in Chocolatey.tests.p1 to check if parameters are passed in the correct order ..

Describe "Call-Chocolatey" {
    Mock Invoke-Reboot
    Mock Test-PendingReboot {return $false}
context "package parameters" {
        $script:passedArgs = ""
        $global:VerbosePreference="Continue"
        Mock Invoke-LocalChocolatey { $script:passedArgs = $chocoArgs }

        choco Install -y pkg --source blah --installargs "ADD_CMAKE_TO_PATH=System"
        $passedArgs | Should Be $null

        it "passes expected params" {
            $passedArgs.count | Should Be 8 # actually 7 + 1 "-y" is always appended
        }
        it "passes all parameters in correct order" {
            $passedArgs[0] | Should Be "Install"
            $passedArgs[1] | Should Be "-y"
            $passedArgs[2] | Should Be "pkg"
            $passedArgs[3] | Should Be "--source"
            $passedArgs[4] | Should Be "blah"
            $passedArgs[5] | Should Be "--installargs"
            $passedArgs[6] | Should Be "ADD_CMAKE_TO_PATH=System"
            $passedArgs[7] | Should Be "-y" # mw: why?
        }
    }
}

I would expect the parameters to be passed in the correct order, but somehow --source gets bound as packageNames here is the output of the unittest:

Context package parameters
AUSFÜHRLICH: Boxstarter: 'choco' wrapper: command: 'Install' packageNames: --source; 
AUSFÜHRLICH: Boxstarter: 'choco' wrapper: PSBoundParameters: command = Install;  packageNames = --source; 
AUSFÜHRLICH: Boxstarter: 'choco' wrapper: args: -y;  pkg;  blah;  --installargs;  ADD_CMAKE_TO_PATH=System; 
...
AUSFÜHRLICH: Boxstarter: Passing the following args to Chocolatey: Install --source -y pkg blah --installargs ADD_CMAKE_TO_PATH=System -Verbose -y

how come the parameter order is swapped when function 'choco' is being called?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment