Skip to content

Commit

Permalink
Dev -> master (#44)
Browse files Browse the repository at this point in the history
* Fix linter warnings (#43)
  • Loading branch information
BoRuDar authored Apr 13, 2024
1 parent 95dc06a commit b762871
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 5 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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?
Expand Down
21 changes: 21 additions & 0 deletions configurator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down Expand Up @@ -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
Expand All @@ -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")
Expand All @@ -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"`
Expand All @@ -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 {
Expand All @@ -149,6 +158,8 @@ func TestFallBackToDefault(t *testing.T) {
}

func TestSetOnFailFn(t *testing.T) {
t.Parallel()

cfg := struct {
Name string `default:"test_name"`
}{}
Expand All @@ -172,6 +183,8 @@ func TestSetOnFailFn(t *testing.T) {
}

func TestProviderName(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
provider Provider
expectedName string
Expand All @@ -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")

Expand Down
6 changes: 6 additions & 0 deletions defaultProvider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
)

func TestDefaultProvider(t *testing.T) {
t.Parallel()

type testStruct struct {
Name string `default:"default_provider_val"`
}
Expand All @@ -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"`
}
Expand All @@ -49,6 +53,8 @@ func TestDefaultProviderPtr(t *testing.T) {
}

func TestDefaultProviderFailed(t *testing.T) {
t.Parallel()

type testStruct struct {
Name string
}
Expand Down
2 changes: 1 addition & 1 deletion envProvider_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// nolint:dupl
// nolint:dupl,paralleltest
package configuration

import (
Expand Down
2 changes: 2 additions & 0 deletions fieldStetter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 1 addition & 2 deletions flagProvider_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// nolint:dupl,goconst
// nolint:dupl,goconst,paralleltest
package configuration

import (
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions jsonProvider_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// nolint:paralleltest
package configuration

import (
Expand Down

0 comments on commit b762871

Please # to comment.