From 0b6c05380cb40ae0d180fdf92df8ee60710ecfb8 Mon Sep 17 00:00:00 2001 From: Kuisong Tong Date: Sat, 11 Nov 2023 22:43:39 -0800 Subject: [PATCH] minimum dependencies (#51) --- benchmark_test.go | 19 ++++--- config_test.go | 55 ++++++++++----------- global_test.go | 27 +++++----- go.mod | 12 ----- go.sum | 18 ------- internal/assert/assert.go | 49 ++++++++++++++++++ internal/maps/insert_test.go | 5 +- internal/maps/merge_test.go | 5 +- leak_test.go | 14 ------ provider/env/benchmark_test.go | 11 ++--- provider/env/env_test.go | 9 ++-- provider/file/benchmark_test.go | 11 ++--- provider/file/file.go | 6 +-- provider/file/file_test.go | 19 ++++--- provider/file/go.mod | 13 +---- provider/file/go.sum | 22 +-------- provider/file/internal/assert/assert.go | 33 +++++++++++++ provider/file/leak_test.go | 14 ------ provider/flag/benchmark_test.go | 11 ++--- provider/flag/flag_test.go | 9 ++-- provider/fs/benchmark_test.go | 11 ++--- provider/fs/fs.go | 12 ++--- provider/fs/fs_test.go | 11 ++--- provider/pflag/benchmark_test.go | 10 ++-- provider/pflag/go.mod | 12 ----- provider/pflag/go.sum | 16 ------ provider/pflag/internal/assert/assert.go | 25 ++++++++++ provider/pflag/internal/maps/insert_test.go | 5 +- provider/pflag/leak_test.go | 14 ------ provider/pflag/pflag_test.go | 8 +-- 30 files changed, 223 insertions(+), 263 deletions(-) create mode 100644 internal/assert/assert.go delete mode 100644 leak_test.go create mode 100644 provider/file/internal/assert/assert.go delete mode 100644 provider/file/leak_test.go create mode 100644 provider/pflag/internal/assert/assert.go delete mode 100644 provider/pflag/leak_test.go diff --git a/benchmark_test.go b/benchmark_test.go index c47186cc..d2992c95 100644 --- a/benchmark_test.go +++ b/benchmark_test.go @@ -8,9 +8,8 @@ import ( "sync" "testing" - "github.com/stretchr/testify/require" - "github.com/ktong/konf" + "github.com/ktong/konf/internal/assert" ) func BenchmarkNew(b *testing.B) { @@ -24,13 +23,13 @@ func BenchmarkNew(b *testing.B) { b.StopTimer() konf.SetGlobal(config) - require.NoError(b, err) - require.Equal(b, "v", konf.Get[string]("k")) + assert.NoError(b, err) + assert.Equal(b, "v", konf.Get[string]("k")) } func BenchmarkGet(b *testing.B) { config, err := konf.New(konf.WithLoader(mapLoader{"k": "v"})) - require.NoError(b, err) + assert.NoError(b, err) konf.SetGlobal(config) b.ResetTimer() @@ -40,17 +39,17 @@ func BenchmarkGet(b *testing.B) { } b.StopTimer() - require.Equal(b, "v", value) + assert.Equal(b, "v", value) } func BenchmarkWatch(b *testing.B) { watcher := mapWatcher(make(chan map[string]any)) config, err := konf.New(konf.WithLoader(watcher)) - require.NoError(b, err) + assert.NoError(b, err) konf.SetGlobal(config) cfg := konf.Get[string]("config") - require.Equal(b, "string", cfg) + assert.Equal(b, "string", cfg) var waitGroup sync.WaitGroup waitGroup.Add(b.N) ctx, cancel := context.WithCancel(context.Background()) @@ -61,7 +60,7 @@ func BenchmarkWatch(b *testing.B) { cfg = konf.Get[string]("config") }) - require.NoError(b, err) + assert.NoError(b, err) }() b.ResetTimer() @@ -71,5 +70,5 @@ func BenchmarkWatch(b *testing.B) { waitGroup.Wait() b.StopTimer() - require.Equal(b, "changed", cfg) + assert.Equal(b, "changed", cfg) } diff --git a/config_test.go b/config_test.go index 9d061be4..9f218fe7 100644 --- a/config_test.go +++ b/config_test.go @@ -9,9 +9,8 @@ import ( "sync" "testing" - "github.com/stretchr/testify/require" - "github.com/ktong/konf" + "github.com/ktong/konf/internal/assert" ) func TestConfig_Unmarshal(t *testing.T) { @@ -26,8 +25,8 @@ func TestConfig_Unmarshal(t *testing.T) { description: "empty values", assert: func(config *konf.Config) { var cfg string - require.NoError(t, config.Unmarshal("config", &cfg)) - require.Equal(t, "", cfg) + assert.NoError(t, config.Unmarshal("config", &cfg)) + assert.Equal(t, "", cfg) }, }, { @@ -35,8 +34,8 @@ func TestConfig_Unmarshal(t *testing.T) { opts: []konf.Option{konf.WithLoader(nil)}, assert: func(config *konf.Config) { var cfg string - require.NoError(t, config.Unmarshal("config", &cfg)) - require.Equal(t, "", cfg) + assert.NoError(t, config.Unmarshal("config", &cfg)) + assert.Equal(t, "", cfg) }, }, { @@ -44,8 +43,8 @@ func TestConfig_Unmarshal(t *testing.T) { opts: []konf.Option{konf.WithLoader(mapLoader{"config": "string"})}, assert: func(config *konf.Config) { var cfg string - require.NoError(t, config.Unmarshal("config", &cfg)) - require.Equal(t, "string", cfg) + assert.NoError(t, config.Unmarshal("config", &cfg)) + assert.Equal(t, "string", cfg) }, }, { @@ -55,8 +54,8 @@ func TestConfig_Unmarshal(t *testing.T) { var cfg struct { Config string } - require.NoError(t, config.Unmarshal("", &cfg)) - require.Equal(t, "struct", cfg.Config) + assert.NoError(t, config.Unmarshal("", &cfg)) + assert.Equal(t, "struct", cfg.Config) }, }, { @@ -72,8 +71,8 @@ func TestConfig_Unmarshal(t *testing.T) { }, assert: func(config *konf.Config) { var cfg string - require.NoError(t, config.Unmarshal("config.nest", &cfg)) - require.Equal(t, "string", cfg) + assert.NoError(t, config.Unmarshal("config.nest", &cfg)) + assert.Equal(t, "string", cfg) }, }, { @@ -90,8 +89,8 @@ func TestConfig_Unmarshal(t *testing.T) { }, assert: func(config *konf.Config) { var cfg string - require.NoError(t, config.Unmarshal("config_nest", &cfg)) - require.Equal(t, "string", cfg) + assert.NoError(t, config.Unmarshal("config_nest", &cfg)) + assert.Equal(t, "string", cfg) }, }, { @@ -107,8 +106,8 @@ func TestConfig_Unmarshal(t *testing.T) { }, assert: func(config *konf.Config) { var cfg string - require.NoError(t, config.Unmarshal("config.nest", &cfg)) - require.Equal(t, "", cfg) + assert.NoError(t, config.Unmarshal("config.nest", &cfg)) + assert.Equal(t, "", cfg) }, }, { @@ -118,8 +117,8 @@ func TestConfig_Unmarshal(t *testing.T) { }, assert: func(config *konf.Config) { var configured bool - require.NoError(t, config.Unmarshal("configured", &configured)) - require.True(t, configured) + assert.NoError(t, config.Unmarshal("configured", &configured)) + assert.True(t, configured) }, }, } @@ -131,7 +130,7 @@ func TestConfig_Unmarshal(t *testing.T) { t.Parallel() config, err := konf.New(testcase.opts...) - require.NoError(t, err) + assert.NoError(t, err) testcase.assert(config) }) } @@ -152,11 +151,11 @@ func TestConfig_Watch(t *testing.T) { watcher := mapWatcher(make(chan map[string]any)) config, err := konf.New(konf.WithLoader(watcher)) - require.NoError(t, err) + assert.NoError(t, err) var cfg string - require.NoError(t, config.Unmarshal("config", &cfg)) - require.Equal(t, "string", cfg) + assert.NoError(t, config.Unmarshal("config", &cfg)) + assert.Equal(t, "string", cfg) ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -167,15 +166,15 @@ func TestConfig_Watch(t *testing.T) { err := config.Watch(ctx, func(config *konf.Config) { defer waitGroup.Done() - require.NoError(t, config.Unmarshal("config", &cfg)) + assert.NoError(t, config.Unmarshal("config", &cfg)) }) - require.NoError(t, err) + assert.NoError(t, err) }() watcher.change(map[string]any{"config": "changed"}) waitGroup.Wait() - require.Equal(t, "changed", cfg) + assert.Equal(t, "changed", cfg) } type mapWatcher chan map[string]any @@ -203,12 +202,12 @@ func TestConfig_Watch_error(t *testing.T) { t.Parallel() config, err := konf.New(konf.WithLoader(errorWatcher{})) - require.NoError(t, err) + assert.NoError(t, err) ctx, cancel := context.WithCancel(context.Background()) defer cancel() - require.EqualError(t, config.Watch(ctx), "[konf] watch configuration change: watch error") + assert.EqualError(t, config.Watch(ctx), "[konf] watch configuration change: watch error") } type errorWatcher struct{} @@ -225,7 +224,7 @@ func TestConfig_error(t *testing.T) { t.Parallel() _, err := konf.New(konf.WithLoader(errorLoader{})) - require.EqualError(t, err, "[konf] load configuration: load error") + assert.EqualError(t, err, "[konf] load configuration: load error") } type errorLoader struct{} diff --git a/global_test.go b/global_test.go index dbf49981..c2c76e4c 100644 --- a/global_test.go +++ b/global_test.go @@ -10,57 +10,56 @@ import ( "sync" "testing" - "github.com/stretchr/testify/require" - "github.com/ktong/konf" + "github.com/ktong/konf/internal/assert" ) func TestUnmarshal(t *testing.T) { t.Parallel() cfg, err := konf.New(konf.WithLoader(mapLoader{"config": "string"})) - require.NoError(t, err) + assert.NoError(t, err) konf.SetGlobal(cfg) var v string - require.NoError(t, konf.Unmarshal("config", &v)) - require.Equal(t, "string", v) + assert.NoError(t, konf.Unmarshal("config", &v)) + assert.Equal(t, "string", v) } func TestGet(t *testing.T) { t.Parallel() cfg, err := konf.New(konf.WithLoader(mapLoader{"config": "string"})) - require.NoError(t, err) + assert.NoError(t, err) konf.SetGlobal(cfg) - require.Equal(t, "string", konf.Get[string]("config")) + assert.Equal(t, "string", konf.Get[string]("config")) } func TestGet_error(t *testing.T) { cfg, err := konf.New(konf.WithLoader(mapLoader{"config": "string"})) - require.NoError(t, err) + assert.NoError(t, err) konf.SetGlobal(cfg) buf := new(bytes.Buffer) log.SetOutput(buf) log.SetFlags(0) - require.False(t, konf.Get[bool]("config")) + assert.True(t, !konf.Get[bool]("config")) expected := "ERROR Could not read config, return empty value instead." + " error=\"[konf] decode: cannot parse '' as bool: strconv.ParseBool: parsing \\\"string\\\": invalid syntax\"" + " path=config type=bool\n" - require.Equal(t, expected, buf.String()) + assert.Equal(t, expected, buf.String()) } func TestWatch(t *testing.T) { watcher := mapWatcher(make(chan map[string]any)) config, err := konf.New(konf.WithLoader(watcher)) - require.NoError(t, err) + assert.NoError(t, err) konf.SetGlobal(config) cfg := konf.Get[string]("config") - require.Equal(t, "string", cfg) + assert.Equal(t, "string", cfg) ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -73,11 +72,11 @@ func TestWatch(t *testing.T) { cfg = konf.Get[string]("config") }) - require.NoError(t, err) + assert.NoError(t, err) }() watcher.change(map[string]any{"config": "changed"}) waitGroup.Wait() - require.Equal(t, "changed", cfg) + assert.Equal(t, "changed", cfg) } diff --git a/go.mod b/go.mod index 648fe635..63f8d7da 100644 --- a/go.mod +++ b/go.mod @@ -3,15 +3,3 @@ module github.com/ktong/konf go 1.21 require github.com/mitchellh/mapstructure v1.5.0 - -require ( // for test - github.com/stretchr/testify v1.8.4 - go.uber.org/goleak v1.3.0 -) - -require ( // indirect - github.com/davecgh/go-spew v1.1.1 // indirect - github.com/kr/text v0.2.0 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect -) diff --git a/go.sum b/go.sum index ae162242..59f4b8e6 100644 --- a/go.sum +++ b/go.sum @@ -1,20 +1,2 @@ -github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= -github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= -github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= -github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= -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/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= -go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= -go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/assert/assert.go b/internal/assert/assert.go new file mode 100644 index 00000000..71d573b3 --- /dev/null +++ b/internal/assert/assert.go @@ -0,0 +1,49 @@ +// Copyright (c) 2023 The konf authors +// Use of this source code is governed by a MIT license found in the LICENSE file. + +package assert + +import ( + "reflect" + "testing" +) + +func Equal[T any](tb testing.TB, expected, actual T) { + tb.Helper() + + if !reflect.DeepEqual(expected, actual) { + tb.Errorf("expected: %v; actual: %v", expected, actual) + } +} + +func NoError(tb testing.TB, err error) { + tb.Helper() + + if err != nil { + tb.Errorf("unexpected error: %v", err) + } +} + +func EqualError(tb testing.TB, err error, message string) { + tb.Helper() + + if err.Error() != message { + tb.Errorf("expected: %v; actual: %v", message, err.Error()) + } +} + +func True(tb testing.TB, value bool) { + tb.Helper() + + if !value { + tb.Errorf("expected True") + } +} + +func NotEmpty(tb testing.TB, value any) { + tb.Helper() + + if reflect.ValueOf(value).IsZero() { + tb.Errorf("expected not empty") + } +} diff --git a/internal/maps/insert_test.go b/internal/maps/insert_test.go index c6bb9645..63f567c9 100644 --- a/internal/maps/insert_test.go +++ b/internal/maps/insert_test.go @@ -6,8 +6,7 @@ package maps_test import ( "testing" - "github.com/stretchr/testify/require" - + "github.com/ktong/konf/internal/assert" "github.com/ktong/konf/internal/maps" ) @@ -69,7 +68,7 @@ func TestInsert(t *testing.T) { t.Parallel() maps.Insert(testcase.dst, testcase.keys, testcase.val) - require.Equal(t, testcase.expected, testcase.dst) + assert.Equal(t, testcase.expected, testcase.dst) }) } } diff --git a/internal/maps/merge_test.go b/internal/maps/merge_test.go index 35de7023..6bfc73cc 100644 --- a/internal/maps/merge_test.go +++ b/internal/maps/merge_test.go @@ -6,8 +6,7 @@ package maps_test import ( "testing" - "github.com/stretchr/testify/require" - + "github.com/ktong/konf/internal/assert" "github.com/ktong/konf/internal/maps" ) @@ -83,7 +82,7 @@ func TestMerge(t *testing.T) { t.Parallel() maps.Merge(testcase.dst, testcase.src) - require.Equal(t, testcase.expected, testcase.dst) + assert.Equal(t, testcase.expected, testcase.dst) }) } } diff --git a/leak_test.go b/leak_test.go deleted file mode 100644 index 2f8f48be..00000000 --- a/leak_test.go +++ /dev/null @@ -1,14 +0,0 @@ -// Copyright (c) 2023 The konf authors -// Use of this source code is governed by a MIT license found in the LICENSE file. - -package konf_test - -import ( - "testing" - - "go.uber.org/goleak" -) - -func TestMain(m *testing.M) { - goleak.VerifyTestMain(m) -} diff --git a/provider/env/benchmark_test.go b/provider/env/benchmark_test.go index db140ab2..57ff7ef7 100644 --- a/provider/env/benchmark_test.go +++ b/provider/env/benchmark_test.go @@ -6,8 +6,7 @@ package env_test import ( "testing" - "github.com/stretchr/testify/require" - + "github.com/ktong/konf/internal/assert" "github.com/ktong/konf/provider/env" ) @@ -19,8 +18,8 @@ func BenchmarkNew(b *testing.B) { b.StopTimer() values, err := loader.Load() - require.NoError(b, err) - require.NotEmpty(b, values["USER"]) + assert.NoError(b, err) + assert.NotEmpty(b, values["USER"]) } func BenchmarkLoad(b *testing.B) { @@ -36,6 +35,6 @@ func BenchmarkLoad(b *testing.B) { } b.StopTimer() - require.NoError(b, err) - require.NotEmpty(b, values["USER"]) + assert.NoError(b, err) + assert.NotEmpty(b, values["USER"]) } diff --git a/provider/env/env_test.go b/provider/env/env_test.go index b0fec2ea..3774b31a 100644 --- a/provider/env/env_test.go +++ b/provider/env/env_test.go @@ -6,9 +6,8 @@ package env_test import ( "testing" - "github.com/stretchr/testify/require" - "github.com/ktong/konf" + "github.com/ktong/konf/internal/assert" "github.com/ktong/konf/provider/env" ) @@ -50,8 +49,8 @@ func TestEnv_Load(t *testing.T) { t.Run(testcase.description, func(t *testing.T) { values, err := env.New(testcase.opts...).Load() - require.NoError(t, err) - require.Equal(t, testcase.expected, values) + assert.NoError(t, err) + assert.Equal(t, testcase.expected, values) }) } } @@ -81,7 +80,7 @@ func TestEnv_String(t *testing.T) { t.Run(testcase.description, func(t *testing.T) { t.Parallel() - require.Equal(t, testcase.expected, env.New(env.WithPrefix(testcase.prefix)).String()) + assert.Equal(t, testcase.expected, env.New(env.WithPrefix(testcase.prefix)).String()) }) } } diff --git a/provider/file/benchmark_test.go b/provider/file/benchmark_test.go index af7ca917..52cf9fe8 100644 --- a/provider/file/benchmark_test.go +++ b/provider/file/benchmark_test.go @@ -6,9 +6,8 @@ package file_test import ( "testing" - "github.com/stretchr/testify/require" - "github.com/ktong/konf/provider/file" + "github.com/ktong/konf/provider/file/internal/assert" ) func BenchmarkNew(b *testing.B) { @@ -19,8 +18,8 @@ func BenchmarkNew(b *testing.B) { b.StopTimer() values, err := loader.Load() - require.NoError(b, err) - require.Equal(b, "v", values["k"]) + assert.NoError(b, err) + assert.Equal(b, "v", values["k"]) } func BenchmarkLoad(b *testing.B) { @@ -36,6 +35,6 @@ func BenchmarkLoad(b *testing.B) { } b.StopTimer() - require.NoError(b, err) - require.Equal(b, "v", values["k"]) + assert.NoError(b, err) + assert.Equal(b, "v", values["k"]) } diff --git a/provider/file/file.go b/provider/file/file.go index 9ac171e3..3a88aca3 100644 --- a/provider/file/file.go +++ b/provider/file/file.go @@ -3,12 +3,10 @@ // Package file loads configuration from files. // -// File loads file with given path from OS file system and returns nested map[string]any -// that is parsed as json. +// File loads file with given path from OS file system +// and returns nested map[string]any that is parsed as json. // // The default behavior can be changed with following options: -// - WithFS provides the fs.FS that config file is loaded from. -// E.g. `WithFS(cfg)` will load configuration from embed file while cfg is embed.FS. // - WithUnmarshal provides the function that parses config file. // E.g. `WithUnmarshal(yaml.Unmarshal)` will parse the file as yaml. // - IgnoreFileNotExit ignores the error if config file does not exist. diff --git a/provider/file/file_test.go b/provider/file/file_test.go index 77b3906b..6777a810 100644 --- a/provider/file/file_test.go +++ b/provider/file/file_test.go @@ -15,9 +15,8 @@ import ( "testing" "time" - "github.com/stretchr/testify/require" - "github.com/ktong/konf/provider/file" + "github.com/ktong/konf/provider/file/internal/assert" ) func TestFile_Load(t *testing.T) { @@ -68,10 +67,10 @@ func TestFile_Load(t *testing.T) { values, err := file.New(testcase.path, testcase.opts...).Load() if err != nil { - require.True(t, strings.HasPrefix(err.Error(), testcase.err)) + assert.True(t, strings.HasPrefix(err.Error(), testcase.err)) } else { - require.NoError(t, err) - require.Equal(t, testcase.expected, values) + assert.NoError(t, err) + assert.Equal(t, testcase.expected, values) } }) } @@ -108,7 +107,7 @@ func TestFile_Watch(t *testing.T) { t.Run(testcase.description, func(t *testing.T) { tmpFile := filepath.Join(t.TempDir(), "watch.json") - require.NoError(t, os.WriteFile(tmpFile, []byte(`{"p": {"k": "v"}}`), 0o600)) + assert.NoError(t, os.WriteFile(tmpFile, []byte(`{"p": {"k": "v"}}`), 0o600)) loader := file.New(tmpFile) var values map[string]any @@ -122,13 +121,13 @@ func TestFile_Watch(t *testing.T) { defer waitGroup.Done() values = changed }) - require.NoError(t, err) + assert.NoError(t, err) }() time.Sleep(time.Second) - require.NoError(t, testcase.action(tmpFile)) + assert.NoError(t, testcase.action(tmpFile)) waitGroup.Wait() - require.Equal(t, testcase.expacted, values) + assert.Equal(t, testcase.expacted, values) }) } } @@ -136,5 +135,5 @@ func TestFile_Watch(t *testing.T) { func TestFile_String(t *testing.T) { t.Parallel() - require.Equal(t, "file:config.json", file.New("config.json").String()) + assert.Equal(t, "file:config.json", file.New("config.json").String()) } diff --git a/provider/file/go.mod b/provider/file/go.mod index d0ccd660..37badfeb 100644 --- a/provider/file/go.mod +++ b/provider/file/go.mod @@ -4,15 +4,4 @@ go 1.21 require github.com/fsnotify/fsnotify v1.7.0 -require ( // for test - github.com/stretchr/testify v1.8.4 - go.uber.org/goleak v1.3.0 -) - -require ( // indirect - github.com/davecgh/go-spew v1.1.1 // indirect - github.com/kr/text v0.2.0 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect - golang.org/x/sys v0.14.0 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect -) +require golang.org/x/sys v0.4.0 // indirect diff --git a/provider/file/go.sum b/provider/file/go.sum index f1d23415..ccd7ce92 100644 --- a/provider/file/go.sum +++ b/provider/file/go.sum @@ -1,22 +1,4 @@ -github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= -github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= -github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= -github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= -github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -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/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= -go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= -go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= -golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q= -golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18= +golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/provider/file/internal/assert/assert.go b/provider/file/internal/assert/assert.go new file mode 100644 index 00000000..c33934a5 --- /dev/null +++ b/provider/file/internal/assert/assert.go @@ -0,0 +1,33 @@ +// Copyright (c) 2023 The konf authors +// Use of this source code is governed by a MIT license found in the LICENSE file. + +package assert + +import ( + "reflect" + "testing" +) + +func Equal[T any](tb testing.TB, expected, actual T) { + tb.Helper() + + if !reflect.DeepEqual(expected, actual) { + tb.Errorf("expected: %v; actual: %v", expected, actual) + } +} + +func NoError(tb testing.TB, err error) { + tb.Helper() + + if err != nil { + tb.Errorf("unexpected error: %v", err) + } +} + +func True(tb testing.TB, value bool) { + tb.Helper() + + if !value { + tb.Errorf("expected True") + } +} diff --git a/provider/file/leak_test.go b/provider/file/leak_test.go deleted file mode 100644 index 7c839234..00000000 --- a/provider/file/leak_test.go +++ /dev/null @@ -1,14 +0,0 @@ -// Copyright (c) 2023 The konf authors -// Use of this source code is governed by a MIT license found in the LICENSE file. - -package file_test - -import ( - "testing" - - "go.uber.org/goleak" -) - -func TestMain(m *testing.M) { - goleak.VerifyTestMain(m) -} diff --git a/provider/flag/benchmark_test.go b/provider/flag/benchmark_test.go index 7fe4c1fa..231025d7 100644 --- a/provider/flag/benchmark_test.go +++ b/provider/flag/benchmark_test.go @@ -7,8 +7,7 @@ import ( "flag" "testing" - "github.com/stretchr/testify/require" - + "github.com/ktong/konf/internal/assert" kflag "github.com/ktong/konf/provider/flag" ) @@ -24,8 +23,8 @@ func BenchmarkNew(b *testing.B) { b.StopTimer() values, err := loader.Load() - require.NoError(b, err) - require.Equal(b, "v", values["k"]) + assert.NoError(b, err) + assert.Equal(b, "v", values["k"]) } func BenchmarkLoad(b *testing.B) { @@ -43,6 +42,6 @@ func BenchmarkLoad(b *testing.B) { } b.StopTimer() - require.NoError(b, err) - require.Equal(b, "v", values["k"]) + assert.NoError(b, err) + assert.Equal(b, "v", values["k"]) } diff --git a/provider/flag/flag_test.go b/provider/flag/flag_test.go index 2aa2ddd0..1e1fd71c 100644 --- a/provider/flag/flag_test.go +++ b/provider/flag/flag_test.go @@ -9,9 +9,8 @@ import ( "flag" "testing" - "github.com/stretchr/testify/require" - "github.com/ktong/konf" + "github.com/ktong/konf/internal/assert" kflag "github.com/ktong/konf/provider/flag" ) @@ -57,8 +56,8 @@ func TestFlag_Load(t *testing.T) { t.Run(testcase.description, func(t *testing.T) { values, err := kflag.New(testcase.opts...).Load() - require.NoError(t, err) - require.Equal(t, testcase.expected, values) + assert.NoError(t, err) + assert.Equal(t, testcase.expected, values) }) } } @@ -88,7 +87,7 @@ func TestFlag_String(t *testing.T) { t.Run(testcase.description, func(t *testing.T) { t.Parallel() - require.Equal( + assert.Equal( t, testcase.expected, kflag.New( diff --git a/provider/fs/benchmark_test.go b/provider/fs/benchmark_test.go index 877c8994..28672855 100644 --- a/provider/fs/benchmark_test.go +++ b/provider/fs/benchmark_test.go @@ -7,8 +7,7 @@ import ( "testing" "testing/fstest" - "github.com/stretchr/testify/require" - + "github.com/ktong/konf/internal/assert" pfs "github.com/ktong/konf/provider/fs" ) @@ -27,8 +26,8 @@ func BenchmarkNew(b *testing.B) { b.StopTimer() values, err := loader.Load() - require.NoError(b, err) - require.Equal(b, "v", values["k"]) + assert.NoError(b, err) + assert.Equal(b, "v", values["k"]) } func BenchmarkLoad(b *testing.B) { @@ -49,6 +48,6 @@ func BenchmarkLoad(b *testing.B) { } b.StopTimer() - require.NoError(b, err) - require.Equal(b, "v", values["k"]) + assert.NoError(b, err) + assert.Equal(b, "v", values["k"]) } diff --git a/provider/fs/fs.go b/provider/fs/fs.go index a4f338eb..195dd032 100644 --- a/provider/fs/fs.go +++ b/provider/fs/fs.go @@ -1,14 +1,12 @@ // Copyright (c) 2023 The konf authors // Use of this source code is governed by a MIT license found in the LICENSE file. -// Package fs loads configuration from files. +// Package fs loads configuration from file system. // -// FS loads file with given path from OS file system and returns nested map[string]any -// that is parsed as json. +// FS loads file with given path from file system +// and returns nested map[string]any that is parsed as json. // // The default behavior can be changed with following options: -// - WithFS provides the fs.FS that config file is loaded from. -// E.g. `WithFS(cfg)` will load configuration from embed file while cfg is embed.FS. // - WithUnmarshal provides the function that parses config file. // E.g. `WithUnmarshal(yaml.Unmarshal)` will parse the file as yaml. // - IgnoreFileNotExit ignores the error if config file does not exist. @@ -22,7 +20,7 @@ import ( "os" ) -// FS is a Provider that loads configuration from file. +// FS is a Provider that loads configuration from file system. type FS struct { fs fs.FS path string @@ -30,7 +28,7 @@ type FS struct { ignoreNotExist bool } -// New returns a FS with the given path and Option(s). +// New returns a FS with the given fs.FS, path and Option(s). func New(fs fs.FS, path string, opts ...Option) FS { option := &options{ fs: fs, diff --git a/provider/fs/fs_test.go b/provider/fs/fs_test.go index 119dbc40..75364bbf 100644 --- a/provider/fs/fs_test.go +++ b/provider/fs/fs_test.go @@ -12,8 +12,7 @@ import ( "testing" "testing/fstest" - "github.com/stretchr/testify/require" - + "github.com/ktong/konf/internal/assert" pfs "github.com/ktong/konf/provider/fs" ) @@ -82,10 +81,10 @@ func TestFile_Load(t *testing.T) { values, err := pfs.New(testcase.fs, testcase.path, testcase.opts...).Load() if err != nil { - require.True(t, strings.HasPrefix(err.Error(), testcase.err)) + assert.True(t, strings.HasPrefix(err.Error(), testcase.err)) } else { - require.NoError(t, err) - require.Equal(t, testcase.expected, values) + assert.NoError(t, err) + assert.Equal(t, testcase.expected, values) } }) } @@ -94,5 +93,5 @@ func TestFile_Load(t *testing.T) { func TestFile_String(t *testing.T) { t.Parallel() - require.Equal(t, "fs:config.json", pfs.New(fstest.MapFS{}, "config.json").String()) + assert.Equal(t, "fs:config.json", pfs.New(fstest.MapFS{}, "config.json").String()) } diff --git a/provider/pflag/benchmark_test.go b/provider/pflag/benchmark_test.go index f1958530..8bd3c9a4 100644 --- a/provider/pflag/benchmark_test.go +++ b/provider/pflag/benchmark_test.go @@ -7,9 +7,9 @@ import ( "testing" "github.com/spf13/pflag" - "github.com/stretchr/testify/require" kflag "github.com/ktong/konf/provider/pflag" + "github.com/ktong/konf/provider/pflag/internal/assert" ) func BenchmarkNew(b *testing.B) { @@ -24,8 +24,8 @@ func BenchmarkNew(b *testing.B) { b.StopTimer() values, err := loader.Load() - require.NoError(b, err) - require.Equal(b, "v", values["k"]) + assert.NoError(b, err) + assert.Equal(b, "v", values["k"]) } func BenchmarkLoad(b *testing.B) { @@ -43,6 +43,6 @@ func BenchmarkLoad(b *testing.B) { } b.StopTimer() - require.NoError(b, err) - require.Equal(b, "v", values["k"]) + assert.NoError(b, err) + assert.Equal(b, "v", values["k"]) } diff --git a/provider/pflag/go.mod b/provider/pflag/go.mod index 36eaf272..9ca8acc2 100644 --- a/provider/pflag/go.mod +++ b/provider/pflag/go.mod @@ -3,15 +3,3 @@ module github.com/ktong/konf/provider/pflag go 1.19 require github.com/spf13/pflag v1.0.5 - -require ( // for test - github.com/stretchr/testify v1.8.4 - go.uber.org/goleak v1.3.0 -) - -require ( // indirect - github.com/davecgh/go-spew v1.1.1 // indirect - github.com/kr/text v0.2.0 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect -) diff --git a/provider/pflag/go.sum b/provider/pflag/go.sum index 14d1b2c5..287f6fa8 100644 --- a/provider/pflag/go.sum +++ b/provider/pflag/go.sum @@ -1,18 +1,2 @@ -github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= -github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= -github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -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/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= -go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= -go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/provider/pflag/internal/assert/assert.go b/provider/pflag/internal/assert/assert.go new file mode 100644 index 00000000..62f4be0b --- /dev/null +++ b/provider/pflag/internal/assert/assert.go @@ -0,0 +1,25 @@ +// Copyright (c) 2023 The konf authors +// Use of this source code is governed by a MIT license found in the LICENSE file. + +package assert + +import ( + "reflect" + "testing" +) + +func Equal[T any](tb testing.TB, expected, actual T) { + tb.Helper() + + if !reflect.DeepEqual(expected, actual) { + tb.Errorf("expected: %v; actual: %v", expected, actual) + } +} + +func NoError(tb testing.TB, err error) { + tb.Helper() + + if err != nil { + tb.Errorf("unexpected error: %v", err) + } +} diff --git a/provider/pflag/internal/maps/insert_test.go b/provider/pflag/internal/maps/insert_test.go index c69b06ee..37b9cc86 100644 --- a/provider/pflag/internal/maps/insert_test.go +++ b/provider/pflag/internal/maps/insert_test.go @@ -6,8 +6,7 @@ package maps_test import ( "testing" - "github.com/stretchr/testify/require" - + "github.com/ktong/konf/provider/pflag/internal/assert" "github.com/ktong/konf/provider/pflag/internal/maps" ) @@ -69,7 +68,7 @@ func TestInsert(t *testing.T) { t.Parallel() maps.Insert(testcase.dst, testcase.keys, testcase.val) - require.Equal(t, testcase.expected, testcase.dst) + assert.Equal(t, testcase.expected, testcase.dst) }) } } diff --git a/provider/pflag/leak_test.go b/provider/pflag/leak_test.go deleted file mode 100644 index 4e635d39..00000000 --- a/provider/pflag/leak_test.go +++ /dev/null @@ -1,14 +0,0 @@ -// Copyright (c) 2023 The konf authors -// Use of this source code is governed by a MIT license found in the LICENSE file. - -package pflag_test - -import ( - "testing" - - "go.uber.org/goleak" -) - -func TestMain(m *testing.M) { - goleak.VerifyTestMain(m) -} diff --git a/provider/pflag/pflag_test.go b/provider/pflag/pflag_test.go index 8055dc5e..36de5ba4 100644 --- a/provider/pflag/pflag_test.go +++ b/provider/pflag/pflag_test.go @@ -9,9 +9,9 @@ import ( "testing" "github.com/spf13/pflag" - "github.com/stretchr/testify/require" kflag "github.com/ktong/konf/provider/pflag" + "github.com/ktong/konf/provider/pflag/internal/assert" ) func TestFlag_Load(t *testing.T) { @@ -54,8 +54,8 @@ func TestFlag_Load(t *testing.T) { t.Run(testcase.description, func(t *testing.T) { values, err := kflag.New(testcase.opts...).Load() - require.NoError(t, err) - require.Equal(t, testcase.expected, values) + assert.NoError(t, err) + assert.Equal(t, testcase.expected, values) }) } } @@ -85,7 +85,7 @@ func TestFlag_String(t *testing.T) { t.Run(testcase.description, func(t *testing.T) { t.Parallel() - require.Equal( + assert.Equal( t, testcase.expected, kflag.New(