test/xEnvironmentVariables.Tests.ps1


Describe 'Module Manifest Tests' {
    It 'Exists' {
        $ModuleManifestName = 'xENVironmentVariables.psd1'
        $ModuleManifestPath = "$PSScriptRoot\..\$ModuleManifestName"
        Test-Path -Path $ModuleManifestPath | Should -Be $true
    }
    It 'Passes Test-ModuleManifest' {
        $ModuleManifestName = 'xENVironmentVariables.psd1'
        $ModuleManifestPath = "$PSScriptRoot\..\$ModuleManifestName"
        Test-ModuleManifest -Path $ModuleManifestPath | Should -Not -BeNullOrEmpty
    }
}


Describe 'Tests for xENVironmentVariables' {
    #pester, import module into all contexts
    # $ModuleManifestName = 'xENVironmentVariables.psd1'
    # $ModuleManifestPath = "$ENV:DEFAULT_DIR\..\$ModuleManifestName"
    # Import-Module $ModuleManifestPath
    Context "Tests for getter for current process" {
        It "gets an ENVironment variable for current process" {
            $var = Get-ENVironmentVariable -Name PSModulePath -Scope Process -ShowProperties

            $var.Name | Should -Be "PSModulePath"
            $var.Value | Should -Be $ENV:PSModulePath
            $var.ValueType | Should -Be "String"
            $var.Scope | Should -Be "Process"
            $var.BeforeExpansion | Should -BeNullOrEmpty

            "$var" | Should -Be $ENV:PSModulePath
        }
    }
    Context "Tests for setter for current process" {
        It "sets a basic ENVironment variable for current process" {
            $params = @{
                Name      = "xENVironmentVariables_TEST_SET_PROCESS"
                Value     = "basic_value"
                Scope     = "Process"
                ValueType = "String"
                Inherit   = "Auto"
            }
            $var = Set-ENVironmentVariable @params
            $var.Name | Should -Be "xENVironmentVariables_TEST_SET_PROCESS"
            $var.Value | Should -Be "basic_value"
            $var.ValueType | Should -Be "String"
            $var.Scope | Should -Be "Process"
            $var.BeforeExpansion | Should -BeNullOrEmpty

            $ENV:xENVironmentVariables_TEST_SET_PROCESS | Should -Be "basic_value"
            "$var" | Should -Be "basic_value"

            # clean up
            $ENV:xENVironmentVariables_TEST_SET_PROCESS = $null
        }
        It "sets an ENVironment variable as ExpandString for current process" {
            $params = @{
                Name      = "xENVironmentVariables_TEST_SET_PROCESS_EX"
                Value     = "USERNAME: %USERNAME%"
                Scope     = "Process"
                ValueType = "ExpandString"
                Inherit   = "Auto"
            }
            $var = Set-ENVironmentVariable @params
            $var.Name | Should -Be "xENVironmentVariables_TEST_SET_PROCESS_EX"
            $var.Value | Should -Be "USERNAME: $ENV:USERNAME"
            $var.ValueType | Should -Be "String"
            $var.Scope | Should -Be "Process"
            $var.BeforeExpansion | Should -BeNullOrEmpty
            "$var" | Should -Be "USERNAME: $ENV:USERNAME"

            $ENV:xENVironmentVariables_TEST_SET_PROCESS_EX | Should -Be "USERNAME: $ENV:USERNAME"

            # clean up
            $ENV:xENVironmentVariables_TEST_SET_PROCESS_EX = $null
        }
    }
    Context "Tests for getter and setter for current user" {
        It "sets a basic ENVironment variable for current user" {
            $params = @{
                Name      = "xENVironmentVariables_TEST_SET_USER"
                Value     = "basic_value"
                Scope     = "User"
                ValueType = "String"
                Inherit   = "Auto"
            }
            $var = Set-ENVironmentVariable @params
            $var.Name | Should -Be "xENVironmentVariables_TEST_SET_USER"
            $var.Value | Should -Be "basic_value"
            $var.ValueType | Should -Be "String"
            $var.Scope | Should -Be "User"
            $var.BeforeExpansion | Should -Be "basic_value"
            "$var" | Should -Be "basic_value"

            # manual check
            [System.ENVironment]::GetENVironmentVariable(
                "xENVironmentVariables_TEST_SET_USER",
                "User"
            ) | Should -Be "basic_value"

            # auto inheritance
            Write-Warning "Disabled due to bug"
            # $ENV:xENVironmentVariables_TEST_SET_USER | Should -Be "basic_value"

            # clean up
            [System.ENVironment]::SetENVironmentVariable(
                "xENVironmentVariables_TEST_SET_USER",
                "",
                "User"
            )
            $ENV:xENVironmentVariables_TEST_SET_USER = $null
        }

        It "sets an ENVironment variable as ExpandString for current user" {
            $params = @{
                Name      = "xENVironmentVariables_TEST_SET_USER_EX"
                Value     = "OS: %OS%"
                Scope     = "User"
                ValueType = "ExpandString"
                Inherit   = "Auto"
            }
            $var = Set-ENVironmentVariable @params
            $var.Name | Should -Be "xENVironmentVariables_TEST_SET_USER_EX"
            $var.Value | Should -Be "OS: $ENV:OS"
            $var.ValueType | Should -Be "ExpandString"
            $var.Scope | Should -Be "User"
            $var.BeforeExpansion | Should -Be "OS: %OS%"
            "$var" | Should -Be "OS: $ENV:OS"

            # manual check
            [System.ENVironment]::GetENVironmentVariable(
                "xENVironmentVariables_TEST_SET_USER_EX",
                "User"
            ) | Should -Be "OS: $ENV:OS"

            # auto inheritance
            Write-Warning "Disabled due to bug"
            # $ENV:xENVironmentVariables_TEST_SET_USER_EX | Should -Be "OS: $ENV:OS"

            # clean up
            [System.ENVironment]::SetENVironmentVariable(
                "xENVironmentVariables_TEST_SET_USER_EX",
                "",
                "User"
            )
            $ENV:xENVironmentVariables_TEST_SET_USER_EX = $null
        }
    }
    Context "Tests Get-ENVironmentVariables Output for current process" {
        $BasicGet = [System.ENVironment]::GetENVironmentVariables("Process")
        foreach ($key in $BasicGet.Keys) {
            It "gets the correct value of the $key variable from current process" {
                $var = Get-ENVironmentVariables -Scope Process
                $BasicGetValue = $BasicGet.$key
                $var.$key | Should -Be $BasicGetValue
            }
        }
    }
    Context "Tests Get-ENVironmentVariables Output for User" {
        $BasicGet = [System.ENVironment]::GetENVironmentVariables("User")
        foreach ($key in $BasicGet.Keys) {
            It "gets the correct value of the $key variable from User" {
                $var = Get-ENVironmentVariables -Scope User
                $BasicGetValue = $BasicGet.$key
                $var.$key | Should -Be $BasicGetValue
            }
        }
    }
    Context "Tests Get-ENVironmentVariables Output for Machine" {
        $BasicGet = [System.ENVironment]::GetENVironmentVariables("Machine")
        foreach ($key in $BasicGet.Keys) {
            It "gets the correct value of the $key variable from Machine" {
                $var = Get-ENVironmentVariables -Scope Machine
                $BasicGetValue = $BasicGet.$key
                $var.$key | Should -Be $BasicGetValue
            }
        }
    }
    Context "Tests Get-ENVironmentVariables Output is JSON" {
        It "Outputs the data as JSON" {
            $var = Get-ENVironmentVariables -OutputType JSON
            $var | ConvertFrom-Json | Should -BeOfType [object]
        }
    }
}