Tests/Get-DotEnv.Tests.ps1

BeforeAll {

    Set-StrictMode -Version "Latest"

    . "$PSScriptRoot\..\Source\ConvertFrom-DotEnv.ps1"
    . "$PSScriptRoot\..\Source\Get-DotEnv.ps1"

}

Describe "Get-DotEnv" {

    Context "When the file does not exist" {

        It "Throws" {

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

            { Get-DotEnv -Path $path } | Should -Throw

        }

    }

    Context "When the file exists" {

        BeforeEach {

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

            @(
                "# comment line"
                "KEY_ONE=value one"
                "KEY_TWO = value two "
            ) | Set-Content -Path $testEnvPath

        }

        It "Returns every key=value pair" {

            $values = Get-DotEnv -Path $testEnvPath

            $values["KEY_ONE"] | Should -Be "value one"
            $values["KEY_TWO"] | Should -Be "value two"

        }

        It "Accepts the file path from the pipeline" {

            $values = $testEnvPath | Get-DotEnv

            $values["KEY_ONE"] | Should -Be "value one"

        }

    }

    Context "When the file has a malformed line" {

        It "Throws" {

            $path = Join-Path -Path $TestDrive -ChildPath "malformed.env"
            "no equals sign here" | Set-Content -Path $path

            { Get-DotEnv -Path $path } | Should -Throw

        }

    }

}