Functions/Set-BT_RunbookEnvironment.Tests.ps1

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

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

    # Clear the global PSDefaultParameters
    $Global:PSDefaultParameterValues = @{}

    # Fill in env PSModulePath with some string
    $prevEnvPSModulePath = $env:PSModulePath
    $basePSModulePath = "C:\Users\dummyUser\Documents\WindowsPowerShell\Modules"
    $env:PSModulePath = $basePSModulePath

    # Retrieve the env BTEnvGitHome
    $prevEnvBTEnvGitHome = $env:BtEnvGitHome

    it -TestCases @(
        @{
            environment             = "Beta"
            isRunningOnLocalMachine = $false
            isTestEnvironment       = $false
        },
        @{
            environment             = "Beta"
            isRunningOnLocalMachine = $true
            isTestEnvironment       = $false
        },
        @{
            environment             = "BT"
            isRunningOnLocalMachine = $false
            isTestEnvironment       = $true
        },
        @{
            environment             = "BT"
            isRunningOnLocalMachine = $true
            isTestEnvironment       = $true
        }
    ) "sets the global PSDefaultParameterValues to '<environment>', '<isRunningOnLocalMachine>' and '<isTestEnvironment>'" {
        param ($environment, $isRunningOnLocalMachine, $isTestEnvironment)

        # Call the function
        Set-BT_RunbookEnvironment -Environment $environment -IsRunningOnLocalMachine $isRunningOnLocalMachine -IsTestEnvironment $isTestEnvironment

        # Verify the settings
        $Global:PSDefaultParameterValues["*-BT_*:Environment"] | Should Be $environment
        $Global:PSDefaultParameterValues["*-BT_*:IsRunningOnLocalMachine"] | Should Be $isRunningOnLocalMachine
        $Global:PSDefaultParameterValues["*-BT_*:IsTestEnvironment"] | Should Be $isTestEnvironment
    }

    it -TestCases @(
        @{
            environment             = "Beta"
            isRunningOnLocalMachine = $false
            isTestEnvironment       = $true
            beforePSModulePath      = "$($basePSModulePath)"
            psModulePath            = "$($basePSModulePath)"
        },
        @{
            environment             = "BT"
            isRunningOnLocalMachine = $false
            isTestEnvironment       = $true
            beforePSModulePath      = "$($basePSModulePath)"
            psModulePath            = "$($basePSModulePath)"
        },
        @{
            environment             = "Beta"
            isRunningOnLocalMachine = $true
            isTestEnvironment       = $true
            beforePSModulePath      = "$($basePSModulePath);$($env:BtEnvGitHome)\Runbooks\Modules"
            psModulePath            = "$($basePSModulePath);$($env:BtEnvGitHome)\Runbooks\BetaModules"
        },
        @{
            environment             = "BT"
            isRunningOnLocalMachine = $true
            isTestEnvironment       = $true
            beforePSModulePath      = "$($basePSModulePath);$($env:BtEnvGitHome)\Runbooks\BetaModules"
            psModulePath            = "$($basePSModulePath);$($env:BtEnvGitHome)\Runbooks\Modules"
        },
        @{
            environment             = "Beta"
            isRunningOnLocalMachine = $true
            isTestEnvironment       = $true
            beforePSModulePath      = "$($basePSModulePath);$($env:BtEnvGitHome)\Runbooks\BetaModules"
            psModulePath            = "$($basePSModulePath);$($env:BtEnvGitHome)\Runbooks\BetaModules"
        },
        @{
            environment             = "BT"
            isRunningOnLocalMachine = $true
            isTestEnvironment       = $true
            beforePSModulePath      = "$($basePSModulePath);$($env:BtEnvGitHome)\Runbooks\Modules"
            psModulePath            = "$($basePSModulePath);$($env:BtEnvGitHome)\Runbooks\Modules"
        }
    ) "sets the env PSModule path from '<beforePSModulePath>' to '<psModulePath>' when setting the runbook environment to '<environment>', '<isRunningOnLocalMachine>' and '<isTestEnvironment>'" {
        param ($environment, $isRunningOnLocalMachine, $isTestEnvironment, $beforePSModulePath, $psModulePath)
        # Set the before env PSModulePath
        $env:PSModulePath = $beforePSModulePath

        # Call the function
        Set-BT_RunbookEnvironment -Environment $environment -IsRunningOnLocalMachine $isRunningOnLocalMachine -IsTestEnvironment $isTestEnvironment

        # Verify the env PSModulePath
        $env:PSModulePath | Should Be $psModulePath
    }

    context "when env BTEnvGitHome is not set" {
        # Mock Get-Location
        mock Get-Location { return "$($prevEnvBTEnvGitHome)\Runbooks" }

        # Mock Split-Path to return the correct BTEnvGitHome
        mock Split-Path { return $prevEnvBTEnvGitHome }

        # Set env BTEnvGitHome to an empty string
        $env:BtEnvGitHome = ""

        it -TestCases @(
            @{
                environment             = "Beta"
                isRunningOnLocalMachine = $true
                isTestEnvironment       = $true
                psModulePath            = "$($basePSModulePath);$($prevEnvBTEnvGitHome)\Runbooks\BetaModules"
            },
            @{
                environment             = "BT"
                isRunningOnLocalMachine = $true
                isTestEnvironment       = $true
                psModulePath            = "$($basePSModulePath);$($prevEnvBTEnvGitHome)\Runbooks\Modules"
            }
        ) "sets the env PSModule path to '<psModulePath>' when setting the runbook environment to '<environment>', '<isRunningOnLocalMachine>' and '<isTestEnvironment>'" {
            param ($environment, $isRunningOnLocalMachine, $isTestEnvironment, $psModulePath)

            # Call the function
            Set-BT_RunbookEnvironment -Environment $environment -IsRunningOnLocalMachine $isRunningOnLocalMachine -IsTestEnvironment $isTestEnvironment

            # Verify the mocks
            Assert-MockCalled Get-Location
            Assert-MockCalled Split-Path -ParameterFilter {
                $Path -eq "$($prevEnvBTEnvGitHome)\Runbooks" -and $Parent
            }

            # Verify the env PSModulePath
            $env:PSModulePath | Should Be $psModulePath
        }
    }

    # Set the global PSDefaultParameters back
    $Global:PSDefaultParameterValues["*-BT_*:Environment"] = "Beta"
    $Global:PSDefaultParameterValues["*-BT_*:IsRunningOnLocalMachine"] = $true
    $Global:PSDefaultParameterValues["*-BT_*:IsTestEnvironment"] = $true

    # Set the env PSModulePath back
    $env:PSModulePath = $prevEnvPSModulePath

    # Set the env BTEnvGitHome back
    $env:BtEnvGitHome = $prevEnvBTEnvGitHome
}