Merge pull request #10 from KevinMarquette/develop !deploy

PSScriptAnalyzer integration into Pester tests and correction of any issues
This commit is contained in:
Kevin Marquette
2017-02-08 00:48:18 -08:00
committed by GitHub
6 changed files with 62 additions and 39 deletions

View File

@@ -4,12 +4,15 @@ class Chronometer
$Breakpoint = @() $Breakpoint = @()
[void]AddBreakpoint([string[]]$Path, [int[]]$LineNumber) [void]AddBreakpoint([string[]]$Path, [int[]]$LineNumber)
{
if(-not [string]::IsNullOrEmpty($Path))
{ {
foreach($file in (Resolve-Path $Path -ea 0)) foreach($file in (Resolve-Path $Path -ea 0))
{ {
$script = [MonitoredScript]@{Path=$file.Path} $script = [MonitoredScript]@{Path=$file.Path}
$lines = $script.SetScript($file) $lines = $script.SetScript($file)
if($LineNumber -ne $null)
if($null -ne $LineNumber)
{ {
$bpLine = $LineNumber $bpLine = $LineNumber
} }
@@ -28,10 +31,11 @@ class Chronometer
$this.breakPoint += Set-PSBreakpoint @breakpointParam $this.breakPoint += Set-PSBreakpoint @breakpointParam
} }
} }
}
[void]ClearBreakpoint() [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 Remove-PSBreakpoint -Breakpoint $this.Breakpoint
} }

View File

@@ -1,6 +1,8 @@
function Write-ScriptLine function Write-ScriptLine
{ {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingWriteHost","")]
[cmdletbinding()]
param( param(
[scriptline] [scriptline]
$line, $line,

View File

@@ -1,3 +1,5 @@
function Format-Chronometer function Format-Chronometer
{ {
<# <#
@@ -9,6 +11,7 @@ function Format-Chronometer
$resultes = Get-Chronometer -Path $script.fullname -ScriptBlock {Invoke-Pester C:\workspace\PSGraph} $resultes = Get-Chronometer -Path $script.fullname -ScriptBlock {Invoke-Pester C:\workspace\PSGraph}
$results | Format-Chronometer -WarnAt 20 -ErrorAt 200 $results | Format-Chronometer -WarnAt 20 -ErrorAt 200
#> #>
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingWriteHost","")]
[cmdletbinding(DefaultParameterSetName='Script')] [cmdletbinding(DefaultParameterSetName='Script')]
param( param(
# This is a MonitoredScript object from Get-Chronometer # This is a MonitoredScript object from Get-Chronometer

View File

@@ -32,7 +32,7 @@ function Get-Chronometer
Write-Verbose "Setting breapoints" Write-Verbose "Setting breapoints"
$Chronometer.AddBreakpoint($Path,$LineNumber) $Chronometer.AddBreakpoint($Path,$LineNumber)
if($Chronometer.breakPoint -ne $null) if($null -ne $Chronometer.breakPoint -and $null -ne $ScriptBlock)
{ {
Write-Verbose "Executing Script" Write-Verbose "Executing Script"
[ScriptProfiler]::Start() [ScriptProfiler]::Start()

View File

@@ -4,10 +4,11 @@ $moduleName = Split-Path $moduleRoot -Leaf
Describe "General project validation: $moduleName" -Tags Build { Describe "General project validation: $moduleName" -Tags Build {
Context "Valid Powershell" {
$scripts = Get-ChildItem $projectRoot -Include *.ps1,*.psm1,*.psd1 -Recurse | where fullname -notmatch 'classes' $scripts = Get-ChildItem $projectRoot -Include *.ps1,*.psm1,*.psd1 -Recurse | where fullname -notmatch 'classes'
# TestCases are splatted to the script so we need hashtables # TestCases are splatted to the script so we need hashtables
$testCase = $scripts | Foreach-Object{@{file=$_}} $testCase = $scripts | Foreach-Object{@{file=$_}}
It "Script <file> should be valid powershell" -TestCases $testCase { It "Script <file> should be valid powershell" -TestCases $testCase {
param($file) param($file)
@@ -29,7 +30,20 @@ Describe "General project validation: $moduleName" -Tags Build {
$null = [System.Management.Automation.PSParser]::Tokenize($contents, [ref]$errors) $null = [System.Management.Automation.PSParser]::Tokenize($contents, [ref]$errors)
$errors.Count | Should Be 0 $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 <file> should pass ScriptAnalyzer rules" -TestCases $testCase {
param($file)
$file.fullname | Should Exist
Invoke-ScriptAnalyzer $file| Should BeNullOrEmpty
}
}
It "Module '$moduleName' can import cleanly" { It "Module '$moduleName' can import cleanly" {
{Import-Module (Join-Path $moduleRoot "$moduleName.psm1") -force } | Should Not Throw {Import-Module (Join-Path $moduleRoot "$moduleName.psm1") -force } | Should Not Throw

View File

@@ -11,9 +11,9 @@ param ($Task = 'Default')
# Grab nuget bits, install modules, set build variables, start build. # Grab nuget bits, install modules, set build variables, start build.
Get-PackageProvider -Name NuGet -ForceBootstrap | Out-Null 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 Install-Module Pester -Force -SkipPublisherCheck
Import-Module Psake, BuildHelpers Import-Module Psake, BuildHelpers, PSScriptAnalyzer
Set-BuildEnvironment Set-BuildEnvironment