Functions/GenXdev.Coding.PowerShell.Modules/Add-MissingGenXdevUnitTests.ps1

################################################################################
function Add-MissingGenXdevUnitTests {

    [CmdletBinding()]
    [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseSingularNouns", "Add-MissingGenXdevUnitTests")]
    param()

    # get all cmdlets that need unit tests
    GenXdev.Helpers\Get-GenXDevCmdlets | Microsoft.PowerShell.Core\ForEach-Object {

        $genXdevCmdlet = $_

        # skip if test file already exists and has content
        if ([IO.File]::Exists($_.ScriptTestFilePath) -and
            ([IO.File]::ReadAllText($_.ScriptTestFilePath).Trim() -ne "")) {

            return
        }

        # create test file content
        $prompt = (@"
################################################################################
Describe `"`$CmdletName`" {
 
    It `"should pass PSScriptAnalyzer rules`" {
 
        # get the script path for analysis
        `$scriptPath = GenXdev.FileSystem\Expand-Path `"`$PSScriptRoot\..\..\Functions\`$FullModuleName\`$CmdLetNoTestName.ps1`"
 
        # run analyzer with explicit settings
        `$analyzerResults = GenXdev.Coding\Invoke-GenXdevScriptAnalyzer ``
            -Path `$scriptPath
 
        [string] `$message = `"`"
        `$analyzerResults | ForEach-Object {
 
            `$message = `$message + @`"
--------------------------------------------------
Rule: `$(`$_.RuleName)``
Description: `$(`$_.Description)
Message: `$(`$_.Message)
``r``n
`"@
        }
 
        `$analyzerResults.Count | Should -Be 0 -Because @`"
The following PSScriptAnalyzer rules are being violated:
`$message
`"@;
    }
}
################################################################################
"@
).Replace(
            "`$CmdletName", $genXdevCmdlet.Name
        ).Replace(
            "`$FullModuleName", $genXdevCmdlet.ModuleName
        ).Replace(
            "`$CmdLetNoTestName", $genXdevCmdlet.Name
        )

        # write test file
        $null = $prompt | Microsoft.PowerShell.Utility\Out-File (
            (GenXdev.FileSystem\Expand-Path ($genXdevCmdlet.ScriptTestFilePath) -CreateDirectory)
        ) -Force

        Microsoft.PowerShell.Utility\Write-Verbose "Created test file: $($genXdevCmdlet.ScriptTestFilePath)"
        Microsoft.PowerShell.Utility\Write-Output $prompt
    }
}
################################################################################