Files
winutil/pester/functions.Tests.ps1
T

74 lines
2.6 KiB
PowerShell
Raw Normal View History

+2
2023-05-09 13:14:27 -05:00
#===========================================================================
# Tests - Functions
#===========================================================================
$functionRoot = Join-Path $PSScriptRoot "..\functions"
$functionCases = @(
Get-ChildItem -Path $functionRoot -Filter *.ps1 -Recurse | ForEach-Object {
@{
Name = $_.Name
Path = $_.FullName
}
2024-01-12 00:34:41 -06:00
}
)
2024-01-12 00:34:41 -06:00
Describe "Function source files" {
foreach ($functionCase in $functionCases) {
Context "Checking $($functionCase.Path)" {
It "has no parser errors" -TestCases $functionCase {
param([string]$Path)
2024-01-12 00:34:41 -06:00
$tokens = $null
$syntaxErrors = $null
[System.Management.Automation.Language.Parser]::ParseFile($Path, [ref]$tokens, [ref]$syntaxErrors) | Out-Null
if ($syntaxErrors.Count -ne 0) {
throw ($syntaxErrors | Out-String)
}
}
2023-10-19 17:12:55 -05:00
It "defines top-level functions with approved verb-noun names" -TestCases $functionCase {
param([string]$Path)
$tokens = $null
$syntaxErrors = $null
$approvedVerbs = (Get-Verb).Verb
$ast = [System.Management.Automation.Language.Parser]::ParseFile($Path, [ref]$tokens, [ref]$syntaxErrors)
if ($syntaxErrors.Count -ne 0) {
throw ($syntaxErrors | Out-String)
}
+2
2023-05-09 13:14:27 -05:00
$topLevelFunctions = @(
$ast.EndBlock.Statements |
Where-Object { $_ -is [System.Management.Automation.Language.FunctionDefinitionAst] }
)
if ($topLevelFunctions.Count -eq 0) {
throw "No top-level function was found in $Path."
}
foreach ($function in $topLevelFunctions) {
if ($function.Name -notmatch '^[A-Za-z]+-[A-Za-z0-9]+$') {
throw "Function '$($function.Name)' does not use Verb-Noun naming."
}
$verb = ($function.Name -split '-', 2)[0]
if ($approvedVerbs -notcontains $verb) {
throw "Function '$($function.Name)' does not use an approved PowerShell verb."
}
}
+2
2023-05-09 13:14:27 -05:00
}
2023-10-19 17:12:55 -05:00
It "imports without throwing" -TestCases $functionCase {
param([string]$Path)
try {
. $Path
} catch {
throw "Failed to import ${Path}: $_"
}
}
+2
2023-05-09 13:14:27 -05:00
}
}
}