generated from brianary/PSModuleTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDetextive.Tests.ps1
314 lines (312 loc) · 14 KB
/
Detextive.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# Pester tests, see https://github.com/Pester/Pester/wiki
Import-LocalizedData -BindingVariable manifest -BaseDirectory ./src/* -FileName (Split-Path $PWD -Leaf)
$psd1 = Resolve-Path ./src/*/bin/Debug/*/*.psd1
if(1 -lt ($psd1 |Measure-Object).Count) {throw "Too many module binaries found: $psd1"}
$module = Import-Module "$psd1" -PassThru -vb
$TestRoot = "$PSScriptRoot\test"
$AsByteStream =
if((Get-Command Get-Content -ParameterName AsByteStream -ErrorAction SilentlyContinue)) {@{AsByteStream=$true}}
else {@{Encoding='Byte'}}
Describe $module.Name {
$env:Path = $env:Path -replace ';A:\\Scripts'
Context "$($module.Name) module" -Tag Module {
It "Given the module, the version should match the manifest version" {
$module.Version |Should -BeExactly $manifest.ModuleVersion
}
It "Given the module, the DLL file version should match the manifest version" {
(Get-Item "$($module.ModuleBase)\$($module.Name).dll").VersionInfo.FileVersionRaw |
Should -BeLike "$($manifest.ModuleVersion)*"
}
It "Given the module, the DLL product version should match the manifest version" {
(Get-Item "$($module.ModuleBase)\$($module.Name).dll").VersionInfo.ProductVersionRaw |
Should -BeLike "$($manifest.ModuleVersion)*"
} -Pending
It "Given the module, the DLL should have a valid semantic product version" {
$v = (Get-Item "$($module.ModuleBase)\$($module.Name).dll").VersionInfo.ProductVersion
[semver]::TryParse($v, [ref]$null) |Should -BeTrue
} -Pending
}
Context 'Test-TextFile cmdlet' -Tag Cmdlet,Test-TextFile {
It "Given the file '<File>', '<Expected>' should be returned." -TestCases (
Get-ChildItem $TestRoot\* -File |
foreach {@{ File = $_.FullName; Expected = $_.Name -notlike 'binary.*' }}
) {
Param($File,$Expected)
Detextive\Test-TextFile $File -vb |Should -BeExactly $Expected
}
}
Context 'Test-BinaryFile cmdlet' -Tag Cmdlet,Test-BinaryFile {
It "Given the file '<File>', '<Expected>' should be returned." -TestCases (
Get-ChildItem $TestRoot\* -File |
foreach {@{ File = $_.FullName; Expected = $_.Name -like 'binary.*' }}
) {
Param($File,$Expected)
Detextive\Test-BinaryFile $File -vb |Should -BeExactly $Expected
}
}
Context 'Test-Utf8Signature cmdlet' -Tag Cmdlet,Test-Utf8Signature {
It "Given the file '<File>', '<Expected>' should be returned." -TestCases (
Get-ChildItem $TestRoot\* -File |
foreach {@{ File = $_.FullName; Expected = $_.Name -like 'utf-8-bom-*' }}
) {
Param($File,$Expected)
Detextive\Test-Utf8Signature $File -vb |Should -BeExactly $Expected
}
}
Context 'Test-Utf8Encoding cmdlet' -Tag Cmdlet,Test-Utf8Encoding {
It "Given the file '<File>', '<Expected>' should be returned." -TestCases (
Get-ChildItem $TestRoot\* -File |
foreach {@{ File = $_.FullName; Expected = $_.Name -like 'utf-8-*' -or $_.Name -like 'ascii-*' }}
) {
Param($File,$Expected)
Detextive\Test-Utf8Encoding $File -vb |Should -BeExactly $Expected
}
}
Context 'Test-FinalNewline cmdlet' -Tag Cmdlet,Test-FinalNewline {
It "Given the file '<File>', '<Expected>' should be returned." -TestCases (
Get-ChildItem $TestRoot\* -File |
foreach {@{ File = $_.FullName; Expected = $_.Name -notlike 'binary.*' -and $_.Name -notlike '*-none-none.txt' }}
) {
Param($File,$Expected)
Detextive\Test-FinalNewline $File -vb |Should -BeExactly $Expected
}
}
Context 'Get-FileEncoding cmdlet' -Tag Cmdlet,Get-FileEncoding {
It "Given the file '<File>', '<Expected>' should be returned." -TestCases (
Get-ChildItem $TestRoot\*.txt,$TestRoot\*.ebcdic -File |
foreach {
@{ File = $_.FullName; Expected = switch -Wildcard ($_.Name) {
ascii-* {@('us-ascii')}
ebcdic-* {@('ibm037')}
latin1-* {@('iso-8859-1','us-ascii')}
utf-8-* {@('utf-8','us-ascii')}
utf-16be-* {@('utf-16BE')}
utf-16le-* {@('utf-16')}
utf-32be-* {@('utf-32BE')}
utf-32le-* {@('utf-32')}
windows-1252-* {@('windows-1252','iso-8859-1','us-ascii')}
default {@([Text.Encoding]::Default.WebName)}
} }
}
) {
Param($File,$Expected)
(Detextive\Get-FileEncoding $File -vb).WebName |Should -BeIn $Expected
}
}
Context 'Get-FileIndents cmdlet' -Tag Cmdlet,Get-FileIndents {
It "Given the file '<File>', '<Indents>' should be returned." -TestCases (
Get-ChildItem $TestRoot\*.txt -File |
foreach {
$ind = ([io.path]::GetFileNameWithoutExtension($_.Name) -split '-')[-2]
@{ File = $_.FullName; Indents = switch($ind){ mixedi {'Mixed'} tab {'Tabs'} space {'Spaces'} none {'None'} default {'Other'} } }
}
) {
Param($File,$Indents)
$e = Detextive\Get-FileIndents $File -vb
$e.Indents |Should -BeExactly $Indents
# not really distinguishing intra-line mixed and inter-line mixed, so skip that check
if($Indents -notin 'Mixed','None') {$e.$Indents |Should -BeGreaterThan 0}
$e.Tabs |Should -BeGreaterOrEqual 0
$e.Spaces |Should -BeGreaterOrEqual 0
$e.Mixed |Should -BeGreaterOrEqual 0
$e.Other |Should -BeGreaterOrEqual 0
}
It "Given the code file '<File>', '<Indents>' should be returned." -TestCases @(
@{ File = "$TestRoot\*.ps1"; Indents = 'Tabs' }
) {
Param($File,$Indents)
$e = Detextive\Get-FileIndents $File -vb
$e.Indents |Should -BeExactly $Indents
# not really distinguishing intra-line mixed and inter-line mixed, so skip that check
if($Indents -notin 'Mixed','None') {$e.$Indents |Should -BeGreaterThan 0}
$e.Tabs |Should -BeGreaterOrEqual 0
$e.Spaces |Should -BeGreaterOrEqual 0
$e.Mixed |Should -BeGreaterOrEqual 0
$e.Other |Should -BeGreaterOrEqual 0
}
}
Context 'Get-FileLineEndings cmdlet' -Tag Cmdlet,Get-FileLineEndings {
It "Given the file '<File>', '<LineEndings>' should be returned." -TestCases (
Get-ChildItem $TestRoot\*.txt -File |
foreach {
$end = ([io.path]::GetFileNameWithoutExtension($_.Name) -split '-')[-1]
@{ File = $_.FullName; LineEndings = switch($end){ mixedle {'Mixed'} none {'None'} default {$end.ToUpperInvariant()} } }
}
) {
Param($File,$LineEndings)
$e = Detextive\Get-FileLineEndings $File -vb
$e.LineEndings |Should -BeExactly $LineEndings
if($LineEndings -notin 'Mixed','None') {$e.$LineEndings |Should -BeGreaterThan 0}
$e.CRLF |Should -BeGreaterOrEqual 0
$e.LF |Should -BeGreaterOrEqual 0
$e.CR |Should -BeGreaterOrEqual 0
$e.NEL |Should -BeGreaterOrEqual 0
$e.LS |Should -BeGreaterOrEqual 0
$e.PS |Should -BeGreaterOrEqual 0
}
}
Context 'Get-FileContentsInfo cmdlet' -Tag Cmdlet,Get-FileContentsInfo {
It "Given the file '<File>', IsBinary should be '<IsBinary>'." -TestCases (
Get-ChildItem $TestRoot\binary.* -File |
foreach {@{ File = $_.FullName; IsBinary = $true }}
) {
Param($File,$IsBinary)
$e = Detextive\Get-FileContentsInfo $File -vb
$e.IsBinary |Should -BeTrue
}
It "Given the file '<File>', {'<Encoding>' '<Indents>' '<LineEndings>'} should be returned." -TestCases (
Get-ChildItem $TestRoot\binary.* -File |
where {$_.Name -notlike 'binary.*'} |
foreach {@{
File = $_.FullName
IsBinary = $false
Utf8Signature = $_.Name -like 'utf-8-bom-*'
Indents = switch -Wildcard ($_.Name)
{
*-tab-* {'Tabs'}
*-space-* {'Spaces'}
*-emsp-* {'Other'}
*-mixedi-* {'Mixed'}
*-none-* {'None'}
}
LineEndings = switch -Wildcard ($_.Name)
{
*-crlf.* {'CRLF'}
*-lf.* {'LF'}
*-cr.* {'CR'}
*-nel.* {'NEL'}
*-ls.* {'LS'}
*-ps.* {'PS'}
*-mixedle.* {'Mixed'}
*-none.* {'None'}
}
FinalNewline = $_.Name -notlike '*-none.*'
}}
) {
Param($File,$Encoding,$Utf8Signature,$Indents,$LineEndings,$FinalNewline)
$e = Detextive\Get-FileContentsInfo $File -vb
$e.IsBinary |Should -BeFalse
$e.Encoding.WebName |Should -BeExactly $Encoding
$e.Utf8Signature |Should -BeExactly $Utf8Signature
$e.Indents |Should -BeExactly $Indents
$e.LineEndings |Should -BeExactly $LineEndings
$e.FinalNewline |Should -BeExactly $FinalNewline
}
}
Context 'Get-FileEditorConfig cmdlet' -Tag Cmdlet,Get-FileEditorConfig {
It "Given the file '<File>', {'<Encoding>' '<Indents>' '<LineEndings>'} should be returned." -TestCases @(
@{ File = "$TestRoot\..\README.md"; Encoding = 'utf-8'; Utf8Signature = $false
Indents = 'Spaces'; LineEndings = 'CRLF'; FinalNewline = $true }
@{ File = "$TestRoot\..\Detextive.svg"; Encoding = 'utf-8'; Utf8Signature = $false
Indents = 'Spaces'; LineEndings = 'CRLF'; FinalNewline = $true }
) {
Param($File,$Encoding,$Utf8Signature,$Indents,$LineEndings,$FinalNewline)
$e = Detextive\Get-FileEditorConfig $File -vb
$e.Encoding.WebName |Should -BeExactly $Encoding
$e.Utf8Signature |Should -BeExactly $Utf8Signature
$e.Indents |Should -BeExactly $Indents
$e.LineEndings |Should -BeExactly $LineEndings
$e.FinalNewline |Should -BeExactly $FinalNewline
}
}
Context 'Add-Utf8Signature cmdlet' -Tag Cmdlet,Add-Utf8Signature {
It "Given the bytes '<Bytes>', the updated file should contain bytes '<Content>'." -TestCases @(
@{ Bytes = [byte[]]@(); Content = [byte[]]@(0xEF,0xBB,0xBF) }
@{ Bytes = [byte[]]@(0x0A); Content = [byte[]]@(0xEF,0xBB,0xBF,0x0A) }
@{ Bytes = [byte[]]@(0xEF,0xBB,0xBF,0x0A); Content = [byte[]]@(0xEF,0xBB,0xBF,0x0A) }
@{ Bytes = [byte[]]@(0x4F,0x4B,0x0A); Content = [byte[]]@(0xEF,0xBB,0xBF,0x4F,0x4B,0x0A) }
@{ Bytes = [byte[]]@(0xF0,0x9F,0x86,0x97,0x0A); Content = [byte[]]@(0xEF,0xBB,0xBF,0xF0,0x9F,0x86,0x97,0x0A) }
) {
Param($Bytes,$Content)
$file = [io.path]::GetTempFileName()
$Bytes |Set-Content $file @AsByteStream
Detextive\Add-Utf8Signature $file -vb
Get-Content $file @AsByteStream |Should -Be $Content
Remove-Item $file
}
}
Context 'Remove-Utf8Signature cmdlet' -Tag Cmdlet,Remove-Utf8Signature {
It "Given the bytes '<Bytes>', the updated file should contain bytes '<Content>'." -TestCases @(
@{ Bytes = [byte[]]@(); Content = [byte[]]@() }
@{ Bytes = [byte[]]@(0xEF,0xBB,0xBF); Content = [byte[]]@() }
@{ Bytes = [byte[]]@(0xEF,0xBB,0xBF,0x0A); Content = [byte[]]@(0x0A) }
@{ Bytes = [byte[]]@(0x0A); Content = [byte[]]@(0x0A) }
@{ Bytes = [byte[]]@(0xEF,0xBB,0xBF,0x4F,0x4B,0x0A); Content = [byte[]]@(0x4F,0x4B,0x0A) }
@{ Bytes = [byte[]]@(0xEF,0xBB,0xBF,0xF0,0x9F,0x86,0x97,0x0A); Content = [byte[]]@(0xF0,0x9F,0x86,0x97,0x0A) }
) {
Param($Bytes,$Content)
$file = [io.path]::GetTempFileName()
$Bytes |Set-Content $file @AsByteStream
Detextive\Remove-Utf8Signature $file -vb
Get-Content $file @AsByteStream |Should -Be $Content
Remove-Item $file
}
}
Context 'Test-BrokenEncoding cmdlet' -Tag Cmdlet,Test-BrokenEncoding {
It "Given the string '<InputObject>', the value '<Expected>' should be returned." -TestCases @(
@{ InputObject = ' '; Expected = $false }
@{ InputObject = 'SmartQuotes Aren’t'; Expected = $true }
@{ InputObject = '1.2.1 – 1.3.4'; Expected = $true }
@{ InputObject = 'Test'; Expected = $false }
) {
Param($InputObject,$Expected)
$InputObject |Detextive\Test-BrokenEncoding -vb |Should -BeExactly $Expected
}
It "Given the file contents '<InputObject>', the file should be updated to contain '<Expected>'." -TestCases @(
@{ InputObject = ' '; Expected = $false }
@{ InputObject = 'SmartQuotes Aren’t'; Expected = $true }
@{ InputObject = '1.2.1 – 1.3.4'; Expected = $true }
@{ InputObject = 'Test'; Expected = $false }
) {
Param($InputObject,$Expected)
$file = [io.path]::GetTempFileName()
$InputObject |Set-Content $file -Encoding utf8
Detextive\Test-BrokenEncoding $file -vb |Should -BeExactly $Expected
Remove-Item $file
}
}
Context 'Repair-Encoding cmdlet' -Tag Cmdlet,Repair-Encoding {
It "Given the string '<InputObject>', the string '<Expected>' should be returned." -TestCases @(
@{ InputObject = 'SmartQuotes Aren’t'; Expected = "SmartQuotes Aren’t" }
@{ InputObject = '1.2.1 – 1.3.4'; Expected = "1.2.1 – 1.3.4" }
) {
Param($InputObject,$Expected)
$InputObject |Detextive\Repair-Encoding -vb |Should -BeExactly $Expected
}
It "Given the file contents '<InputObject>', the file should be updated to contain '<Expected>'." -TestCases @(
@{ InputObject = 'SmartQuotes Aren’t'; Expected = "SmartQuotes Aren’t" }
@{ InputObject = '1.2.1 – 1.3.4'; Expected = "1.2.1 – 1.3.4" }
) {
Param($InputObject,$Expected)
$file = [io.path]::GetTempFileName()
$InputObject |Set-Content $file -Encoding utf8
Detextive\Repair-Encoding $file -vb
Get-Content $file -Encoding utf8 -TotalCount 1 |Should -BeExactly $Expected
Remove-Item $file
}
}
Context 'Test-FileEditorConfig cmdlet' -Tag Cmdlet,Test-FileEditorConfig {
It "Given the file '<File>', the result '<Expected>' should be returned." -TestCases @(
@{ File = "$TestRoot\..\README.md"; Expected = $true }
@{ File = "$TestRoot\..\Detextive.Tests.ps1"; Expected = $false }
@{ File = "$TestRoot\..\test.cmd"; Expected = $true }
) {
Param($File,$Expected)
Detextive\Test-FileEditorConfig $File -vb |Should -BeExactly $Expected
}
}
Context 'Repair-FileEditorConfig cmdlet' -Tag Cmdlet,Test-FileEditorConfig {
It "Given the text '<InputObject>', the result '<Expected>' should be returned." -TestCases @(
@{ InputObject = " # test`n"; Extension = 'ps1'; Encoding = New-Object Text.UTF8Encoding $true;
Expected = "`t# test`r`n" }
) {
Param($InputObject,$Extension,$Encoding,$Expected)
$file = "$([guid]::NewGuid()).$Extension"
$Encoding.GetBytes($InputObject) |Set-Content $file @AsByteStream
Get-Content $file -Raw |Should -BeExactly $InputObject
Detextive\Repair-FileEditorConfig $file -vb
Get-Content $file -Raw |Should -BeExactly $Expected
Remove-Item $file
}
}
}.GetNewClosure()