test/xEnvironmentVariables.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
$ModuleManifestName = 'xEnvironmentVariables.psd1'
$ModuleManifestPath = "$PSScriptRoot\..\$ModuleManifestName"

Describe 'Module Manifest Tests' {
    It 'Passes Test-ModuleManifest' {
        Test-ModuleManifest -Path $ModuleManifestPath | Should Not BeNullOrEmpty
        $? | Should Be $true
    }
}

Import-Module $ModuleManifestPath

Describe 'Tests for xEnvironmentVariables' {
    Context "Tests for getter for current process" {
        It "gets an environment variable for current process" {
            $var = Get-EnvironmentVariable -Name USERPROFILE -Scope Process -ShowProperties

            $var.Name | Should Be "USERPROFILE"
            $var.Value | Should Be $Env:USERPROFILE
            $var.ValueType | Should Be "String"
            $var.Scope | Should Be "Process"
            $var.BeforeExpansion | Should BeNullOrEmpty

            "$var" | Should Be $Env:USERPROFILE
        }
    }
    Context "Tests for setter for current process" {
        It "sets a basic environment variable for current process" {
            $params = @{
                Name      = "xEnvironmentVariables_TEST_SET_PROCESS"
                Value     = "basic_value"
                Scope     = "Process"
                ValueType = "String"
                Inherit   = "Auto"
            }
            $var = Set-EnvironmentVariable @params
            $var.Name | Should Be "xEnvironmentVariables_TEST_SET_PROCESS"
            $var.Value | Should Be "basic_value"
            $var.ValueType | Should Be "String"
            $var.Scope | Should Be "Process"
            $var.BeforeExpansion | Should BeNullOrEmpty

            $Env:xEnvironmentVariables_TEST_SET_PROCESS | Should Be "basic_value"
            "$var" | Should Be "basic_value"

            # clean up
            $Env:xEnvironmentVariables_TEST_SET_PROCESS = $null
        }
        It "sets an environment variable as ExpandString for current process" {
            $params = @{
                Name      = "xEnvironmentVariables_TEST_SET_PROCESS_EX"
                Value     = "USERNAME: %USERNAME%"
                Scope     = "Process"
                ValueType = "ExpandString"
                Inherit   = "Auto"
            }
            $var = Set-EnvironmentVariable @params
            $var.Name | Should Be "xEnvironmentVariables_TEST_SET_PROCESS_EX"
            $var.Value | Should Be "USERNAME: $Env:USERNAME"
            $var.ValueType | Should Be "String"
            $var.Scope | Should Be "Process"
            $var.BeforeExpansion | Should BeNullOrEmpty
            "$var" | Should Be "USERNAME: $Env:USERNAME"

            $Env:xEnvironmentVariables_TEST_SET_PROCESS_EX | Should Be "USERNAME: $Env:USERNAME"

            # clean up
            $Env:xEnvironmentVariables_TEST_SET_PROCESS_EX = $null
        }
    }
    Context "Tests for getter and setter for current user" {
        It "sets a basic environment variable for current user" {
            $params = @{
                Name      = "xEnvironmentVariables_TEST_SET_USER"
                Value     = "basic_value"
                Scope     = "User"
                ValueType = "String"
                Inherit   = "Auto"
            }
            $var = Set-EnvironmentVariable @params
            $var.Name | Should Be "xEnvironmentVariables_TEST_SET_USER"
            $var.Value | Should Be "basic_value"
            $var.ValueType | Should Be "String"
            $var.Scope | Should Be "User"
            $var.BeforeExpansion | Should be "basic_value"
            "$var" | Should Be "basic_value"

            # manual check
            [System.Environment]::GetEnvironmentVariable(
                "xEnvironmentVariables_TEST_SET_USER",
                "User"
            ) | Should Be "basic_value"

            # auto inheritance
            $Env:xEnvironmentVariables_TEST_SET_USER | Should Be "basic_value"

            # clean up
            [System.Environment]::SetEnvironmentVariable(
                "xEnvironmentVariables_TEST_SET_USER",
                "",
                "User"
            )
            $Env:xEnvironmentVariables_TEST_SET_USER = $null
        }

        It "sets an environment variable as ExpandString for current user" {
            $params = @{
                Name      = "xEnvironmentVariables_TEST_SET_USER_EX"
                Value     = "OS: %OS%"
                Scope     = "User"
                ValueType = "ExpandString"
                Inherit   = "Auto"
            }
            $var = Set-EnvironmentVariable @params
            $var.Name | Should Be "xEnvironmentVariables_TEST_SET_USER_EX"
            $var.Value | Should Be "OS: $Env:OS"
            $var.ValueType | Should Be "ExpandString"
            $var.Scope | Should Be "User"
            $var.BeforeExpansion | Should Be "OS: %OS%"
            "$var" | Should Be "OS: $Env:OS"

            # manual check
            [System.Environment]::GetEnvironmentVariable(
                "xEnvironmentVariables_TEST_SET_USER_EX",
                "User"
            ) | Should Be "OS: $Env:OS"

            # auto inheritance
            $Env:xEnvironmentVariables_TEST_SET_USER_EX | Should Be "OS: $Env:OS"

            # clean up
            [System.Environment]::SetEnvironmentVariable(
                "xEnvironmentVariables_TEST_SET_USER_EX",
                "",
                "User"
            )
            $Env:xEnvironmentVariables_TEST_SET_USER_EX = $null
        }
    }
    Context "Tests Get-EnvironmentVariables Output for current process" {
        $BasicGet = [System.Environment]::GetEnvironmentVariables("Process")
        foreach ($key in $BasicGet.Keys) {
            It "gets the correct value of the $key variable from current process" {
                $var = Get-EnvironmentVariables -Scope Process
                $BasicGetValue = $BasicGet.$key
                $var.$key | Should Be $BasicGetValue
            }
        }
    }
    Context "Tests Get-EnvironmentVariables Output for User" {
        $BasicGet = [System.Environment]::GetEnvironmentVariables("User")
        foreach ($key in $BasicGet.Keys) {
            It "gets the correct value of the $key variable from User" {
                $var = Get-EnvironmentVariables -Scope User
                $BasicGetValue = $BasicGet.$key
                $var.$key | Should Be $BasicGetValue
            }
        }
    }
    Context "Tests Get-EnvironmentVariables Output for Machine" {
        $BasicGet = [System.Environment]::GetEnvironmentVariables("Machine")
        foreach ($key in $BasicGet.Keys) {
            It "gets the correct value of the $key variable from Machine" {
                $var = Get-EnvironmentVariables -Scope Machine
                $BasicGetValue = $BasicGet.$key
                $var.$key | Should Be $BasicGetValue
            }
        }
    }
    Context "Tests Get-EnvironmentVariables Output is JSON" {
        It "Outputs the data as JSON" {
            $var = Get-EnvironmentVariables -OutputType JSON
            $var | ConvertFrom-Json | Should BeOfType [Object[]]
        }
    }
}