Skip to content

Commit

Permalink
Refactor: Add t.Helper() on test helper function (#1935)
Browse files Browse the repository at this point in the history
* refactor pkg/agent/handler test

Signed-off-by: kevindiu <[email protected]>

* fix gotest gen command

Signed-off-by: kevindiu <[email protected]>

* fix part of beforeFunc helper function call

Signed-off-by: kevindiu <[email protected]>

* Format code with prettier and gofumpt

* add remaining beforeFunc

Signed-off-by: kevindiu <[email protected]>

* Revert "fix gotest gen command"

This reverts commit 9de6c54.

* Update internal/errgroup/group_test.go

* update afterFunc

Signed-off-by: kevindiu <[email protected]>

* fix

Signed-off-by: kevindiu <[email protected]>

* Update internal/net/net_test.go

* Update pkg/agent/core/ngt/handler/grpc/upsert_test.go

---------

Signed-off-by: kevindiu <[email protected]>
Co-authored-by: deepsource-autofix[bot] <62050782+deepsource-autofix[bot]@users.noreply.github.com>
  • Loading branch information
kevindiu and deepsource-autofix[bot] authored Feb 1, 2023
1 parent 589fe4e commit f529cbd
Show file tree
Hide file tree
Showing 38 changed files with 1,240 additions and 1,279 deletions.
21 changes: 12 additions & 9 deletions internal/cache/gache/gache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func Test_cache_Get(t *testing.T) {
fields fields
want want
checkFunc func(want, interface{}, bool) error
beforeFunc func(args, *cache)
beforeFunc func(*testing.T, args, *cache)
afterFunc func(args)
}
defaultCheckFunc := func(w want, got interface{}, got1 bool) error {
Expand Down Expand Up @@ -255,7 +255,8 @@ func Test_cache_Get(t *testing.T) {
want: "vald",
want1: true,
},
beforeFunc: func(args args, c *cache) {
beforeFunc: func(t *testing.T, args args, c *cache) {
t.Helper()
c.Set(args.key, "vald")
},
},
Expand All @@ -272,7 +273,7 @@ func Test_cache_Get(t *testing.T) {
expiredHook: test.fields.expiredHook,
}
if test.beforeFunc != nil {
test.beforeFunc(test.args, c)
test.beforeFunc(tt, test.args, c)
}
if test.afterFunc != nil {
defer test.afterFunc(test.args)
Expand Down Expand Up @@ -395,7 +396,7 @@ func Test_cache_Delete(t *testing.T) {
fields fields
want want
checkFunc func(want, *cache) error
beforeFunc func(args, *cache)
beforeFunc func(*testing.T, args, *cache)
afterFunc func(args)
}
defaultCheckFunc := func(w want, c *cache) error {
Expand Down Expand Up @@ -442,7 +443,8 @@ func Test_cache_Delete(t *testing.T) {
want: nil,
want1: false,
},
beforeFunc: func(args args, c *cache) {
beforeFunc: func(t *testing.T, args args, c *cache) {
t.Helper()
c.Set(args.key, "vald")
},
},
Expand All @@ -459,7 +461,7 @@ func Test_cache_Delete(t *testing.T) {
expiredHook: test.fields.expiredHook,
}
if test.beforeFunc != nil {
test.beforeFunc(test.args, c)
test.beforeFunc(tt, test.args, c)
}
if test.afterFunc != nil {
defer test.afterFunc(test.args)
Expand Down Expand Up @@ -497,7 +499,7 @@ func Test_cache_GetAndDelete(t *testing.T) {
fields fields
want want
checkFunc func(want, interface{}, bool) error
beforeFunc func(args, *cache)
beforeFunc func(*testing.T, args, *cache)
afterFunc func(args)
}
defaultCheckFunc := func(w want, got interface{}, got1 bool) error {
Expand Down Expand Up @@ -541,7 +543,8 @@ func Test_cache_GetAndDelete(t *testing.T) {
want: "vald",
want1: true,
},
beforeFunc: func(args args, c *cache) {
beforeFunc: func(t *testing.T, args args, c *cache) {
t.Helper()
c.Set(args.key, "vald")
},
},
Expand All @@ -558,7 +561,7 @@ func Test_cache_GetAndDelete(t *testing.T) {
expiredHook: test.fields.expiredHook,
}
if test.beforeFunc != nil {
test.beforeFunc(test.args, c)
test.beforeFunc(tt, test.args, c)
}
if test.afterFunc != nil {
defer test.afterFunc(test.args)
Expand Down
7 changes: 4 additions & 3 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestGlobalConfig_Bind(t *testing.T) {
fields fields
want want
checkFunc func(want, *GlobalConfig) error
beforeFunc func()
beforeFunc func(*testing.T)
afterFunc func()
}
defaultCheckFunc := func(w want, got *GlobalConfig) error {
Expand Down Expand Up @@ -231,7 +231,8 @@ func TestGlobalConfig_Bind(t *testing.T) {
},
},
},
beforeFunc: func() {
beforeFunc: func(t *testing.T) {
t.Helper()
for key, val := range env {
t.Setenv(key, val)
}
Expand All @@ -245,7 +246,7 @@ func TestGlobalConfig_Bind(t *testing.T) {
t.Run(test.name, func(tt *testing.T) {
defer goleak.VerifyNone(tt, goleakIgnoreOptions...)
if test.beforeFunc != nil {
test.beforeFunc()
test.beforeFunc(tt)
}
if test.afterFunc != nil {
defer test.afterFunc()
Expand Down
7 changes: 4 additions & 3 deletions internal/config/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestLogging_Bind(t *testing.T) {
fields fields
want want
checkFunc func(want, *Logging) error
beforeFunc func()
beforeFunc func(*testing.T)
afterFunc func()
}
defaultCheckFunc := func(w want, got *Logging) error {
Expand Down Expand Up @@ -70,7 +70,8 @@ func TestLogging_Bind(t *testing.T) {
Level: "_LOGGING_BIND_LEVEL_",
Format: "_LOGGING_BIND_FORMAT_",
},
beforeFunc: func() {
beforeFunc: func(t *testing.T) {
t.Helper()
t.Setenv("LOGGING_BIND_LOGGER", "glg")
t.Setenv("LOGGING_BIND_LEVEL", "info")
t.Setenv("LOGGING_BIND_FORMAT", "json")
Expand All @@ -95,7 +96,7 @@ func TestLogging_Bind(t *testing.T) {
test := tc
t.Run(test.name, func(tt *testing.T) {
if test.beforeFunc != nil {
test.beforeFunc()
test.beforeFunc(tt)
}
if test.afterFunc != nil {
defer test.afterFunc()
Expand Down
7 changes: 4 additions & 3 deletions internal/config/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestMySQL_Bind(t *testing.T) {
fields fields
want want
checkFunc func(want, *MySQL) error
beforeFunc func()
beforeFunc func(*testing.T)
afterFunc func()
}
defaultCheckFunc := func(w want, got *MySQL) error {
Expand Down Expand Up @@ -190,7 +190,8 @@ func TestMySQL_Bind(t *testing.T) {
Net: new(Net),
},
},
beforeFunc: func() {
beforeFunc: func(t *testing.T) {
t.Helper()
t.Setenv("MYSQL_BIND_DB", "db")
t.Setenv("MYSQL_BIND_HOST", "host")
t.Setenv("MYSQL_BIND_USER", "user")
Expand All @@ -209,7 +210,7 @@ func TestMySQL_Bind(t *testing.T) {
test := tc
t.Run(test.name, func(tt *testing.T) {
if test.beforeFunc != nil {
test.beforeFunc()
test.beforeFunc(tt)
}
if test.afterFunc != nil {
defer test.afterFunc()
Expand Down
7 changes: 4 additions & 3 deletions internal/config/ngt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestNGT_Bind(t *testing.T) {
fields fields
want want
checkFunc func(want, *NGT) error
beforeFunc func()
beforeFunc func(*testing.T)
afterFunc func()
}
defaultCheckFunc := func(w want, got *NGT) error {
Expand Down Expand Up @@ -118,7 +118,8 @@ func TestNGT_Bind(t *testing.T) {
VQueue: new(VQueue),
KVSDB: new(KVSDB),
},
beforeFunc: func() {
beforeFunc: func(t *testing.T) {
t.Helper()
t.Setenv("NGT_BIND_INDEX_PATH", "config/ngt")
t.Setenv("NGT_BIND_DISTANCE_TYPE", "l2")
t.Setenv("NGT_BIND_OBJECT_TYPE", "float")
Expand Down Expand Up @@ -162,7 +163,7 @@ func TestNGT_Bind(t *testing.T) {
test := tc
t.Run(test.name, func(tt *testing.T) {
if test.beforeFunc != nil {
test.beforeFunc()
test.beforeFunc(tt)
}
if test.afterFunc != nil {
defer test.afterFunc()
Expand Down
7 changes: 4 additions & 3 deletions internal/config/tls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestTLS_Bind(t *testing.T) {
fields fields
want want
checkFunc func(want, *TLS) error
beforeFunc func()
beforeFunc func(*testing.T)
afterFunc func()
}
defaultCheckFunc := func(w want, got *TLS) error {
Expand Down Expand Up @@ -75,7 +75,8 @@ func TestTLS_Bind(t *testing.T) {
Key: "_TLS_BIND_KEY_",
CA: "_TLS_BIND_CA_",
},
beforeFunc: func() {
beforeFunc: func(t *testing.T) {
t.Helper()
t.Setenv("TLS_BIND_CERT", "tls_cert")
t.Setenv("TLS_BIND_KEY", "tls_key")
t.Setenv("TLS_BIND_CA", "tls_ca")
Expand All @@ -101,7 +102,7 @@ func TestTLS_Bind(t *testing.T) {
test := tc
t.Run(test.name, func(tt *testing.T) {
if test.beforeFunc != nil {
test.beforeFunc()
test.beforeFunc(tt)
}
if test.afterFunc != nil {
defer test.afterFunc()
Expand Down
7 changes: 4 additions & 3 deletions internal/db/nosql/cassandra/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestWithHosts(t *testing.T) {
args args
want want
checkFunc func(want, *T, error) error
beforeFunc func(*T)
beforeFunc func(*testing.T, *T)
afterFunc func(args)
}
defaultCheckFunc := func(w want, obj *T, err error) error {
Expand Down Expand Up @@ -112,7 +112,8 @@ func TestWithHosts(t *testing.T) {
args: args{
hosts: []string{"hosts1"},
},
beforeFunc: func(obj *T) {
beforeFunc: func(t *testing.T, obj *T) {
t.Helper()
_ = WithHosts("vald.vdaas.org")(obj)
},
want: want{
Expand All @@ -138,7 +139,7 @@ func TestWithHosts(t *testing.T) {
got := WithHosts(test.args.hosts...)
obj := new(T)
if test.beforeFunc != nil {
test.beforeFunc(obj)
test.beforeFunc(tt, obj)
}
if err := checkFunc(test.want, obj, got(obj)); err != nil {
tt.Errorf("error = %v", err)
Expand Down
Loading

0 comments on commit f529cbd

Please sign in to comment.