tests/Unit/Public/Install-ChocolateySoftware.tests.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
InModuleScope Chocolatey {
    Describe Install-ChocolateySoftware {

        Mock Get-RemoteFile -MockWith {}
        Mock Get-RemoteString -MockWith {
            "<result><feed><entry><content><src>https://chocolatey.org/api/v2/packages/chocolatey/0.10.8/</src></content></entry></feed></result>"
        }
        Mock Copy-Item -MockWith {}
        Mock Expand-Archive -MockWith {}
        Mock New-Item -MockWith {}
        Mock Repair-PowerShellOutputRedirectionBug {}
        Mock Join-Path -ParameterFilter {$ChildPath -eq 'chocolateyInstall.ps1'} -MockWith { {$true} }

        Context 'Default' {
            #Need to be first before Get-RemoteString has been called
            It 'Ensure Get-RemoteString is NOT called when install from Package URL' {
                $null = Install-ChocolateySoftware -ChocolateyPackageUrl 'https://chocolatey.org/api/v2/package/chocolatey/0.10.8/'
                { Assert-MockCalled Get-RemoteString } | Should -Throw
            }

            It 'Ensure Get-RemoteString is called when install from Feed without version' {
                $null = Install-ChocolateySoftware
                { Assert-MockCalled Get-RemoteString } | Should -Not -Throw
            }

            It 'Ensure Get-RemoteFile is called' {
                $null = Install-ChocolateySoftware
                { Assert-MockCalled Get-RemoteFile } | Should -Not -Throw
            }

            if($PSVersionTable.PSVersion.Major -ge 5) {
                It 'Ensure Expand-Archive is called' {
                    $null = Install-ChocolateySoftware
                    { Assert-MockCalled Expand-Archive } | Should -Not -Throw
                }
            }
        }
    }
}