Skip to content

Commit

Permalink
Unit tests are now running using Pester 5 (dsccommunity#41)
Browse files Browse the repository at this point in the history
* Update unit tests to support Pester 5

* Update build.yaml

* Fix typo in CHANGELOG.md

* Update CHANGELOG.md

* Fix unit test

* Hard-code module name prior to sending in Pester issue

* Fix failing unit test

* Add removal of stub module in AfterAll

* Improve test

* Add comment

* Update comment

* Update tests

* Update more tests

* Update the rest of the tests

* Fix unit test after rebase

* Fix unit test

* Fix HQRM tests

* Fix for codecov.io

* Fix QA test

* Set code coverage threshold

* Lower code coverage threshold a tiny bit for Linux and macOS

* Fix formatting in QA test

* Fix review comments at r5

* Fix missed review comments
  • Loading branch information
johlju authored Jun 25, 2021
1 parent 28d4666 commit 01ead63
Show file tree
Hide file tree
Showing 29 changed files with 4,425 additions and 3,787 deletions.
11 changes: 9 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Added cmdlet `ConvertFrom-DscResourceInstance` wich can be used to convert any
object to in another format. It accepts objects from pipeline. [issue #71](https://github.com/dsccommunity/DscResource.Common/issues/71).
- Added cmdlet `ConvertFrom-DscResourceInstance` which can be used to convert any
object to in another format. It accepts objects from pipeline. [issue #71](https://github.com/dsccommunity/DscResource.Common/issues/71).
- Now code coverage is uploaded to codecov.io.

### Changed

- Unit tests are now running using Pester 5 ([issue #40](https://github.com/dsccommunity/DscResource.Common/issues/40)).
- Excludes the PowerShell module script file _DscResource.Common.psm1_ located
in folder _source_ from the HQRM testing.

## [0.10.2] - 2021-03-24

Expand Down
2 changes: 1 addition & 1 deletion RequiredModules.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

InvokeBuild = 'latest'
PSScriptAnalyzer = 'latest'
Pester = '4.10.1'
Pester = 'latest'
Plaster = 'latest'
ModuleBuilder = 'latest'
ChangelogManagement = 'latest'
Expand Down
52 changes: 34 additions & 18 deletions build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ BuildWorkflow:
- package_module_nupkg

hqrmtest:
- DscResource_Tests_Stop_On_Fail
- Invoke_HQRM_Tests_Stop_On_Fail

test:
- Pester_Tests_Stop_On_Fail
- Convert_Pester_Coverage
- Pester_if_Code_Coverage_Under_Threshold

publish:
Expand All @@ -44,26 +45,39 @@ BuildWorkflow:
####################################################

Pester:
OutputFormat: NUnitXML
Configuration:
Run:
Path:
- tests/QA
- tests/Unit
- tests/Integration
CodeCoverage:
CoveragePercentTarget: 78
OutputPath: JaCoCo_coverage.xml
OutputEncoding: ascii
ExcludeFromCodeCoverage:
Script:
- tests/QA
- tests/Unit
- tests/Integration
ExcludeTag:
Tag:
CodeCoverageThreshold: 50
CodeCoverageOutputFile: JaCoCo_coverage.xml
CodeCoverageOutputFileEncoding: ascii

DscTest:
ExcludeTag:
- 'Common Tests - New Error-Level Script Analyzer Rules'
Tag:
ExcludeSourceFile:
- output
ExcludeModuleFile:
MainGitBranch: main
Pester:
Configuration:
Filter:
Tag:
ExcludeTag:
- Common Tests - New Error-Level Script Analyzer Rules
Output:
Verbosity: Detailed
TestResult:
Enabled: true
OutputFormat: NUnitXML
OutputPath: ./output/testResults/NUnitXml_DscResourceCommon_HQRM.xml
OutputEncoding: UTF-8
TestSuiteName: DscResourceCommonHQRM
Script:
ExcludeSourceFile:
- output
- DscResource.Common
ExcludeModuleFile:
MainGitBranch: main

Resolve-Dependency:
Gallery: 'PSGallery'
Expand All @@ -75,6 +89,8 @@ ModuleBuildTasks:
- '*.build.Sampler.ib.tasks'
Sampler.GitHubTasks:
- '*.ib.tasks'
DscResource.Test:
- 'Task.*'

TaskHeader: |
param($Path)
Expand Down
25 changes: 25 additions & 0 deletions cocdecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
codecov:
require_ci_to_pass: no
# main should be the baseline for reporting
branch: main

comment:
layout: "reach, diff, flags, files"
behavior: default

coverage:
range: 50..80
round: down
precision: 0

status:
project:
default:
# Set the overall project code coverage requirement to 70%
target: 70
patch:
default:
# Set the pull request requirement to not regress overall coverage by more than 5%
# and let codecov.io set the goal for the code changed in the patch.
target: auto
threshold: 5
25 changes: 12 additions & 13 deletions tests/Integration/Public/Set-DscMachineRebootRequired.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
$ProjectPath = "$PSScriptRoot\..\..\.." | Convert-Path
$ProjectName = ((Get-ChildItem -Path $ProjectPath\*\*.psd1).Where{
($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and
$(try
{
Test-ModuleManifest $_.FullName -ErrorAction Stop
}
catch
{
$false
} )
}).BaseName
BeforeAll {
$script:moduleName = 'DscResource.Common'

Import-Module $ProjectName -Force
Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue'

Get-Module -Name $script:moduleName -ListAvailable |
Select-Object -First 1 |
Import-Module -Force -ErrorAction 'Stop'
}

Describe 'Set-DscMachineRebootRequired' -Tag 'Set-DscMachineRebootRequired' {
BeforeAll {
Expand All @@ -36,4 +31,8 @@ Describe 'Set-DscMachineRebootRequired' -Tag 'Set-DscMachineRebootRequired' {
$global:DSCMachineStatus | Should -Be 1
}
}

It 'Should have reverted the value of $global:DSCMachineStatus' {
$global:DSCMachineStatus | Should -Be $script:currentDSCMachineStatus
}
}
81 changes: 36 additions & 45 deletions tests/Integration/Public/Set-PSModulePath.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,41 +1,34 @@
$ProjectPath = "$PSScriptRoot\..\..\.." | Convert-Path
$ProjectName = ((Get-ChildItem -Path $ProjectPath\*\*.psd1).Where{
($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and
$(try
{
Test-ModuleManifest $_.FullName -ErrorAction Stop
}
catch
{
$false
} )
}).BaseName
BeforeDiscovery {
# Determines if we should skip tests.
if ($isWindows -or $PSEdition -eq 'Desktop')
{
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())

Import-Module $ProjectName -Force
$skipTest = -not $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}
else
{
$skipTest = $true
}
}

Describe 'Set-PSModulePath' -Tag 'SetPSModulePath' {
BeforeAll {
$currentPSModulePath = $env:PSModulePath
BeforeAll {
$script:moduleName = 'DscResource.Common'

if ($isWindows -or $PSEdition -eq 'Desktop')
{
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue'

$skipTest = -not $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}
else
{
$skipTest = $true
}
Get-Module -Name $script:moduleName -ListAvailable |
Select-Object -First 1 |
Import-Module -Force -ErrorAction 'Stop'
}

if (-not $skipTest)
{
$currentMachinePSModulePath = [Environment]::GetEnvironmentVariable('PSModulePath', 'Machine')
Describe 'Set-PSModulePath' -Tag 'SetPSModulePath' {
Context 'When updating the session environment variable PSModulePath' {
BeforeAll {
$currentPSModulePath = $env:PSModulePath
}
}

Context 'When updating the session environment variable PSModulePath' {
AfterAll {
AfterEach {
$env:PSModulePath = $currentPSModulePath
}

Expand All @@ -44,31 +37,29 @@ Describe 'Set-PSModulePath' -Tag 'SetPSModulePath' {

$env:PSModulePath | Should -Be 'C:\Module'
}

It 'Should have returned the session PSModulePath to the original value' {
$env:PSModulePath | Should -Be $currentPSModulePath
}
}

Context 'When updating the machine environment variable PSModulePath' {
AfterAll {
if (-not $skipTest)
{
[System.Environment]::SetEnvironmentVariable('PSModulePath', $currentMachinePSModulePath, [System.EnvironmentVariableTarget]::Machine)
}
Context 'When updating the machine environment variable PSModulePath' -Skip:$skipTest {
BeforeAll {
$currentMachinePSModulePath = [Environment]::GetEnvironmentVariable('PSModulePath', 'Machine')
}

It 'Should not throw an error and have set the correct value' -Skip:$skipTest {
AfterEach {
[System.Environment]::SetEnvironmentVariable('PSModulePath', $currentMachinePSModulePath, [System.EnvironmentVariableTarget]::Machine)
}

It 'Should not throw an error and have set the correct value' {
{ Set-PSModulePath -Path 'C:\Module' -Machine } | Should -Not -Throw

[Environment]::GetEnvironmentVariable('PSModulePath', 'Machine') | Should -Be 'C:\Module'
}
}

Context 'When the tests have run for Set-PSModulePath' {
It 'Should have returned the session PSModulePath to the original value' {
$env:PSModulePath | Should -Be $currentPSModulePath
}

It 'Should have returned the machine PSModulePath to the original value' -Skip:$skipTest {
[Environment]::GetEnvironmentVariable('PSModulePath', 'Machine') | Should -Be $currentMachinePSModulePath
}
}
}

Loading

0 comments on commit 01ead63

Please sign in to comment.