powershell-devops.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#Requires -Version 7
#Requires -Module @{ModuleName='Pester';ModuleVersion='5.2.0'}

Describe 'powershell-devops.psm1' {
    BeforeAll {
        Import-Module $PSCommandPath.Replace('.tests.ps1','.psm1')
        $env:TF_BUILD=$null
        $env:GITHUB_ACTIONS=$null
    }

    BeforeEach {
        $env:ENV_VARIABLE=$null
    }

    Context 'Test-AdoPipeline' {
        It 'when TF_BUILD is not defined should return false' {
            Test-AdoPipeline | Should -Be $false
        }
    }

    Context 'Test-GitHubWorkflow' {
        It 'when GITHUB_ACTIONS is not defined should return false' {
            Test-GitHubWorkflow | Should -Be $false
        }
    }

    Context 'Set-EnvironmentVariable' {
        It 'when Set-EnvironmentVariable then the environment variable should be set' {
            Set-EnvironmentVariable ENV_VARIABLE VALUE
            $env:ENV_VARIABLE | Should -Be VALUE
        }
    }

    Context 'Get-EnvironmentVariable' {
        It 'when an environment variable is defined then the value should be returned' {
            $env:ENV_VARIABLE = 'VALUE'
            Get-EnvironmentVariable ENV_VARIABLE | Should -Be VALUE
        }

        It 'when an environment variable not defined and -Require is true then should throw' {
            { Get-EnvironmentVariable ENV_VARIABLE -Require } | Should -Throw
        }
    }

    Context 'Azure DevOps' {
        BeforeEach {
            $env:TF_BUILD=1
        }

        AfterEach {
            $env:TF_BUILD=$null
        }

        Context 'Test-AdoPipeline' {
            It 'when TF_BUILD is defined should return true' {
                Test-AdoPipeline | Should -Be $true
            }
        }

        Context 'Set-EnvironmentVariable' {
            It 'should write command ##vso[task.setvariable]' {
                Set-EnvironmentVariable ENV_VARIABLE VALUE *>&1 | Should -BeLike '##vso`[task.setvariable *'
            }

            It 'with -Secret then should write command ##vso[task.setvariable issecret=true`]' {
                Set-EnvironmentVariable ENV_VARIABLE VALUE -Secret *>&1 | Should -BeLike '##vso`[task.setvariable *;issecret=true;*'
            }

            It 'with -Output then should write command ##vso[task.setvariable isoutput=true`]' {
                Set-EnvironmentVariable ENV_VARIABLE VALUE -Output *>&1 | Should -BeLike '##vso`[task.setvariable *;isoutput=true;*'
            }
        }

        Context 'Enter-Group' {
            It 'should write command ##[group]' {
                Enter-Group GROUP *>&1 | Should -BeLike '##`[group`]*'
            }
        }

        Context 'Exit-Group' {
            It 'should write command ##[endgroup]' {
                Exit-Group *>&1 | Should -BeLike '##`[endgroup`]*'
            }
        }

        Context 'Add-Path' {
            It 'should write command ##vso[task.prependpath]' {
                Add-Path /path *>&1 | Should -BeLike '##vso`[task.prependpath`]*'
            }
        }
    }

    Context 'GitHub' {
        BeforeEach {
            $env:GITHUB_ACTIONS=1
            $env:GITHUB_ENV = [System.IO.Path]::GetTempFileName()
            $env:GITHUB_PATH = [System.IO.Path]::GetTempFileName()
        }

        AfterEach {
            $env:GITHUB_ACTIONS=$null
            Remove-Item -Path $env:GITHUB_ENV -Force
            Remove-Item -Path $env:GITHUB_PATH -Force
        }

        Context 'Test-GitHubWorkflow' {
            It 'when GITHUB_ACTIONS is defined should return true' {
                Test-GitHubWorkflow | Should -Be $true
            }
        }

        Context 'Set-EnvironmentVariable' {
            It 'should put name=value in GITHUB_ENV' {
                Set-EnvironmentVariable ENV_VARIABLE VALUE
                Get-Content -Path $env:GITHUB_ENV | Should -BeLike 'ENV_VARIABLE=*'
            }

            It 'with -Secret then should write command ::add-mask::' {
                Set-EnvironmentVariable ENV_VARIABLE VALUE -Secret *>&1 | Should -BeLike '::add-mask::*'
            }

            It 'with -Output then should write command ::set-output name=' {
                Set-EnvironmentVariable ENV_VARIABLE VALUE -Output *>&1 | Should -BeLike '::set-output name=*'
            }
        }

        Context 'Enter-Group' {
            It 'should write command ::group::' {
                Enter-Group GROUP *>&1 | Should -BeLike '::group::*'
            }
        }

        Context 'Exit-Group' {
            It 'should write command ::endgroup::' {
                Exit-Group *>&1 | Should -BeLike '::endgroup::*'
            }
        }

        Context 'Add-Path' {
            It 'should put value in GITHUB_PATH' {
                Add-Path /path
                Get-Content -Path $env:GITHUB_PATH | Should -Be '/path'
            }
        }
    }
}