2023-05-09 13:14:27 -05:00
|
|
|
#===========================================================================
|
|
|
|
|
# Tests - Functions
|
|
|
|
|
#===========================================================================
|
2026-07-01 17:15:26 -05:00
|
|
|
|
|
|
|
|
$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
|
|
|
}
|
2026-07-01 17:15:26 -05:00
|
|
|
)
|
2024-01-12 00:34:41 -06:00
|
|
|
|
2026-07-01 17:15:26 -05: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
|
|
|
|
2026-07-01 17:15:26 -05:00
|
|
|
$tokens = $null
|
2024-04-20 18:30:38 -05:00
|
|
|
$syntaxErrors = $null
|
2026-07-01 17:15:26 -05:00
|
|
|
[System.Management.Automation.Language.Parser]::ParseFile($Path, [ref]$tokens, [ref]$syntaxErrors) | Out-Null
|
2024-04-20 18:30:38 -05:00
|
|
|
|
2026-07-01 17:15:26 -05:00
|
|
|
if ($syntaxErrors.Count -ne 0) {
|
|
|
|
|
throw ($syntaxErrors | Out-String)
|
|
|
|
|
}
|
2024-04-20 18:30:38 -05:00
|
|
|
}
|
2023-10-19 17:12:55 -05:00
|
|
|
|
2026-07-01 17:15:26 -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)
|
2024-04-20 18:30:38 -05:00
|
|
|
}
|
2023-05-09 13:14:27 -05:00
|
|
|
|
2026-07-01 17:15:26 -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."
|
|
|
|
|
}
|
2024-04-20 18:30:38 -05:00
|
|
|
}
|
2023-05-09 13:14:27 -05:00
|
|
|
}
|
2023-10-19 17:12:55 -05:00
|
|
|
|
2026-07-01 17:15:26 -05:00
|
|
|
It "imports without throwing" -TestCases $functionCase {
|
|
|
|
|
param([string]$Path)
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
. $Path
|
|
|
|
|
} catch {
|
|
|
|
|
throw "Failed to import ${Path}: $_"
|
2024-04-20 18:30:38 -05:00
|
|
|
}
|
|
|
|
|
}
|
2023-05-09 13:14:27 -05:00
|
|
|
}
|
|
|
|
|
}
|
2024-04-20 18:30:38 -05:00
|
|
|
}
|