Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backend/env: treat empty environment variables as not found #78

Merged
merged 1 commit into from Nov 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 2 additions & 7 deletions backend/env/env.go
Expand Up @@ -13,18 +13,13 @@ import (
// lowercase letters to uppercase.
func NewBackend() backend.Backend {
return backend.Func("env", func(ctx context.Context, key string) ([]byte, error) {
val, ok := os.LookupEnv(key)
if ok {
if val := os.Getenv(key); val != "" {
return []byte(val), nil
}

key = strings.Replace(strings.ToUpper(key), "-", "_", -1)

val, ok = os.LookupEnv(key)
if ok {
if val := os.Getenv(key); val != "" {
return []byte(val), nil
}

return nil, backend.ErrNotFound
})
}
10 changes: 9 additions & 1 deletion backend/env/env_test.go
Expand Up @@ -10,11 +10,19 @@ import (
)

func TestEnvBackend(t *testing.T) {
t.Run("NotFound", func(t *testing.T) {
t.Run("NotFoundBecauseUnset", func(t *testing.T) {
b := NewBackend()

_, err := b.Get(context.Background(), "something that doesn't exist")
require.Equal(t, backend.ErrNotFound, err)

})

t.Run("NotFoundBecauseEmpty", func(t *testing.T) {
b := NewBackend()
os.Setenv("TESTCONFIG", "")
_, err := b.Get(context.Background(), "TESTCONFIG")
require.Equal(t, backend.ErrNotFound, err)
})

t.Run("ExactMatch", func(t *testing.T) {
Expand Down