URL Pointers are no longer left nil
jgustie opened this issue · comments
Jeremy Gustie commented
Prior to 11.0.1 (related?) a URL pointer would be left nil
if the environment variable was unset.
I tried writing a quick test to illustrate:
import (
"net/url"
"testing"
"github.com/caarlos0/env/v11"
"github.com/stretchr/testify/assert"
)
func TestParseEnv(t *testing.T) {
type TestConfig struct {
FooBar *url.URL `env:"FOOBAR"`
}
cases := []struct {
desc string
environment map[string]string
expected *url.URL
}{
{
desc: "unset",
environment: map[string]string{},
expected: nil,
},
{
desc: "empty",
environment: map[string]string{"FOOBAR": ""},
expected: &url.URL{},
},
{
desc: "set",
environment: map[string]string{"FOOBAR": "https://example.com/"},
expected: &url.URL{Scheme: "https", Host: "example.com", Path: "/"},
},
}
for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
cfg := TestConfig{}
err := env.ParseWithOptions(&cfg, env.Options{Environment: tc.environment})
assert.NoError(t, err)
assert.Equal(t, tc.expected, cfg.FooBar)
})
}
}
The unset
test case passes in v11.0.0 but fails in v11.0.1.
The set
test case always passes, and the empty
test case never passes (I'm guessing there is plumbing that does not differentiate between "" and unset).
Carlos Alexandro Becker commented
ah, sorry for that, good catch!