Initial
This commit is contained in:
19
tests/Feature.Tests.ps1
Normal file
19
tests/Feature.Tests.ps1
Normal file
@@ -0,0 +1,19 @@
|
||||
$script:ModuleName = 'Chocolatey-tools'
|
||||
|
||||
# Removes all versions of the module from the session before importing
|
||||
Get-Module $ModuleName | Remove-Module
|
||||
|
||||
$ModuleBase = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||||
|
||||
# For tests in .\Tests subdirectory
|
||||
if ((Split-Path $ModuleBase -Leaf) -eq 'Tests') {
|
||||
$ModuleBase = Split-Path $ModuleBase -Parent
|
||||
}
|
||||
|
||||
## This variable is for the VSTS tasks and is to be used for referencing any mock artifacts
|
||||
$Env:ModuleBase = $ModuleBase
|
||||
|
||||
Import-Module $ModuleBase\$ModuleName.psd1 -PassThru -ErrorAction Stop | Out-Null
|
||||
Describe "Basic function feature tests" -Tags Build {
|
||||
|
||||
}
|
||||
2
tests/Help.Exceptions.txt
Normal file
2
tests/Help.Exceptions.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
|
||||
|
||||
95
tests/Help.Tests.ps1
Normal file
95
tests/Help.Tests.ps1
Normal file
@@ -0,0 +1,95 @@
|
||||
$script:ModuleName = 'Chocolatey-tools'
|
||||
|
||||
# Removes all versions of the module from the session before importing
|
||||
Get-Module $ModuleName | Remove-Module
|
||||
|
||||
$ModuleBase = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||||
|
||||
# Get the list of functions we are not going to run tests against
|
||||
$FunctionHelpTestExceptions = Get-Content -Path "$ModuleBase\Help.Exceptions.txt"
|
||||
|
||||
# For tests in .\Tests subdirectory
|
||||
if ((Split-Path $ModuleBase -Leaf) -eq 'Tests') {
|
||||
$ModuleBase = Split-Path $ModuleBase -Parent
|
||||
}
|
||||
|
||||
$Module = Import-Module $ModuleBase\$ModuleName.psd1 -PassThru -ErrorAction Stop
|
||||
$commands = Get-Command -Module $module -CommandType Cmdlet, Function, Workflow # Not alias
|
||||
|
||||
## When testing help, remember that help is cached at the beginning of each session.
|
||||
## To test, restart session.
|
||||
|
||||
foreach ($command in $commands) {
|
||||
$commandName = $command.Name
|
||||
|
||||
# Skip all functions that are on the exclusions list
|
||||
if ($script:FunctionHelpTestExceptions -contains $commandName) { continue } ## may not be correct check with a function that needs exceptions
|
||||
|
||||
# The module-qualified command fails on Microsoft.PowerShell.Archive cmdlets
|
||||
$Help = Get-Help $commandName -ErrorAction SilentlyContinue
|
||||
|
||||
Describe "Test help for $commandName" -Tag Help {
|
||||
|
||||
# If help is not found, synopsis in auto-generated help is the syntax diagram
|
||||
It "should not be auto-generated" {
|
||||
$Help.Synopsis | Should Not BeLike '*`[`<CommonParameters`>`]*'
|
||||
}
|
||||
|
||||
# Should be a description for every function
|
||||
It "gets description for $commandName" {
|
||||
$Help.Description | Should Not BeNullOrEmpty
|
||||
}
|
||||
|
||||
# Should be at least one example
|
||||
It "gets example code from $commandName" {
|
||||
($Help.Examples.Example | Select-Object -First 1).Code | Should Not BeNullOrEmpty
|
||||
}
|
||||
|
||||
# Should be at least one example description
|
||||
It "gets example help from $commandName" {
|
||||
($Help.Examples.Example.Remarks | Select-Object -First 1).Text | Should Not BeNullOrEmpty
|
||||
}
|
||||
|
||||
Context "Test parameter help for $commandName" {
|
||||
|
||||
$Common = 'Debug', 'ErrorAction', 'ErrorVariable', 'InformationAction', 'InformationVariable', 'OutBuffer', 'OutVariable',
|
||||
'PipelineVariable', 'Verbose', 'WarningAction', 'WarningVariable'
|
||||
|
||||
$parameters = $command.ParameterSets.Parameters | Sort-Object -Property Name -Unique | Where-Object Name -notin $common
|
||||
$parameterNames = $parameters.Name
|
||||
$HelpParameterNames = $Help.Parameters.Parameter.Name | Sort-Object -Unique
|
||||
|
||||
foreach ($parameter in $parameters) {
|
||||
$parameterName = $parameter.Name
|
||||
$parameterHelp = $Help.parameters.parameter | Where-Object Name -EQ $parameterName
|
||||
|
||||
# Should be a description for every parameter
|
||||
It "gets help for parameter: $parameterName : in $commandName" {
|
||||
$parameterHelp.Description.Text | Should Not BeNullOrEmpty
|
||||
}
|
||||
|
||||
# Required value in Help should match IsMandatory property of parameter
|
||||
It "help for $parameterName parameter in $commandName has correct Mandatory value" {
|
||||
$codeMandatory = $parameter.IsMandatory.toString()
|
||||
$parameterHelp.Required | Should Be $codeMandatory
|
||||
}
|
||||
|
||||
# Parameter type in Help should match code
|
||||
It "help for $commandName has correct parameter type for $parameterName" {
|
||||
$codeType = $parameter.ParameterType.Name
|
||||
# To avoid calling Trim method on a null object.
|
||||
$helpType = if ($parameterHelp.parameterValue) { $parameterHelp.parameterValue.Trim() }
|
||||
$helpType | Should be $codeType
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($helpParm in $HelpParameterNames) {
|
||||
# Shouldn't find extra parameters in help.
|
||||
It "finds help parameter in code: $helpParm" {
|
||||
$helpParm -in $parameterNames | Should Be $true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
0
tests/Project.Exceptions.txt
Normal file
0
tests/Project.Exceptions.txt
Normal file
49
tests/Project.Tests.ps1
Normal file
49
tests/Project.Tests.ps1
Normal file
@@ -0,0 +1,49 @@
|
||||
if (-not(Get-Module -ListAvailable -Name "PSScriptAnalyzer")) {
|
||||
Write-Warning "Installing latest version of PSScriptAnalyzer"
|
||||
# Install PSScriptAnalyzer
|
||||
Install-Module PSScriptAnalyzer -Force -Scope CurrentUser
|
||||
}
|
||||
|
||||
$script:ModuleName = 'Chocolatey-tools'
|
||||
|
||||
$ModuleBase = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||||
|
||||
# Get the list of Pester Tests we are going to skip
|
||||
$PesterTestExceptions = Get-Content -Path "$ModuleBase\Project.Exceptions.txt"
|
||||
|
||||
# For tests in .\Tests subdirectory
|
||||
if ((Split-Path $ModuleBase -Leaf) -eq 'Tests') {
|
||||
$ModuleBase = Split-Path $ModuleBase -Parent
|
||||
}
|
||||
|
||||
Describe "PSScriptAnalyzer rule-sets" -Tag Build , ScriptAnalyzer {
|
||||
|
||||
$Rules = Get-ScriptAnalyzerRule
|
||||
$scripts = Get-ChildItem $ModuleBase -Include *.ps1, *.psm1, *.psd1 -Recurse | Where-Object fullname -notmatch 'classes'
|
||||
|
||||
foreach ( $Script in $scripts )
|
||||
{
|
||||
Context "Script '$($script.FullName)'" {
|
||||
|
||||
foreach ( $rule in $rules )
|
||||
{
|
||||
# Skip all rules that are on the exclusions list
|
||||
if ($PesterTestExceptions -contains $rule.RuleName) { continue }
|
||||
It "Rule [$rule]" {
|
||||
|
||||
(Invoke-ScriptAnalyzer -Path $script.FullName -IncludeRule $rule.RuleName ).Count | Should Be 0
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Describe "General project validation: $moduleName" -Tags Build {
|
||||
BeforeAll {
|
||||
Get-Module $ModuleName | Remove-Module
|
||||
}
|
||||
It "Module '$moduleName' can import cleanly" {
|
||||
{Import-Module $ModuleBase\$ModuleName.psd1 -force } | Should Not Throw
|
||||
}
|
||||
}
|
||||
10
tests/README_TESTS.md
Normal file
10
tests/README_TESTS.md
Normal file
@@ -0,0 +1,10 @@
|
||||
# Understanding Tests
|
||||
|
||||
This template comes with the following files:
|
||||
|
||||
* **Feature.Tests.ps1** - Place tests of the features of the module within this file.
|
||||
* **Help.Exceptions.txt** - If there are functions you wish to exclude from the Comment Based Help tests, place their names in this file.
|
||||
* **Help.Tests.ps1** - Performs tests on the Comment Based Help for public functions within the module. You shouldn't need to edit this file.
|
||||
* **Project.Exceptions.txt** - If there are specific PowerShell Script Analyser tests you wish to disable, place their names in this file. I recommend however to use the exclusions listed at the top of your functions.
|
||||
* **Project.Tests.ps1** - Tests that your module meets all PowerShell Script Analyser tests. Also tests that the module successfully loads.
|
||||
* **Unit.Tests.ps1** - This provides a basic structure for your unit tests. Best practice is to copy this file for each function/CMDLet within the module. This structure allows for the execution of the tests for a specific function/CMDLet.
|
||||
27
tests/Unit.Tests.ps1
Normal file
27
tests/Unit.Tests.ps1
Normal file
@@ -0,0 +1,27 @@
|
||||
$script:ModuleName = 'Chocolatey-tools'
|
||||
|
||||
# Removes all versions of the module from the session before importing
|
||||
Get-Module $ModuleName | Remove-Module
|
||||
|
||||
$ModuleBase = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||||
|
||||
# For tests in .\Tests subdirectory
|
||||
if ((Split-Path $ModuleBase -Leaf) -eq 'Tests') {
|
||||
$ModuleBase = Split-Path $ModuleBase -Parent
|
||||
}
|
||||
|
||||
## This variable is for the VSTS tasks and is to be used for referencing any mock artifacts
|
||||
$Env:ModuleBase = $ModuleBase
|
||||
|
||||
Import-Module $ModuleBase\$ModuleName.psd1 -PassThru -ErrorAction Stop | Out-Null
|
||||
|
||||
# InModuleScope runs the test in module scope.
|
||||
# It creates all variables and functions in module scope.
|
||||
# As a result, test has access to all functions, variables and aliases
|
||||
# in the module even if they're not exported.
|
||||
InModuleScope $script:ModuleName {
|
||||
Describe "Basic function unit tests" -Tags Build , Unit{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user