From 9be00e68b1fd3dd1bea5092d932af0a8b484b252 Mon Sep 17 00:00:00 2001 From: Wes Carroll Date: Sun, 21 Jul 2019 12:17:33 -0400 Subject: [PATCH] Update to new test format --- .../Invoke-ZertoFailoverCommit.Tests.ps1 | 4 +- Tests/Public/Invoke-ZertoMoveCommit.Tests.ps1 | 70 +++++++++++++------ .../Public/Invoke-ZertoMoveRollback.Tests.ps1 | 37 +++++----- 3 files changed, 68 insertions(+), 43 deletions(-) diff --git a/Tests/Public/Invoke-ZertoFailoverCommit.Tests.ps1 b/Tests/Public/Invoke-ZertoFailoverCommit.Tests.ps1 index aec697f..b8abfd8 100644 --- a/Tests/Public/Invoke-ZertoFailoverCommit.Tests.ps1 +++ b/Tests/Public/Invoke-ZertoFailoverCommit.Tests.ps1 @@ -6,8 +6,8 @@ Describe $global:function -Tag 'Unit', 'Source', 'Built' { Context "$global:function::Parameter Unit Tests" { - it "$global:function should have exactly 15 parameters defined" { - (get-command $global:function).Parameters.Count | Should -Be 15 + it "$global:function should have exactly 16 parameters defined" { + (get-command $global:function).Parameters.Count | Should -Be 16 } $ParameterTestCases = @( diff --git a/Tests/Public/Invoke-ZertoMoveCommit.Tests.ps1 b/Tests/Public/Invoke-ZertoMoveCommit.Tests.ps1 index 89cf63f..aec697f 100644 --- a/Tests/Public/Invoke-ZertoMoveCommit.Tests.ps1 +++ b/Tests/Public/Invoke-ZertoMoveCommit.Tests.ps1 @@ -1,28 +1,56 @@ #Requires -Modules Pester -$moduleFileName = "ZertoApiWrapper.psd1" -$here = (Split-Path -Parent $MyInvocation.MyCommand.Path).Replace("Tests", "ZertoApiWrapper") -$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") -$file = Get-ChildItem "$here\$sut" -$modulePath = $here -replace "Public", "" -$moduleFile = Get-ChildItem "$modulePath\$moduleFileName" -Get-Module -Name ZertoApiWrapper | Remove-Module -Force -Import-Module $moduleFile -Force +$global:here = (Split-Path -Parent $MyInvocation.MyCommand.Path) +$global:function = ((Split-Path -leaf $MyInvocation.MyCommand.Path).Split('.'))[0] -Describe $file.BaseName -Tag 'Unit' { +Describe $global:function -Tag 'Unit', 'Source', 'Built' { - It "is valid Powershell (Has no script errors)" { - $contents = Get-Content -Path $file -ErrorAction Stop - $errors = $null - $null = [System.Management.Automation.PSParser]::Tokenize($contents, [ref]$errors) - $errors | Should -HaveCount 0 - } + Context "$global:function::Parameter Unit Tests" { - Context "$($file.BaseName)::Parameter Unit Tests" { + it "$global:function should have exactly 15 parameters defined" { + (get-command $global:function).Parameters.Count | Should -Be 15 + } - it "has a mandatory string parameter for the vpgName" { - Get-Command $file.BaseName | Should -HaveParameter vpgName - Get-Command $file.BaseName | Should -HaveParameter vpgName -Type string[] - Get-Command $file.BaseName | Should -HaveParameter vpgName -Mandatory + $ParameterTestCases = @( + @{ParameterName = 'vpgName'; Type = 'String[]'; Mandatory = $true; Validation = 'NotNullOrEmpty' } + @{ParameterName = 'reverseProtection'; Type = 'switch'; Mandatory = $false; Validation = $null } + @{ParameterName = 'whatIf'; Type = 'Switch'; Mandatory = $false; Validation = 'ShouldProcess' } + ) + + It " parameter is of type" -TestCases $ParameterTestCases { + param($ParameterName, $Type, $Mandatory, $Validation) + Get-Command $global:function | Should -HaveParameter $ParameterName -Mandatory:$Mandatory -Type $Type + } + + It " parameter has correct validation setting" -TestCases $ParameterTestCases { + param($ParameterName, $Validation) + Switch ($Validation) { + 'NotNullOrEmpty' { + $attrs = (Get-Command $global:function).Parameters[$ParameterName].Attributes + $attrs.Where{ $_ -is [ValidateNotNullOrEmpty] }.Count | Should -Be 1 + } + + 'ShouldProcess' { + $scriptBlock = (Get-Command $global:function).ScriptBlock + $scriptBlock | Should -match 'SupportsShouldProcess' + $scriptBlock | Should -match '\$PSCmdlet\.ShouldProcess\(.+\)' + } + + $null { + $attrs = (Get-Command $global:function).Parameters[$ParameterName].Attributes + $attrs.TypeId.Count | Should -Be 2 + } + + default { + $true | should be $false -Because "No Validation Selected. Review test cases" + } + } } } -} \ No newline at end of file + + Context "$global:function::Parameter Functional Tests" { + + } +} + +Remove-Variable -Name here -Scope Global +Remove-Variable -Name function -Scope Global diff --git a/Tests/Public/Invoke-ZertoMoveRollback.Tests.ps1 b/Tests/Public/Invoke-ZertoMoveRollback.Tests.ps1 index 89cf63f..53c3383 100644 --- a/Tests/Public/Invoke-ZertoMoveRollback.Tests.ps1 +++ b/Tests/Public/Invoke-ZertoMoveRollback.Tests.ps1 @@ -1,28 +1,25 @@ #Requires -Modules Pester -$moduleFileName = "ZertoApiWrapper.psd1" -$here = (Split-Path -Parent $MyInvocation.MyCommand.Path).Replace("Tests", "ZertoApiWrapper") -$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") -$file = Get-ChildItem "$here\$sut" -$modulePath = $here -replace "Public", "" -$moduleFile = Get-ChildItem "$modulePath\$moduleFileName" -Get-Module -Name ZertoApiWrapper | Remove-Module -Force -Import-Module $moduleFile -Force +$global:here = (Split-Path -Parent $MyInvocation.MyCommand.Path) +$global:function = ((Split-Path -leaf $MyInvocation.MyCommand.Path).Split('.'))[0] -Describe $file.BaseName -Tag 'Unit' { +Describe $global:function -Tag 'Unit', 'Source', 'Built' { - It "is valid Powershell (Has no script errors)" { - $contents = Get-Content -Path $file -ErrorAction Stop - $errors = $null - $null = [System.Management.Automation.PSParser]::Tokenize($contents, [ref]$errors) - $errors | Should -HaveCount 0 - } + Context "$global:function::Parameter Unit Tests" { - Context "$($file.BaseName)::Parameter Unit Tests" { + it "$global:function should have exactly 14 parameters defined" { + (get-command $global:function).Parameters.Count | Should -Be 14 + } it "has a mandatory string parameter for the vpgName" { - Get-Command $file.BaseName | Should -HaveParameter vpgName - Get-Command $file.BaseName | Should -HaveParameter vpgName -Type string[] - Get-Command $file.BaseName | Should -HaveParameter vpgName -Mandatory + Get-Command $global:function | Should -HaveParameter vpgName + Get-Command $global:function | Should -HaveParameter vpgName -Type string[] + Get-Command $global:function | Should -HaveParameter vpgName -Mandatory } } -} \ No newline at end of file + Context "$global:function::Parameter Functional Tests" { + + } +} + +Remove-Variable -Name here -Scope Global +Remove-Variable -Name function -Scope Global