diff --git a/internal/configuration/sources/env/helpers_test.go b/internal/configuration/sources/env/helpers_test.go new file mode 100644 index 00000000..9c60657f --- /dev/null +++ b/internal/configuration/sources/env/helpers_test.go @@ -0,0 +1,22 @@ +package env + +import ( + "os" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// setTestEnv is used to set environment variables in +// parallel tests. +func setTestEnv(t *testing.T, key, value string) { + t.Helper() + existing := os.Getenv(key) + err := os.Setenv(key, value) //nolint:tenv + t.Cleanup(func() { + err = os.Setenv(key, existing) + assert.NoError(t, err) + }) + require.NoError(t, err) +} diff --git a/internal/configuration/sources/env/system.go b/internal/configuration/sources/env/system.go index d8139cc5..672f0938 100644 --- a/internal/configuration/sources/env/system.go +++ b/internal/configuration/sources/env/system.go @@ -54,7 +54,7 @@ func (r *Reader) readID(key, retroKey string) ( if err != nil { return nil, fmt.Errorf("environment variable %s: %w: %s: %s", idEnvKey, ErrSystemIDNotValid, idString, err) - } else if idInt < 0 || idInt > 2^16 { + } else if idInt < 0 || idInt > 65535 { return nil, fmt.Errorf("environment variable %s: %w: %d: must be between 0 and 65535", idEnvKey, ErrSystemIDNotValid, idInt) } diff --git a/internal/configuration/sources/env/system_test.go b/internal/configuration/sources/env/system_test.go new file mode 100644 index 00000000..7d575110 --- /dev/null +++ b/internal/configuration/sources/env/system_test.go @@ -0,0 +1,52 @@ +package env + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_Reader_readID(t *testing.T) { + t.Parallel() + + testCases := map[string]struct { + keyPrefix string + keyValue string + retroKeyPrefix string + retroValue string + id *uint16 + errWrapped error + errMessage string + }{ + "id 1000": { + keyPrefix: "ID", + keyValue: "1000", + retroKeyPrefix: "RETRO_ID", + id: uint16Ptr(1000), + }, + } + + for name, testCase := range testCases { + testCase := testCase + t.Run(name, func(t *testing.T) { + t.Parallel() + + suffix := t.Name() + key := testCase.keyPrefix + suffix + retroKey := testCase.retroKeyPrefix + suffix + + setTestEnv(t, key, testCase.keyValue) + setTestEnv(t, retroKey, testCase.retroValue) + + reader := &Reader{} + id, err := reader.readID(key, retroKey) + + assert.ErrorIs(t, err, testCase.errWrapped) + if err != nil { + assert.EqualError(t, err, testCase.errMessage) + } + + assert.Equal(t, testCase.id, id) + }) + } +}