Tests/Import-DotEnv.Tests.ps1
|
BeforeAll { Set-StrictMode -Version "Latest" . "$PSScriptRoot\..\Source\ConvertFrom-DotEnv.ps1" . "$PSScriptRoot\..\Source\Import-DotEnv.ps1" } Describe "Import-DotEnv" { Context "When the file does not exist" { It "Warns and does not throw" { $path = Join-Path -Path $TestDrive -ChildPath "missing.env" { Import-DotEnv -Path $path -WarningAction "SilentlyContinue" } | Should -Not -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 } AfterEach { Remove-Item -Path "Env:\KEY_ONE" -ErrorAction "SilentlyContinue" Remove-Item -Path "Env:\KEY_TWO" -ErrorAction "SilentlyContinue" } It "Sets environment variables from valid KEY=VALUE lines" { Import-DotEnv -Path $testEnvPath $env:KEY_ONE | Should -Be "value one" } It "Trims whitespace around keys and values" { Import-DotEnv -Path $testEnvPath $env:KEY_TWO | Should -Be "value two" } It "Accepts the file path from the pipeline" { $testEnvPath | Import-DotEnv $env:KEY_ONE | Should -Be "value one" } It "Does nothing under -WhatIf" { Import-DotEnv -Path $testEnvPath -WhatIf $env:KEY_ONE | Should -BeNullOrEmpty } } 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 { Import-DotEnv -Path $path } | Should -Throw } } } |