Skip to content

Commit

Permalink
Add MustProcess that panics if processing fails
Browse files Browse the repository at this point in the history
  • Loading branch information
sethvargo committed Jul 6, 2024
1 parent a8f0681 commit f82d670
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 23 deletions.
11 changes: 10 additions & 1 deletion envconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,16 @@ func Process(ctx context.Context, i any, mus ...Mutator) error {
}

// MustProcess is a helper that calls [Process] and panics if an error is
// encountered. The input value is returned after processing.
// encountered. Unlike [Process], the input value is returned, making it ideal
// for anonymous initializations:
//
// var env = envconfig.MustProcess(context.Background(), &struct{
// Field string `env:"FIELD,required"`
// })
//
// This is not recommend for production services, but it can be useful for quick
// CLIs and scripts that want to take advantage of envconfig's environment
// parsing at the expense of testability and graceful error handling.
func MustProcess[T any](ctx context.Context, i T, mus ...Mutator) T {
if err := Process(ctx, i, mus...); err != nil {
panic(err)
Expand Down
87 changes: 65 additions & 22 deletions envconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2958,6 +2958,71 @@ func TestProcessWith(t *testing.T) {
}
}

func TestMustProcess(t *testing.T) {
cases := []struct {
name string
target any
exp any
env map[string]string
expPanic bool
}{
{
name: "panics_on_error",
target: &struct {
Field string `env:"FIELD,required"`
}{},
exp: &struct {
Field string `env:"FIELD,required"`
}{},
env: nil,
expPanic: true,
},
{
name: "returns_value",
target: &struct {
Field string `env:"FIELD,required"`
}{},
exp: &struct {
Field string `env:"FIELD,required"`
}{
Field: "value",
},
env: map[string]string{
"FIELD": "value",
},
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
for k, v := range tc.env {
t.Setenv(k, v)
}

defer func() {
if r := recover(); r != nil {
if !tc.expPanic {
t.Fatal(r)
}
} else if tc.expPanic {
t.Errorf("expected a panic")
}
}()

ctx := context.Background()
got := MustProcess(ctx, tc.target)

if got != tc.target {
t.Errorf("expected result to be the same object as target (%#v, %#v)", got, tc.target)
}

if diff := cmp.Diff(tc.exp, tc.target); diff != "" {
t.Fatalf("mismatch (-want, +got):\n%s", diff)
}
})
}
}

func TestValidateEnvName(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -3034,25 +3099,3 @@ func TestValidateEnvName(t *testing.T) {
func ptrTo[T any](i T) *T {
return &i
}

func TestMustProcess_Panic(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("expected a panic")
}
}()
MustProcess(context.Background(), struct {
Unset string `env:"UNSET" required:"true"`
}{})
}

func TestMustProcess_Value(t *testing.T) {
t.Setenv("SET", "value")
s := MustProcess(context.Background(), &struct {
Set string `env:"SET"`
}{})

if s.Set != "value" {
t.Fatalf("expected %q to be %q", s.Set, "value")
}
}

0 comments on commit f82d670

Please sign in to comment.