Tests/Add-PsmInternalFunction.Tests.ps1

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

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

        Context "Adding a function" {
            InModuleScope PSModuleBuilder {
                # if mocks are needed
                # It "should use mocked functionality -- Verified" {
                # Assert-VerifiableMock
                # }
                # It "should use mocked functionality -- Times Called" {
                # Assert-MockCalled Get-Module -Times 1
                # }
                $testFunc = "New-GoodInternalFunction"
                $expectedFile = "$PSScriptRoot\TestPester\PsmInternalFunction\Internal\$testFunc.ps1"
                    
                Add-PsmInternalFunction "$testFunc"
                it "should have a package xml file" {
                    "$PSScriptRoot\TestPester\PsmInternalFunction\module-psd1.xml" | Should -Exist
                }
                it "should have created a $testFunc.ps1 file in the Internal/ folder" {
                    $expectedFile | Should -Exist
                }
                it "$testFunc.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
                }
                it "should not add a function to packagexml.FunctionsToExport -- Null Check" {
                    (Import-Clixml "module-psd1.xml").FunctionsToExport | Should -BeNullOrEmpty
                }
            }
        }
    }

    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-PsmInternalFunction "New-BadFunction" } | Should -Throw
            }
        }
    }

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