35 lines
955 B
Go
35 lines
955 B
Go
package app
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestLoadDotEnv(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), ".env")
|
|
contents := "# comment\nDOTENV_TEST_ONE=value\nDOTENV_TEST_TWO=Preface Tools - Comic Animator\nDOTENV_TEST_THREE=\"quoted value\"\n"
|
|
if err := os.WriteFile(path, []byte(contents), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Setenv("DOTENV_TEST_ONE", "existing")
|
|
if err := LoadDotEnv(path); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := os.Getenv("DOTENV_TEST_ONE"); got != "existing" {
|
|
t.Fatalf("existing environment overwritten: %q", got)
|
|
}
|
|
if got := os.Getenv("DOTENV_TEST_TWO"); got != "Preface Tools - Comic Animator" {
|
|
t.Fatalf("unquoted value: %q", got)
|
|
}
|
|
if got := os.Getenv("DOTENV_TEST_THREE"); got != "quoted value" {
|
|
t.Fatalf("quoted value: %q", got)
|
|
}
|
|
}
|
|
|
|
func TestLoadDotEnvMissingFile(t *testing.T) {
|
|
if err := LoadDotEnv(filepath.Join(t.TempDir(), "missing")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|