Tests/Import-DotEnvValue.Tests.ps1
|
BeforeAll { Set-StrictMode -Version "Latest" . "$PSScriptRoot\..\Source\ConvertFrom-DotEnv.ps1" . "$PSScriptRoot\..\Source\Get-DotEnv.ps1" . "$PSScriptRoot\..\Source\Get-DotEnvValue.ps1" . "$PSScriptRoot\..\Source\Import-DotEnvValue.ps1" } Describe "Import-DotEnvValue" { Context "When the file does not exist" { It "Throws" { $path = Join-Path -Path $TestDrive -ChildPath "missing.env" { Import-DotEnvValue -Path $path -Name "TOOLBOX_DATA" } | Should -Throw } } Context "When the file exists" { BeforeEach { $script:testEnvPath = Join-Path -Path $TestDrive -ChildPath "test.env" @( "# comment line" "TOOLBOX_DATA=dummy-value" ) | Set-Content -Path $testEnvPath } AfterEach { Remove-Item -Path "Env:\TOOLBOX_DATA" -ErrorAction "SilentlyContinue" } It "Sets the environment variable for an existing key" { Import-DotEnvValue -Path $testEnvPath -Name "TOOLBOX_DATA" $env:TOOLBOX_DATA | Should -Be "dummy-value" } It "Does not return a value" { Import-DotEnvValue -Path $testEnvPath -Name "TOOLBOX_DATA" | Should -BeNullOrEmpty } It "Throws when the key does not exist" { { Import-DotEnvValue -Path $testEnvPath -Name "MISSING_KEY" } | Should -Throw } It "Accepts the key name from the pipeline" { "TOOLBOX_DATA" | Import-DotEnvValue -Path $testEnvPath $env:TOOLBOX_DATA | Should -Be "dummy-value" } It "Does nothing under -WhatIf" { Import-DotEnvValue -Path $testEnvPath -Name "TOOLBOX_DATA" -WhatIf $env:TOOLBOX_DATA | Should -BeNullOrEmpty } } } |