Skip to content

Commit 7b7113f

Browse files
committed
tests: Move style constraints tests to separate test file
- This allows importing these tests in other buckets - Simpler UTF-8 BOM reading function
1 parent f261ccd commit 7b7113f

2 files changed

Lines changed: 155 additions & 173 deletions

File tree

test/00-Project.Tests.ps1

Lines changed: 2 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
$repo_dir = (Get-Item $MyInvocation.MyCommand.Path).directory.parent.FullName
1+
$repo_dir = (Get-Item $MyInvocation.MyCommand.Path).Directory.Parent.FullName
22

33
$repo_files = @( Get-ChildItem $repo_dir -file -recurse -force )
44

@@ -75,175 +75,4 @@ describe 'Project code' {
7575

7676
}
7777

78-
describe 'Style constraints for non-binary project files' {
79-
80-
$files = @(
81-
# gather all files except '*.exe', '*.zip', or any .git repository files
82-
$repo_files |
83-
where-object { $_.fullname -inotmatch $($project_file_exclusions -join '|') } |
84-
where-object { $_.fullname -inotmatch '(.exe|.zip|.dll)$' } |
85-
where-object { $_.fullname -inotmatch '(unformated)' }
86-
)
87-
88-
$files_exist = ($files.Count -gt 0)
89-
90-
it $('non-binary project files exist ({0} found)' -f $files.Count) -skip:$(-not $files_exist) {
91-
if (-not ($files.Count -gt 0))
92-
{
93-
throw "No non-binary project were found"
94-
}
95-
}
96-
97-
it 'files do not contain leading utf-8 BOM' -skip:$(-not $files_exist) {
98-
# utf-8 BOM == 0xEF 0xBB 0xBF
99-
# see http://www.powershellmagazine.com/2012/12/17/pscxtip-how-to-determine-the-byte-order-mark-of-a-text-file @@ https://archive.is/RgT42
100-
# ref: http://poshcode.org/2153 @@ https://archive.is/sGnnu
101-
102-
# As of PowerShell 6.0, `Get-Content` on non-Windows platform does not support `-Encoding Byte`
103-
# The following try-catch is to work around that by using .NET framework
104-
$read_bytes_type = 'default'
105-
function read_bytes ([string]$FileName, [int]$Size) {
106-
if ($read_bytes_type -eq 'default') {
107-
return Get-Content "$FileName" -Encoding Byte -TotalCount $Size
108-
}
109-
else {
110-
$buffer = [byte[]](0, 0, 0)
111-
$fstream = (New-Object -TypeName System.IO.FileStream -ArgumentList ("$FileName", [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read))
112-
$fstream.Read($buffer, 0, $Size) > $null
113-
return $buffer
114-
}
115-
}
116-
try {
117-
# Following would throw exception if `-Encoding Byte` is not supported
118-
$null = read_bytes -FileName $MyInvocation.PSCommandPath -Size 3
119-
}
120-
catch {
121-
$read_bytes_type = 'dotnet'
122-
}
123-
124-
$badFiles = @(
125-
foreach ($file in $files)
126-
{
127-
# Ignore previous TestResults.xml
128-
if ($file -match "TestResults.xml") {
129-
continue
130-
}
131-
$buffer = read_bytes -FileName "$($file.FullName)" -Size 3
132-
$content = ([char[]]$buffer -join '')
133-
if ([regex]::match($content, '(?ms)^\xEF\xBB\xBF').success)
134-
{
135-
$file.FullName
136-
}
137-
}
138-
)
139-
140-
if ($badFiles.Count -gt 0)
141-
{
142-
throw "The following files have utf-8 BOM: `r`n`r`n$($badFiles -join "`r`n")"
143-
}
144-
}
145-
146-
it 'files end with a newline' -skip:$(-not $files_exist) {
147-
$badFiles = @(
148-
foreach ($file in $files)
149-
{
150-
# Ignore previous TestResults.xml
151-
if ($file -match "TestResults.xml") {
152-
continue
153-
}
154-
$string = [System.IO.File]::ReadAllText($file.FullName)
155-
if ($string.Length -gt 0 -and $string[-1] -ne "`n")
156-
{
157-
$file.FullName
158-
}
159-
}
160-
)
161-
162-
if ($badFiles.Count -gt 0)
163-
{
164-
throw "The following files do not end with a newline: `r`n`r`n$($badFiles -join "`r`n")"
165-
}
166-
}
167-
168-
it 'file newlines are CRLF' -skip:$(-not $files_exist) {
169-
$badFiles = @(
170-
foreach ($file in $files)
171-
{
172-
$content = Get-Content -raw $file.FullName
173-
if(!$content) {
174-
throw "File contents are null: $($file.FullName)"
175-
}
176-
$lines = [regex]::split($content, '\r\n')
177-
$lineCount = $lines.Count
178-
179-
for ($i = 0; $i -lt $lineCount; $i++)
180-
{
181-
if ( [regex]::match($lines[$i], '\r|\n').success )
182-
{
183-
$file.FullName
184-
break
185-
}
186-
}
187-
}
188-
)
189-
190-
if ($badFiles.Count -gt 0)
191-
{
192-
throw "The following files have non-CRLF line endings: `r`n`r`n$($badFiles -join "`r`n")"
193-
}
194-
}
195-
196-
it 'files have no lines containing trailing whitespace' -skip:$(-not $files_exist) {
197-
$badLines = @(
198-
foreach ($file in $files)
199-
{
200-
# Ignore previous TestResults.xml
201-
if ($file -match "TestResults.xml") {
202-
continue
203-
}
204-
$lines = [System.IO.File]::ReadAllLines($file.FullName)
205-
$lineCount = $lines.Count
206-
207-
for ($i = 0; $i -lt $lineCount; $i++)
208-
{
209-
if ($lines[$i] -match '\s+$')
210-
{
211-
'File: {0}, Line: {1}' -f $file.FullName, ($i + 1)
212-
}
213-
}
214-
}
215-
)
216-
217-
if ($badLines.Count -gt 0)
218-
{
219-
throw "The following $($badLines.Count) lines contain trailing whitespace: `r`n`r`n$($badLines -join "`r`n")"
220-
}
221-
}
222-
223-
it 'any leading whitespace consists only of spaces (excepting makefiles)' -skip:$(-not $files_exist) {
224-
$badLines = @(
225-
foreach ($file in $files)
226-
{
227-
if ($file.fullname -inotmatch '(^|.)makefile$')
228-
{
229-
$lines = [System.IO.File]::ReadAllLines($file.FullName)
230-
$lineCount = $lines.Count
231-
232-
for ($i = 0; $i -lt $lineCount; $i++)
233-
{
234-
if ($lines[$i] -notmatch '^[ ]*(\S|$)')
235-
{
236-
'File: {0}, Line: {1}' -f $file.FullName, ($i + 1)
237-
}
238-
}
239-
}
240-
}
241-
)
242-
243-
if ($badLines.Count -gt 0)
244-
{
245-
throw "The following $($badLines.Count) lines contain TABs within leading whitespace: `r`n`r`n$($badLines -join "`r`n")"
246-
}
247-
}
248-
249-
}
78+
. "$psscriptroot\Import-File-Tests.ps1"

test/Import-File-Tests.ps1

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
if([String]::IsNullOrEmpty($MyInvocation.PSScriptRoot)) {
2+
Write-Error 'This script should not be called directly! It has to be imported from a buckets test file!'
3+
exit 1
4+
}
5+
6+
describe 'Style constraints for non-binary project files' {
7+
8+
$files = @(
9+
# gather all files except '*.exe', '*.zip', or any .git repository files
10+
$repo_files |
11+
where-object { $_.fullname -inotmatch $($project_file_exclusions -join '|') } |
12+
where-object { $_.fullname -inotmatch '(.exe|.zip|.dll)$' } |
13+
where-object { $_.fullname -inotmatch '(unformated)' }
14+
)
15+
16+
$files_exist = ($files.Count -gt 0)
17+
18+
it $('non-binary project files exist ({0} found)' -f $files.Count) -skip:$(-not $files_exist) {
19+
if (-not ($files.Count -gt 0))
20+
{
21+
throw "No non-binary project were found"
22+
}
23+
}
24+
25+
it 'files do not contain leading UTF-8 BOM' -skip:$(-not $files_exist) {
26+
# UTF-8 BOM == 0xEF 0xBB 0xBF
27+
# see http://www.powershellmagazine.com/2012/12/17/pscxtip-how-to-determine-the-byte-order-mark-of-a-text-file @@ https://archive.is/RgT42
28+
# ref: http://poshcode.org/2153 @@ https://archive.is/sGnnu
29+
$badFiles = @(
30+
foreach ($file in $files)
31+
{
32+
if((Get-Command Get-Content).parameters.ContainsKey('AsByteStream')) {
33+
# PowerShell Core (6.0+) '-Encoding byte' is replaced by '-AsByteStream'
34+
$content = ([char[]](Get-Content $file.FullName -AsByteStream -TotalCount 3) -join '')
35+
} else {
36+
$content = ([char[]](Get-Content $file.FullName -Encoding byte -TotalCount 3) -join '')
37+
}
38+
if ([regex]::match($content, '(?ms)^\xEF\xBB\xBF').success) {
39+
$file.FullName
40+
}
41+
}
42+
)
43+
44+
if ($badFiles.Count -gt 0)
45+
{
46+
throw "The following files have utf-8 BOM: `r`n`r`n$($badFiles -join "`r`n")"
47+
}
48+
}
49+
50+
it 'files end with a newline' -skip:$(-not $files_exist) {
51+
$badFiles = @(
52+
foreach ($file in $files)
53+
{
54+
# Ignore previous TestResults.xml
55+
if ($file -match "TestResults.xml") {
56+
continue
57+
}
58+
$string = [System.IO.File]::ReadAllText($file.FullName)
59+
if ($string.Length -gt 0 -and $string[-1] -ne "`n")
60+
{
61+
$file.FullName
62+
}
63+
}
64+
)
65+
66+
if ($badFiles.Count -gt 0)
67+
{
68+
throw "The following files do not end with a newline: `r`n`r`n$($badFiles -join "`r`n")"
69+
}
70+
}
71+
72+
it 'file newlines are CRLF' -skip:$(-not $files_exist) {
73+
$badFiles = @(
74+
foreach ($file in $files)
75+
{
76+
$content = Get-Content -raw $file.FullName
77+
if(!$content) {
78+
throw "File contents are null: $($file.FullName)"
79+
}
80+
$lines = [regex]::split($content, '\r\n')
81+
$lineCount = $lines.Count
82+
83+
for ($i = 0; $i -lt $lineCount; $i++)
84+
{
85+
if ( [regex]::match($lines[$i], '\r|\n').success )
86+
{
87+
$file.FullName
88+
break
89+
}
90+
}
91+
}
92+
)
93+
94+
if ($badFiles.Count -gt 0)
95+
{
96+
throw "The following files have non-CRLF line endings: `r`n`r`n$($badFiles -join "`r`n")"
97+
}
98+
}
99+
100+
it 'files have no lines containing trailing whitespace' -skip:$(-not $files_exist) {
101+
$badLines = @(
102+
foreach ($file in $files)
103+
{
104+
# Ignore previous TestResults.xml
105+
if ($file -match "TestResults.xml") {
106+
continue
107+
}
108+
$lines = [System.IO.File]::ReadAllLines($file.FullName)
109+
$lineCount = $lines.Count
110+
111+
for ($i = 0; $i -lt $lineCount; $i++)
112+
{
113+
if ($lines[$i] -match '\s+$')
114+
{
115+
'File: {0}, Line: {1}' -f $file.FullName, ($i + 1)
116+
}
117+
}
118+
}
119+
)
120+
121+
if ($badLines.Count -gt 0)
122+
{
123+
throw "The following $($badLines.Count) lines contain trailing whitespace: `r`n`r`n$($badLines -join "`r`n")"
124+
}
125+
}
126+
127+
it 'any leading whitespace consists only of spaces (excepting makefiles)' -skip:$(-not $files_exist) {
128+
$badLines = @(
129+
foreach ($file in $files)
130+
{
131+
if ($file.fullname -inotmatch '(^|.)makefile$')
132+
{
133+
$lines = [System.IO.File]::ReadAllLines($file.FullName)
134+
$lineCount = $lines.Count
135+
136+
for ($i = 0; $i -lt $lineCount; $i++)
137+
{
138+
if ($lines[$i] -notmatch '^[ ]*(\S|$)')
139+
{
140+
'File: {0}, Line: {1}' -f $file.FullName, ($i + 1)
141+
}
142+
}
143+
}
144+
}
145+
)
146+
147+
if ($badLines.Count -gt 0)
148+
{
149+
throw "The following $($badLines.Count) lines contain TABs within leading whitespace: `r`n`r`n$($badLines -join "`r`n")"
150+
}
151+
}
152+
153+
}

0 commit comments

Comments
 (0)