Tests/Test-DotEnvValue.Tests.ps1

BeforeAll {

    Set-StrictMode -Version "Latest"

    . "$PSScriptRoot\..\Source\ConvertFrom-DotEnv.ps1"
    . "$PSScriptRoot\..\Source\Test-DotEnvValue.ps1"

}

Describe "Test-DotEnvValue" {

    Context "When the file does not exist" {

        It "Returns false" {

            $path = Join-Path -Path $TestDrive -ChildPath "missing.env"

            Test-DotEnvValue -Path $path -Name "TOOLBOX_DATA" | Should -BeFalse

        }

    }

    Context "When the file exists" {

        BeforeEach {

            $script:testEnvPath = Join-Path -Path $TestDrive -ChildPath "test.env"

            @(
                "# comment line"
                "TOOLBOX_DATA=dummy-value"
                "OTHER_KEY = other value "
            ) | Set-Content -Path $testEnvPath

        }

        It "Returns true when the key exists" {

            Test-DotEnvValue -Path $testEnvPath -Name "TOOLBOX_DATA" | Should -BeTrue

        }

        It "Returns false when the key does not exist" {

            Test-DotEnvValue -Path $testEnvPath -Name "MISSING_KEY" | Should -BeFalse

        }

        It "Matches keys regardless of surrounding whitespace" {

            Test-DotEnvValue -Path $testEnvPath -Name "OTHER_KEY" | Should -BeTrue

        }

        It "Accepts the key name from the pipeline" {

            $results = "TOOLBOX_DATA", "MISSING_KEY" | Test-DotEnvValue -Path $testEnvPath

            $results | Should -Be @($true, $false)

        }

    }

}