Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 27 additions & 15 deletions tests/Help.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ BeforeDiscovery {
# the values it needs (BHPSModuleManifest, BHProjectName) — when running
# via ./build.ps1 this happens before psake; running tests in isolation
# bypasses that, so we do it here.
Set-BuildEnvironment -Path (Split-Path -Parent $PSScriptRoot) -Force
Set-BuildEnvironment -Path (Split-Path -Path $PSScriptRoot -Parent) -Force
$buildFilePath = Join-Path -Path $PSScriptRoot -ChildPath '..\build.psake.ps1'
$invokePsakeParameters = @{
TaskList = 'Build'
Expand All @@ -82,10 +82,10 @@ BeforeDiscovery {
}

# PowerShellBuild outputs to Output/<ModuleName>/<Version>/, override BHBuildOutput
$projectRoot = Split-Path -Parent $PSScriptRoot
$sourceManifest = Join-Path $projectRoot "$Env:BHProjectName/$Env:BHProjectName.psd1"
$moduleVersion = (Import-PowerShellDataFile -Path $sourceManifest).ModuleVersion
$Env:BHBuildOutput = Join-Path $projectRoot "Output/$Env:BHProjectName/$moduleVersion"
$projectRoot = Split-Path -Path $PSScriptRoot -Parent
$sourceManifest = Join-Path -Path $projectRoot -ChildPath "$Env:BHProjectName/$Env:BHProjectName.psd1"
$moduleVersion = (Import-PowerShellDataFile $sourceManifest).ModuleVersion
$Env:BHBuildOutput = Join-Path -Path $projectRoot -ChildPath "Output/$Env:BHProjectName/$moduleVersion"

# Define the path to the module manifest
$moduleManifestFilename = $Env:BHProjectName + '.psd1'
Expand All @@ -104,7 +104,7 @@ BeforeDiscovery {

# Remove all versions of the module from the session. Pester can't handle multiple versions.
Get-Module $Env:BHProjectName | Remove-Module -Force -ErrorAction 'Ignore'
Import-Module -Name $moduleManifestPath -Verbose:$false -ErrorAction 'Stop'
Import-Module $moduleManifestPath -Verbose:$false -ErrorAction 'Stop'

# Get module commands
$getCommandParameters = @{
Expand All @@ -130,7 +130,7 @@ BeforeAll {
# the values it needs (BHPSModuleManifest, BHProjectName) — when running
# via ./build.ps1 this happens before psake; running tests in isolation
# bypasses that, so we do it here.
Set-BuildEnvironment -Path (Split-Path -Parent $PSScriptRoot) -Force
Set-BuildEnvironment -Path (Split-Path -Path $PSScriptRoot -Parent) -Force
$buildFilePath = Join-Path -Path $PSScriptRoot -ChildPath '..\build.psake.ps1'
$invokePsakeParameters = @{
TaskList = 'Build'
Expand All @@ -148,10 +148,10 @@ Describe "Test help for <_.Name>" -ForEach $commands {
# -ForEach, which Pester evaluates during discovery (before BeforeAll runs).
$command = $_
$commandName = $command.Name
$commandHelp = Get-Help -Name $command.Name -ErrorAction 'SilentlyContinue'
$commandParameters = global:FilterOutCommonParameters -Parameters $command.ParameterSets.Parameters
$commandHelp = Get-Help $command.Name -ErrorAction 'SilentlyContinue'
$commandParameters = global:FilterOutCommonParameters $command.ParameterSets.Parameters
$commandParameterNames = $commandParameters.Name
$helpParameters = global:FilterOutCommonParameters -Parameters $commandHelp.Parameters.Parameter
$helpParameters = global:FilterOutCommonParameters $commandHelp.Parameters.Parameter
$helpParameterNames = $helpParameters.Name
$helpLinks = $commandHelp.relatedLinks.navigationLink.uri | Where-Object { $_ -match '^https?://' }
}
Expand All @@ -160,10 +160,10 @@ Describe "Test help for <_.Name>" -ForEach $commands {
# These variables are needed in both discovery and test phases so we need to duplicate them here
$command = $_
$commandName = $_.Name
$commandHelp = Get-Help -Name $command.Name -ErrorAction 'SilentlyContinue'
$commandParameters = global:FilterOutCommonParameters -Parameters $command.ParameterSets.Parameters
$commandHelp = Get-Help $command.Name -ErrorAction 'SilentlyContinue'
$commandParameters = global:FilterOutCommonParameters $command.ParameterSets.Parameters
$commandParameterNames = $commandParameters.Name
$helpParameters = global:FilterOutCommonParameters -Parameters $commandHelp.Parameters.Parameter
$helpParameters = global:FilterOutCommonParameters $commandHelp.Parameters.Parameter
$helpParameterNames = $helpParameters.Name
}

Expand Down Expand Up @@ -217,8 +217,20 @@ Describe "Test help for <_.Name>" -ForEach $commands {

# Required value in Help should match IsMandatory property of parameter
It 'Has correct [mandatory] value' {
$codeMandatory = $_.IsMandatory.toString()
$parameterHelp.Required | Should -Be $codeMandatory
# Skip parameters that have different mandatory status across parameter sets
$parameterSetsWithParam = $command.ParameterSets | Where-Object { $_.Parameters.Name -contains $parameterName }
$mandatoryValues = $parameterSetsWithParam | ForEach-Object {
($_.Parameters | Where-Object { $_.Name -eq $parameterName }).IsMandatory
} | Sort-Object -Unique

if ($mandatoryValues.Count -gt 1) {
Set-ItResult -Skipped -Because "Parameter '$parameterName' has varying mandatory status across parameter sets"
return
}

$codeMandatory = $_.IsMandatory.toString().ToLower()
$helpRequired = $parameterHelp.Required.ToLower()
$helpRequired | Should -Be $codeMandatory
}

# Parameter type in help should match code
Expand Down
4 changes: 2 additions & 2 deletions tests/Meta.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ BeforeAll {
Set-StrictMode -Version 'Latest'

# Make sure MetaFixers.psm1 is loaded - it contains Get-TextFilesList
Import-Module -Name (Join-Path -Path $PSScriptRoot -ChildPath 'MetaFixers.psm1') -Verbose:$false -Force
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath 'MetaFixers.psm1') -Verbose:$false -Force

$projectRoot = $Env:BHProjectPath
if (-not $projectRoot) {
Expand All @@ -24,7 +24,7 @@ BeforeAll {
$unicodeFilesCount | Should -Be 0

$fileName = $textFile.FullName
(Get-Content $fileName -Raw) | Select-String "`t" | Foreach-Object {
(Get-Content -Path $fileName -Raw) | Select-String "`t" | Foreach-Object {
Write-Warning (
"There are tabs in $fileName." +
' Use Fixer "Get-TextFilesList `$pwd | ConvertTo-SpaceIndentation".'
Expand Down
4 changes: 4 additions & 0 deletions tests/MetaFixers.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function ConvertTo-UTF8 {
[OutputType([void])]
param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[ValidateNotNull()]
[System.IO.FileInfo]$FileInfo
)

Expand Down Expand Up @@ -56,6 +57,7 @@ function ConvertTo-SpaceIndentation {
[OutputType([void])]
param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[ValidateNotNull()]
[System.IO.FileInfo]$FileInfo
)

Expand Down Expand Up @@ -85,6 +87,7 @@ function Get-TextFilesList {
[OutputType([System.IO.FileInfo])]
param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[ValidateNotNullOrEmpty()]
[string]$Root
)

Expand Down Expand Up @@ -159,6 +162,7 @@ function Get-UnicodeFilesList {
[OutputType([System.IO.FileInfo])]
param(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$Root
)

Expand Down
Loading