|
1 | | -$repo_dir = (Get-Item $MyInvocation.MyCommand.Path).directory.parent.FullName |
| 1 | +$repo_dir = (Get-Item $MyInvocation.MyCommand.Path).Directory.Parent.FullName |
2 | 2 |
|
3 | 3 | $repo_files = @( Get-ChildItem $repo_dir -file -recurse -force ) |
4 | 4 |
|
@@ -75,175 +75,4 @@ describe 'Project code' { |
75 | 75 |
|
76 | 76 | } |
77 | 77 |
|
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" |
0 commit comments