Tests/Add-PsmFunction.Tests.ps1

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

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

        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-GoodFunction"
                $expectedFile = "$PSScriptRoot\TestPester\PsmFunction\Public\$testFunc.ps1"
                    
                Add-PsmFunction "$testFunc"
                it "should have a package xml file" {
                    "$PSScriptRoot\TestPester\PsmFunction\module-psd1.xml" | Should -Exist
                }
                it "should have created a $testFunc.ps1 file in the Public/ 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 add a function to packagexml.FunctionsToExport -- Null Check" {
                    (Import-Clixml "module-psd1.xml").FunctionsToExport | Should -Not -BeNullOrEmpty
                }
                it "should add a function to packagexml.FunctionsToExport -- > 0 Check" {
                    (Import-Clixml "module-psd1.xml").FunctionsToExport.Count -gt 0 | Should -Be $true
                }
                it "should add a function to packagexml.FunctionsToExport -- Name Check" {
                    (Import-Clixml "module-psd1.xml").FunctionsToExport[0] -eq "$testFunc" | Should -Be $true
                }
            }
        }
        Context "Adding a function with an alias" {
            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-GoodererFunction"
                $expectedFile = "$PSScriptRoot\TestPester\PsmFunction\Public\$testFunc.ps1"
                 
                Add-PsmFunction "$testFunc" "ngf"
                it "should have a package xml file" {
                    "$PSScriptRoot\TestPester\PsmFunction\module-psd1.xml" | Should -Exist
                }
                it "should have created a $testFunc.ps1 file in the Public/ 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 add a function to packagexml.FunctionsToExport -- Null Check" {
                    (Import-Clixml "module-psd1.xml").FunctionsToExport | Should -Not -BeNullOrEmpty
                }
                it "should add a function to packagexml.FunctionsToExport -- > 0 Check" {
                    (Import-Clixml "module-psd1.xml").FunctionsToExport.Count -gt 1 | Should -Be $true
                }
                it "should add a function to packagexml.FunctionsToExport -- Name Check" {
                    (Import-Clixml "module-psd1.xml").FunctionsToExport[1] -eq "$testFunc" | Should -Be $true
                }
                it "should add an alias to packagexml.AliasesToExport -- Null Check" {
                    (Import-Clixml "module-psd1.xml").AliasesToExport | Should -Not -BeNullOrEmpty
                }
                it "should add an alias to packagexml.AliasesToExport -- > 0 Check" {
                    (Import-Clixml "module-psd1.xml").AliasesToExport.Count -gt 0 | Should -Be $true
                }
                it "should add an alias to packagexml.AliasesToExport -- Name Check" {
                    (Import-Clixml "module-psd1.xml").AliasesToExport[0] -eq "ngf" | Should -Be $true
                }
            }
        }
    }

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

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