Skip to content

Commit

Permalink
Merge pull request #23 from andrewrdavidson/release-1.2.0
Browse files Browse the repository at this point in the history
Release 1.2.0 - Include/Exclude tests
  • Loading branch information
andrewrdavidson authored Jan 21, 2021
2 parents 9f064f5 + 8a1255b commit 2e78457
Show file tree
Hide file tree
Showing 12 changed files with 543 additions and 121 deletions.
18 changes: 9 additions & 9 deletions Checks/Module.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ BeforeDiscovery {

}

Describe "Module Tests" {
Describe "Module Tests" -Tag "Module" {

Context "Script: <_.Name> at <_.Directory>" -ForEach $moduleFiles {
Context "Script: <_.Name> at <_.Directory>" -Foreach $moduleFiles {

BeforeEach {

Expand All @@ -35,45 +35,45 @@ Describe "Module Tests" {

}

It "Module should exist" {
It "Module should exist" -Tag "ModuleShouldExist" {

$moduleFile | Should -Exist

}

It "Manifest should exist" {
It "Manifest should exist" -Tag "ManifestShouldExist" {

$manifestFile | Should -Exist

}

It "Manifest should be valid" {
It "Manifest should be valid" -Tag "ValidManifest" {

$manifest = Test-ModuleManifest -Path $manifestFile -ErrorAction SilentlyContinue

$manifest | Should -BeOfType [System.Management.Automation.PSModuleInfo]

}

It "Manifest should export Functions" {
It "Manifest should export Functions" -Tag "ModuleShouldExportFunctions" {

($ExportedCommandsCount) | Should -BeGreaterOrEqual 1

}

It "Module should have Functions" {
It "Module should have Functions" -Tag "ModuleShouldHaveFunctions" {

($CommandInModuleCount) | Should -BeGreaterOrEqual 1

}

It "all exported Functions from Manifest should exist in the Module" {
It "all exported Functions from Manifest should exist in the Module" -Tag "FunctionsFromManifestExistInModule" {

($ExportedCommandsCount -eq $CommandFoundInModuleCount -and $ExportedCommandsCount -ge 1) | Should -BeTrue

}

It "all Functions in the Module should exist in Manifest " {
It "all Functions in the Module should exist in Manifest " -Tag "FunctionsFromModuleExistInManifest" {

($CommandInModuleCount -eq $CommandFoundInManifestCount -and $CommandFoundInManifestCount -ge 1 ) | Should -BeTrue

Expand Down
84 changes: 55 additions & 29 deletions Checks/Script.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ param(
[string[]]$Source,

[parameter(Mandatory = $false)]
[string]$SonarQubeRules
[string[]]$ScriptAnalyzerRulesPath
)

BeforeDiscovery {
Expand All @@ -23,17 +23,37 @@ BeforeDiscovery {

}

if ( -not ($ScriptAnalyzerRulesPath -is [Array])) {
$ScriptAnalyzerRulesPath = @($ScriptAnalyzerRulesPath)
}

$rulesPath = @()

$ScriptAnalyzerRulesPath | ForEach-Object {

$rulesPath += @{
'Path' = $_

}

}

}

Describe "Script Tests" {
Describe "Script Tests" -Tag "Script" {

Context "Script: <_.Name> at <_.Directory>" -ForEach $scriptFiles {
Context "Script: <File.Name> at <File.Directory>" -ForEach $scriptFiles {

BeforeEach {
BeforeAll {

$file = $_

$scriptFile = $_.FullName
}

BeforeEach {

$fileContent = Get-FileContent -Path $_.FullName
$scriptFile = $file.FullName
$fileContent = Get-FileContent -Path $file.FullName

if (-not([string]::IsNullOrEmpty($fileContent))) {
($ParsedFile, $ErrorCount) = Get-ParsedContent -Content $fileContent
Expand All @@ -46,13 +66,13 @@ Describe "Script Tests" {

}

It "check script has valid PowerShell syntax" {
It "check script has valid PowerShell syntax" -Tag "ValidSyntax" {

$ErrorCount | Should -Be 0

}

It "check help must contain required elements" {
It "check help must contain required elements" -Tag "HelpMustContainRequiredElements" {

{

Expand All @@ -67,7 +87,8 @@ Describe "Script Tests" {
Should -Not -Throw

}
It "check help must not contain unspecified elements" {

It "check help must not contain unspecified elements" -Tag "HelpMustContainUnspecifiedElements" {

{

Expand All @@ -83,7 +104,7 @@ Describe "Script Tests" {

}

It "check help elements text is not empty" {
It "check help elements text is not empty" -Tag "HelpElementsNotEmpty" {

{

Expand All @@ -98,7 +119,7 @@ Describe "Script Tests" {

}

It "check help elements Min/Max counts are valid" {
It "check help elements Min/Max counts are valid" -Tag "HelpElementsMinMaxCount" {

{

Expand All @@ -113,23 +134,23 @@ Describe "Script Tests" {

}

It "check script contains [CmdletBinding] attribute" {
It "check script contains [CmdletBinding] attribute" -Tag "ContainsCmdletBinding" {

$cmdletBindingCount = (@(Get-TokenMarker -ParsedContent $ParsedFile -Type "Attribute" -Content "CmdletBinding")).Count

$cmdletBindingCount | Should -Be 1

}

It "check script contains [OutputType] attribute" {
It "check script contains [OutputType] attribute" -Tag "ContainsOutputType" {

$outputTypeCount = (@(Get-TokenMarker -ParsedContent $ParsedFile -Type "Attribute" -Content "OutputType")).Count

$outputTypeCount | Should -Be 1

}

It "check script [OutputType] attribute is not empty" {
It "check script [OutputType] attribute is not empty" -Tag "OutputTypeNotEmpty" {

$outputTypeToken = (Get-Token -ParsedContent $ParsedFile -Type "Attribute" -Content "OutputType")

Expand All @@ -139,15 +160,16 @@ Describe "Script Tests" {

}

It "check script contains param attribute" {
# Note: Disabled because I'm questioning the validity of the rule. So many function haven't got a need for params
# It "check script contains param attribute" -Tag "ContainsParam" {

$paramCount = (@(Get-TokenMarker -ParsedContent $ParsedFile -Type "Keyword" -Content "param")).Count
# $paramCount = (@(Get-TokenMarker -ParsedContent $ParsedFile -Type "Keyword" -Content "param")).Count

$paramCount | Should -Be 1
# $paramCount | Should -Be 1

}
# }

It "check script param block variables have type" {
It "check script param block variables have type" -Tag "ParamVariablesHaveType" {

$parameterVariables = Get-ScriptParameter -Content $fileContent

Expand All @@ -165,7 +187,7 @@ Describe "Script Tests" {

}

It "check .PARAMETER help matches variables in param block" {
It "check .PARAMETER help matches variables in param block" -Tag "HelpMatchesParamVariables" {

{

Expand All @@ -182,43 +204,47 @@ Describe "Script Tests" {

}

It "check script contains no PSScriptAnalyzer suppressions" {
It "check script contains no PSScriptAnalyzer suppressions" -Tag "NoScriptAnalyzerSuppressions" {

$suppressCount = (@(Get-TokenMarker -ParsedContent $ParsedFile -Type "Attribute" -Content "Diagnostics.CodeAnalysis.SuppressMessageAttribute")).Count
$suppressCount | Should -Be 0

$suppressCount = (@(Get-TokenMarker -ParsedContent $ParsedFile -Type "Attribute" -Content "Diagnostics.CodeAnalysis.SuppressMessage")).Count
$suppressCount | Should -Be 0

}

It "check script contains no PSScriptAnalyzer failures" {
It "check script contains no PSScriptAnalyzer failures" -Tag "NoScriptAnalyzerFailures" {

$AnalyserFailures = @(Invoke-ScriptAnalyzer -Path $scriptFile)

($AnalyserFailures | ForEach-Object { $_.Message }) | Should -BeNullOrEmpty

}

It "check script contains no PSScriptAnalyser SonarQube rule failures" {
It "check script contains no PSScriptAnalyser rule failures '<_.Path>" -Tag "NoScriptAnalyzerExtraRulesFailures" -TestCases $rulesPath {

param($Path)

if ( [string]::IsNullOrEmpty($SonarQubeRules) ) {
if ( [string]::IsNullOrEmpty($Path)) {

Set-ItResult -Inconclusive -Because "No SonarQube PSScriptAnalyzer rules folder specified"
Set-ItResult -Inconclusive -Because "Empty ScriptAnalyzerRulesPath '$Path'"

}

if ( -not (Test-Path -Path $SonarQubeRules -ErrorAction SilentlyContinue)) {
if ( -not (Test-Path -Path $Path -ErrorAction SilentlyContinue)) {

Set-ItResult -Inconclusive -Because "SonarQube PSScriptAnalyzer rules not found"
Set-ItResult -Inconclusive -Because "ScriptAnalyzerRulesPath path '$Path' not found"

}

$AnalyserFailures = @(Invoke-ScriptAnalyzer -Path $scriptFile -CustomRulePath $SonarQubeRules)
$AnalyserFailures = @(Invoke-ScriptAnalyzer -Path $scriptFile -CustomRulePath $Path)

$AnalyserFailures | ForEach-Object { $_.Message } | Should -BeNullOrEmpty

}

It "check Import-Module statements have valid format" {
It "check Import-Module statements have valid format" -Tag "ValidImportModuleStatements" {

$importModuleTokens = @($ParsedFile | Where-Object { $_.Type -eq "Command" -and $_.Content -eq "Import-Module" })

Expand Down
6 changes: 3 additions & 3 deletions PSQualityCheck.Functions.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
# Generated by: Andrew Davidson
#
# Generated on: 20/01/2021
# Generated on: 21/01/2021
#

@{
Expand All @@ -12,7 +12,7 @@
RootModule = 'PSQualityCheck.Functions.psm1'

# Version number of this module.
ModuleVersion = '1.1.1'
ModuleVersion = '1.2.0'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down Expand Up @@ -71,7 +71,7 @@ PowerShellVersion = '5.0'
# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = 'Convert-Help', 'Export-FunctionsFromModule', 'Get-FileContent',
'Get-FileList', 'Get-FunctionCount', 'Get-ParsedContent',
'Get-ParsedFile', 'Get-ScriptParameter', 'Get-Token',
'Get-ParsedFile', 'Get-ScriptParameter', 'Get-TagList', 'Get-Token',
'Get-TokenComponent', 'Get-TokenMarker',
'Test-HelpTokensCountIsValid', 'Test-HelpTokensParamsMatch',
'Test-HelpTokensTextIsValid', 'Test-ImportModuleIsValid',
Expand Down
73 changes: 73 additions & 0 deletions PSQualityCheck.Functions.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,79 @@ function Get-ScriptParameter {

}

function Get-TagList {
<#
.SYNOPSIS
Return a list of test tags
.DESCRIPTION
Return a list of test tags from the module and script checks file
.EXAMPLE
($moduleTags, $scriptTags) = Get-TagList
#>
[CmdletBinding()]
[OutputType([System.Object[]])]
param (
)

# Get the list of test tags from the checks files
$moduleTags = @()
$scriptTags = @()

$modulePath = (Get-Module -Name 'PSQualityCheck').ModuleBase

$checksPath = (Join-Path -Path $modulePath -ChildPath "Checks")

Get-Content -Path (Join-Path -Path $checksPath -ChildPath "Module.Tests.ps1") -Raw | ForEach-Object {
$ast = [Management.Automation.Language.Parser]::ParseInput($_, [ref]$null, [ref]$null)
$ast.FindAll( {
param($node)
$node -is [System.Management.Automation.Language.CommandAst] -and
$node.CommandElements[0].Value -eq "Describe" -and
$node.CommandElements[2] -is [System.Management.Automation.Language.CommandParameterAst] -and
$node.CommandElements[2].ParameterName -eq "Tag"
}, $true) | ForEach-Object {
$moduleTags += $_.CommandElements[3].Value
}
$ast.FindAll( {
param($node)
$node -is [System.Management.Automation.Language.CommandAst] -and
$node.CommandElements[0].Value -eq "It" -and
$node.CommandElements[2] -is [System.Management.Automation.Language.CommandParameterAst] -and
$node.CommandElements[2].ParameterName -eq "Tag"
}, $true) | ForEach-Object {
$moduleTags += $_.CommandElements[3].Value
}
}

Get-Content -Path (Join-Path -Path $checksPath -ChildPath "Script.Tests.ps1") -Raw | ForEach-Object {
$ast = [Management.Automation.Language.Parser]::ParseInput($_, [ref]$null, [ref]$null)
$ast.FindAll( {
param($node)
$node -is [System.Management.Automation.Language.CommandAst] -and
$node.CommandElements[0].Value -eq "Describe" -and
$node.CommandElements[2] -is [System.Management.Automation.Language.CommandParameterAst] -and
$node.CommandElements[2].ParameterName -eq "Tag"
}, $true) | ForEach-Object {
$scriptTags += $_.CommandElements[3].Value
}
$ast.FindAll( {
param($node)
$node -is [System.Management.Automation.Language.CommandAst] -and
$node.CommandElements[0].Value -eq "It" -and
$node.CommandElements[2] -is [System.Management.Automation.Language.CommandParameterAst] -and
$node.CommandElements[2].ParameterName -eq "Tag"
}, $true) | ForEach-Object {
$scriptTags += $_.CommandElements[3].Value
}
}

return $moduleTags, $scriptTags

}

function Get-Token {
<#
.SYNOPSIS
Expand Down
Loading

0 comments on commit 2e78457

Please sign in to comment.