Tests/Add-PsmTest.Tests.ps1

$moduleRoot = Resolve-Path "$PSScriptRoot\.."
$moduleName = Split-Path $moduleRoot -Leaf
Import-Module "$moduleRoot\PSModuleBuilder.psm1"

Describe "Add-PsmTest" -Tag "Unit" {
    
    Context 'When in an initialized module folder' {   
        BeforeAll { 
            Write-Host -for DarkGreen "In Describe BeforeAll" 
            
            Write-Verbose "Creating new directory PsmTest"
            Remove-ItemIfExists "$PSScriptRoot\TestPester\PsmTest" | Out-Null
            New-Item "$PSScriptRoot\TestPester\PsmTest" -ItemType Directory -Force
            
            Write-Verbose "Changing location to PsmTest"
            Set-Location "$PSScriptRoot\TestPester\PsmTest"
            
            Initialize-Psm -y
            Write-PsmManifest
            Get-ChildItem "$PSScriptRoot\TestPester\PsmTest"
        }

        Context "Adding a test" {
            InModuleScope PSModuleBuilder {
                
                $testFunc = "New-GoodInternalFunction"
                $expectedFile = "$PSScriptRoot\TestPester\PsmTest\Tests\$testFunc.Tests.ps1"
                    
                Add-PsmTest "$testFunc"
                it "should have a package xml file" {
                    "$PSScriptRoot\TestPester\PsmTest\module-psd1.xml" | Should -Exist
                }
                it "should have created a $testFunc.ps1 file in the Tests/ folder" {
                    $expectedFile | Should -Exist
                }
                it "$testFunc.Tests.ps1 should be valid powershell" {
                    $contents = Get-Content -Path $expectedFile -ErrorAction Stop
                    $errors = $null
                    $null = [System.Management.Automation.PSParser]::Tokenize($contents, [ref]$errors)
                    $errors.Count | Should -Be 0
                }
            }
        }
    }

    Context 'When in a non-initialized module folder' {   
        BeforeAll { 
            Write-Host -for DarkGreen "In Describe BeforeAll" 
            Set-Location "$PSScriptRoot\TestPester"
        }
        
        InModuleScope PSModuleBuilder {
            it "should throw due to missing package file and module folders" {
                { Add-PsmTest "New-BadFunction" } | Should -Throw
            }
        }
    }

    AfterAll {
        Write-Host "Changing back to moduleRoot: $PSScriptRoot"
        Set-Location $PSScriptRoot
    }
}