improve project "style contraints" testing

* includes code refactoring/improvements and improved test descriptions
This commit is contained in:
Roy Ivy III
2015-12-12 13:46:30 -06:00
parent b37bba7e53
commit 8f9695b411

View File

@@ -1,11 +1,28 @@
$repo_dir = (Get-Item $MyInvocation.MyCommand.Path).directory.parent.FullName
$repo_files = @( Get-ChildItem $repo_dir -file -recurse -force )
$project_file_exclusions = @(
$([regex]::Escape($repo_dir.fullname)+'\\.git\\.*$')
)
describe 'Project code' {
$files = @(
Get-ChildItem $repo_dir -file -recurse -force | ? { $_.fullname -match '.(ps1|psm1)$' }
Get-ChildItem $repo_dir -file -recurse -force |
? { $_.fullname -inotmatch $($project_file_exclusions -join '|') } |
? { $_.fullname -imatch '.(ps1|psm1)$' }
)
$files_exist = ($files.Count -gt 0)
it $('PowerShell code files exist ({0} found)' -f $files.Count) -skip:$(-not $files_exist) {
if (-not ($files.Count -gt 0))
{
throw "No PowerShell code files were found"
}
}
function Test-PowerShellSyntax {
# ref: http://powershell.org/wp/forums/topic/how-to-check-syntax-of-scripts-automatically @@ https://archive.is/xtSv6
# originally created by Alexander Petrovskiy & Dave Wyatt
@@ -35,7 +52,7 @@ describe 'Project code' {
}
}
it 'PowerShell files do not contain syntax errors' {
it 'PowerShell code files do not contain syntax errors' -skip:$(-not $files_exist) {
$badFiles = @(
foreach ($file in $files)
{
@@ -54,14 +71,25 @@ describe 'Project code' {
}
describe 'Project style constraints' {
describe 'Style constraints for non-binary project files' {
$files = @(
# gather all files except '*.exe', '*.zip', or any .git repository files
Get-ChildItem $repo_dir -file -recurse -force | ? { $_.fullname -notmatch '(.git(|\\.*)|.exe|.zip)$' }
Get-ChildItem $repo_dir -file -recurse -force |
? { $_.fullname -inotmatch $($project_file_exclusions -join '|') } |
? { $_.fullname -inotmatch '(.exe|.zip)$' }
)
it 'files do not contain leading utf-8 BOM' {
$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
@@ -82,7 +110,25 @@ describe 'Project style constraints' {
}
}
it 'files all have line endings which are CRLF' {
it 'files end with a newline' -skip:$(-not $files_exist) {
$badFiles = @(
foreach ($file in $files)
{
$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)
{
@@ -107,51 +153,7 @@ describe 'Project style constraints' {
}
}
it 'files all end with a newline' {
$badFiles = @(
foreach ($file in $files)
{
$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 'files have leading whitespace consisting only of spaces' {
$badLines = @(
foreach ($file in $files)
{
if ($file.fullname -notmatch '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")"
}
}
it 'files have no lines containing trailing whitespace' {
it 'files have no lines containing trailing whitespace' -skip:$(-not $files_exist) {
$badLines = @(
foreach ($file in $files)
{
@@ -174,4 +176,30 @@ describe 'Project style constraints' {
}
}
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")"
}
}
}