Skip to content

Commit

Permalink
fix config overrides and non-existing env files
Browse files Browse the repository at this point in the history
  • Loading branch information
cinemast committed Oct 9, 2024
1 parent 61639e8 commit 06abbdb
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
13 changes: 10 additions & 3 deletions tinyviper.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@ type multiResolver struct {
resolvers []Resolver
}

func NewEnvFileResolver(filename string) *EnvFileResolver {
func NewEnvResolver() Resolver {
return EnvResolver{}
}

func NewEnvFileResolver(filename string) Resolver {
readFile, err := os.Open(filename)
if err != nil {
return nil
return EnvFileResolver{make(map[string]string)}
}
defer readFile.Close()
r := &EnvFileResolver{make(map[string]string, 0)}
r := EnvFileResolver{make(map[string]string, 0)}
fileScanner := bufio.NewScanner(readFile)
fileScanner.Split(bufio.ScanLines)
for fileScanner.Scan() {
Expand All @@ -49,6 +53,9 @@ func NewEnvFileResolver(filename string) *EnvFileResolver {

func (m multiResolver) Get(key string) string {
for _, r := range m.resolvers {
if r == nil {
continue
}
v := r.Get(key)
if r.Get(key) != "" {
return v
Expand Down
53 changes: 48 additions & 5 deletions tinyviper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tinyviper

import (
"errors"
"os"
"testing"
)

Expand All @@ -10,8 +11,9 @@ type Config struct {
Email string `env:"MY_APP_EMAIL"`
Password string `env:"MY_APP_PASSWORD"`
}
Endpoint string `env:"MY_APP_ENDPOINT"`
AppUrl string `env:"MY_APP_URL"`
Endpoint string `env:"MY_APP_ENDPOINT"`
AppUrl string `env:"MY_APP_URL"`
Undefined string `env:"MY_UNDEFINED"`
}

type testEnvResolver struct{}
Expand All @@ -28,7 +30,9 @@ func (t testEnvResolver) Get(key string) string {
}

func TestConfigErrors(t *testing.T) {
cfg := Config{}
cfg := Config{
Undefined: "foo",
}
err := LoadFromResolver(&cfg, testEnvResolver{})
if err == nil {
t.Fatalf("Expected error, got none")
Expand All @@ -39,8 +43,10 @@ func TestConfigErrors(t *testing.T) {
}

func TestConfigNew(t *testing.T) {
cfg := Config{}
err := LoadFromResolver(&cfg, EnvResolver{}, NewEnvFileResolver(".env.sample"))
cfg := Config{
Undefined: "foo",
}
err := LoadFromResolver(&cfg, NewEnvResolver(), NewEnvFileResolver(".env.sample"))
if err != nil {
t.Error(err)
}
Expand All @@ -57,3 +63,40 @@ func TestConfigNew(t *testing.T) {
t.Error(errors.New("unexpected url"))
}
}

func TestConfigNewWithoutFile(t *testing.T) {
cfg := Config{
Undefined: "foo",
}
err := LoadFromResolver(&cfg, EnvResolver{}, NewEnvFileResolver(".env.sample2"))
if err == nil {
t.Fatalf("Expected error, got none")
}
if err.Error() != "missing config variables: MY_APP_EMAIL,MY_APP_PASSWORD,MY_APP_ENDPOINT,MY_APP_URL" {
t.Error("Expected error, got wrong one: " + err.Error())
}
}

func TestConfigOverride(t *testing.T) {
_ = os.Setenv("MY_APP_EMAIL", "[email protected]")

cfg := Config{
Undefined: "foo",
}
err := LoadFromResolver(&cfg, NewEnvResolver(), NewEnvFileResolver(".env.sample"))
if err != nil {
t.Error(err)
}

if cfg.UserConfig.Email != "[email protected]" {
t.Error(errors.New("unexpected email"))
}

if cfg.Undefined != "foo" {
t.Error(errors.New("unexpected app url"))
}

if cfg.UserConfig.Password != "password2" {
t.Error(errors.New("unexpected password"))
}
}

0 comments on commit 06abbdb

Please sign in to comment.