Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pester v5 migration #815

Merged
merged 5 commits into from
Oct 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/CHANGELOG-v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ See [upgrade notes][upgrade-notes] for helpful information when upgrading from p

## Unreleased

What's changed since pre-release v1.8.0-B2110006:

- Engineering:
- Migration of Pester v4 tests to Pester v5. [#478](https://github.com/microsoft/PSRule/issues/478)

## v1.8.0-B2110006 (pre-release)

What's changed since pre-release v1.8.0-B2109022:
Expand Down
34 changes: 26 additions & 8 deletions pipeline.build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,10 @@ task NuGet {

# Synopsis: Install Pester module
task Pester NuGet, {
if ($Null -eq (Get-InstalledModule -Name Pester -RequiredVersion 4.10.1 -ErrorAction Ignore)) {
Install-Module -Name Pester -RequiredVersion 4.10.1 -Scope CurrentUser -Force -SkipPublisherCheck;
if ($Null -eq (Get-InstalledModule -Name Pester -RequiredVersion 5.3.0 -ErrorAction Ignore)) {
Install-Module -Name Pester -RequiredVersion 5.3.0 -Scope CurrentUser -Force -SkipPublisherCheck;
}
Import-Module -Name Pester -RequiredVersion 4.10.1 -Verbose:$False;
Import-Module -Name Pester -RequiredVersion 5.3.0 -Verbose:$False;
}

# Synopsis: Install PSScriptAnalyzer module
Expand All @@ -267,22 +267,40 @@ task platyPS {
# Synopsis: Test the module
task TestModule Pester, PSScriptAnalyzer, {
# Run Pester tests
$pesterParams = @{ Path = $PWD; OutputFile = 'reports/pester-unit.xml'; OutputFormat = 'NUnitXml'; PesterOption = @{ IncludeVSCodeMarker = $True }; PassThru = $True; };
$pesterOptions = @{
Run = @{
Path = $PWD;
PassThru = $True;
}
TestResult = @{
Enabled = $True;
OutputFormat = 'NUnitXml';
OutputPath = 'reports/pester-unit.xml';
};
}

if ($CodeCoverage) {
$pesterParams.Add('CodeCoverage', (Join-Path -Path $PWD -ChildPath 'out/modules/**/*.psm1'));
$pesterParams.Add('CodeCoverageOutputFile', (Join-Path -Path $PWD -ChildPath reports/pester-coverage.xml));
$codeCoverageOptions = @{
Enabled = $True;
OutputPath = (Join-Path -Path $PWD -ChildPath reports/pester-coverage.xml);
Path = (Join-Path -Path $PWD -ChildPath 'out/modules/**/*.psm1');
};

$pesterOptions.Add('CodeCoverage', $codeCoverageOptions);
}

if (!(Test-Path -Path reports)) {
$Null = New-Item -Path reports -ItemType Directory -Force;
}

if ($Null -ne $TestGroup) {
$pesterParams['Tags'] = $TestGroup;
$pesterOptions.Add('Filter', @{ Tag = $TestGroup });
}

$results = Invoke-Pester @pesterParams;
# https://pester.dev/docs/commands/New-PesterConfiguration
$pesterConfiguration = New-PesterConfiguration -Hashtable $pesterOptions;

$results = Invoke-Pester -Configuration $pesterConfiguration;

# Throw an error if pester tests failed
if ($Null -eq $results) {
Expand Down
40 changes: 23 additions & 17 deletions tests/PSRule.Tests/PSRule.AllOf.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,37 @@ param (

)

# Setup error handling
$ErrorActionPreference = 'Stop';
Set-StrictMode -Version latest;
BeforeAll {
# Setup error handling
$ErrorActionPreference = 'Stop';
Set-StrictMode -Version latest;

if ($Env:SYSTEM_DEBUG -eq 'true') {
$VerbosePreference = 'Continue';
}
if ($Env:SYSTEM_DEBUG -eq 'true') {
$VerbosePreference = 'Continue';
}

# Setup tests paths
$rootPath = $PWD;
# Setup tests paths
$rootPath = $PWD;

Import-Module (Join-Path -Path $rootPath -ChildPath out/modules/PSRule) -Force;
$here = (Resolve-Path $PSScriptRoot).Path;
Import-Module (Join-Path -Path $rootPath -ChildPath out/modules/PSRule) -Force;
$here = (Resolve-Path $PSScriptRoot).Path;
}

Describe 'PSRule -- AllOf keyword' -Tag 'AllOf' {
$ruleFilePath = (Join-Path -Path $here -ChildPath 'FromFile.Rule.ps1');
$invokeParams = @{
Path = $ruleFilePath
BeforeAll {
$ruleFilePath = (Join-Path -Path $here -ChildPath 'FromFile.Rule.ps1');
$invokeParams = @{
Path = $ruleFilePath
}
}

Context 'AllOf' {
$testObject = @{
Key = 'Value'
BeforeAll {
$testObject = @{
Key = 'Value'
}
$result = $testObject | Invoke-PSRule @invokeParams -Name 'AllOfTest','AllOfTestNegative';
}
$result = $testObject | Invoke-PSRule @invokeParams -Name 'AllOfTest','AllOfTestNegative';

It 'Should succeed on all positive conditions' {
$filteredResult = $result | Where-Object { $_.RuleName -eq 'AllOfTest' };
Expand Down
38 changes: 22 additions & 16 deletions tests/PSRule.Tests/PSRule.AnyOf.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,37 @@ param (

)

# Setup error handling
$ErrorActionPreference = 'Stop';
Set-StrictMode -Version latest;
BeforeAll {
# Setup error handling
$ErrorActionPreference = 'Stop';
Set-StrictMode -Version latest;

if ($Env:SYSTEM_DEBUG -eq 'true') {
$VerbosePreference = 'Continue';
}
if ($Env:SYSTEM_DEBUG -eq 'true') {
$VerbosePreference = 'Continue';
}

# Setup tests paths
$rootPath = $PWD;
# Setup tests paths
$rootPath = $PWD;

Import-Module (Join-Path -Path $rootPath -ChildPath out/modules/PSRule) -Force;
$here = (Resolve-Path $PSScriptRoot).Path;
Import-Module (Join-Path -Path $rootPath -ChildPath out/modules/PSRule) -Force;
$here = (Resolve-Path $PSScriptRoot).Path;
}

Describe 'PSRule -- AnyOf keyword' -Tag 'AnyOf' {
$ruleFilePath = (Join-Path -Path $here -ChildPath 'FromFile.Rule.ps1');
$invokeParams = @{
Path = $ruleFilePath
BeforeAll {
$ruleFilePath = (Join-Path -Path $here -ChildPath 'FromFile.Rule.ps1');
$invokeParams = @{
Path = $ruleFilePath
}
}

Context 'AnyOf' {
$testObject = @{
Key = 'Value'
BeforeAll {
$testObject = @{
Key = 'Value'
}
$result = $testObject | Invoke-PSRule @invokeParams -Name 'AnyOfTest', 'AnyOfTestNegative';
}
$result = $testObject | Invoke-PSRule @invokeParams -Name 'AnyOfTest', 'AnyOfTestNegative';

It 'Should succeed on any positive conditions' {
$filteredResult = $result | Where-Object { $_.RuleName -eq 'AnyOfTest' };
Expand Down
190 changes: 99 additions & 91 deletions tests/PSRule.Tests/PSRule.Assert.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,97 +8,103 @@
[CmdletBinding()]
param ()

# Setup error handling
$ErrorActionPreference = 'Stop';
Set-StrictMode -Version latest;
BeforeAll {
# Setup error handling
$ErrorActionPreference = 'Stop';
Set-StrictMode -Version latest;

if ($Env:SYSTEM_DEBUG -eq 'true') {
$VerbosePreference = 'Continue';
}
if ($Env:SYSTEM_DEBUG -eq 'true') {
$VerbosePreference = 'Continue';
}

# Setup tests paths
$rootPath = $PWD;
# Setup tests paths
$rootPath = $PWD;

Import-Module (Join-Path -Path $rootPath -ChildPath out/modules/PSRule) -Force;
$here = (Resolve-Path $PSScriptRoot).Path;
Import-Module (Join-Path -Path $rootPath -ChildPath out/modules/PSRule) -Force;
$here = (Resolve-Path $PSScriptRoot).Path;
}

#region PSRule variables

Describe 'PSRule assertions' -Tag 'Assert' {
$ruleFilePath = (Join-Path -Path $here -ChildPath 'FromFileAssert.Rule.ps1');
$invokeParams = @{
Path = $ruleFilePath
BeforeAll {
$ruleFilePath = (Join-Path -Path $here -ChildPath 'FromFileAssert.Rule.ps1');
$invokeParams = @{
Path = $ruleFilePath
}
}

Context '$Assert' {
$testObject = @(
[PSCustomObject]@{
Name = 'TestObject1'
Type = 'TestType'
Value = 'Value1'
Array = 'Item1', 'Item2'
String = 'Value'
OtherField = 'Other'
Int = 1
Bool = $True
Version = '2.0.0'
CompareNumeric = 3
CompareArray = 1, 2, 3
CompareString = 'abc'
CompareDate = [DateTime]::Now.AddDays(3)
InArray = @(
'Item1'
'Item3'
'Item4'
)
Path = $PSCommandPath
ParentPath = $here
Lower = 'test123'
Upper = 'TEST123'
LetterLower = 'test'
LetterUpper = 'TEST'
IsInteger = 1
IsBoolean = $True
IsArray = 1, 2, 3
IsDateTime = [DateTime]::Now.AddDays(4)
}
[PSCustomObject]@{
'$schema' = "http://json-schema.org/draft-07/schema`#"
Name = 'TestObject2'
NotType = 'TestType'
Value = $Null
Array = @()
String = ''
Int = 2
Bool = $False
OtherBool = $False
OtherInt = 2
Version = '1.0.0'
CompareNumeric = 0
CompareArray = @()
CompareString = ''
CompareDate = [DateTime]::Now
InArray = @(
'item1'
'item2'
'item3'
)
InArrayPSObject = [PSObject[]]@(
'item1'
'item2'
'item3'
)
ParentPath = (Join-Path -Path $here -ChildPath 'notapath/template.json')
Lower = 'Test123'
Upper = 'Test123'
LetterLower = 'test123'
LetterUpper = 'TEST123'
IsInteger = '1'
IsBoolean = 'true'
IsArray = '123'
IsDateTime = ([DateTime]::Now.AddDays(4).ToString('o'))
}
)
BeforeAll {
$testObject = @(
[PSCustomObject]@{
Name = 'TestObject1'
Type = 'TestType'
Value = 'Value1'
Array = 'Item1', 'Item2'
String = 'Value'
OtherField = 'Other'
Int = 1
Bool = $True
Version = '2.0.0'
CompareNumeric = 3
CompareArray = 1, 2, 3
CompareString = 'abc'
CompareDate = [DateTime]::Now.AddDays(3)
InArray = @(
'Item1'
'Item3'
'Item4'
)
Path = $PSCommandPath
ParentPath = $here
Lower = 'test123'
Upper = 'TEST123'
LetterLower = 'test'
LetterUpper = 'TEST'
IsInteger = 1
IsBoolean = $True
IsArray = 1, 2, 3
IsDateTime = [DateTime]::Now.AddDays(4)
}
[PSCustomObject]@{
'$schema' = "http://json-schema.org/draft-07/schema`#"
Name = 'TestObject2'
NotType = 'TestType'
Value = $Null
Array = @()
String = ''
Int = 2
Bool = $False
OtherBool = $False
OtherInt = 2
Version = '1.0.0'
CompareNumeric = 0
CompareArray = @()
CompareString = ''
CompareDate = [DateTime]::Now
InArray = @(
'item1'
'item2'
'item3'
)
InArrayPSObject = [PSObject[]]@(
'item1'
'item2'
'item3'
)
ParentPath = (Join-Path -Path $here -ChildPath 'notapath/template.json')
Lower = 'Test123'
Upper = 'Test123'
LetterLower = 'test123'
LetterUpper = 'TEST123'
IsInteger = '1'
IsBoolean = 'true'
IsArray = '123'
IsDateTime = ([DateTime]::Now.AddDays(4).ToString('o'))
}
)
}

It 'In pre-conditions' {
$result = @($testObject | Invoke-PSRule @invokeParams -Name 'Assert.Precondition' -Outcome All -WarningAction SilentlyContinue);
Expand Down Expand Up @@ -802,16 +808,18 @@ Describe 'PSRule assertions' -Tag 'Assert' {
}

Context '$Assert extension' {
$testObject = @(
[PSCustomObject]@{
Name = 'TestObject1'
Type = 'TestType'
}
[PSCustomObject]@{
Name = 'TestObject2'
NotType = 'TestType'
}
)
BeforeAll {
$testObject = @(
[PSCustomObject]@{
Name = 'TestObject1'
Type = 'TestType'
}
[PSCustomObject]@{
Name = 'TestObject2'
NotType = 'TestType'
}
)
}

It 'With Add-Member' {
$result = @($testObject | Invoke-PSRule @invokeParams -Name 'Assert.AddMember');
Expand Down