Private/New-TestFile.ps1
Function New-TestFile { <# .SYNOPSIS Creates a basic Test.ps1 file with a Pester configuration. .DESCRIPTION This function generates a Test.ps1 file containing a basic Pester test configuration. The configuration includes code coverage settings and paths for targeting the module's code. .PARAMETER Path Required. Specifies the absolute path where the Test.ps1 file will be created. .PARAMETER Name Required. The name of the module being tested. Used to determine code coverage paths. .EXAMPLE New-TestFile -Path C:\MyProject\Tests -Name MyModule This creates a file named "Test.ps1" in the "C:\MyProject\Tests" directory. .NOTES Author: owen.heaume Version: 1.0.0 - Initial release #> [CmdletBinding()] param ( [Parameter(Mandatory)] [string]$Path, [Parameter(Mandatory)] [string]$Name ) Begin {} Process { try { $TestPath = Join-Path -Path $Path -ChildPath "Test.ps1" -ea Stop } catch { throw "Error joining path: $_" } $TestContent = @" Set-Location `$PSScriptRoot `$config = New-PesterConfiguration `$config.CodeCoverage.Enabled = `$true `$config.CodeCoverage.CoveragePercentTarget = 90 `$config.Output.Verbosity = 'Detailed' `$config.CodeCoverage.Path = @( "`$psscriptroot\..\$name\$name\Public", "`$psscriptroot\..\$name\$name\Private" ) `$config.CodeCoverage.OutputPath = "`$psscriptroot\..\$name\coverage.xml" Invoke-Pester -Configuration `$config "@ try { Write-Host "Creating Test.ps1 file: $TestPath" -ForegroundColor DarkCyan if (Test-Path $TestPath) { Write-Host "Test.ps1 file already exists" -ForegroundColor DarkYellow } else { $TestContent | Out-File -FilePath $TestPath -ea Stop Write-Host "Test.ps1 file created successfully" -ForegroundColor DarkGreen } } catch { throw "Error writing Test.ps1 file: $_" } } End {} } |