77 lines
2.1 KiB
Go
77 lines
2.1 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestLoadDotEnvFileParsesAndPreservesExistingValues(t *testing.T) {
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, ".env")
|
|
content := "" +
|
|
"# comment\n" +
|
|
"KEY1 = value1\n" +
|
|
"KEY2='quoted value'\n" +
|
|
"KEY3=\"double quoted\"\n" +
|
|
"INVALID_LINE\n" +
|
|
"=missing_key\n"
|
|
|
|
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
|
|
t.Fatalf("write env file: %v", err)
|
|
}
|
|
|
|
t.Setenv("KEY1", "existing")
|
|
_ = os.Unsetenv("KEY2")
|
|
_ = os.Unsetenv("KEY3")
|
|
|
|
if ok := loadDotEnvFile(path); !ok {
|
|
t.Fatal("expected loadDotEnvFile to return true")
|
|
}
|
|
|
|
if got := os.Getenv("KEY1"); got != "existing" {
|
|
t.Fatalf("expected KEY1 to preserve existing value, got %q", got)
|
|
}
|
|
if got := os.Getenv("KEY2"); got != "quoted value" {
|
|
t.Fatalf("expected KEY2 parsed value, got %q", got)
|
|
}
|
|
if got := os.Getenv("KEY3"); got != "double quoted" {
|
|
t.Fatalf("expected KEY3 parsed value, got %q", got)
|
|
}
|
|
}
|
|
|
|
func TestLoadDotEnvCandidatesStopsAtFirstExistingFile(t *testing.T) {
|
|
dir := t.TempDir()
|
|
first := filepath.Join(dir, "first.env")
|
|
second := filepath.Join(dir, "second.env")
|
|
|
|
if err := os.WriteFile(first, []byte("KEY_A=first\nKEY_B=from_first\n"), 0o600); err != nil {
|
|
t.Fatalf("write first env file: %v", err)
|
|
}
|
|
if err := os.WriteFile(second, []byte("KEY_A=second\nKEY_C=from_second\n"), 0o600); err != nil {
|
|
t.Fatalf("write second env file: %v", err)
|
|
}
|
|
|
|
_ = os.Unsetenv("KEY_A")
|
|
_ = os.Unsetenv("KEY_B")
|
|
_ = os.Unsetenv("KEY_C")
|
|
|
|
LoadDotEnvCandidates([]string{filepath.Join(dir, "missing.env"), first, second})
|
|
|
|
if got := os.Getenv("KEY_A"); got != "first" {
|
|
t.Fatalf("expected KEY_A from first file, got %q", got)
|
|
}
|
|
if got := os.Getenv("KEY_B"); got != "from_first" {
|
|
t.Fatalf("expected KEY_B from first file, got %q", got)
|
|
}
|
|
if got := os.Getenv("KEY_C"); got != "" {
|
|
t.Fatalf("expected KEY_C to remain unset, got %q", got)
|
|
}
|
|
}
|
|
|
|
func TestLoadDotEnvFileReturnsFalseWhenMissing(t *testing.T) {
|
|
if ok := loadDotEnvFile(filepath.Join(t.TempDir(), "does-not-exist.env")); ok {
|
|
t.Fatal("expected loadDotEnvFile to return false for missing file")
|
|
}
|
|
}
|