fix(settings): system ID max value
This commit is contained in:
22
internal/configuration/sources/env/helpers_test.go
vendored
Normal file
22
internal/configuration/sources/env/helpers_test.go
vendored
Normal file
@@ -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)
|
||||||
|
}
|
||||||
2
internal/configuration/sources/env/system.go
vendored
2
internal/configuration/sources/env/system.go
vendored
@@ -54,7 +54,7 @@ func (r *Reader) readID(key, retroKey string) (
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("environment variable %s: %w: %s: %s",
|
return nil, fmt.Errorf("environment variable %s: %w: %s: %s",
|
||||||
idEnvKey, ErrSystemIDNotValid, idString, err)
|
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",
|
return nil, fmt.Errorf("environment variable %s: %w: %d: must be between 0 and 65535",
|
||||||
idEnvKey, ErrSystemIDNotValid, idInt)
|
idEnvKey, ErrSystemIDNotValid, idInt)
|
||||||
}
|
}
|
||||||
|
|||||||
52
internal/configuration/sources/env/system_test.go
vendored
Normal file
52
internal/configuration/sources/env/system_test.go
vendored
Normal file
@@ -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)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user