Skip to content

Commit

Permalink
ci(lint): enable errorlint linter (#1604)
Browse files Browse the repository at this point in the history
* ci(lint): enable errorlint linter

Signed-off-by: Matthieu MOREL <[email protected]>

* Apply suggestions from code review

Co-authored-by: Manuel de la Peña <[email protected]>

---------

Signed-off-by: Matthieu MOREL <[email protected]>
Co-authored-by: Manuel de la Peña <[email protected]>
  • Loading branch information
mmorel-35 and mdelapenya authored Sep 11, 2023
1 parent 30fd5cd commit 3ee91d5
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 18 deletions.
11 changes: 11 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
linters:
enable:
- errorlint
- gci
- gocritic
- gofumpt
- misspell

linters-settings:
errorlint:
# Check whether fmt.Errorf uses the %w verb for formatting errors.
# See the https://github.com/polyfloyd/go-errorlint for caveats.
errorf: true
# Permit more than 1 %w verb, valid per Go 1.20 (Requires errorf:true)
errorf-multi: true
# Check for plain type assertions and type switches.
asserts: true
# Check for plain error comparisons.
comparison: true
gci:
sections:
- standard
Expand Down
6 changes: 4 additions & 2 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,8 @@ func (p *DockerProvider) BuildImage(ctx context.Context, img ImageBuildInfo) (st
err = backoff.Retry(func() error {
resp, err = p.client.ImageBuild(ctx, buildContext, buildOptions)
if err != nil {
if _, ok := err.(errdefs.ErrNotFound); ok {
var enf errdefs.ErrNotFound
if errors.As(err, &enf) {
return backoff.Permanent(err)
}
Logger.Printf("Failed to build image: %s, will retry", err)
Expand Down Expand Up @@ -1161,7 +1162,8 @@ func (p *DockerProvider) attemptToPullImage(ctx context.Context, tag string, pul
err = backoff.Retry(func() error {
pull, err = p.client.ImagePull(ctx, tag, pullOpt)
if err != nil {
if _, ok := err.(errdefs.ErrNotFound); ok {
var enf errdefs.ErrNotFound
if errors.As(err, &enf) {
return backoff.Permanent(err)
}
Logger.Printf("Failed to pull image: %s, will retry", err)
Expand Down
2 changes: 1 addition & 1 deletion internal/testcontainersdocker/docker_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func extractDockerHost(ctx context.Context) string {
for _, dockerHostFn := range dockerHostFns {
dockerHost, err := dockerHostFn(ctx)
if err != nil {
outerErr = fmt.Errorf("%w: %v", outerErr, err)
outerErr = fmt.Errorf("%w: %w", outerErr, err)
continue
}

Expand Down
2 changes: 1 addition & 1 deletion internal/testcontainersdocker/docker_rootless.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func rootlessDockerSocketPath(_ context.Context) (string, error) {
for _, socketPathFn := range socketPathFns {
s, err := socketPathFn()
if err != nil {
outerErr = fmt.Errorf("%w: %v", outerErr, err)
outerErr = fmt.Errorf("%w: %w", outerErr, err)
continue
}

Expand Down
10 changes: 6 additions & 4 deletions parallel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package testcontainers

import (
"context"
"errors"
"fmt"
"testing"
"time"
Expand Down Expand Up @@ -100,10 +101,10 @@ func TestParallelContainers(t *testing.T) {
res, err := ParallelContainers(context.Background(), tc.reqs, ParallelContainersOptions{})
if err != nil {
require.NotZero(t, tc.expErrors)
e, _ := err.(ParallelContainersError)

var e ParallelContainersError
errors.As(err, &e)
if len(e.Errors) != tc.expErrors {
t.Fatalf("expected erorrs: %d, got: %d\n", tc.expErrors, len(e.Errors))
t.Fatalf("expected errors: %d, got: %d\n", tc.expErrors, len(e.Errors))
}
}

Expand Down Expand Up @@ -157,7 +158,8 @@ func TestParallelContainersWithReuse(t *testing.T) {

res, err := ParallelContainers(ctx, parallelRequest, ParallelContainersOptions{})
if err != nil {
e, _ := err.(ParallelContainersError)
var e ParallelContainersError
errors.As(err, &e)
t.Fatalf("expected errors: %d, got: %d\n", 0, len(e.Errors))
}
// Container is reused, only terminate first container
Expand Down
7 changes: 5 additions & 2 deletions wait/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@

package wait

import "syscall"
import (
"errors"
"syscall"
)

func isConnRefusedErr(err error) bool {
return err == syscall.ECONNREFUSED
return errors.Is(err, syscall.ECONNREFUSED)
}
2 changes: 1 addition & 1 deletion wait/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func TestExecStrategyWaitUntilReady_DeadlineExceeded(t *testing.T) {
}
wg := wait.NewExecStrategy([]string{"true"})
err := wg.WaitUntilReady(ctx, target)
if err != context.DeadlineExceeded {
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatal(err)
}
}
Expand Down
8 changes: 5 additions & 3 deletions wait/host_port.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (hp *HostPortStrategy) WaitUntilReady(ctx context.Context, target StrategyT

select {
case <-ctx.Done():
return fmt.Errorf("%s:%w", ctx.Err(), err)
return fmt.Errorf("%w: %w", ctx.Err(), err)
case <-time.After(waitInterval):
if err := checkTarget(ctx, target); err != nil {
return err
Expand Down Expand Up @@ -155,8 +155,10 @@ func externalCheck(ctx context.Context, ipAddress string, port nat.Port, target
}
conn, err := dialer.DialContext(ctx, proto, address)
if err != nil {
if v, ok := err.(*net.OpError); ok {
if v2, ok := (v.Err).(*os.SyscallError); ok {
var v *net.OpError
if errors.As(err, &v) {
var v2 *os.SyscallError
if errors.As(v.Err, &v2) {
if isConnRefusedErr(v2.Err) {
time.Sleep(waitInterval)
continue
Expand Down
4 changes: 2 additions & 2 deletions wait/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (ws *HTTPStrategy) WaitUntilReady(ctx context.Context, target StrategyTarge
for err != nil {
select {
case <-ctx.Done():
return fmt.Errorf("%s:%w", ctx.Err(), err)
return fmt.Errorf("%w: %w", ctx.Err(), err)
case <-time.After(ws.PollInterval):
if err := checkTarget(ctx, target); err != nil {
return err
Expand All @@ -177,7 +177,7 @@ func (ws *HTTPStrategy) WaitUntilReady(ctx context.Context, target StrategyTarge
for mappedPort == "" {
select {
case <-ctx.Done():
return fmt.Errorf("%s:%w", ctx.Err(), err)
return fmt.Errorf("%w: %w", ctx.Err(), err)
case <-time.After(ws.PollInterval):
if err := checkTarget(ctx, target); err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions wait/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (w *waitForSql) WaitUntilReady(ctx context.Context, target StrategyTarget)
for port == "" {
select {
case <-ctx.Done():
return fmt.Errorf("%s:%w", ctx.Err(), err)
return fmt.Errorf("%w: %w", ctx.Err(), err)
case <-ticker.C:
if err := checkTarget(ctx, target); err != nil {
return err
Expand All @@ -98,7 +98,7 @@ func (w *waitForSql) WaitUntilReady(ctx context.Context, target StrategyTarget)

db, err := sql.Open(w.Driver, w.URL(host, port))
if err != nil {
return fmt.Errorf("sql.Open: %v", err)
return fmt.Errorf("sql.Open: %w", err)
}
defer db.Close()
for {
Expand Down

0 comments on commit 3ee91d5

Please sign in to comment.