Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -834,11 +834,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