Skip to content

Commit

Permalink
test for test files based on function name
Browse files Browse the repository at this point in the history
  • Loading branch information
jworkmanjc committed Sep 29, 2023
1 parent 896b9b3 commit bb1f1ef
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Function Get-FunctionReport {
$FileFullName = $File.FullName
$FileName = $File.Name
$FileBaseName = $File.BaseName

# Parse the file and look for function syntax to identify functions
[regex]$Function_Regex = '(?<=^Function)(.*?)(?=$|\{|\()'
$FunctionContent = Get-Content -Path:($FileFullName)
Expand All @@ -22,10 +23,16 @@ Function Get-FunctionReport {
# Remove the function from the current runspace
$ScriptFunctions | ForEach-Object { Remove-Item -Path:('function:\' + $_) }
# $ScriptFunctions.Visibility
$FolderLocation = If ($FileFullName -like '*Private*') { 'Private' }ElseIf ($FileFullName -like '*Public*') { 'Public' } Else { 'Unknown' }
$FolderLocation = If ($FileFullName -like '*Private*') {
'Private'
} ElseIf ($FileFullName -like '*Public*') {
'Public'
} Else {
'Unknown'
}
# Build dataset to perform validations against
[PSCustomObject]@{
# 'FullName' = $FileFullName;
'FullName' = $File.FullName;
'FileName' = $FileName;
'LineNumber' = $FunctionRegexMatchObject.LineNumber
'FileBaseName' = $FileBaseName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ Describe -Tag:('ModuleValidation') 'Function Format Tests' {
$ModuleRoot = (Get-Item -Path:($PSScriptRoot)).Parent.Parent.FullName
$FunctionList = Get-FunctionReport -Folder:(("$ModuleRoot/Public"), ("$ModuleRoot/Private")) | Where-Object { $_.FileName -notlike 'ScriptBlock_*' }
$FunctionListTestCases = $FunctionList | ForEach-Object {
# "$_.FileFullName"
@{
Content = $_.Content
FileBaseName = $_.FileBaseName
Function = $_.Function
MatchValue = $_.MatchValue
FileName = $_.FileName
FolderLocation = $_.FolderLocation
FullName = $_.FullName
}
}
Return $FunctionListTestCases;
Expand Down Expand Up @@ -51,6 +53,16 @@ Describe -Tag:('ModuleValidation') 'Function Format Tests' {
($MatchValue | Group-Object).Count | Should -Be 1
}
}
Context ('Tests that each public function file has a corresponding .test.ps1 file') {
It ('Test File Exists <FullName>') -TestCases:(Get-FunctionReportTestCases) {
If ($FolderLocation -eq 'Public') {
$newName = ("$($FullName)" -replace "Public", "Tests/Public") -replace ".ps1", ".Tests.ps1"
$newName | should -Exist
$testContent = Get-Content -Path $newName
$testContent | Should -Not -BeNullOrEmpty
}
}
}
}


Expand Down

0 comments on commit bb1f1ef

Please sign in to comment.