Clean up Pester tests

This commit is contained in:
Kevin Marquette
2018-12-28 15:01:15 -08:00
parent 2eae9f2bb2
commit e5a0147d71
11 changed files with 312 additions and 164 deletions

View File

@@ -0,0 +1,16 @@
InModuleScope Chronometer {
Describe "Class: MonitoredScript" -Tag Build {
It "Creates an object" {
{[MonitoredScript]::New()} | Should Not Throw
}
It "SetScript()" {
$monitor = [MonitoredScript]::New()
{$monitor.SetScript("$PSScriptRoot\..\..\scratchfiles\example.ps1")} | Should Not Throw
}
}
}

View File

@@ -0,0 +1,16 @@
InModuleScope Chronometer {
Describe "Class: ScriptLine" -Tag Build {
It "Creates an Object" {
{[ScriptLine]::New()} | Should Not Throw
}
It "ToString()" {
{[ScriptLine]::New().toString()} | Should Not Throw
}
It "Creates an Object" {
{[ScriptLine]::New().AddExecutionTime(1)} | Should Not Throw
}
}
}

View File

@@ -0,0 +1,13 @@
InModuleScope Chronometer {
Describe "Class: ScriptProfiler" -Tag Build {
It "Creates an Object" {
{[ScriptProfiler]::New()} | Should Not Throw
}
It "Start()" {
{[ScriptProfiler]::Start()} | Should Not Throw
}
}
}

View File

@@ -0,0 +1,11 @@
Describe "Function: Format-Chronometer" -Tag Build {
It "Does not throw" {
{$null | Format-Chronometer } | Should Not Throw
}
It "Can process a result object without throwing" {
$results = Get-Chronometer -Path $PSScriptRoot\..\ScratchFiles\example.ps1 -Script {. "$PSScriptRoot\..\ScratchFiles\example.ps1"}
$results | Format-Chronometer *>&1 | Should Not BeNullOrEmpty
}
}

View File

@@ -0,0 +1,23 @@
Describe "Function: Get-Chronometer" -Tag Build {
It "Does not throw" {
# Get-Chronometer -Path ScratchFiles\example.ps1 -Script {"Test"}
{Get-Chronometer -Path $PSScriptRoot\..\ScratchFiles\example.ps1 -Script {"Test"} } | Should Not Throw
}
It "Executes a script and gives results" {
# Get-Chronometer -Path ScratchFiles\example.ps1 -Script {"Test"}
$results = Get-Chronometer -Path $PSScriptRoot\..\ScratchFiles\example.ps1 -Script {. "$PSScriptRoot\..\ScratchFiles\example.ps1"}
$results | Should Not BeNullOrEmpty
}
It "Executes a script with linenumbers and gives results" {
# Get-Chronometer -Path ScratchFiles\example.ps1 -Script {"Test"}
$params = @{
Path = "$PSScriptRoot\..\ScratchFiles\example.ps1"
Script = {. "$PSScriptRoot\..\ScratchFiles\example.ps1"}
LineNumber = 2,3,5,6
}
$results = Get-Chronometer @params
$results | Should Not BeNullOrEmpty
}
}

View File

@@ -1,30 +0,0 @@
$projectRoot = Resolve-Path "$PSScriptRoot\.."
$moduleRoot = Split-Path (Resolve-Path "$projectRoot\*\*.psm1")
$moduleName = Split-Path $moduleRoot -Leaf
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}
foreach($node in $help)
{
Context $node.name {
it "has a description" {
$node.description | Should Not BeNullOrEmpty
}
it "has an example" {
$node.examples | Should Not BeNullOrEmpty
}
foreach($parameter in $node.parameters.parameter)
{
it "parameter $($parameter.name) has a description" {
$parameter.Description.text | Should Not BeNullOrEmpty
}
}
}
}
}

View File

@@ -1,51 +0,0 @@
$projectRoot = Resolve-Path "$PSScriptRoot\.."
$moduleRoot = Split-Path (Resolve-Path "$projectRoot\*\*.psd1")
$moduleName = Split-Path $moduleRoot -Leaf
Describe "General project validation: $moduleName" -Tags Build {
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=$_}}
It "Script <file> should be valid powershell" -TestCases $testCase {
param($file)
$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
}
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
}
}
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" {
{Import-Module (Join-Path $moduleRoot "$moduleName.psm1") -force } | Should Not Throw
}
}

View File

@@ -1,83 +0,0 @@
$projectRoot = Resolve-Path "$PSScriptRoot\.."
$moduleRoot = Split-Path (Resolve-Path "$projectRoot\*\*.psd1")
$moduleName = Split-Path $moduleRoot -Leaf
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
}
it "Executes a script and gives results" {
# Get-Chronometer -Path ScratchFiles\example.ps1 -Script {"Test"}
$results = Get-Chronometer -Path $PSScriptRoot\..\ScratchFiles\example.ps1 -Script {. "$PSScriptRoot\..\ScratchFiles\example.ps1"}
$results | Should Not BeNullOrEmpty
}
it "Executes a script with linenumbers and gives results" {
# Get-Chronometer -Path ScratchFiles\example.ps1 -Script {"Test"}
$params = @{
Path = "$PSScriptRoot\..\ScratchFiles\example.ps1"
Script = {. "$PSScriptRoot\..\ScratchFiles\example.ps1"}
LineNumber = 2,3,5,6
}
$results = Get-Chronometer @params
$results | Should Not BeNullOrEmpty
}
}
Context "Function: Format-Chronometer" {
it "Does not throw" {
{$null | Format-Chronometer } | Should Not Throw
}
it "Can process a result object without throwing" {
$results = Get-Chronometer -Path $PSScriptRoot\..\ScratchFiles\example.ps1 -Script {. "$PSScriptRoot\..\ScratchFiles\example.ps1"}
$results | Format-Chronometer *>&1 | Should Not BeNullOrEmpty
}
}
InModuleScope $moduleName {
Context "Class: ScriptLine" {
it "Creates an Object" {
{[ScriptLine]::New()} | Should Not Throw
}
it "ToString()" {
{[ScriptLine]::New().toString()} | Should Not Throw
}
it "Creates an Object" {
{[ScriptLine]::New().AddExecutionTime(1)} | Should Not Throw
}
}
Context "Class: ScriptProfiler" {
it "Creates an Object" {
{[ScriptProfiler]::New()} | Should Not Throw
}
it "Start()" {
{[ScriptProfiler]::Start()} | Should Not Throw
}
}
Context "Class: MonitoredScript" {
it "Creates an object" {
{[MonitoredScript]::New()} | Should Not Throw
}
it "SetScript()" {
pushd $projectRoot
$monitor = [MonitoredScript]::New()
{$monitor.SetScript(".\scratchfiles\example.ps1")} | Should Not Throw
popd
}
}
}
}