Functions/Get-BT_RunbookEnvironment.Tests.ps1

describe "BitTitan.Runbooks.Common/Get-BT_RunbookEnvironment" -Tags "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 'Beta' and `$false 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

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

    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 -Scope it -Times 1 -Exactly -ParameterFilter {
            $Environment -eq "Beta" -and $isRunningOnLocalMachine -eq $false
        }

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

    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)) {
                $Global:PSDefaultParameterValues["*-BT_*:Environment"] = $environment
                $Global:PSDefaultParameterValues["*-BT_*:IsRunningOnLocalMachine"] = $isRunningOnLocalMachine

                # Call the function
                $btRunbookEnvironment = Get-BT_RunbookEnvironment

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

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