From af7e4143cc975f0b1b6315b95f29df023cdaf8fc Mon Sep 17 00:00:00 2001 From: KevinMarquette Date: Sat, 4 Feb 2017 20:53:08 -0800 Subject: [PATCH] reworked class importing, passing pester tests skip ci --- Chronometer/Classes/Chronometer.ps1 | 14 +++++++++++--- Chronometer/chronometer.psm1 | 25 ++++++++++++++++++------- Tests/Help.Tests.ps1 | 3 ++- Tests/Project.Tests.ps1 | 14 +++++++++++++- Tests/Unit.Tests.ps1 | 5 +++-- 5 files changed, 47 insertions(+), 14 deletions(-) diff --git a/Chronometer/Classes/Chronometer.ps1 b/Chronometer/Classes/Chronometer.ps1 index 43386d1..94ad8ed 100644 --- a/Chronometer/Classes/Chronometer.ps1 +++ b/Chronometer/Classes/Chronometer.ps1 @@ -23,12 +23,20 @@ class Chronometer [void]ClearBreakpoint() { - Remove-PSBreakpoint $this.Breakpointbreakpoint + if($this.Breakpoint -ne $null -and $this.Breakpoint.count -gt 0) + { + Remove-PSBreakpoint -Breakpoint $this.Breakpoint + } + } [void] AddExecution([hashtable]$Execution) { - $this.FileMap[$Execution.Breakpoint.Script].AddExecution($Execution) + $script = $Execution.Breakpoint.Script + if($this.FileMap.ContainsKey($script)) + { + $this.FileMap[$script].AddExecution($Execution) + } } } @@ -48,7 +56,7 @@ class MonitoredScript [int] SetScript([string]$Path) { Get-Content -Path $Path | %{ $this.Line.Add( [ScriptLine]@{text=$_})} - return $this.Line.Count() + return $this.Line.Count } [void] AddExecution([hashtable]$node) diff --git a/Chronometer/chronometer.psm1 b/Chronometer/chronometer.psm1 index 3b06336..f63a722 100644 --- a/Chronometer/chronometer.psm1 +++ b/Chronometer/chronometer.psm1 @@ -1,9 +1,23 @@ #Requires -Version 5.0 +[cmdletbinding()] +param() -Write-Verbose "Importing Functions" +Write-Verbose $PSScriptRoot +Write-Verbose 'Import Classes in order because of dependencies' +$classList = @( + 'ScriptLine', + 'ScriptProfiler', + 'Chronometer' +) -# Import everything in sub folders folder -foreach($folder in @('classes', 'private', 'public','includes')) +foreach($class in $classList) +{ + Write-Verbose " Class: $class" + . "$psscriptroot\classes\$class.ps1" +} + +Write-Verbose 'Import everything in sub folders folder' +foreach($folder in @('private', 'public','includes')) { $root = Join-Path -Path $PSScriptRoot -ChildPath $folder if(Test-Path -Path $root) @@ -13,12 +27,9 @@ foreach($folder in @('classes', 'private', 'public','includes')) # dot source each file $files | where-Object{ $_.name -NotLike '*.Tests.ps1'} | - ForEach-Object{Write-Verbose $_.name; . $_.FullName} + ForEach-Object{Write-Verbose $_.basename; . $_.FullName} } } Export-ModuleMember -function (Get-ChildItem -Path "$PSScriptRoot\public\*.ps1").basename -# Hack for my build system that had a conflit with the keyword node -New-Alias -Name 'DiGraph' -Value 'Graph' -ErrorAction SilentlyContinue -Export-ModuleMember -Alias 'DiGraph' diff --git a/Tests/Help.Tests.ps1 b/Tests/Help.Tests.ps1 index 6f4d3fb..7a61ead 100644 --- a/Tests/Help.Tests.ps1 +++ b/Tests/Help.Tests.ps1 @@ -2,9 +2,10 @@ $projectRoot = Resolve-Path "$PSScriptRoot\.." $moduleRoot = Split-Path (Resolve-Path "$projectRoot\*\*.psm1") $moduleName = Split-Path $moduleRoot -Leaf -Import-Module (Join-Path $moduleRoot "$moduleName.psm1") -force Describe "Help tests for $moduleName" -Tags Build { + + Import-Module (Join-Path $moduleRoot "$moduleName.psm1") -force $functions = Get-Command -Module $moduleName $help = $functions | %{Get-Help $_.name} diff --git a/Tests/Project.Tests.ps1 b/Tests/Project.Tests.ps1 index 5ed72b3..c12f8bd 100644 --- a/Tests/Project.Tests.ps1 +++ b/Tests/Project.Tests.ps1 @@ -4,7 +4,7 @@ $moduleName = Split-Path $moduleRoot -Leaf Describe "General project validation: $moduleName" -Tags Build { - $scripts = Get-ChildItem $projectRoot -Include *.ps1,*.psm1,*.psd1 -Recurse + $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=$_}} @@ -19,6 +19,18 @@ Describe "General project validation: $moduleName" -Tags Build { $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 + + $errors = $null + $null = [System.Management.Automation.PSParser]::Tokenize($contents, [ref]$errors) + $errors.Count | Should Be 0 + } + + It "Module '$moduleName' can import cleanly" { {Import-Module (Join-Path $moduleRoot "$moduleName.psm1") -force } | Should Not Throw } diff --git a/Tests/Unit.Tests.ps1 b/Tests/Unit.Tests.ps1 index 0ea483b..ecb61cc 100644 --- a/Tests/Unit.Tests.ps1 +++ b/Tests/Unit.Tests.ps1 @@ -2,12 +2,13 @@ $projectRoot = Resolve-Path "$PSScriptRoot\.." $moduleRoot = Split-Path (Resolve-Path "$projectRoot\*\*.psd1") $moduleName = Split-Path $moduleRoot -Leaf -Import-Module (Join-Path $moduleRoot "$moduleName.psm1") -force - Describe "Basic unit tests" -Tags Build { + + Import-Module (Join-Path $moduleRoot "$moduleName.psm1") -force Context "Function: Get-Chronometer" { it "Does not throw" { + # Get-Chronometer -Path ScratchFiles\example.ps1 -Script {"Test"} {Get-Chronometer -Path $PSScriptRoot\..\ScratchFiles\example.ps1 -Script {"Test"} } | Should Not Throw } }