-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.Tests.ps1
244 lines (214 loc) · 10 KB
/
config.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
Describe 'Verify-Config' {
BeforeAll {
. (Resolve-Path "$PSScriptRoot\..\src\config.ps1").Path
}
It 'Should pass all verifications.' {
$jacocoxml2htmlConfig = [PSCustomObject]@{
'xml_file' = 'lorem ipsum';
'destination_directory' = 'lorem ipsum';
'sources_directory' = 'lorem ipsum';
'theme' = 'dark';
}
$Global:jacocoxml2htmlConfig = $jacocoxml2htmlConfig
Verify-Config
}
Context 'XML file property is invalid.' {
It 'Should throw an error when XML file property is not set in configuration.' {
$jacocoxml2htmlConfig = [PSCustomObject]@{
'destination_directory' = 'lorem ipsum';
'sources_directory' = 'lorem ipsum';
'theme' = 'dark';
}
$Global:jacocoxml2htmlConfig = $jacocoxml2htmlConfig
{ Verify-Config } | Should -Throw -ExpectedMessage "'xml_file' configuration property is missing."
}
It 'Should throw an error when XML file property exists in configuration, but it is empty.' {
$jacocoxml2htmlConfig = [PSCustomObject]@{
'xml_file' = '';
'destination_directory' = 'lorem ipsum';
'sources_directory' = 'lorem ipsum';
'theme' = 'dark';
}
$Global:jacocoxml2htmlConfig = $jacocoxml2htmlConfig
{ Verify-Config } | Should -Throw -ExpectedMessage "'xml_file' configuration property is missing."
}
}
Context 'destination_directory property is invalid.' {
It 'Should throw an error when destination directory property is not set in configuration.' {
$jacocoxml2htmlConfig = [PSCustomObject]@{
'xml_file' = 'lorem ipsum'
'sources_directory' = 'lorem ipsum';
'theme' = 'dark';
}
$Global:jacocoxml2htmlConfig = $jacocoxml2htmlConfig
{ Verify-Config } | Should -Throw -ExpectedMessage "'destination_directory' configuration property is missing."
}
It 'Should throw an error when destination directory property exists in configuration, but it is empty.' {
$jacocoxml2htmlConfig = [PSCustomObject]@{
'xml_file' = 'lorem ipsum';
'destination_directory' = '';
'sources_directory' = 'lorem ipsum';
'theme' = 'dark';
}
$Global:jacocoxml2htmlConfig = $jacocoxml2htmlConfig
{ Verify-Config } | Should -Throw -ExpectedMessage "'destination_directory' configuration property is missing."
}
}
Context 'sources_directory property is invalid.' {
It 'Should throw an error when sources directory property is not set in configuration.' {
$jacocoxml2htmlConfig = [PSCustomObject]@{
'xml_file' = 'lorem ipsum'
'destination_directory' = 'lorem ipsum';
'theme' = 'dark';
}
$Global:jacocoxml2htmlConfig = $jacocoxml2htmlConfig
{ Verify-Config } | Should -Throw -ExpectedMessage "'sources_directory' configuration property is missing."
}
It 'Should throw an error when sources directory property exists in configuration, but it is empty.' {
$jacocoxml2htmlConfig = [PSCustomObject]@{
'xml_file' = 'lorem ipsum';
'destination_directory' = 'lorem ipsum';
'sources_directory' = '';
'theme' = 'dark';
}
$Global:jacocoxml2htmlConfig = $jacocoxml2htmlConfig
{ Verify-Config } | Should -Throw -ExpectedMessage "'sources_directory' configuration property is missing."
}
}
Context 'theme property is invalid.' {
It 'Should throw an error when theme property is not set in configuration.' {
$jacocoxml2htmlConfig = [PSCustomObject]@{
'xml_file' = 'lorem ipsum'
'destination_directory' = 'lorem ipsum';
'sources_directory' = 'lorem ipsum';
}
$Global:jacocoxml2htmlConfig = $jacocoxml2htmlConfig
{ Verify-Config } | Should -Throw -ExpectedMessage "'theme' configuration property is missing or invalid. Valid values for 'theme': 'light', 'dark'."
}
It 'Should throw an error when theme property exists in configuration, but it is empty.' {
$jacocoxml2htmlConfig = [PSCustomObject]@{
'xml_file' = 'lorem ipsum';
'destination_directory' = 'lorem ipsum';
'sources_directory' = 'lorem ipsum';
'theme' = '';
}
$Global:jacocoxml2htmlConfig = $jacocoxml2htmlConfig
{ Verify-Config } | Should -Throw -ExpectedMessage "'theme' configuration property is missing or invalid. Valid values for 'theme': 'light', 'dark'."
}
It 'Should throw an error when theme property exists in configuration, but its value is invalid.' {
$jacocoxml2htmlConfig = [PSCustomObject]@{
'xml_file' = 'lorem ipsum';
'destination_directory' = 'lorem ipsum';
'sources_directory' = 'lorem ipsum';
'theme' = 'green';
}
$Global:jacocoxml2htmlConfig = $jacocoxml2htmlConfig
{ Verify-Config } | Should -Throw -ExpectedMessage "'theme' configuration property is missing or invalid. Valid values for 'theme': 'light', 'dark'."
}
}
}
Describe 'Load-Config' {
BeforeAll {
. (Resolve-Path "$PSScriptRoot\..\src\config.ps1").Path
}
Context 'Config is properly loaded and verified' {
BeforeAll {
Set-Content -Path "TestDrive:\config.ps1" -Value @'
$Global:jacocoxml2htmlConfig = [PSCustomObject]@{
'xml_file' = "TestDrive:\coverage.xml";
'destination_directory' = "TestDrive:\coverage\html";
'sources_directory' = "TestDrive:\src";
}
'@
Set-Content -Path "TestDrive:\default-config.ps1" -Value @'
$Global:jacocoxml2htmlConfig = [PSCustomObject]@{
# light theme
'source_code_theme_light' = 'vs.min.css';
'line_number_yellow_light' = '#ffffc8';
'line_number_red_light' = '#ffc8c8';
'line_number_green_light' = '#c8ffc8';
'source_code_yellow_light' = '#fffff0';
'source_code_red_light' = '#fff0f0';
'source_code_green_light' = '#f0fff0';
# dark theme
'source_code_theme_dark' = 'vs2015.min.css';
'line_number_yellow_dark' = '#ffffc840';
'line_number_red_dark' = '#ffc8c840';
'line_number_green_dark' = '#c8ffc840';
'source_code_yellow_dark' = '#ffff8c20';
'source_code_red_dark' = '#ff8c8c20';
'source_code_green_dark' = '#8cff8c20';
'theme' = 'light';
}
'@
Mock -CommandName Resolve-Path -MockWith {
param($Path)
switch -Wildcard ($path) {
"*../default-config.ps1" { return [PSCustomObject]@{ "Path" = "TestDrive:\default-config.ps1" } }
Default { return [PSCustomObject]@{ "Path" = $Path } }
}
}
}
It 'Should properly load and verify configuration' {
Test-Path -Path "TestDrive:\config.ps1" | Should -Be $true
Test-Path -Path "TestDrive:\default-config.ps1" | Should -Be $true
Load-Config -configFileLocation "TestDrive:\config.ps1"
$Global:jacocoxml2htmlConfig.xml_file | Should -Be "TestDrive:\coverage.xml"
$Global:jacocoxml2htmlConfig.destination_directory | Should -Be "TestDrive:\coverage\html"
$Global:jacocoxml2htmlConfig.sources_directory | Should -Be "TestDrive:\src"
}
}
Context 'Config is not properly loaded' {
It 'Should throw an error when default config can not be loaded' {
Mock -CommandName Resolve-Path -MockWith {
param($Path)
switch -Wildcard ($path) {
"*../default-config.ps1" { return [PSCustomObject]@{ "Path" = "TestDrive:\non-existing-default-config.ps1" } }
Default { return [PSCustomObject]@{ "Path" = $Path } }
}
}
Test-Path -Path "TestDrive:\default-config.ps1" | Should -Be $false
Set-Content -Path TestDrive:\config.ps1 -Value @'
$Global:jacocoxml2htmlConfig = [PSCustomObject]@{
'xml_file' = "TestDrive:\coverage.xml";
'destination_directory' = "TestDrive:\coverage\html";
'sources_directory' = "TestDrive:\src";
}
'@
{ Load-Config -configFileLocation 'TestDrive:\config.ps1' } | Should -Throw -ExpectedMessage "Default config failed to load."
Remove-Item "TestDrive:\config.ps1"
}
It 'Should throw an error when config file can not be loaded' {
Mock -CommandName Resolve-Path -MockWith {
param($Path)
switch -Wildcard ($path) {
"*../default-config.ps1" { return [PSCustomObject]@{ "Path" = "TestDrive:\default-config.ps1" } }
Default { return $Path }
}
}
Test-Path -Path "TestDrive:\config.ps1" | Should -Be $false
Set-Content -Path "TestDrive:\default-config.ps1" -Value @'
$Global:jacocoxml2htmlConfig = [PSCustomObject]@{
# light theme
'source_code_theme_light' = 'vs.min.css';
'line_number_yellow_light' = '#ffffc8';
'line_number_red_light' = '#ffc8c8';
'line_number_green_light' = '#c8ffc8';
'source_code_yellow_light' = '#fffff0';
'source_code_red_light' = '#fff0f0';
'source_code_green_light' = '#f0fff0';
# dark theme
'source_code_theme_dark' = 'vs2015.min.css';
'line_number_yellow_dark' = '#ffffc840';
'line_number_red_dark' = '#ffc8c840';
'line_number_green_dark' = '#c8ffc840';
'source_code_yellow_dark' = '#ffff8c20';
'source_code_red_dark' = '#ff8c8c20';
'source_code_green_dark' = '#8cff8c20';
'theme' = 'light';
}
'@
{ Load-Config -configFileLocation 'TestDrive:\config.ps1' } | Should -Throw -ExpectedMessage "Configuration file failed to laod."
}
}
}