-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored UI to allow for default values and value overrides. Also g…
…et better error messages for missing env values
- Loading branch information
Showing
4 changed files
with
88 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
MY_APP_EMAIL=[email protected] | ||
#MY_APP_PASSWORD=password | ||
MY_APP_PASSWORD=password2 | ||
MY_APP_ENDPOINT=some-endpoint | ||
MY_APP_ENDPOINT=some-endpoint | ||
MY_APP_URL=https://some.exmpale.org/foo?token=bar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,23 +11,49 @@ type Config struct { | |
Password string `env:"MY_APP_PASSWORD"` | ||
} | ||
Endpoint string `env:"MY_APP_ENDPOINT"` | ||
AppUrl string `env:"MY_APP_URL"` | ||
} | ||
|
||
func TestConfig(t *testing.T) { | ||
cfg, err := NewEnvFileConfig[Config](".env.sample") | ||
type testEnvResolver struct{} | ||
|
||
func (t testEnvResolver) Get(key string) string { | ||
switch key { | ||
case "MY_APP_EMAIL": | ||
return "[email protected]" | ||
case "MY_APP_PASSWORD": | ||
return "[email protected]" | ||
default: | ||
return "" | ||
} | ||
} | ||
|
||
func TestConfigErrors(t *testing.T) { | ||
cfg := Config{} | ||
err := LoadFromResolver(&cfg, testEnvResolver{}) | ||
if err == nil { | ||
t.Fatalf("Expected error, got none") | ||
} | ||
if err.Error() != "missing config variables: MY_APP_ENDPOINT,MY_APP_URL" { | ||
t.Error("Expected error, got wrong one: " + err.Error()) | ||
} | ||
} | ||
|
||
func TestConfigNew(t *testing.T) { | ||
cfg := Config{} | ||
err := LoadFromResolver(&cfg, EnvResolver{}, NewEnvFileResolver(".env.sample")) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
if cfg.UserConfig.Email != "[email protected]" { | ||
t.Error(errors.New("unexpected email")) | ||
} | ||
|
||
if cfg.UserConfig.Password != "password2" { | ||
t.Error(errors.New("unexpected password")) | ||
} | ||
|
||
if cfg.Endpoint != "some-endpoint" { | ||
t.Error(errors.New("unexpected endpoint")) | ||
} | ||
if cfg.AppUrl != "https://some.exmpale.org/foo?token=bar" { | ||
t.Error(errors.New("unexpected url")) | ||
} | ||
} |