mirror of
https://github.com/joho/godotenv.git
synced 2025-07-08 17:54:04 +00:00
Extract a test helper
This commit is contained in:
@ -5,6 +5,21 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func loadEnvAndCompareValues(t *testing.T, envFileName string, expectedValues map[string]string) {
|
||||||
|
err := Load(envFileName)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Error loading %v", envFileName)
|
||||||
|
}
|
||||||
|
|
||||||
|
for k := range expectedValues {
|
||||||
|
envValue := os.Getenv(k)
|
||||||
|
v := expectedValues[k]
|
||||||
|
if envValue != v {
|
||||||
|
t.Errorf("Mismatch for key '%v': expected '%v' got '%v'", k, v, envValue)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestLoadFileNotFound(t *testing.T) {
|
func TestLoadFileNotFound(t *testing.T) {
|
||||||
err := Load("somefilethatwillneverexistever.env")
|
err := Load("somefilethatwillneverexistever.env")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@ -14,11 +29,6 @@ func TestLoadFileNotFound(t *testing.T) {
|
|||||||
|
|
||||||
func TestLoadPlainEnv(t *testing.T) {
|
func TestLoadPlainEnv(t *testing.T) {
|
||||||
envFileName := "fixtures/plain.env"
|
envFileName := "fixtures/plain.env"
|
||||||
err := Load(envFileName)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Error loading %v", envFileName)
|
|
||||||
}
|
|
||||||
|
|
||||||
plainValues := map[string]string{
|
plainValues := map[string]string{
|
||||||
"OPTION_A": "1",
|
"OPTION_A": "1",
|
||||||
"OPTION_B": "2",
|
"OPTION_B": "2",
|
||||||
@ -27,11 +37,5 @@ func TestLoadPlainEnv(t *testing.T) {
|
|||||||
"OPTION_E": "5",
|
"OPTION_E": "5",
|
||||||
}
|
}
|
||||||
|
|
||||||
for k := range plainValues {
|
loadEnvAndCompareValues(t, envFileName, plainValues)
|
||||||
envValue := os.Getenv(k)
|
|
||||||
v := plainValues[k]
|
|
||||||
if envValue != v {
|
|
||||||
t.Errorf("Mismatch for key '%v': expected '%v' got '%v'", k, v, envValue)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user