Skip to content

Commit da5dd74

Browse files
committed
Add tests CRLF, Tabs, UTF-8 BOM, trailing whitespaces and EOF newlines
1 parent e86ff46 commit da5dd74

2 files changed

Lines changed: 147 additions & 1 deletion

File tree

‎.gitattributes‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
* text eol=lf
1+
# retain windows line-endings in case checked out on mac or linux
2+
* text eol=crlf

‎Scoop-Bucket.Tests.ps1‎

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,151 @@
22
. "$env:scoop_home\lib\core.ps1"
33
. "$env:scoop_home\lib\manifest.ps1"
44

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

0 commit comments

Comments
 (0)