Skip to content

Commit fe985b6

Browse files
authored
chore: add support for thelper (#2184)
1 parent f91db50 commit fe985b6

9 files changed

+18
-0
lines changed

.golangci.yml

+4
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,14 @@ linters:
4040
- nolintlint # Reports ill-formed or insufficient nolint directives [fast: true, auto-fix: false]
4141
- nosprintfhostport # Checks for misuse of Sprintf to construct a host with port in a URL. [fast: true, auto-fix: false]
4242
- perfsprint # Checks that fmt.Sprintf can be replaced with a faster alternative. [fast: false, auto-fix: false]
43+
- prealloc # Finds slice declarations that could potentially be pre-allocated [fast: true, auto-fix: false]
4344
- rowserrcheck # checks whether Err of rows is checked successfully [fast: false, auto-fix: false]
4445
- sloglint # ensure consistent code style when using log/slog [fast: false, auto-fix: false]
4546
- sqlclosecheck # Checks that sql.Rows and sql.Stmt are closed. [fast: false, auto-fix: false]
4647
- staticcheck #(megacheck): Staticcheck is a go vet on steroids, applying a ton of static analysis checks [fast: false, auto-fix: false]
48+
- tenv # tenv is analyzer that detects using os.Setenv instead of t.Setenv since Go1.17 [fast: false, auto-fix: false]
49+
- testifylint # Checks usage of github.com/stretchr/testify. [fast: false, auto-fix: false]
50+
- thelper # thelper detects golang test helpers without t.Helper() call and checks the consistency of test helpers [fast: false, auto-fix: false]
4751
- typecheck # Like the front-end of a Go compiler, parses and type-checks Go code [fast: false, auto-fix: false]
4852
- unconvert # Remove unnecessary type conversions [fast: false, auto-fix: false]
4953
- unused #(megacheck): Checks Go code for unused constants, variables, functions and types [fast: false, auto-fix: false]

api/instance/v1/image_utils_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func TestWaitForImage(t *testing.T) {
3333
// createImage cis a helper that create an image.
3434
// It return the newly created image and a cleanup function
3535
func createImage(t *testing.T, instanceAPI *API, imageName string) (*Image, func()) {
36+
t.Helper()
3637
serverRes, err := instanceAPI.CreateServer(&CreateServerRequest{
3738
CommercialType: "DEV1-M",
3839
Image: "ubuntu_focal",

api/instance/v1/security_group_utils_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ func TestAPI_UpdateSecurityGroupRule(t *testing.T) {
6969
instanceAPI := NewAPI(client)
7070

7171
bootstrap := func(t *testing.T) (*SecurityGroup, *SecurityGroupRule, func()) {
72+
t.Helper()
7273
createSecurityGroupResponse, err := instanceAPI.CreateSecurityGroup(&CreateSecurityGroupRequest{
7374
Name: "name",
7475
Description: "description",

api/instance/v1/snapshot_utils_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func TestWaitForSnapshot(t *testing.T) {
3333
// createSnapshot is a helper that create an snapshot.
3434
// It returns the newly created snapshot and a cleanup function
3535
func createSnapshot(t *testing.T, instanceAPI *API, snapshotName string) (*Snapshot, func()) {
36+
t.Helper()
3637
serverRes, err := instanceAPI.CreateServer(&CreateServerRequest{
3738
CommercialType: "DEV1-M",
3839
Image: "ubuntu_focal",

internal/testhelpers/test_helpers.go

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
// Assert fails the test if the condition is false.
1212
func Assert(tb testing.TB, condition bool, msg string, v ...interface{}) {
13+
tb.Helper()
1314
if !condition {
1415
_, file, line, _ := runtime.Caller(1)
1516
fmt.Printf("\033[31m%s:%d: "+msg+"\033[39m\n\n", append([]interface{}{filepath.Base(file), line}, v...)...)
@@ -19,6 +20,7 @@ func Assert(tb testing.TB, condition bool, msg string, v ...interface{}) {
1920

2021
// AssertNoError fails the test if an err is not nil.
2122
func AssertNoError(tb testing.TB, err error) {
23+
tb.Helper()
2224
if err != nil {
2325
_, file, line, _ := runtime.Caller(1)
2426
fmt.Printf("\033[31m%s:%d: unexpected error: %s\033[39m\n\n", filepath.Base(file), line, err.Error())
@@ -28,6 +30,7 @@ func AssertNoError(tb testing.TB, err error) {
2830

2931
// Equals fails the test if exp is not equal to act.
3032
func Equals(tb testing.TB, exp, act interface{}) {
33+
tb.Helper()
3134
if !reflect.DeepEqual(exp, act) {
3235
_, file, line, _ := runtime.Caller(1)
3336
fmt.Printf("\033[31m%s:%d:\n\n\texp: %#v\n\n\tgot: %#v\033[39m\n\n", filepath.Base(file), line, exp, act)

logger/logger_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ func TestEnableDebugMode(t *testing.T) {
6565
}
6666

6767
func testThings(t *testing.T, expectedEvents []string, actualOutput string) {
68+
t.Helper()
6869
lines := strings.Split(actualOutput, "\n")
6970
for i, line := range lines[:len(lines)-1] { // last line is always empty
7071
tmp := strings.Split(line, " ")

scw/config_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,7 @@ func TestMergeProfiles(t *testing.T) {
508508
}
509509

510510
func initEnv(t *testing.T) string {
511+
t.Helper()
511512
dir, err := os.MkdirTemp("", "home")
512513
if err != nil {
513514
t.Fatal(err)
@@ -516,12 +517,14 @@ func initEnv(t *testing.T) string {
516517
}
517518

518519
func cleanEnv(t *testing.T, files map[string]string, homeDir string) {
520+
t.Helper()
519521
for path := range files {
520522
testhelpers.AssertNoError(t, os.RemoveAll(filepath.Join(homeDir, path)))
521523
}
522524
}
523525

524526
func setEnv(t *testing.T, env, files map[string]string, homeDir string) {
527+
t.Helper()
525528
os.Clearenv()
526529
for key, value := range env {
527530
value = strings.ReplaceAll(value, "{HOME}", homeDir)
@@ -537,6 +540,7 @@ func setEnv(t *testing.T, env, files map[string]string, homeDir string) {
537540

538541
// function taken from https://golang.org/src/os/env_test.go
539542
func resetEnv(t *testing.T, origEnv []string, homeDir string) {
543+
t.Helper()
540544
testhelpers.AssertNoError(t, os.RemoveAll(homeDir))
541545
for _, pair := range origEnv {
542546
// Environment variables on Windows can begin with =
@@ -572,6 +576,7 @@ func TestConfig_ConfigFile(t *testing.T) {
572576

573577
run := func(c *testCase) func(t *testing.T) {
574578
return func(t *testing.T) {
579+
t.Helper()
575580
config, err := c.config.HumanConfig()
576581
testhelpers.AssertNoError(t, err)
577582
testhelpers.Equals(t, c.result, config)

scw/custom_types_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ func TestFile_UnmarshalJSON(t *testing.T) {
340340

341341
run := func(c *testCase) func(t *testing.T) {
342342
return func(t *testing.T) {
343+
t.Helper()
343344
f := File{}
344345
err := json.Unmarshal([]byte(c.json), &f)
345346
testhelpers.AssertNoError(t, err)

scw/errors_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ func TestNonStandardError(t *testing.T) {
3636

3737
run := func(c *testCase) func(t *testing.T) {
3838
return func(t *testing.T) {
39+
t.Helper()
3940
res := &http.Response{
4041
Status: c.resStatus,
4142
StatusCode: c.resStatusCode,

0 commit comments

Comments
 (0)