From 14d51e66f78265a4dd1f0ab4eecd4f2db13a04f5 Mon Sep 17 00:00:00 2001 From: Sotirios Mantziaris Date: Sun, 13 Oct 2024 12:35:31 +0300 Subject: [PATCH] Linting (#181) --- .golangci.yml | 46 +++++++--- Makefile | 4 +- config/config_test.go | 28 +++--- config/custom_type_test.go | 15 ++-- harvester_integration_test.go | 6 +- harvester_test.go | 26 +++--- monitor/consul/watcher.go | 4 +- monitor/consul/watcher_integration_test.go | 2 +- monitor/consul/watcher_test.go | 8 +- monitor/monitor_test.go | 12 +-- monitor/redis/watcher_test.go | 10 +-- seed/consul/getter_integration_test.go | 6 +- seed/consul/getter_test.go | 5 +- seed/redis/getter_integration_test.go | 2 +- seed/redis/getter_test.go | 5 +- seed/seed.go | 4 +- seed/seed_test.go | 44 +++++----- sync/sync.go | 4 +- sync/sync_test.go | 99 +++++++++++----------- 19 files changed, 178 insertions(+), 152 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index de06c13c..ea83dc52 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,12 +1,8 @@ run: - concurrency: 4 timeout: 5m issues-exit-code: 1 tests: true - skip-dirs: - - vendor - modules-download-mode: vendor # list of build tags, all linters use it. Default is empty list @@ -15,9 +11,6 @@ run: # output configuration options output: - # colored-line-number|line-number|json|tab|checkstyle|code-climate, default is "colored-line-number" - format: colored-line-number - # print lines of code with issue, default is true print-issued-lines: true @@ -29,15 +22,16 @@ output: linters: disable-all: true enable: - - golint - gofmt - - gosec + - gosimple - unparam - goconst - prealloc - stylecheck - unconvert + - unused - staticcheck + - ineffassign - gosec - tparallel - whitespace @@ -48,19 +42,47 @@ linters: - errname - govet - predeclared + - nestif - exhaustive - tenv - gofumpt - forcetypeassert - nilerr - errcheck - # - promlinter this is a very nice linter, but it will most probably break things... - # - nestif + - bodyclose + - goimports + - durationcheck + - errchkjson + - sloglint + - dupword + - noctx + - makezero + - nilnil + - reassign + - sloglint + - spancheck + - testifylint + - wastedassign + - rowserrcheck + - sqlclosecheck + - goprintffuncname + - testableexamples + - wastedassign + - nonamedreturns + - perfsprint + - dogsled + - protogetter + - usestdlibvars + - testableexamples fast: false issues: + exclude-dirs: + - vendor + max-same-issues: 10 + exclude-rules: # Exclude some staticcheck messages - linters: - staticcheck - text: "SA1019:" \ No newline at end of file + text: "SA1019:" diff --git a/Makefile b/Makefile index 67a69530..63959400 100644 --- a/Makefile +++ b/Makefile @@ -24,10 +24,10 @@ fmtcheck: @sh -c "'$(CURDIR)/scripts/gofmtcheck.sh'" lint: fmtcheck - $(DOCKER) run --env=GOFLAGS=-mod=vendor --rm -v $(CURDIR):/app -w /app golangci/golangci-lint:v1.54.2 golangci-lint -v run + $(DOCKER) run --env=GOFLAGS=-mod=vendor --rm -v $(CURDIR):/app -w /app golangci/golangci-lint:v1.61.0 golangci-lint -v run deeplint: fmtcheck - $(DOCKER) run --env=GOFLAGS=-mod=vendor --rm -v $(CURDIR):/app -w /app golangci/golangci-lint:v1.54.2 golangci-lint run --exclude-use-default=false --enable-all -D dupl --build-tags integration + $(DOCKER) run --env=GOFLAGS=-mod=vendor --rm -v $(CURDIR):/app -w /app golangci/golangci-lint:v1.61.0 golangci-lint run --exclude-use-default=false --enable-all -D dupl --build-tags integration deps: docker container inspect harvester-consul > /dev/null 2>&1 || docker run -d --rm -p 8500:8500 -p 8600:8600/udp --name=harvester-consul consul:1.4.3 agent -server -ui -node=server-1 -bootstrap-expect=1 -client=0.0.0.0 -http-port 8500 -log-level=err diff --git a/config/config_test.go b/config/config_test.go index 38dc593f..ae8fb51e 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -35,9 +35,9 @@ func TestField_Set(t *testing.T) { t.Run(name, func(t *testing.T) { err := tt.field.Set(tt.args.value, tt.args.version) if tt.wantErr { - assert.Error(t, err) + require.Error(t, err) } else { - assert.NoError(t, err) + require.NoError(t, err) } }) } @@ -64,10 +64,10 @@ func TestNew(t *testing.T) { t.Run(name, func(t *testing.T) { got, err := New(tt.args.cfg, nil) if tt.wantErr { - assert.Error(t, err) + require.Error(t, err) assert.Nil(t, got) } else { - assert.NoError(t, err) + require.NoError(t, err) assert.NotNil(t, got) assert.Len(t, got.Fields, 7) assertField(t, got.Fields[0], "Name", "String", @@ -102,40 +102,40 @@ func TestConfig_Set(t *testing.T) { cfg, err := New(&c, chNotify) require.NoError(t, err) err = cfg.Fields[0].Set("John Doe", 1) - assert.NoError(t, err) + require.NoError(t, err) change := <-chNotify assert.Equal(t, "field [Name] of type [String] changed from [] to [John Doe]", change.String()) err = cfg.Fields[1].Set("18", 1) - assert.NoError(t, err) + require.NoError(t, err) change = <-chNotify assert.Equal(t, "field [Age] of type [Int64] changed from [0] to [18]", change.String()) err = cfg.Fields[2].Set("99.9", 1) - assert.NoError(t, err) + require.NoError(t, err) change = <-chNotify assert.Equal(t, "field [Balance] of type [Float64] changed from [0.000000] to [99.9]", change.String()) err = cfg.Fields[3].Set("true", 1) - assert.NoError(t, err) + require.NoError(t, err) change = <-chNotify assert.Equal(t, "field [HasJob] of type [Bool] changed from [false] to [true]", change.String()) err = cfg.Fields[4].Set("6000", 1) - assert.NoError(t, err) + require.NoError(t, err) change = <-chNotify assert.Equal(t, "field [PositionSalary] of type [Int64] changed from [0] to [6000]", change.String()) err = cfg.Fields[5].Set("baz", 1) - assert.NoError(t, err) + require.NoError(t, err) change = <-chNotify assert.Equal(t, "field [LevelOneLevelTwoDeepField] of type [String] changed from [] to [baz]", change.String()) err = cfg.Fields[6].Set("true", 1) - assert.NoError(t, err) + require.NoError(t, err) change = <-chNotify assert.Equal(t, "field [IsAdult] of type [Bool] changed from [false] to [true]", change.String()) assert.Equal(t, "John Doe", c.Name.Get()) assert.Equal(t, int64(18), c.Age.Get()) - assert.Equal(t, 99.9, c.Balance.Get()) - assert.Equal(t, true, c.HasJob.Get()) + assert.InDelta(t, 99.9, c.Balance.Get(), 0.01) + assert.True(t, c.HasJob.Get()) assert.Equal(t, int64(6000), c.Position.Salary.Get()) assert.Equal(t, "baz", c.LevelOne.LevelTwo.DeepField.Get()) - assert.Equal(t, true, c.IsAdult.Get()) + assert.True(t, c.IsAdult.Get()) } type testNestedConfig struct { diff --git a/config/custom_type_test.go b/config/custom_type_test.go index 9f415138..5450debe 100644 --- a/config/custom_type_test.go +++ b/config/custom_type_test.go @@ -1,23 +1,24 @@ package config_test import ( - "fmt" + "errors" "sync" "testing" "github.com/beatlabs/harvester/config" stdTypes "github.com/beatlabs/harvester/sync" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestCustomField(t *testing.T) { c := &testConfig{} cfg, err := config.New(c, nil) - assert.NoError(t, err) + require.NoError(t, err) err = cfg.Fields[0].Set("expected", 1) - assert.NoError(t, err) + require.NoError(t, err) err = cfg.Fields[1].Set("bar", 1) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, "expected", c.CustomValue.Get()) assert.Equal(t, "bar", c.SomeString.Get()) } @@ -25,9 +26,9 @@ func TestCustomField(t *testing.T) { func TestErrorValidationOnCustomField(t *testing.T) { c := &testConfig{} cfg, err := config.New(c, nil) - assert.NoError(t, err) + require.NoError(t, err) err = cfg.Fields[0].Set("not_expected", 1) - assert.Error(t, err) + require.Error(t, err) } type testConcreteValue struct { @@ -53,7 +54,7 @@ func (f *testConcreteValue) String() string { func (f *testConcreteValue) SetString(value string) error { if value != "expected" { - return fmt.Errorf("unable to store provided value") + return errors.New("unable to store provided value") } f.Set(value) return nil diff --git a/harvester_integration_test.go b/harvester_integration_test.go index 86a5f765..09875a3c 100644 --- a/harvester_integration_test.go +++ b/harvester_integration_test.go @@ -86,11 +86,11 @@ func Test_harvester_Harvest(t *testing.T) { defer cnl() err = h.Harvest(ctx) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, "Mr. Smith", cfg.Name.Get()) assert.Equal(t, int64(99), cfg.Age.Get()) - assert.Equal(t, 111.1, cfg.Balance.Get()) - assert.Equal(t, false, cfg.HasJob.Get()) + assert.InDelta(t, 111.1, cfg.Balance.Get(), 0.01) + assert.False(t, cfg.HasJob.Get()) assert.Equal(t, 1*time.Second, cfg.FunTime.Get()) assert.Equal(t, 2*time.Second, cfg.WorkTime.Get()) assert.Equal(t, int64(123), cfg.Foo.Bar.Get()) diff --git a/harvester_test.go b/harvester_test.go index 56911abf..37beaaf5 100644 --- a/harvester_test.go +++ b/harvester_test.go @@ -9,6 +9,7 @@ import ( "github.com/beatlabs/harvester/sync" "github.com/go-redis/redis/v8" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) const ( @@ -93,10 +94,10 @@ func TestCreateWithConsulAndRedis(t *testing.T) { WithRedisMonitor(tt.args.monitorRedisClient, tt.args.monitoringPollInterval)) if tt.expectedErr != "" { - assert.EqualError(t, err, tt.expectedErr) + require.EqualError(t, err, tt.expectedErr) assert.Nil(t, got) } else { - assert.NoError(t, err) + require.NoError(t, err) assert.NotNil(t, got) } }) @@ -117,7 +118,7 @@ func TestWithNotification(t *testing.T) { for name, tt := range tests { t.Run(name, func(t *testing.T) { got, err := New(tt.args.cfg, tt.args.chNotify) - assert.NoError(t, err) + require.NoError(t, err) assert.NotNil(t, got) }) } @@ -126,16 +127,15 @@ func TestWithNotification(t *testing.T) { func TestCreate_NoConsulOrRedis(t *testing.T) { cfg := &testConfigNoConsul{} got, err := New(cfg, nil) - assert.NoError(t, err) + require.NoError(t, err) assert.NotNil(t, got) ctx, cnl := context.WithCancel(context.Background()) defer cnl() - err = got.Harvest(ctx) - assert.NoError(t, err) + require.NoError(t, got.Harvest(ctx)) assert.Equal(t, "John Doe", cfg.Name.Get()) assert.Equal(t, int64(18), cfg.Age.Get()) - assert.Equal(t, 99.9, cfg.Balance.Get()) - assert.Equal(t, true, cfg.HasJob.Get()) + assert.InDelta(t, 99.9, cfg.Balance.Get(), 0.01) + assert.True(t, cfg.HasJob.Get()) assert.Equal(t, int64(8000), cfg.Position.Salary.Get()) assert.Equal(t, int64(24), cfg.Position.Place.RoomNumber.Get()) } @@ -143,19 +143,19 @@ func TestCreate_NoConsulOrRedis(t *testing.T) { func TestCreate_SeedError(t *testing.T) { cfg := &testConfigSeedError{} got, err := New(cfg, nil) - assert.NoError(t, err) + require.NoError(t, err) assert.NotNil(t, got) ctx, cnl := context.WithCancel(context.Background()) defer cnl() err = got.Harvest(ctx) - assert.Error(t, err) + require.Error(t, err) } type testConfig struct { Name sync.String `seed:"John Doe" consul:"harvester1/name"` - Age sync.Int64 `seed:"18" consul:"harvester/age"` - Balance sync.Float64 `seed:"99.9" consul:"harvester/balance"` - HasJob sync.Bool `seed:"true" consul:"harvester/has-job"` + Age sync.Int64 `seed:"18" consul:"harvester/age"` + Balance sync.Float64 `seed:"99.9" consul:"harvester/balance"` + HasJob sync.Bool `seed:"true" consul:"harvester/has-job"` FunTime sync.TimeDuration `seed:"1s" consul:"harvester/fun-time"` IsAdult sync.Bool `seed:"false" redis:"is-adult"` } diff --git a/monitor/consul/watcher.go b/monitor/consul/watcher.go index 3ac36471..7dfdfb6a 100644 --- a/monitor/consul/watcher.go +++ b/monitor/consul/watcher.go @@ -112,7 +112,7 @@ func (w *Watcher) createKeyPlanWithPrefix(key, prefix string, ch chan<- []*chang if err != nil { return nil, err } - pl.Handler = func(idx uint64, data interface{}) { + pl.Handler = func(_ uint64, data interface{}) { if data == nil { return } @@ -132,7 +132,7 @@ func (w *Watcher) createKeyPrefixPlan(keyPrefix string, ch chan<- []*change.Chan if err != nil { return nil, err } - pl.Handler = func(idx uint64, data interface{}) { + pl.Handler = func(_ uint64, data interface{}) { if data == nil { return } diff --git a/monitor/consul/watcher_integration_test.go b/monitor/consul/watcher_integration_test.go index 82c81ff5..c398c5ff 100644 --- a/monitor/consul/watcher_integration_test.go +++ b/monitor/consul/watcher_integration_test.go @@ -71,7 +71,7 @@ func TestWatch(t *testing.T) { default: assert.Fail(t, "key invalid", cng.Key()) } - assert.True(t, cng.Version() > 0) + assert.Positive(t, cng.Version()) } } } diff --git a/monitor/consul/watcher_test.go b/monitor/consul/watcher_test.go index ff2a1125..fe7cfb69 100644 --- a/monitor/consul/watcher_test.go +++ b/monitor/consul/watcher_test.go @@ -30,10 +30,10 @@ func TestNew(t *testing.T) { t.Run(name, func(t *testing.T) { got, err := New(tt.args.addr, "dc", "token", tt.args.timeout, tt.args.ii...) if tt.wantErr { - assert.Error(t, err) + require.Error(t, err) assert.Nil(t, got) } else { - assert.NoError(t, err) + require.NoError(t, err) assert.NotNil(t, got) } }) @@ -58,9 +58,9 @@ func TestWatcher_Watch(t *testing.T) { t.Run(name, func(t *testing.T) { err = w.Watch(tt.args.ctx, tt.args.ch) if tt.wantErr { - assert.Error(t, err) + require.Error(t, err) } else { - assert.NoError(t, err) + require.NoError(t, err) } }) } diff --git a/monitor/monitor_test.go b/monitor/monitor_test.go index f1673d5d..dffc8508 100644 --- a/monitor/monitor_test.go +++ b/monitor/monitor_test.go @@ -38,10 +38,10 @@ func TestNew(t *testing.T) { t.Run(name, func(t *testing.T) { got, err := New(tt.args.cfg, tt.args.ww...) if tt.wantErr { - assert.Error(t, err) + require.Error(t, err) assert.Nil(t, got) } else { - assert.NoError(t, err) + require.NoError(t, err) assert.NotNil(t, got) } }) @@ -55,7 +55,7 @@ func TestMonitor_Monitor_Error(t *testing.T) { mon, err := New(cfg, watchers...) require.NoError(t, err) err = mon.Monitor(context.Background()) - assert.Error(t, err) + require.Error(t, err) } func TestMonitor_Monitor(t *testing.T) { @@ -67,12 +67,12 @@ func TestMonitor_Monitor(t *testing.T) { require.NoError(t, err) ctx, cnl := context.WithCancel(context.Background()) err = mon.Monitor(ctx) - assert.NoError(t, err) + require.NoError(t, err) time.Sleep(100 * time.Millisecond) cnl() assert.Equal(t, int64(25), c.Age.Get()) - assert.Equal(t, 111.11, c.Balance.Get()) - assert.Equal(t, false, c.HasJob.Get()) + assert.InDelta(t, 111.11, c.Balance.Get(), 0.01) + assert.False(t, c.HasJob.Get()) assert.Equal(t, 6*time.Hour, c.WorkHours.Get()) assert.Equal(t, 7*time.Hour, c.NonWorkHours.Get()) } diff --git a/monitor/redis/watcher_test.go b/monitor/redis/watcher_test.go index 255af7f4..3c7a97f6 100644 --- a/monitor/redis/watcher_test.go +++ b/monitor/redis/watcher_test.go @@ -33,10 +33,10 @@ func TestNew(t *testing.T) { t.Run(name, func(t *testing.T) { got, err := New(tt.args.client, tt.args.pollInterval, tt.args.keys) if tt.expectedErr != "" { - assert.EqualError(t, err, tt.expectedErr) + require.EqualError(t, err, tt.expectedErr) assert.Nil(t, got) } else { - assert.NoError(t, err) + require.NoError(t, err) assert.NotNil(t, got) } }) @@ -61,9 +61,9 @@ func TestWatcher_Watch(t *testing.T) { t.Run(name, func(t *testing.T) { err = w.Watch(tt.args.ctx, tt.args.ch) if tt.wantErr { - assert.Error(t, err) + require.Error(t, err) } else { - assert.NoError(t, err) + require.NoError(t, err) } }) } @@ -135,7 +135,7 @@ func TestWatcher_Versioning(t *testing.T) { ch := make(chan []*change.Change, 10) err = w.Watch(ctx, ch) - assert.NoError(t, err) + require.NoError(t, err) // time for completing all the polling for the different states time.Sleep(100 * time.Millisecond) diff --git a/seed/consul/getter_integration_test.go b/seed/consul/getter_integration_test.go index 5027e275..ccb31ac0 100644 --- a/seed/consul/getter_integration_test.go +++ b/seed/consul/getter_integration_test.go @@ -65,12 +65,12 @@ func TestGetter_Get(t *testing.T) { require.NoError(t, err) got, version, err := gtr.Get(tt.args.key) if tt.wantErr { - assert.Error(t, err) + require.Error(t, err) assert.Empty(t, got) } else { - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, tt.want, got) - assert.True(t, version >= uint64(0)) + assert.GreaterOrEqual(t, version, uint64(0)) } }) } diff --git a/seed/consul/getter_test.go b/seed/consul/getter_test.go index a1847252..9a45f07d 100644 --- a/seed/consul/getter_test.go +++ b/seed/consul/getter_test.go @@ -5,6 +5,7 @@ import ( "time" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestNew(t *testing.T) { @@ -24,10 +25,10 @@ func TestNew(t *testing.T) { t.Run(name, func(t *testing.T) { got, err := New(tt.args.addr, "dc", "token", 0) if tt.wantErr { - assert.Error(t, err) + require.Error(t, err) assert.Nil(t, got) } else { - assert.NoError(t, err) + require.NoError(t, err) assert.NotNil(t, got) } }) diff --git a/seed/redis/getter_integration_test.go b/seed/redis/getter_integration_test.go index 537785d1..8cc3d2b0 100644 --- a/seed/redis/getter_integration_test.go +++ b/seed/redis/getter_integration_test.go @@ -25,7 +25,7 @@ func TestGetter_Get(t *testing.T) { gtr, err := New(client) require.NoError(t, err) got, _, err := gtr.Get(key) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, val, *got) delResult, err := client.Del(context.Background(), key).Result() diff --git a/seed/redis/getter_test.go b/seed/redis/getter_test.go index 7920bd4b..e3bdf5bd 100644 --- a/seed/redis/getter_test.go +++ b/seed/redis/getter_test.go @@ -5,6 +5,7 @@ import ( "github.com/go-redis/redis/v8" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestNew(t *testing.T) { @@ -22,10 +23,10 @@ func TestNew(t *testing.T) { t.Run(name, func(t *testing.T) { got, err := New(tt.args.client) if tt.expectedErr != "" { - assert.EqualError(t, err, tt.expectedErr) + require.EqualError(t, err, tt.expectedErr) assert.Nil(t, got) } else { - assert.NoError(t, err) + require.NoError(t, err) assert.NotNil(t, got) } }) diff --git a/seed/seed.go b/seed/seed.go index 4d2da02e..a9d1110a 100644 --- a/seed/seed.go +++ b/seed/seed.go @@ -69,7 +69,7 @@ func (s *Seeder) Seed(cfg *config.Config) error { seedMap[f] = true } key, ok := ss[config.SourceEnv] - if ok { + if ok { //nolint:nestif val, ok := os.LookupEnv(key) if ok { err := f.Set(val, 0) @@ -154,7 +154,7 @@ func (s *Seeder) Seed(cfg *config.Config) error { } } - if len(flagInfos) > 0 { + if len(flagInfos) > 0 { //nolint:nestif if !flagSet.Parsed() { // Set the flagSet output to something that will not be displayed, otherwise in case of an error // it will display the usage, which we don't want. diff --git a/seed/seed_test.go b/seed/seed_test.go index 28a1172b..54ebded8 100644 --- a/seed/seed_test.go +++ b/seed/seed_test.go @@ -28,10 +28,10 @@ func TestNewParam(t *testing.T) { t.Run(name, func(t *testing.T) { got, err := NewParam(tt.args.src, tt.args.getter) if tt.wantErr { - assert.Error(t, err) + require.Error(t, err) assert.Nil(t, got) } else { - assert.NoError(t, err) + require.NoError(t, err) assert.NotNil(t, got) } }) @@ -127,9 +127,9 @@ func TestSeeder_Seed_Flags(t *testing.T) { err = seeder.Seed(cfg) if tC.expectedErr != nil { - assert.EqualError(t, err, tC.expectedErr.Error()) + require.EqualError(t, err, tC.expectedErr.Error()) } else { - assert.NoError(t, err) + require.NoError(t, err) actualAge := tC.inputConfig.GetAge() assert.Equal(t, tC.expectedAge, actualAge.Get()) } @@ -138,8 +138,8 @@ func TestSeeder_Seed_Flags(t *testing.T) { } func TestSeeder_Seed(t *testing.T) { - require.NoError(t, os.Setenv("ENV_AGE", "25")) - require.NoError(t, os.Setenv("ENV_WORK_HOURS", "9h")) + t.Setenv("ENV_AGE", "25") + t.Setenv("ENV_WORK_HOURS", "9h") consulParamSuccess, err := NewParam(config.SourceConsul, &stubGetter{}) require.NoError(t, err) @@ -154,14 +154,14 @@ func TestSeeder_Seed(t *testing.T) { err = New(*consulParamSuccess, *redisParamSuccess).Seed(goodCfg) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, "John Doe", c.Name.Get()) assert.Equal(t, int64(25), c.Age.Get()) - assert.Equal(t, 99.9, c.Balance.Get()) + assert.InDelta(t, 99.9, c.Balance.Get(), 0.01) assert.True(t, c.HasJob.Get()) assert.Equal(t, "foobar", c.About.Get()) assert.Equal(t, 9*time.Hour, c.WorkHours.Get()) - assert.Equal(t, true, c.IsAdult.Get()) + assert.True(t, c.IsAdult.Get()) }) t.Run("consul error, success", func(t *testing.T) { @@ -174,14 +174,14 @@ func TestSeeder_Seed(t *testing.T) { err = New(*consulParamError, *redisParamSuccess).Seed(goodCfg) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, "John Doe", c.Name.Get()) assert.Equal(t, int64(25), c.Age.Get()) - assert.Equal(t, 99.9, c.Balance.Get()) + assert.InDelta(t, 99.9, c.Balance.Get(), 0.1) assert.True(t, c.HasJob.Get()) assert.Equal(t, "foobar", c.About.Get()) assert.Equal(t, 9*time.Hour, c.WorkHours.Get()) - assert.Equal(t, true, c.IsAdult.Get()) + assert.True(t, c.IsAdult.Get()) }) t.Run("redis error, success", func(t *testing.T) { @@ -194,14 +194,14 @@ func TestSeeder_Seed(t *testing.T) { err = New(*consulParamSuccess, *redisParamFailure).Seed(goodCfg) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, "John Doe", c.Name.Get()) assert.Equal(t, int64(25), c.Age.Get()) - assert.Equal(t, 99.9, c.Balance.Get()) + assert.InDelta(t, 99.9, c.Balance.Get(), 0.01) assert.True(t, c.HasJob.Get()) assert.Equal(t, "foobar", c.About.Get()) assert.Equal(t, 9*time.Hour, c.WorkHours.Get()) - assert.Equal(t, false, c.IsAdult.Get()) + assert.False(t, c.IsAdult.Get()) }) t.Run("file not exists, success", func(t *testing.T) { @@ -211,7 +211,7 @@ func TestSeeder_Seed(t *testing.T) { err = New().Seed(fileNotExistCfg) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, int64(20), c.Age.Get()) }) @@ -222,7 +222,7 @@ func TestSeeder_Seed(t *testing.T) { err = New().Seed(goodCfg) - assert.Error(t, err) + require.Error(t, err) }) t.Run("consul missing value, failure", func(t *testing.T) { @@ -231,7 +231,7 @@ func TestSeeder_Seed(t *testing.T) { err = New().Seed(missingCfg) - assert.Error(t, err) + require.Error(t, err) }) t.Run("invalid int, failure", func(t *testing.T) { @@ -240,7 +240,7 @@ func TestSeeder_Seed(t *testing.T) { err = New().Seed(invalidIntCfg) - assert.Error(t, err) + require.Error(t, err) }) t.Run("invalid float, failure", func(t *testing.T) { @@ -249,7 +249,7 @@ func TestSeeder_Seed(t *testing.T) { err = New().Seed(invalidFloatCfg) - assert.Error(t, err) + require.Error(t, err) }) t.Run("invalid bool, failure", func(t *testing.T) { @@ -258,7 +258,7 @@ func TestSeeder_Seed(t *testing.T) { err = New().Seed(invalidBoolCfg) - assert.Error(t, err) + require.Error(t, err) }) t.Run("invalid file int, failure", func(t *testing.T) { @@ -267,7 +267,7 @@ func TestSeeder_Seed(t *testing.T) { err = New().Seed(invalidFileIntCfg) - assert.Error(t, err) + require.Error(t, err) }) } diff --git a/sync/sync.go b/sync/sync.go index ce37d658..a18aa6d8 100644 --- a/sync/sync.go +++ b/sync/sync.go @@ -104,7 +104,7 @@ func (i *Int64) UnmarshalJSON(d []byte) error { func (i *Int64) String() string { i.rw.RLock() defer i.rw.RUnlock() - return fmt.Sprintf("%d", i.value) + return strconv.FormatInt(i.value, 10) } // SetString parses and sets a value from string type. @@ -287,7 +287,7 @@ func (s *Secret) Set(value string) { } // MarshalJSON returns the JSON encoding of the value. -func (s *Secret) MarshalJSON() (out []byte, err error) { +func (s *Secret) MarshalJSON() ([]byte, error) { return json.Marshal(s.String()) } diff --git a/sync/sync_test.go b/sync/sync_test.go index 692f9afd..aca631ac 100644 --- a/sync/sync_test.go +++ b/sync/sync_test.go @@ -1,12 +1,13 @@ package sync import ( - "fmt" "regexp" + "strconv" "testing" "time" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestBool(t *testing.T) { @@ -21,25 +22,25 @@ func TestBool(t *testing.T) { assert.Equal(t, "true", b.String()) d, err := b.MarshalJSON() - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, "true", string(d)) } func TestBool_SetString(t *testing.T) { var b Bool - assert.Error(t, b.SetString("wrong")) - assert.NoError(t, b.SetString("true")) + require.Error(t, b.SetString("wrong")) + require.NoError(t, b.SetString("true")) assert.True(t, b.Get()) } func TestBool_UnmarshalJSON(t *testing.T) { var b Bool err := b.UnmarshalJSON([]byte("wrong")) - assert.Error(t, err) + require.Error(t, err) assert.False(t, b.Get()) err = b.UnmarshalJSON([]byte("true")) - assert.NoError(t, err) + require.NoError(t, err) assert.True(t, b.Get()) } @@ -55,25 +56,25 @@ func TestInt64(t *testing.T) { assert.Equal(t, "10", i.String()) d, err := i.MarshalJSON() - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, "10", string(d)) } func TestInt64_SetString(t *testing.T) { var i Int64 - assert.Error(t, i.SetString("wrong")) - assert.NoError(t, i.SetString("10")) + require.Error(t, i.SetString("wrong")) + require.NoError(t, i.SetString("10")) assert.Equal(t, int64(10), i.Get()) } func TestInt64_UnmarshalJSON(t *testing.T) { var b Int64 err := b.UnmarshalJSON([]byte("123.544")) // this is wrong - assert.Error(t, err) + require.Error(t, err) assert.Equal(t, int64(0), b.Get()) err = b.UnmarshalJSON([]byte("123")) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, int64(123), b.Get()) } @@ -85,30 +86,30 @@ func TestFloat64(t *testing.T) { ch <- struct{}{} }() <-ch - assert.Equal(t, 1.23, f.Get()) + assert.InDelta(t, 1.23, f.Get(), 0.01) assert.Equal(t, "1.230000", f.String()) d, err := f.MarshalJSON() - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, "1.23", string(d)) } func TestFloat64_UnmarshalJSON(t *testing.T) { var b Float64 err := b.UnmarshalJSON([]byte("wrong")) - assert.Error(t, err) - assert.Equal(t, float64(0), b.Get()) + require.Error(t, err) + assert.InDelta(t, float64(0), b.Get(), 0.01) err = b.UnmarshalJSON([]byte("123.321")) - assert.NoError(t, err) - assert.Equal(t, float64(123.321), b.Get()) + require.NoError(t, err) + assert.InDelta(t, float64(123.321), b.Get(), 0.01) } func TestFloat64_SetString(t *testing.T) { var f Float64 - assert.Error(t, f.SetString("wrong")) - assert.NoError(t, f.SetString("1.230000")) - assert.Equal(t, 1.23, f.Get()) + require.Error(t, f.SetString("wrong")) + require.NoError(t, f.SetString("1.230000")) + assert.InDelta(t, 1.23, f.Get(), 0.01) } func TestString(t *testing.T) { @@ -123,24 +124,24 @@ func TestString(t *testing.T) { assert.Equal(t, "Hello", s.String()) d, err := s.MarshalJSON() - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, `"Hello"`, string(d)) } func TestString_SetString(t *testing.T) { var s String - assert.NoError(t, s.SetString("foo")) + require.NoError(t, s.SetString("foo")) assert.Equal(t, "foo", s.Get()) } func TestString_UnmarshalJSON(t *testing.T) { var b String err := b.UnmarshalJSON([]byte(`foo`)) - assert.Error(t, err) + require.Error(t, err) assert.Equal(t, "", b.Get()) err = b.UnmarshalJSON([]byte(`"foo"`)) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, "foo", b.Get()) } @@ -156,20 +157,20 @@ func TestSecret(t *testing.T) { assert.Equal(t, "***", s.String()) d, err := s.MarshalJSON() - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, `"***"`, string(d)) } func TestSecret_SetString(t *testing.T) { var s Secret - assert.NoError(t, s.SetString("foo")) + require.NoError(t, s.SetString("foo")) assert.Equal(t, "foo", s.Get()) } func TestSecret_UnmarshalJSON(t *testing.T) { var b String err := b.UnmarshalJSON([]byte(`"foo"`)) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, "foo", b.Get()) } @@ -186,25 +187,25 @@ func TestTimeDuration(t *testing.T) { assert.Equal(t, testTime.String(), f.String()) d, err := f.MarshalJSON() - assert.NoError(t, err) - assert.Equal(t, fmt.Sprintf("%d", testTime.Nanoseconds()), string(d)) + require.NoError(t, err) + assert.Equal(t, strconv.FormatInt(testTime.Nanoseconds(), 10), string(d)) } func TestTimeDuration_SetString(t *testing.T) { var f TimeDuration - assert.Error(t, f.SetString("kuku")) - assert.NoError(t, f.SetString("3s")) + require.Error(t, f.SetString("kuku")) + require.NoError(t, f.SetString("3s")) assert.Equal(t, 3*time.Second, f.Get()) } func TestTimeDuration_UnmarshalJSON(t *testing.T) { var b TimeDuration err := b.UnmarshalJSON([]byte(`foo`)) - assert.Error(t, err) + require.Error(t, err) assert.Equal(t, time.Duration(0), b.Get()) err = b.UnmarshalJSON([]byte(`1`)) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, time.Duration(1), b.Get()) } @@ -222,23 +223,23 @@ func TestRegexp(t *testing.T) { assert.Equal(t, regex.String(), r.String()) d, err := r.MarshalJSON() - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, `".*"`, string(d)) } func TestRegexp_UnmarshalJSON(t *testing.T) { var r Regexp err := r.UnmarshalJSON([]byte(`invalid json`)) - assert.Error(t, err) + require.Error(t, err) assert.Nil(t, r.Get()) // Invalid regex: err = r.UnmarshalJSON([]byte(`"[a-z]++"`)) - assert.Error(t, err) + require.Error(t, err) assert.Nil(t, r.Get()) err = r.UnmarshalJSON([]byte(`"[a-z0-7]+"`)) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, regexp.MustCompile("[a-z0-7]+"), r.Get()) } @@ -259,7 +260,7 @@ func TestRegexp_SetString(t *testing.T) { err := sr.SetString(test.input) if test.throwsError { - assert.Error(t, err) + require.Error(t, err) } assert.Equal(t, test.result, sr.Get()) @@ -276,7 +277,7 @@ func TestRegexp_MarshalJSON(t *testing.T) { sr := Regexp{} json, err := sr.MarshalJSON() assert.Equal(t, []byte(`""`), json) - assert.NoError(t, err) + require.NoError(t, err) } func TestStringMap(t *testing.T) { @@ -291,7 +292,7 @@ func TestStringMap(t *testing.T) { assert.Equal(t, "key=\"value\"", sm.String()) d, err := sm.MarshalJSON() - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, `{"key":"value"}`, string(d)) } @@ -317,7 +318,7 @@ func TestStringMap_SetString(t *testing.T) { err := sm.SetString(test.input) if test.throwsError { - assert.Error(t, err) + require.Error(t, err) } assert.Equal(t, test.result, sm.Get()) @@ -328,21 +329,21 @@ func TestStringMap_SetString(t *testing.T) { func TestStringMap_SetString_DoesntOverrideValueIfError(t *testing.T) { sm := StringMap{} - assert.NoError(t, sm.SetString("k1:v1")) + require.NoError(t, sm.SetString("k1:v1")) assert.Equal(t, map[string]string{"k1": "v1"}, sm.Get()) - assert.Error(t, sm.SetString("k1:v1,k2:v2,k3")) + require.Error(t, sm.SetString("k1:v1,k2:v2,k3")) assert.Equal(t, map[string]string{"k1": "v1"}, sm.Get()) } func TestStringMap_UnmarshalJSON(t *testing.T) { var b StringMap err := b.UnmarshalJSON([]byte(`wrong`)) - assert.Error(t, err) + require.Error(t, err) assert.Equal(t, map[string]string(nil), b.Get()) err = b.UnmarshalJSON([]byte(`{ "a": "b" }`)) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, map[string]string{"a": "b"}, b.Get()) } @@ -358,7 +359,7 @@ func TestStringSlice(t *testing.T) { assert.Equal(t, "value1,value2", sl.String()) d, err := sl.MarshalJSON() - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, `["value1","value2"]`, string(d)) } @@ -381,7 +382,7 @@ func TestStringSlice_SetString(t *testing.T) { err := sm.SetString(test.input) if test.throwsError { - assert.Error(t, err) + require.Error(t, err) } assert.Equal(t, test.result, sm.Get()) @@ -392,10 +393,10 @@ func TestStringSlice_SetString(t *testing.T) { func TestStringSlice_UnmarshalJSON(t *testing.T) { var b StringSlice err := b.UnmarshalJSON([]byte(`wrong`)) - assert.Error(t, err) + require.Error(t, err) assert.Equal(t, []string(nil), b.Get()) err = b.UnmarshalJSON([]byte(`["a", "b"]`)) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, []string{"a", "b"}, b.Get()) }