Skip to content
Open
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
34 changes: 23 additions & 11 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"
$projectRoot = Split-Path -Path $PSScriptRoot -Parent
$sourceManifest = Join-Path -Path $projectRoot -ChildPath "$Env:BHProjectName/$Env:BHProjectName.psd1"
$moduleVersion = (Import-PowerShellDataFile -Path $sourceManifest).ModuleVersion
$Env:BHBuildOutput = Join-Path $projectRoot "Output/$Env:BHProjectName/$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 @@ -97,18 +97,18 @@ BeforeDiscovery {
'Classes'
) | ForEach-Object {
$path = Join-Path -Path $Env:BHBuildOutput -ChildPath $_
if (Test-Path $path) {
if (Test-Path -Path $path) {
$global:CustomTypes += (Get-ChildItem -Path $path -Recurse -ErrorAction 'SilentlyContinue').BaseName
}
}

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

# Get module commands
$getCommandParameters = @{
Module = (Get-Module $Env:BHProjectName)
Module = (Get-Module -Name $Env:BHProjectName)
CommandType = [System.Management.Automation.CommandTypes[]]'Cmdlet, Function' # Not alias
}
if ($PSVersionTable.PSVersion.Major -lt 6) {
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 Down Expand Up @@ -212,8 +212,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 All @@ -233,7 +245,7 @@ Describe "Test help for <_.Name>" -ForEach $commands {

# Shouldn't find extra parameters in help
It 'finds help parameter in code: <_>' {
$_ -in $parameterNames | Should -Be $true
$_ -in $commandParameterNames | Should -Be $true
}
}
}
6 changes: 3 additions & 3 deletions tests/Meta.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ BeforeAll {
$projectRoot = $PSScriptRoot
}

$allTextFiles = Get-TextFilesList $projectRoot
$allTextFiles = Get-TextFilesList -Root $projectRoot
$unicodeFilesCount = 0
$totalTabsCount = 0
foreach ($textFile in $allTextFiles) {
if (Test-FileUnicode $textFile) {
if (Test-FileUnicode -FileInfo $textFile) {
$unicodeFilesCount++
Write-Warning (
"File $($textFile.FullName) contains 0x00 bytes." +
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 -Pattern "`t" | Foreach-Object {
Write-Warning (
"There are tabs in $fileName." +
' Use Fixer "Get-TextFilesList `$pwd | ConvertTo-SpaceIndentation".'
Expand Down
6 changes: 5 additions & 1 deletion 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,10 +162,11 @@ function Get-UnicodeFilesList {
[OutputType([System.IO.FileInfo])]
param(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$Root
)

$root | Get-TextFilesList | Where-Object {
Test-FileUnicode $_
Test-FileUnicode -FileInfo $_
}
}
Loading