-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathdeploy.Tests.ps1
More file actions
194 lines (168 loc) · 9.04 KB
/
Copy pathdeploy.Tests.ps1
File metadata and controls
194 lines (168 loc) · 9.04 KB
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
#Requires -Modules @{ ModuleName = 'Pester'; ModuleVersion = '5.0' }
BeforeAll {
. (Join-Path $PSScriptRoot 'deploy.ps1')
}
$script:PSScriptAnalyzerAvailable = [bool](Get-Module -ListAvailable -Name PSScriptAnalyzer)
Describe 'deploy.ps1 (Windows PowerShell 5.1 compatibility)' {
# deploy.ps1 has no BOM, so Windows PowerShell 5.1 reads it using the system ANSI codepage
# (Windows-1252) instead of UTF-8. Some Unicode punctuation - e.g. an em dash - decodes under
# that codepage into a "smart quote" character (U+2018/2019/201C/201D), which PowerShell's
# tokenizer accepts as an alternate string/char delimiter, corrupting the parse. This test
# simulates that misread directly and catches any future non-ASCII character reintroducing
# the same failure mode, regardless of which line it lands on.
It 'parses cleanly when read as Windows-1252 (no-BOM fallback encoding)' {
$bytes = [System.IO.File]::ReadAllBytes((Join-Path $PSScriptRoot 'deploy.ps1'))
$misreadText = [System.Text.Encoding]::GetEncoding(1252).GetString($bytes)
$tokens = $null
$parseErrors = $null
[System.Management.Automation.Language.Parser]::ParseInput($misreadText, [ref]$tokens, [ref]$parseErrors) | Out-Null
$parseErrors | Should -BeNullOrEmpty
}
}
Describe 'deploy.ps1 (PowerShell version/platform compatibility)' {
# Catches syntax or commands/parameters that don't exist on a target PowerShell version or
# platform - e.g. a PS7-only operator that would break Windows PowerShell 5.1, or a cmdlet
# parameter not yet available on an older baseline. Skipped (not failed) if PSScriptAnalyzer
# isn't installed locally; run `Install-Module PSScriptAnalyzer -Scope CurrentUser` to enable
# it. This is a static check - it does not catch semantic/runtime issues like Add-Type
# resetting [Environment]::CurrentDirectory, which is why the compatibility test above and
# TC5 in tests/test-deploy.ps1 exist separately.
It 'has no PSScriptAnalyzer compatibility findings for Windows PowerShell 5.1 / PowerShell 7.0' -Skip:(-not $script:PSScriptAnalyzerAvailable) {
Import-Module PSScriptAnalyzer
$results = Invoke-ScriptAnalyzer -Path (Join-Path $PSScriptRoot 'deploy.ps1') -Settings (Join-Path $PSScriptRoot 'PSScriptAnalyzerSettings.psd1')
$results | Should -BeNullOrEmpty
}
}
Describe 'Test-IsUtf8Compatible' {
BeforeAll {
$TestDir = New-Item -ItemType Directory -Path (Join-Path ([System.IO.Path]::GetTempPath()) "deploy-tests-$([System.Guid]::NewGuid())") -Force
}
AfterAll {
Remove-Item $TestDir.FullName -Recurse -Force -ErrorAction SilentlyContinue
}
It 'returns $false for UTF-32 LE BOM (FF FE 00 00)' {
$file = Join-Path $TestDir 'utf32le.md'
[System.IO.File]::WriteAllBytes($file, [byte[]]@(0xFF, 0xFE, 0x00, 0x00, 0x48, 0x00, 0x00, 0x00))
Test-IsUtf8Compatible -Path $file | Should -BeFalse
}
It 'returns $false for UTF-32 BE BOM (00 00 FE FF)' {
$file = Join-Path $TestDir 'utf32be.md'
[System.IO.File]::WriteAllBytes($file, [byte[]]@(0x00, 0x00, 0xFE, 0xFF, 0x00, 0x00, 0x00, 0x48))
Test-IsUtf8Compatible -Path $file | Should -BeFalse
}
It 'returns $false for UTF-16 LE BOM (FF FE)' {
$file = Join-Path $TestDir 'utf16le.md'
[System.IO.File]::WriteAllBytes($file, [byte[]]@(0xFF, 0xFE, 0x48, 0x00, 0x65, 0x00))
Test-IsUtf8Compatible -Path $file | Should -BeFalse
}
It 'returns $false for UTF-16 BE BOM (FE FF)' {
$file = Join-Path $TestDir 'utf16be.md'
[System.IO.File]::WriteAllBytes($file, [byte[]]@(0xFE, 0xFF, 0x00, 0x48, 0x00, 0x65))
Test-IsUtf8Compatible -Path $file | Should -BeFalse
}
It 'returns $true for UTF-8 BOM (EF BB BF)' {
$file = Join-Path $TestDir 'utf8bom.md'
[System.IO.File]::WriteAllBytes($file, [byte[]]@(0xEF, 0xBB, 0xBF, 0x48, 0x65, 0x6C, 0x6C, 0x6F))
Test-IsUtf8Compatible -Path $file | Should -BeTrue
}
It 'returns $true for ASCII file with no BOM' {
$file = Join-Path $TestDir 'ascii.md'
[System.IO.File]::WriteAllText($file, "Hello World`n")
Test-IsUtf8Compatible -Path $file | Should -BeTrue
}
It 'returns $true for UTF-8 multi-byte content with no BOM' {
$file = Join-Path $TestDir 'utf8-multibyte.md'
# café (c3 a9) encoded as UTF-8, no BOM
[System.IO.File]::WriteAllBytes($file, [byte[]]@(0x63, 0x61, 0x66, 0xC3, 0xA9, 0x0A))
Test-IsUtf8Compatible -Path $file | Should -BeTrue
}
It 'returns $true for an empty file' {
$file = Join-Path $TestDir 'empty.md'
[System.IO.File]::WriteAllBytes($file, [byte[]]@())
Test-IsUtf8Compatible -Path $file | Should -BeTrue
}
It 'returns $true for a single-byte file' {
$file = Join-Path $TestDir 'onebyte.md'
[System.IO.File]::WriteAllBytes($file, [byte[]]@(0x41))
Test-IsUtf8Compatible -Path $file | Should -BeTrue
}
It 'returns $false for a non-existent file' {
Test-IsUtf8Compatible -Path (Join-Path $TestDir 'nonexistent.md') | Should -BeFalse
}
It 'does not misidentify UTF-32 LE (FF FE 00 00) as UTF-16 LE (FF FE)' {
$file = Join-Path $TestDir 'utf32le-vs-utf16le.md'
[System.IO.File]::WriteAllBytes($file, [byte[]]@(0xFF, 0xFE, 0x00, 0x00, 0x48, 0x00, 0x00, 0x00))
Test-IsUtf8Compatible -Path $file | Should -BeFalse
}
}
Describe 'Copy-SingleFile' {
BeforeAll {
$TestDir = New-Item -ItemType Directory -Path (Join-Path ([System.IO.Path]::GetTempPath()) "deploy-copyfile-$([System.Guid]::NewGuid())") -Force
$SrcDir = New-Item -ItemType Directory -Path (Join-Path $TestDir 'src') -Force
$DstDir = New-Item -ItemType Directory -Path (Join-Path $TestDir 'dst') -Force
$script:OverwriteMode = 'all'
}
AfterAll {
Remove-Item $TestDir.FullName -Recurse -Force -ErrorAction SilentlyContinue
$script:OverwriteMode = ''
}
It 'normalises CRLF to LF for UTF-8 text files' {
$src = Join-Path $SrcDir 'crlf.md'
[System.IO.File]::WriteAllBytes($src, [System.Text.Encoding]::UTF8.GetBytes("line1`r`nline2`r`n"))
$dst = Join-Path $DstDir 'crlf.md'
Copy-SingleFile -Source $src -Destination $dst
[System.IO.File]::ReadAllText($dst) | Should -Be "line1`nline2`n"
}
It 'writes UTF-8 without BOM for text files' {
$src = Join-Path $SrcDir 'nobom.md'
[System.IO.File]::WriteAllBytes($src, [System.Text.Encoding]::UTF8.GetBytes("Hello`r`n"))
$dst = Join-Path $DstDir 'nobom.md'
Copy-SingleFile -Source $src -Destination $dst
$bytes = [System.IO.File]::ReadAllBytes($dst)
$hasBom = $bytes.Length -ge 3 -and $bytes[0] -eq 0xEF -and $bytes[1] -eq 0xBB -and $bytes[2] -eq 0xBF
$hasBom | Should -BeFalse
}
It 'copies a UTF-16 LE text file byte-for-byte without re-encoding' {
$src = Join-Path $SrcDir 'utf16le.md'
$original = [byte[]]@(0xFF, 0xFE, 0x48, 0x00, 0x65, 0x00, 0x6C, 0x00, 0x6C, 0x00, 0x6F, 0x00)
[System.IO.File]::WriteAllBytes($src, $original)
$dst = Join-Path $DstDir 'utf16le.md'
Copy-SingleFile -Source $src -Destination $dst
[System.IO.File]::ReadAllBytes($dst) | Should -Be $original
}
It 'copies a UTF-16 BE text file byte-for-byte without re-encoding' {
$src = Join-Path $SrcDir 'utf16be.md'
$original = [byte[]]@(0xFE, 0xFF, 0x00, 0x48, 0x00, 0x65, 0x00, 0x6C, 0x00, 0x6C, 0x00, 0x6F)
[System.IO.File]::WriteAllBytes($src, $original)
$dst = Join-Path $DstDir 'utf16be.md'
Copy-SingleFile -Source $src -Destination $dst
[System.IO.File]::ReadAllBytes($dst) | Should -Be $original
}
It 'copies a binary (non-text extension) file byte-for-byte via Copy-Item' {
$src = Join-Path $SrcDir 'binary.bin'
$original = [byte[]]@(0x00, 0x01, 0x02, 0x03, 0xFF)
[System.IO.File]::WriteAllBytes($src, $original)
$dst = Join-Path $DstDir 'binary.bin'
Copy-SingleFile -Source $src -Destination $dst
[System.IO.File]::ReadAllBytes($dst) | Should -Be $original
}
It 'creates destination directory if it does not exist' {
$src = Join-Path $SrcDir 'nested.md'
[System.IO.File]::WriteAllText($src, 'hello')
$dst = Join-Path $DstDir 'subdir\newdir\nested.md'
Copy-SingleFile -Source $src -Destination $dst
Test-Path $dst | Should -BeTrue
}
It 'strips UTF-8 BOM and normalises line endings for UTF-8 BOM text files' {
$src = Join-Path $SrcDir 'utf8bom.md'
$contentBytes = [System.Text.Encoding]::UTF8.GetBytes("line1`r`nline2`r`n")
$bomBytes = [byte[]]@(0xEF, 0xBB, 0xBF)
[System.IO.File]::WriteAllBytes($src, $bomBytes + $contentBytes)
$dst = Join-Path $DstDir 'utf8bom.md'
Copy-SingleFile -Source $src -Destination $dst
$result = [System.IO.File]::ReadAllText($dst)
$result | Should -Be "line1`nline2`n"
$firstBytes = [System.IO.File]::ReadAllBytes($dst)
$firstBytes[0] | Should -Not -Be 0xEF
}
}