Functions/Get-BT_RunbookEnvironment.Tests.ps1

describe "BitTitan.Runbooks.Common/Get-BT_RunbookEnvironment" -Tag "module", "unit" {

    # Import the function to test
    . "$($PSScriptRoot)\Get-BT_RunbookEnvironment.ps1"

    # Mock Set-BT_RunbookEnvironment so that Get-BT_RunbookEnvironment does not affect anything external
    mock Set-BT_RunbookEnvironment {}

    it "returns the default settings when the environment settings have not been set" {
        # Clear the global PSDefaultParameters
        $prevPSDefaultParameters = $Global:PSDefaultParameterValues
        $Global:PSDefaultParameterValues = @{}

        # Call the function
        $btRunbookEnvironment = Get-BT_RunbookEnvironment

        # Verify the output
        $btRunbookEnvironment.Environment | Should Be "Beta"
        $btRunbookEnvironment.IsRunningOnLocalMachine | Should Be $false
        $btRunbookEnvironment.IsTestEnvironment | Should Be $false

        # Set the global PSDefaultParameters back
        $Global:PSDefaultParameterValues = $prevPSDefaultParameters
        $env:IsBtTestEnvironment = $true
    }

    it "sets the environment settings to their default if the environment settings have not been set" {
        # Clear the global PSDefaultParameters
        $prevPSDefaultParameters = $Global:PSDefaultParameterValues
        $Global:PSDefaultParameterValues = @{}

        # Call the function
        Get-BT_RunbookEnvironment | Out-Null

        # Verify the mock
        Assert-MockCalled Set-BT_RunbookEnvironment -Times 1 -Exactly -ParameterFilter {
            $Environment -eq "Beta" -and $isRunningOnLocalMachine -eq $false -and $IsTestEnvironment -eq $false
        } -Scope it

        # Set the global PSDefaultParameters back
        $Global:PSDefaultParameterValues = $prevPSDefaultParameters
        $env:IsBtTestEnvironment = $true
    }

    it "returns the environment settings when they have been set" {
        # Retrieve the global PSDefaultParameters
        $prevPSDefaultParameters = $Global:PSDefaultParameterValues

        # Test for the possible environments
        $environments = @("BT", "Beta")

        # Loop through all the possible settings combinations
        foreach ($environment in $environments) {
            foreach ($isRunningOnLocalMachine in @($true, $false)) {
                foreach ($isTestEnvironment in @($true, $false)) {
                    $Global:PSDefaultParameterValues["*-BT_*:Environment"] = $environment
                    $Global:PSDefaultParameterValues["*-BT_*:IsRunningOnLocalMachine"] = $isRunningOnLocalMachine
                    $Global:PSDefaultParameterValues["*-BT_*:IsTestEnvironment"] = $isTestEnvironment

                    # Call the function
                    $btRunbookEnvironment = Get-BT_RunbookEnvironment

                    # Verify the output
                    $btRunbookEnvironment.Environment | Should Be $environment
                    $btRunbookEnvironment.IsRunningOnLocalMachine | Should Be $isRunningOnLocalMachine
                    $btRunbookEnvironment.IsTestEnvironment | Should Be $isTestEnvironment
                }
            }
        }

        # Set the global PSDefaultParameters back
        $Global:PSDefaultParameterValues = $prevPSDefaultParameters
        $env:IsBtTestEnvironment = $true
    }
}