Private/New-SpecTestsPS1File.ps1
Function New-SpecTestsPS1File { <# .SYNOPSIS Creates a tests.ps1 file for a specified PowerShell module. .DESCRIPTION The New-SpecTestsPS1File function generates a tests.ps1 file for a PowerShell module. It creates a PowerShell script file with the name "<ModuleName>.tests.ps1" in the module's "Tests" subdirectory. .PARAMETER moduleName Specifies the name of the module. .PARAMETER modulePath Specifies the path to the module's root directory. .EXAMPLE New-SpecTestsPS1File -moduleName "MyModule" -modulePath "C:\Path\To\Module" This example creates a MyModule.tests.ps1 file for a module named "MyModule" located at "C:\Path\To\Module". .NOTES Author : owen.heaume Version : 1.1 #> [cmdletbinding()] param ( [parameter (mandatory = $true)] [string]$moduleName, [parameter (mandatory = $true)] [string]$modulePath ) $code = @" `$moduleName = '$moduleName' `$testRoot = Split-Path -Path `$MyInvocation.MyCommand.Path -Parent `$modulePath = Join-Path -Path `$testRoot -ChildPath "..\`$moduleName\`$moduleName.psm1" get-module `$moduleName | remove-module -Force Import-Module `$modulePath -Force InModuleScope $moduleName { } "@ $testsPath = Join-Path "$modulePath" "$moduleName\Tests" try { Write-verbose "Creating $testsPath\$moduleName.tests.ps1" new-item -Path $testsPath -Name "$moduleName.tests.ps1" -ItemType File -ea stop Set-Content -Path (Join-Path $testsPath "$modulename.tests.ps1") -Value $code -ea stop return $true } catch { return $false } } |