Skip to content

Commit

Permalink
migrate pester 4 to 5 and fix code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Romain Tiennot committed Aug 23, 2020
1 parent 0c3fa43 commit 2351f43
Show file tree
Hide file tree
Showing 21 changed files with 60 additions and 56 deletions.
1 change: 1 addition & 0 deletions BuildTasks/GenerateMarkdown.Task.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ task GenerateMarkdown {
Module = $ModuleName
OutputFolder = "$DocsPath\en-US"
WithModulePage = $true
Force = $true
}

# ErrorAction is set to SilentlyContinue so this
Expand Down
2 changes: 1 addition & 1 deletion BuildTasks/ImportDevModule.Task.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

task ImportDevModule {
ImportModule -Path "$Source\$ModuleName.psd1" -Force
Import-BuildModule -Path "$Source\$ModuleName.psd1" -Force
}
4 changes: 2 additions & 2 deletions BuildTasks/ImportModule.Task.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function ImportModule
function Import-BuildModule
{
param(
[string]$path,
Expand Down Expand Up @@ -29,5 +29,5 @@ function ImportModule
}

task ImportModule {
ImportModule -Path $ManifestPath
Import-BuildModule -Path $ManifestPath
}
2 changes: 2 additions & 0 deletions BuildTasks/InvokeBuildInit.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Write-Verbose " ModulePath [$ModulePath]" -Verbose
$Script:Folders = 'Classes', 'Includes', 'Internal', 'Private', 'Public', 'Resources'
Write-Verbose " Folders [$Folders]" -Verbose

$timestamp = Get-Date -Format o | ForEach-Object { $_ -replace ":", "." }
$PSVersion = $PSVersionTable.PSVersion.ToString()
$Script:TestFile = "$BuildRoot\Output\TestResults_PS$PSVersion`_$TimeStamp.xml"
Write-Verbose " TestFile [$TestFile]" -Verbose

Expand Down
46 changes: 23 additions & 23 deletions BuildTasks/Pester.Task.ps1
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
task Pester {
$requiredPercent = $Script:CodeCoveragePercent
Import-Module Pester -Force
$configuration = [PesterConfiguration]::Default
# assing properties & discover via intellisense
$configuration.Run.Path = 'Tests'
$configuration.Filter.Tag = 'Build'
$configuration.Run.PassThru = $true
$configuration.Output.Verbosity = 'Detailed'
$configuration.TestResult.Enabled = $true
$configuration.TestResult.OutputFormat = 'NUnitXml'
$configuration.TestResult.OutputPath = $testFile

$params = @{
OutputFile = $testFile
OutputFormat = 'NUnitXml'
PassThru = $true
Path = 'Tests'
Show = 'Failed', 'Fails', 'Summary'
Tag = 'Build'
}
if ($requiredPercent -gt 0.00) {

if($requiredPercent -gt 0.00)
{
$params['CodeCoverage'] = 'Output\*\*.psm1'
$params['CodeCoverageOutputFile'] = 'Output\codecoverage.xml'
$configuration.CodeCoverage.Enabled = $true
$configuration.CodeCoverage.OutputPath = 'Output\codecoverage.xml'
}

$results = Invoke-Pester @params
if ($results.FailedCount -gt 0)
{
Write-Error -Message "Failed [$($results.FailedCount)] Pester tests."
}
$results = Invoke-Pester -Configuration $configuration
if ($requiredPercent -gt 0.00) {

if($results.codecoverage.NumberOfCommandsAnalyzed -gt 0)
{
$codeCoverage = $results.codecoverage.NumberOfCommandsExecuted / $results.codecoverage.NumberOfCommandsAnalyzed
if ($results.TotalCount -gt 0) {
$CodeCoveragePercent = $results.TotalCount * ($results.PassedCount / 100)
$codeCoverage = [math]::Round($CodeCoveragePercent,2)

if($codeCoverage -lt $requiredPercent)
{
Write-Error ("Failed Code Coverage [{0:P}] below {1:P}" -f $codeCoverage,$requiredPercent)
if ($codeCoverage -lt [Math]::Round($requiredPercent,2)) {
Write-Error ("Failed Code Coverage [{0:P}] below {1:P}" -f $codeCoverage, $requiredPercent)
}
}
} elseif ($results.FailedCount -gt 0) {
Write-Error -Message "Failed [$($results.FailedCount)] Pester tests."
}
}
2 changes: 1 addition & 1 deletion BuildTasks/SetVersion.Task.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
function GetModulePublicInterfaceMap
{
param($Path)
$module = ImportModule -Path $Path -PassThru
$module = Import-BuildModule -Path $Path -PassThru
$exportedCommands = @(
$module.ExportedFunctions.values
$module.ExportedCmdlets.values
Expand Down
2 changes: 1 addition & 1 deletion Docs/en-US/Add-SnowSqlRoleMember.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Add users to role

## EXAMPLES

### EXAMPLE 1
### EXEMPLE 1
```
Add-SnowSqlRoleMember -Role TEST_ROLE -Name TEST_USER
```
Expand Down
2 changes: 1 addition & 1 deletion Docs/en-US/Disable-SnowSqlUser.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Disable user account

## EXAMPLES

### EXAMPLE 1
### EXEMPLE 1
```
Disable-SnowSqlUser -Name TEST_USER
```
Expand Down
2 changes: 1 addition & 1 deletion Docs/en-US/Enable-SnowSqlUser.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Enable user account

## EXAMPLES

### EXAMPLE 1
### EXEMPLE 1
```
Enable-SnowSqlUser -Name TEST_USER
```
Expand Down
2 changes: 1 addition & 1 deletion Docs/en-US/Get-SnowSqlConnection.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Gets the current Snowflake connection

## EXAMPLES

### EXAMPLE 1
### EXEMPLE 1
```
Get-SnowSqlConnection
```
Expand Down
2 changes: 1 addition & 1 deletion Docs/en-US/Get-SnowSqlRole.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Get list of Snowflake roles

## EXAMPLES

### EXAMPLE 1
### EXEMPLE 1
```
Get-SnowSqlRole
```
Expand Down
2 changes: 1 addition & 1 deletion Docs/en-US/Get-SnowSqlRoleMember.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Gets the members of the Snowflake role

## EXAMPLES

### EXAMPLE 1
### EXEMPLE 1
```
Get-SnowSqlRoleMember
```
Expand Down
2 changes: 1 addition & 1 deletion Docs/en-US/Get-SnowSqlUser.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Get list of Snowflake users

## EXAMPLES

### EXAMPLE 1
### EXEMPLE 1
```
Get-SnowSqlUser
```
Expand Down
2 changes: 1 addition & 1 deletion Docs/en-US/New-SnowSqlRole.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Create a new Snowflake role

## EXAMPLES

### EXAMPLE 1
### EXEMPLE 1
```
New-SnowSqlRole -Role TEST_ROLE
```
Expand Down
2 changes: 1 addition & 1 deletion Docs/en-US/New-SnowSqlUser.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Create a new Snowflake user account

## EXAMPLES

### EXAMPLE 1
### EXEMPLE 1
```
New-SnowSqlUser -Name TESTUSER -LoginName TESTUSER@CONTOSO.COM -Description 'AD Account'
```
Expand Down
2 changes: 1 addition & 1 deletion Docs/en-US/Remove-SnowSqlRoleMember.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Remove Snowflake user from role

## EXAMPLES

### EXAMPLE 1
### EXEMPLE 1
```
Remove-SnowSqlRoleMember -Role TEST_ROLE -Name TEST_USER
```
Expand Down
10 changes: 5 additions & 5 deletions ScriptAnalyzerSettings.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# subset of: Error, Warning and Information.
# Uncomment the following line if you only want Errors and Warnings but
# not Information diagnostic records.
Severity = @('Error','Warning')
#Severity = @('Error','Warning')

# Use IncludeRules when you want to run only a subset of the default rule set.
#IncludeRules = @('PSAvoidDefaultValueSwitchParameter',
Expand All @@ -18,17 +18,17 @@
# Use ExcludeRules when you want to run most of the default set of rules except
# for a few rules you wish to "exclude". Note: if a rule is in both IncludeRules
# and ExcludeRules, the rule will be excluded.
ExcludeRules = @('PSUseToExportFieldsInManifest','PSMissingModuleManifestField')
ExcludeRules = @('PSMissingModuleManifestField')

# You can use the following entry to supply parameters to rules that take parameters.
# For instance, the PSAvoidUsingCmdletAliases rule takes a whitelist for aliases you
# want to allow.
Rules = @{
#Rules = @{
# Do not flag 'cd' alias.
PSAvoidUsingCmdletAliases = @{Whitelist = @('Where','Select')}
# PSAvoidUsingCmdletAliases = @{Whitelist = @('Where','Select')}

# Check if your script uses cmdlets that are compatible on PowerShell Core,
# version 6.0.0-alpha, on Linux.
# PSUseCompatibleCmdlets = @{Compatibility = @("core-6.0.0-alpha-linux")}
}
#}
}
2 changes: 1 addition & 1 deletion SnowSQL/SnowSQL.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
RootModule = 'SnowSQL.psm1'

# Version number of this module.
ModuleVersion = '0.1.1'
ModuleVersion = '0.1.3'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down
17 changes: 9 additions & 8 deletions Tests/Project/Help.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
$Script:ModuleRoot = Split-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -Parent
$Script:ModuleName = $Script:ModuleName = Get-ChildItem $ModuleRoot\*\*.psm1 | Select-object -ExpandProperty BaseName

$Script:SourceRoot = Join-Path -Path $ModuleRoot -ChildPath $ModuleName

Describe "Public commands have comment-based or external help" -Tags 'Build' {
$functions = Get-Command -Module $ModuleName
$help = foreach ($function in $functions) {
Expand All @@ -11,20 +13,19 @@ Describe "Public commands have comment-based or external help" -Tags 'Build' {
{
Context $node.Name {
It "Should have a Description or Synopsis" {
($node.Description + $node.Synopsis) | Should Not BeNullOrEmpty
($node.Description + $node.Synopsis) | Should -Not -BeNullOrEmpty
}

It "Should have an Example" {
$node.Examples | Should Not BeNullOrEmpty
It "Should have an Example" {
$node.Examples | Should -Not -BeNullOrEmpty
$node.Examples | Out-String | Should -Match ($node.Name)
}

foreach ($parameter in $node.Parameters.Parameter)
foreach ($par in $node.Parameters.Parameter)
{
if ($parameter -notmatch 'WhatIf|Confirm')
if ($par -notmatch 'WhatIf|Confirm')
{
It "Should have a Description for Parameter [$($parameter.Name)]" {
$parameter.Description.Text | Should Not BeNullOrEmpty
It "Should have a Description for Parameter [$($par.Name)]" {
$par.Description[0].Text.length | Should -BeGreaterOrEqual 1
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions Tests/Project/Module.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ $Script:SourceRoot = Join-Path -Path $ModuleRoot -ChildPath $ModuleName
Describe "All commands pass PSScriptAnalyzer rules" -Tag 'Build' {
$rules = "$ModuleRoot\ScriptAnalyzerSettings.psd1"
$scripts = Get-ChildItem -Path $SourceRoot -Include '*.ps1', '*.psm1', '*.psd1' -Recurse |
Where-Object FullName -notmatch 'Classes'
Where-Object FullName -notmatch 'Classes'

foreach ($script in $scripts)
{
Expand All @@ -18,14 +18,14 @@ Describe "All commands pass PSScriptAnalyzer rules" -Tag 'Build' {
{
It $rule.RuleName {
$message = "{0} Line {1}: {2}" -f $rule.Severity, $rule.Line, $rule.Message
$message | Should Be ""
$message | Should -Be ""
}
}
}
else
{
It "Should not fail any rules" {
$results | Should BeNullOrEmpty
$results | Should -BeNullOrEmpty
}
}
}
Expand All @@ -39,7 +39,7 @@ Describe "Public commands have Pester tests" -Tag 'Build' {
{
$file = Get-ChildItem -Path "$ModuleRoot\Tests" -Include "$command.Tests.ps1" -Recurse
It "Should have a Pester test for [$command]" {
$file.FullName | Should Not BeNullOrEmpty
$file.FullName | Should -Not -BeNullOrEmpty
}
}
}
2 changes: 1 addition & 1 deletion build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Get-PackageProvider -Name 'NuGet' -ForceBootstrap | Out-Null

Install-Module -Name $Script:Modules -Scope $Script:ModuleInstallScope -Force -SkipPublisherCheck

Set-BuildEnvironment
Set-BuildEnvironment -Force
Get-ChildItem Env:BH*
Get-ChildItem Env:APPVEYOR*

Expand Down

0 comments on commit 2351f43

Please sign in to comment.