From b762871c386910de893d331c1e3ddd24af5fc91a Mon Sep 17 00:00:00 2001 From: Bogdan Daragan <19273017+BoRuDar@users.noreply.github.com> Date: Sat, 13 Apr 2024 23:01:27 +0100 Subject: [PATCH] Dev -> master (#44) * Fix linter warnings (#43) --- README.md | 5 +++-- configurator_test.go | 21 +++++++++++++++++++++ defaultProvider_test.go | 6 ++++++ envProvider_test.go | 2 +- fieldStetter_test.go | 2 ++ flagProvider_test.go | 3 +-- jsonProvider_test.go | 1 + 7 files changed, 35 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index e349ad7..5157805 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ [![Go Report Card](https://goreportcard.com/badge/github.com/BoRuDar/configuration/v4)](https://goreportcard.com/report/github.com/BoRuDar/configuration/v4) [![codecov](https://codecov.io/gh/BoRuDar/configuration/branch/master/graph/badge.svg)](https://codecov.io/gh/BoRuDar/configuration) +[![OpenSSF Best Practices](https://www.bestpractices.dev/projects/6295/badge)](https://www.bestpractices.dev/projects/6295) [![GoDoc](https://godoc.org/github.com/BoRuDar/configuration?status.png)](https://godoc.org/github.com/BoRuDar/configuration/v4) [![Mentioned in Awesome Go](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go) -[![OpenSSF Best Practices](https://www.bestpractices.dev/projects/6295/badge)](https://www.bestpractices.dev/projects/6295) # Configuration @@ -13,7 +13,7 @@ Available features: - setting values from command line *flags* - `NewFlagProvider(&cfg)` - setting values from a JSON *file* - `NewJSONFileProvider("./testdata/input.json")` -Supported types: +## Supported types: - `string`, `*string`, `[]string`, `[]*string` - `bool`, `*bool`, `[]bool`, `[]*bool` - `int`, `int8`, `int16`, `int32`, `int64` + slices of these types @@ -24,6 +24,7 @@ Supported types: - `*float32`, `*float64` + slices of these types - `time.Duration` from strings like `12ms`, `2s` etc. - embedded structs and pointers to structs +- any custom type which satisfies `FieldSetter` [interface](#FieldSetter-interface) # Why? diff --git a/configurator_test.go b/configurator_test.go index c32a4bc..a7a0da8 100644 --- a/configurator_test.go +++ b/configurator_test.go @@ -7,6 +7,8 @@ import ( "time" ) +// nolint:paralleltest +// t.Setenv doesn't work with t.Parallel() func TestConfigurator(t *testing.T) { // setting command line flag os.Args = []string{"smth", "-name=flag_value"} @@ -83,6 +85,8 @@ func TestConfigurator(t *testing.T) { } func TestConfigurator_Errors(t *testing.T) { + t.Parallel() + tests := map[string]struct { input any providers []Provider @@ -102,6 +106,8 @@ func TestConfigurator_Errors(t *testing.T) { for name, test := range tests { test := test t.Run(name, func(t *testing.T) { + t.Parallel() + err := New(test.input, test.providers...).InitValues() if err == nil { t.Fatal("expected error but got nil") @@ -111,6 +117,8 @@ func TestConfigurator_Errors(t *testing.T) { } func TestEmbeddedFlags(t *testing.T) { + t.Parallel() + type ( Client struct { ServerAddress string `flag:"addr|127.0.0.1:443|server address"` @@ -130,6 +138,7 @@ func TestEmbeddedFlags(t *testing.T) { assert(t, cfg.Client.ServerAddress, "addr_value") } +// nolint:paralleltest func TestFallBackToDefault(t *testing.T) { // defining a struct cfg := struct { @@ -149,6 +158,8 @@ func TestFallBackToDefault(t *testing.T) { } func TestSetOnFailFn(t *testing.T) { + t.Parallel() + cfg := struct { Name string `default:"test_name"` }{} @@ -172,6 +183,8 @@ func TestSetOnFailFn(t *testing.T) { } func TestProviderName(t *testing.T) { + t.Parallel() + testCases := map[string]struct { provider Provider expectedName string @@ -198,21 +211,29 @@ func TestProviderName(t *testing.T) { test := test t.Run(name, func(t *testing.T) { + t.Parallel() + assert(t, test.expectedName, test.provider.Name()) }) } } func TestConfigurator_NameCollision(t *testing.T) { + t.Parallel() + err := New(&struct{}{}, NewDefaultProvider(), NewDefaultProvider()).InitValues() assert(t, ErrProviderNameCollision, err) } func TestConfigurator_FailedProvider(t *testing.T) { + t.Parallel() + err := New(&struct{}{}, NewJSONFileProvider("doesn't exist")).InitValues() assert(t, "cannot init [JSONFileProvider] provider: JSONFileProvider.Init: open doesn't exist: no such file or directory", err.Error()) } +// nolint:paralleltest +// t.Setenv doesn't work with t.Parallel() func Test_FromEnvAndDefault(t *testing.T) { t.Setenv("AGE", "24") diff --git a/defaultProvider_test.go b/defaultProvider_test.go index d007252..5f53314 100644 --- a/defaultProvider_test.go +++ b/defaultProvider_test.go @@ -7,6 +7,8 @@ import ( ) func TestDefaultProvider(t *testing.T) { + t.Parallel() + type testStruct struct { Name string `default:"default_provider_val"` } @@ -28,6 +30,8 @@ func TestDefaultProvider(t *testing.T) { } func TestDefaultProviderPtr(t *testing.T) { + t.Parallel() + type testStruct struct { Name *string `default:"default_provider_val_ptr"` } @@ -49,6 +53,8 @@ func TestDefaultProviderPtr(t *testing.T) { } func TestDefaultProviderFailed(t *testing.T) { + t.Parallel() + type testStruct struct { Name string } diff --git a/envProvider_test.go b/envProvider_test.go index a785f59..4b5d01d 100644 --- a/envProvider_test.go +++ b/envProvider_test.go @@ -1,4 +1,4 @@ -// nolint:dupl +// nolint:dupl,paralleltest package configuration import ( diff --git a/fieldStetter_test.go b/fieldStetter_test.go index 40c1def..966498a 100644 --- a/fieldStetter_test.go +++ b/fieldStetter_test.go @@ -275,6 +275,8 @@ func TestSetPtrValue_Floats(t *testing.T) { } func TestSetPtrValue_Bool(t *testing.T) { + t.Parallel() + var testBool *bool fieldType := reflect.TypeOf(&testBool).Elem() fieldVal := reflect.ValueOf(&testBool).Elem() diff --git a/flagProvider_test.go b/flagProvider_test.go index 1366edf..e7f2e1e 100644 --- a/flagProvider_test.go +++ b/flagProvider_test.go @@ -1,4 +1,4 @@ -// nolint:dupl,goconst +// nolint:dupl,goconst,paralleltest package configuration import ( @@ -210,7 +210,6 @@ func TestFlagProvider_Errors(t *testing.T) { for name, test := range testCases { test := test - t.Run(name, func(t *testing.T) { fieldType := reflect.TypeOf(test.obj).Elem().Field(0) fieldVal := reflect.ValueOf(test.obj).Elem().Field(0) diff --git a/jsonProvider_test.go b/jsonProvider_test.go index 55c3cef..6079e43 100644 --- a/jsonProvider_test.go +++ b/jsonProvider_test.go @@ -1,3 +1,4 @@ +// nolint:paralleltest package configuration import (