Skip to content

URL Pointers are no longer left nil #317

@jgustie

Description

@jgustie

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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions