Functions/Environment.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
InModuleScope -ModuleName Pester {
    Describe 'Get-PowerShellVersion' {
        Mock Get-Variable
        It 'Returns value of $PSVersionTable.PsVersion.Major' {
            Mock Get-Variable -ParameterFilter { $Name -eq 'PSVersionTable' -and $ValueOnly } -MockWIth {
                @{ PSVersion = [Version]'1.0.0' }
            }

            Get-PowerShellVersion | Should -Be 1
        }
    }

    Describe "Get-OperatingSystem" {
        Mock Get-Variable
        Context "Windows with PowerShell 5 and lower" {
            It "Returns 'Windows' when PowerShell version is lower than 6" {
                Mock Get-PowerShellVersion { 5 }

                Get-OperatingSystem | Should -Be 'Windows'
            }
        }

        Context "Windows with PowerShell 6 and higher" {
            Mock Get-Variable -ParameterFilter { $Name -eq 'IsWindows' -and $ValueOnly } -MockWith { $true }
            Mock Get-Variable -ParameterFilter { $Name -eq 'IsLinux' -and $ValueOnly } -MockWith { $false }
            Mock Get-Variable -ParameterFilter { $Name -eq 'IsOSX' -and $ValueOnly } -MockWith { $false }
            Mock Get-PowerShellVersion { 6 }

            $os = Get-OperatingSystem
            It "Returns 'Windows' when `$IsWindows is `$true and powershell version is 6 or higher" {
                $os | Should -Be 'Windows'
            }

            It "Uses Get-Variable to retreive IsWindows" {
                # IsWindows is a constant and cannot be overwritten, so check that we are using
                # Get-Variable to access its value, which allows us to mock it easily without
                # depending on the OS

                Assert-MockCalled Get-Variable -ParameterFilter { $Name -eq 'IsWindows' -and ($ValueOnly) } -Exactly 1
            }
        }

        Context "Linux with PowerShell 6 and higher" {
            Mock Get-Variable -ParameterFilter { $Name -eq 'IsWindows' -and $ValueOnly } -MockWith { $false }
            Mock Get-Variable -ParameterFilter { $Name -eq 'IsLinux' -and $ValueOnly } -MockWith { $true }
            Mock Get-Variable -ParameterFilter { $Name -eq 'IsOSX' -and $ValueOnly } -MockWith { $false }
            Mock Get-PowerShellVersion { 6 }

            $os = Get-OperatingSystem
            It "Returns 'Linux' when `$IsLinux is `$true and powershell version is 6 or higher" {
                $os | Should -Be 'Linux'
            }

            It "Uses Get-Variable to retreive IsLinux" {
                Assert-MockCalled Get-Variable -ParameterFilter { $Name -eq 'IsLinux' -and $ValueOnly } -Exactly 1
            }
        }

        Context "OSx with PowerShell 6 and higher" {
            Mock Get-Variable -ParameterFilter { $Name -eq 'IsWindows' -and $ValueOnly } -MockWith { $false }
            Mock Get-Variable -ParameterFilter { $Name -eq 'IsLinux' -and $ValueOnly } -MockWith { $false }
            Mock Get-Variable -ParameterFilter { $Name -eq 'IsOSX' -and $ValueOnly } -MockWith { $true }
            Mock Get-PowerShellVersion { 6 }

            $os = Get-OperatingSystem
            It "Returns 'OSX' when `$IsOSX is `$true and powershell version is 6 or higher" {
                $os | Should -Be 'OSX'
            }

            It "Uses Get-Variable to retreive IsOSX" {
                Assert-MockCalled Get-Variable -ParameterFilter { $Name -eq 'IsOSX' -and $ValueOnly } -Exactly 1
            }
        }
    }


    Describe 'Get-TempDirectory' {
        It 'returns the correct temp directory for Windows' {
            Mock 'Get-OperatingSystem' {
                'Windows'
            }
            $expected = $env:TEMP = "C:\temp"

            $temp = Get-TempDirectory
            $temp | Should -Not -BeNullOrEmpty
            $temp | Should -Be $expected
        }

        It "returns '/tmp' directory for MacOS" {
            Mock 'Get-OperatingSystem' {
                'MacOS'
            }
            Get-TempDirectory | Should -Be '/tmp'
        }

        It "returns '/tmp' directory for Linux" {
            Mock 'Get-OperatingSystem' {
                'Linux'
            }
            Get-TempDirectory | Should -Be '/tmp'
        }
    }
}