Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use environment variables when error is strict #731

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion config_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func readConfig(r io.Reader) (Config, error) {
}
unmarshalErr := unmarshalSemiStrictly(bts, &c)
if unmarshalErr != nil {
if _, ok := err.(*UnknownConfigKeys); !ok {
if _, ok := unmarshalErr.(*UnknownConfigKeys); !ok {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oufff, that's a good catch!

return c, unmarshalErr
}
}
Expand Down
29 changes: 29 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ func TestReadBadConfig(t *testing.T) {
assert.Equal(t, c, Config{}, "Parsing invalid config file should return zero struct")
}

func TestReadBadConfig_EnvVarsAreNotRead(t *testing.T) {
os.Setenv("VENEUR_HOSTNAME", "cux")
defer os.Unsetenv("VENEUR_HOSTNAME")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I'm mildly worried about setting and unsetting environment variables in tests that could run parallel with other things (even though you don't use t.Parallel() here) - couldn't find envconfig testing recommendations either, but I guess it's probably fine as long as we never parallelize.

Cc @aditya-stripe for a second opinion

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for reviewing this!

Environment variables are in effect global variables, and these IMO should be accessed only from the most exterior layer of the app, and then passed as arguments to more interior layers. Having a package with "constants" might be helpful if we don't want to "pollute" the signatures of the functions. In Go I know this cannot be enforced at compile time since consts are only for values known at compile time, but an alternative would be having functions instead that return the desired environment variables values. It's just an idea :)


const exampleConfig = `--- api_hostname: :bad`
r := strings.NewReader(exampleConfig)
c, err := readConfig(r)

assert.NotNil(t, err, "Should have encountered parsing error when reading invalid config file")
assert.Equal(t, c, Config{}, "Parsing invalid config file should return zero struct")
}

func TestReadUnknownKeysConfig(t *testing.T) {
const config = `---
no_such_key: 1
Expand All @@ -49,6 +61,23 @@ hostname: foobar
assert.Equal(t, "foobar", c.Hostname)
}

func TestReadUnknownKeysConfig_EnvVarsAreParsed(t *testing.T) {
os.Setenv("VENEUR_HOSTNAME", "cux")
defer os.Unsetenv("VENEUR_HOSTNAME")

const config = `---
no_such_key: 1
hostname: foobar
`
r := strings.NewReader(config)
c, err := readConfig(r)
assert.Error(t, err)
_, ok := err.(*UnknownConfigKeys)
t.Log(err)
assert.True(t, ok, "Returned error should indicate a strictness error")
assert.Equal(t, "cux", c.Hostname)
}

func TestReadUnknownKeysProxyConfig(t *testing.T) {
const config = `---
no_such_key: 1
Expand Down