Skip to content

Commit

Permalink
fix: log full key def (#5)
Browse files Browse the repository at this point in the history
* fix: log the full key if not found

* fix: update tests
  • Loading branch information
FluffyBucket authored Jul 2, 2024
1 parent badde30 commit d3a94c5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
6 changes: 1 addition & 5 deletions envconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,7 @@ func Process(prefix string, spec interface{}) error {
req := info.Tags.Get("required")
if value == "" {
if isTrue(req) {
key := info.Key
if info.Alt != "" {
key = info.Alt
}
return fmt.Errorf("required key %s missing value", key)
return fmt.Errorf("required key %s missing value", info.Key)
}
continue
}
Expand Down
36 changes: 36 additions & 0 deletions envconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -926,11 +926,47 @@ func TestErrorMessageForRequiredAltVar(t *testing.T) {
t.Error("no failure when missing required variable")
}

if !strings.Contains(err.Error(), " ENV_CONFIG_BAR ") {
t.Errorf("expected error message to contain ENV_CONFIG_BAR, got \"%v\"", err)
}
}

func TestErrorMessageForRequiredAltVarNoPrefix(t *testing.T) {
var s struct {
Foo string `envconfig:"BAR" required:"true"`
}

os.Clearenv()
err := Process("", &s)

if err == nil {
t.Error("no failure when missing required variable")
}

if !strings.Contains(err.Error(), " BAR ") {
t.Errorf("expected error message to contain BAR, got \"%v\"", err)
}
}

func TestErrorMessageForRequiredAltVarNestedStruct(t *testing.T) {
var s struct {
Foo struct {
Bar string `envconfig:"BAR" required:"true"`
} `envconfig:"FOO" required:"true"`
}

os.Clearenv()
err := Process("ENV_CONFIG", &s)

if err == nil {
t.Error("no failure when missing required variable")
}

if !strings.Contains(err.Error(), " ENV_CONFIG_FOO_BAR ") {
t.Errorf("expected error message to contain ENV_CONFIG_FOO_BAR, got \"%v\"", err)
}
}

func TestNonTaggedFields(t *testing.T) {
var s struct {
Foo string `envconfig:"FOO"`
Expand Down

0 comments on commit d3a94c5

Please sign in to comment.