forked from ScoopInstaller/Scoop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImport-File-Tests.ps1
153 lines (135 loc) · 5.27 KB
/
Import-File-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
if([String]::IsNullOrEmpty($MyInvocation.PSScriptRoot)) {
Write-Error 'This script should not be called directly! It has to be imported from a buckets test file!'
exit 1
}
describe 'Style constraints for non-binary project files' {
$files = @(
# gather all files except '*.exe', '*.zip', or any .git repository files
$repo_files |
where-object { $_.fullname -inotmatch $($project_file_exclusions -join '|') } |
where-object { $_.fullname -inotmatch '(.exe|.zip|.dll)$' } |
where-object { $_.fullname -inotmatch '(unformated)' }
)
$files_exist = ($files.Count -gt 0)
it $('non-binary project files exist ({0} found)' -f $files.Count) -skip:$(-not $files_exist) {
if (-not ($files.Count -gt 0))
{
throw "No non-binary project were found"
}
}
it 'files do not contain leading UTF-8 BOM' -skip:$(-not $files_exist) {
# UTF-8 BOM == 0xEF 0xBB 0xBF
# see http://www.powershellmagazine.com/2012/12/17/pscxtip-how-to-determine-the-byte-order-mark-of-a-text-file @@ https://archive.is/RgT42
# ref: http://poshcode.org/2153 @@ https://archive.is/sGnnu
$badFiles = @(
foreach ($file in $files)
{
if((Get-Command Get-Content).parameters.ContainsKey('AsByteStream')) {
# PowerShell Core (6.0+) '-Encoding byte' is replaced by '-AsByteStream'
$content = ([char[]](Get-Content $file.FullName -AsByteStream -TotalCount 3) -join '')
} else {
$content = ([char[]](Get-Content $file.FullName -Encoding byte -TotalCount 3) -join '')
}
if ([regex]::match($content, '(?ms)^\xEF\xBB\xBF').success) {
$file.FullName
}
}
)
if ($badFiles.Count -gt 0)
{
throw "The following files have utf-8 BOM: `r`n`r`n$($badFiles -join "`r`n")"
}
}
it 'files end with a newline' -skip:$(-not $files_exist) {
$badFiles = @(
foreach ($file in $files)
{
# Ignore previous TestResults.xml
if ($file -match "TestResults.xml") {
continue
}
$string = [System.IO.File]::ReadAllText($file.FullName)
if ($string.Length -gt 0 -and $string[-1] -ne "`n")
{
$file.FullName
}
}
)
if ($badFiles.Count -gt 0)
{
throw "The following files do not end with a newline: `r`n`r`n$($badFiles -join "`r`n")"
}
}
it 'file newlines are CRLF' -skip:$(-not $files_exist) {
$badFiles = @(
foreach ($file in $files)
{
$content = Get-Content -raw $file.FullName
if(!$content) {
throw "File contents are null: $($file.FullName)"
}
$lines = [regex]::split($content, '\r\n')
$lineCount = $lines.Count
for ($i = 0; $i -lt $lineCount; $i++)
{
if ( [regex]::match($lines[$i], '\r|\n').success )
{
$file.FullName
break
}
}
}
)
if ($badFiles.Count -gt 0)
{
throw "The following files have non-CRLF line endings: `r`n`r`n$($badFiles -join "`r`n")"
}
}
it 'files have no lines containing trailing whitespace' -skip:$(-not $files_exist) {
$badLines = @(
foreach ($file in $files)
{
# Ignore previous TestResults.xml
if ($file -match "TestResults.xml") {
continue
}
$lines = [System.IO.File]::ReadAllLines($file.FullName)
$lineCount = $lines.Count
for ($i = 0; $i -lt $lineCount; $i++)
{
if ($lines[$i] -match '\s+$')
{
'File: {0}, Line: {1}' -f $file.FullName, ($i + 1)
}
}
}
)
if ($badLines.Count -gt 0)
{
throw "The following $($badLines.Count) lines contain trailing whitespace: `r`n`r`n$($badLines -join "`r`n")"
}
}
it 'any leading whitespace consists only of spaces (excepting makefiles)' -skip:$(-not $files_exist) {
$badLines = @(
foreach ($file in $files)
{
if ($file.fullname -inotmatch '(^|.)makefile$')
{
$lines = [System.IO.File]::ReadAllLines($file.FullName)
$lineCount = $lines.Count
for ($i = 0; $i -lt $lineCount; $i++)
{
if ($lines[$i] -notmatch '^[ ]*(\S|$)')
{
'File: {0}, Line: {1}' -f $file.FullName, ($i + 1)
}
}
}
}
)
if ($badLines.Count -gt 0)
{
throw "The following $($badLines.Count) lines contain TABs within leading whitespace: `r`n`r`n$($badLines -join "`r`n")"
}
}
}