Skip to content
Merged
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
12 changes: 6 additions & 6 deletions config_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
func TestConfigFile(t *testing.T) {
assert := assert.New(t)

tmp, _ := os.CreateTemp("", "ldlm-test-*.yml")
tmp, _ := os.CreateTemp("", "cfgr-test-*.yml")
defer os.Remove(tmp.Name())
tmp.Write([]byte("foo_int: 4\nsub_foo_string: 'yes'\nkeepalive_timeout: 3m\nbool: true\n"))
tmp.Close()
Expand All @@ -46,7 +46,7 @@ func TestConfigFile(t *testing.T) {

func TestConfigFile_BadField(t *testing.T) {
if os.Getenv("TEST_PASSTHROUGH") == "1" {
tmp, _ := os.CreateTemp("", "ldlm-test-*.yml")
tmp, _ := os.CreateTemp("", "cfgr-test-*.yml")
defer os.Remove(tmp.Name())
tmp.Write([]byte("foo_int: 4\nsub_string: 'yes'\nkeepalive_timeout: 3m\nbool: true\n"))
tmp.Close()
Expand All @@ -66,7 +66,7 @@ func TestConfigFile_BadField(t *testing.T) {

func TestConfigFile_BadValue(t *testing.T) {
if os.Getenv("TEST_PASSTHROUGH") == "1" {
tmp, _ := os.CreateTemp("", "ldlm-test-*.yml")
tmp, _ := os.CreateTemp("", "cfgr-test-*.yml")
defer os.Remove(tmp.Name())
tmp.Write([]byte("foo_int: asdf\nsub_foo_string: 'ok'\nkeepalive_timeout: 3m\nbool: true\n"))
tmp.Close()
Expand All @@ -87,7 +87,7 @@ func TestConfigFile_BadValue(t *testing.T) {

func TestConfigFile_UnsupportedType(t *testing.T) {
if os.Getenv("TEST_PASSTHROUGH") == "1" {
tmp, _ := os.CreateTemp("", "ldlm-test-*.foo")
tmp, _ := os.CreateTemp("", "cfgr-test-*.foo")
defer os.Remove(tmp.Name())
tmp.Write([]byte("foo_int: asdf\nsub_foo_string: 'ok'\nkeepalive_timeout: 3m\nbool: true\n"))
tmp.Close()
Expand All @@ -109,7 +109,7 @@ func TestConfigFile_UnsupportedType(t *testing.T) {
func TestNested_Yaml(t *testing.T) {
assert := assert.New(t)

tmp, _ := os.CreateTemp("", "ldlm-test-*.yml")
tmp, _ := os.CreateTemp("", "cfgr-test-*.yml")
defer os.Remove(tmp.Name())
tmp.Write([]byte(`s_slice: [af, 'as"oidjf', 3]
my_map:
Expand Down Expand Up @@ -150,7 +150,7 @@ os:
func TestNested_JSON(t *testing.T) {
assert := assert.New(t)

tmp, _ := os.CreateTemp("", "ldlm-test-*.json")
tmp, _ := os.CreateTemp("", "cfgr-test-*.json")
defer os.Remove(tmp.Name())
tmp.Write([]byte(`{"s_slice": ["af", "asoidjf", "3"], "my_map": {"foo": "bar", "baz": "qux"}, "sub": {"default_lock_timeout": "30s", "foo_seconds": 20, "foo_int": 2, "foo_ints": [2, 4, 5]}, "os": {"sub_foo_string": "there and everywhere"}}`))
tmp.Close()
Expand Down
17 changes: 14 additions & 3 deletions configure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ type TestNestedConfig struct {
}

func runExternal(t *testing.T) (out string, err string) {
cmd := exec.Command(os.Args[0], "-test.run="+t.Name())
cmd := exec.Command(os.Args[0], "-test.v=false", "-test.run="+t.Name())
cmd.Env = append(os.Environ(), "TEST_PASSTHROUGH=1")
cmdout, cmderr := cmd.Output()
if cmdout != nil {
Expand All @@ -91,6 +91,17 @@ func runExternal(t *testing.T) (out string, err string) {
return out, err
}

func tmpFile(t *testing.T, suffix string) string {
tmp, _ := os.CreateTemp("", fmt.Sprintf("cfgr-test-*.%s", suffix))
tmp.Close()
os.Setenv("TEST_TEMP_FILE_NAME", tmp.Name())
t.Cleanup(func() {
os.Remove(tmp.Name())
os.Unsetenv("TEST_TEMP_FILE_NAME")
})
return tmp.Name()
}

func TestDefaults(t *testing.T) {
assert := assert.New(t)
c := co.Configure[TestConfig](&co.Options{NoRecover: true})
Expand Down Expand Up @@ -219,7 +230,7 @@ func TestFlagOverrideEnv(t *testing.T) {
func TestConfigFile_FromEnv(t *testing.T) {
assert := assert.New(t)

tmp, _ := os.CreateTemp("", "ldlm-test-*.yml")
tmp, _ := os.CreateTemp("", "cfgr-test-*.yml")
defer os.Remove(tmp.Name())
tmp.Write([]byte("foo_int: 4\nsub_foo_string: 'yes'\nkeepalive_timeout: 3m\nbool: true\n"))
tmp.Close()
Expand All @@ -244,7 +255,7 @@ func TestConfigFile_Precedence(t *testing.T) {
os.Setenv("TEST_CONF_FOO_INT", "7")
os.Setenv("TEST_CONF_SUB_FOO_STRING", "asdf")

tmp, _ := os.CreateTemp("", "ldlm-test-*.yml")
tmp, _ := os.CreateTemp("", "cfgr-test-*.yml")
defer os.Remove(tmp.Name())
tmp.Write([]byte("foo_int: 4\nsub_foo_string: 'yes'\nkeepalive_timeout: 3m\nbool: false\n"))
tmp.Close()
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@ github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSAS
github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down
32 changes: 18 additions & 14 deletions types_handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestRegisterCustomType(t *testing.T) {

func TestCustomType_Flag(t *testing.T) {
addImageFileTypes()
tmp, _ := os.CreateTemp("", "ldlm-test-*.png")
tmp, _ := os.CreateTemp("", "cfgr-test-*.png")
tmp.Close()
defer os.Remove(tmp.Name())

Expand All @@ -97,27 +97,29 @@ func TestCustomType_Error(t *testing.T) {
if os.Getenv("TEST_PASSTHROUGH") == "1" {

co.Configure[MyConfig](&co.Options{
Args: []string{"--image", "./go.mod"},
Args: []string{"--image", os.Getenv("TEST_TEMP_FILE_NAME")},
Usage: func(_ *flag.FlagSet) {},
})
os.Exit(0)
}

fileName := tmpFile(t, "mod")
stdout, stderr := runExternal(t)
assert := assert.New(t)
assert.Equal(`invalid argument "./go.mod" for "--image" flag: file type `+
assert.Equal(`invalid argument "`+fileName+`" for "--image" flag: file type `+
`".mod" not supported`+"\n", stdout)
assert.Equal(`invalid argument "./go.mod" for "--image" flag: file type `+
assert.Equal(`invalid argument "`+fileName+`" for "--image" flag: file type `+
`".mod" not supported`+"\n", stderr)

}

func TestCustomType_ErrorConfigFile(t *testing.T) {
addImageFileTypes()
if os.Getenv("TEST_PASSTHROUGH") == "1" {
tmp, _ := os.CreateTemp("", "ldlm-test-*.yml")
tmp, _ := os.CreateTemp("", "cfgr-test-*.yml")
defer os.Remove(tmp.Name())
tmp.Write([]byte("image: ./go.mod\n"))
fileName := tmpFile(t, "mod")
tmp.Write([]byte("image: " + fileName + "\n"))
tmp.Close()

co.Configure[MyConfig](&co.Options{
Expand All @@ -141,7 +143,7 @@ func TestCustomSliceType_Flag(t *testing.T) {
files := make([]string, 3)
expected := make([]ImageFile, 3)
for idx := range 3 {
tmp, _ := os.CreateTemp("", "ldlm-test-*.png")
tmp, _ := os.CreateTemp("", "cfgr-test-*.png")
tmp.Close()
defer os.Remove(tmp.Name())

Expand All @@ -163,17 +165,18 @@ func TestCustomSliceType_Error(t *testing.T) {
if os.Getenv("TEST_PASSTHROUGH") == "1" {

co.Configure[MyConfig](&co.Options{
Args: []string{"--images", "./go.mod"},
Args: []string{"--images", os.Getenv("TEST_TEMP_FILE_NAME")},
Usage: func(_ *flag.FlagSet) {},
})
os.Exit(0)
}

fileName := tmpFile(t, "mod")
stdout, stderr := runExternal(t)
assert := assert.New(t)
assert.Equal(`invalid argument "./go.mod" for "--images" flag: file type `+
assert.Equal(`invalid argument "`+fileName+`" for "--images" flag: file type `+
`".mod" not supported`+"\n", stdout)
assert.Equal(`invalid argument "./go.mod" for "--images" flag: file type `+
assert.Equal(`invalid argument "`+fileName+`" for "--images" flag: file type `+
`".mod" not supported`+"\n", stderr)

}
Expand All @@ -183,15 +186,15 @@ func TestCustomSliceType_ConfigFile(t *testing.T) {
files := make([]string, 3)
expected := make([]ImageFile, 3)
for idx := range 3 {
tmp, _ := os.CreateTemp("", "ldlm-test-config-file*.png")
tmp, _ := os.CreateTemp("", "cfgr-test-config-file*.png")
tmp.Close()
defer os.Remove(tmp.Name())

files[idx] = tmp.Name()
expected[idx] = ImageFile(tmp.Name())
}

tmp, _ := os.CreateTemp("", "ldlm-test-*.yaml")
tmp, _ := os.CreateTemp("", "cfgr-test-*.yaml")
tmp.Write([]byte("images:\n - " + strings.Join(files, "\n - ") + "\n"))
tmp.Close()
defer os.Remove(tmp.Name())
Expand All @@ -207,9 +210,10 @@ func TestCustomSliceType_ConfigFile(t *testing.T) {
func TestCustomSliceType_ErrorConfigFile(t *testing.T) {
addImageFileTypes()
if os.Getenv("TEST_PASSTHROUGH") == "1" {
tmp, _ := os.CreateTemp("", "ldlm-test-*.yml")
tmp, _ := os.CreateTemp("", "cfgr-test-*.yml")
defer os.Remove(tmp.Name())
tmp.Write([]byte("image: ./go.mod\n"))
fileName := tmpFile(t, "mod")
tmp.Write([]byte("image: " + fileName + "\n"))
tmp.Close()

co.Configure[MyConfig](&co.Options{
Expand Down