diff --git a/Chronometer/Classes/Chronometer.ps1 b/Chronometer/Classes/Chronometer.ps1 index 37eed40..b852f4f 100644 --- a/Chronometer/Classes/Chronometer.ps1 +++ b/Chronometer/Classes/Chronometer.ps1 @@ -9,7 +9,7 @@ class Chronometer { $script = [MonitoredScript]@{Path=$file.Path} $lines = $script.SetScript($file) - if($LineNumber -ne $null) + if($null -eq $LineNumber) { $bpLine = $LineNumber } @@ -31,7 +31,7 @@ class Chronometer [void]ClearBreakpoint() { - if($this.Breakpoint -ne $null -and $this.Breakpoint.count -gt 0) + if($null -ne $this.Breakpoint -and $this.Breakpoint.count -gt 0) { Remove-PSBreakpoint -Breakpoint $this.Breakpoint } diff --git a/Chronometer/Private/Write-ScriptLine.ps1 b/Chronometer/Private/Write-ScriptLine.ps1 index 217728c..39f729f 100644 --- a/Chronometer/Private/Write-ScriptLine.ps1 +++ b/Chronometer/Private/Write-ScriptLine.ps1 @@ -1,6 +1,8 @@ function Write-ScriptLine { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingWriteHost")] + [cmdletbinding()] param( [scriptline] $line, diff --git a/Chronometer/Public/Format-Chronometer.ps1 b/Chronometer/Public/Format-Chronometer.ps1 index ccb3211..f67557c 100644 --- a/Chronometer/Public/Format-Chronometer.ps1 +++ b/Chronometer/Public/Format-Chronometer.ps1 @@ -1,3 +1,5 @@ + + function Format-Chronometer { <# @@ -9,6 +11,7 @@ function Format-Chronometer $resultes = Get-Chronometer -Path $script.fullname -ScriptBlock {Invoke-Pester C:\workspace\PSGraph} $results | Format-Chronometer -WarnAt 20 -ErrorAt 200 #> + [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingWriteHost")] [cmdletbinding(DefaultParameterSetName='Script')] param( # This is a MonitoredScript object from Get-Chronometer diff --git a/Chronometer/Public/Get-Chronometer.ps1 b/Chronometer/Public/Get-Chronometer.ps1 index 0db677d..3cc7211 100644 --- a/Chronometer/Public/Get-Chronometer.ps1 +++ b/Chronometer/Public/Get-Chronometer.ps1 @@ -32,7 +32,7 @@ function Get-Chronometer Write-Verbose "Setting breapoints" $Chronometer.AddBreakpoint($Path,$LineNumber) - if($Chronometer.breakPoint -ne $null) + if($null -ne $Chronometer.breakPoint) { Write-Verbose "Executing Script" [ScriptProfiler]::Start() diff --git a/Tests/Project.Tests.ps1 b/Tests/Project.Tests.ps1 index c12f8bd..0cd47cb 100644 --- a/Tests/Project.Tests.ps1 +++ b/Tests/Project.Tests.ps1 @@ -4,32 +4,46 @@ $moduleName = Split-Path $moduleRoot -Leaf Describe "General project validation: $moduleName" -Tags Build { - $scripts = Get-ChildItem $projectRoot -Include *.ps1,*.psm1,*.psd1 -Recurse | where fullname -notmatch 'classes' + Context "Valid Powershell" { + $scripts = Get-ChildItem $projectRoot -Include *.ps1,*.psm1,*.psd1 -Recurse | where fullname -notmatch 'classes' + # TestCases are splatted to the script so we need hashtables + $testCase = $scripts | Foreach-Object{@{file=$_}} - # TestCases are splatted to the script so we need hashtables - $testCase = $scripts | Foreach-Object{@{file=$_}} - It "Script should be valid powershell" -TestCases $testCase { - param($file) + It "Script should be valid powershell" -TestCases $testCase { + param($file) - $file.fullname | Should Exist + $file.fullname | Should Exist - $contents = Get-Content -Path $file.fullname -ErrorAction Stop - $errors = $null - $null = [System.Management.Automation.PSParser]::Tokenize($contents, [ref]$errors) - $errors.Count | Should Be 0 - } + $contents = Get-Content -Path $file.fullname -ErrorAction Stop + $errors = $null + $null = [System.Management.Automation.PSParser]::Tokenize($contents, [ref]$errors) + $errors.Count | Should Be 0 + } - It "Classes are valid" { - $classes = Get-ChildItem $projectRoot -Include *.ps1,*.psm1,*.psd1 -Recurse | where fullname -match 'classes' - - # Must be imported togehter incase they depend on each other - $contents = Get-Content -Path $classes.FullName | Out-String + It "Classes are valid" { + $classes = Get-ChildItem $projectRoot -Include *.ps1,*.psm1,*.psd1 -Recurse | where fullname -match 'classes' + + # Must be imported togehter incase they depend on each other + $contents = Get-Content -Path $classes.FullName | Out-String - $errors = $null - $null = [System.Management.Automation.PSParser]::Tokenize($contents, [ref]$errors) - $errors.Count | Should Be 0 + $errors = $null + $null = [System.Management.Automation.PSParser]::Tokenize($contents, [ref]$errors) + $errors.Count | Should Be 0 + } } + Context "ScriptAnalyzer" { + + $scripts = Get-ChildItem $moduleRoot -Include *.ps1,*.psm1,*.psd1 -Recurse | where fullname -notmatch 'classes' + $testCase = $scripts | Foreach-Object{@{file=$_}} + + it "Script should pass ScriptAnalyzer rules" -TestCases $testCase { + param($file) + + $file.fullname | Should Exist + Invoke-ScriptAnalyzer $file| Should BeNullOrEmpty + } + } It "Module '$moduleName' can import cleanly" { {Import-Module (Join-Path $moduleRoot "$moduleName.psm1") -force } | Should Not Throw diff --git a/build.ps1 b/build.ps1 index ef97c13..b257254 100644 --- a/build.ps1 +++ b/build.ps1 @@ -11,9 +11,9 @@ param ($Task = 'Default') # Grab nuget bits, install modules, set build variables, start build. Get-PackageProvider -Name NuGet -ForceBootstrap | Out-Null -Install-Module Psake, PSDeploy, BuildHelpers -force +Install-Module Psake, PSDeploy, BuildHelpers, PSScriptAnalyzer -force Install-Module Pester -Force -SkipPublisherCheck -Import-Module Psake, BuildHelpers +Import-Module Psake, BuildHelpers, PSScriptAnalyzer Set-BuildEnvironment