Skip to content

Commit

Permalink
lint: enable testifylint (#2120)
Browse files Browse the repository at this point in the history
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
  • Loading branch information
mmorel-35 authored Jan 17, 2024
1 parent 46ce47f commit ecf0a8e
Show file tree
Hide file tree
Showing 26 changed files with 316 additions and 296 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci-test-go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ jobs:
uses: golangci/golangci-lint-action@3a919529898de77ec3da873e3063ca4b10e7f5cc # v3
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: v1.54.1
version: v1.55.2
# Optional: working directory, useful for monorepos
working-directory: ${{ inputs.project-directory }}
# Optional: golangci-lint command line arguments.
Expand Down
16 changes: 16 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ linters:
- gofumpt
- misspell
- nonamedreturns
- testifylint

linters-settings:
errorlint:
Expand All @@ -23,5 +24,20 @@ linters-settings:
- standard
- default
- prefix(github.com/testcontainers)
testifylint:
disable:
- compares
- float-compare
- go-require
enable:
- bool-compare
- empty
- error-is-as
- error-nil
- expected-actual
- len
- require-error
- suite-dont-use-pkg
- suite-extra-assert-call
run:
timeout: 5m
26 changes: 13 additions & 13 deletions docker_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ func TestGetDockerConfig(t *testing.T) {
// Verify that the default docker config file exists before any test in this suite runs.
// Then, we can safely run the tests that rely on it.
defaultCfg, err := dockercfg.LoadDefaultConfig()
require.Nil(t, err)
require.NoError(t, err)
require.NotEmpty(t, defaultCfg)

t.Run("without DOCKER_CONFIG env var retrieves default", func(t *testing.T) {
t.Setenv("DOCKER_CONFIG", "")

cfg, err := getDockerConfig()
require.Nil(t, err)
require.NoError(t, err)
require.NotEmpty(t, cfg)

assert.Equal(t, defaultCfg, cfg)
Expand All @@ -46,18 +46,18 @@ func TestGetDockerConfig(t *testing.T) {
t.Setenv("DOCKER_CONFIG", filepath.Join(testDockerConfigDirPath, "non-existing"))

cfg, err := getDockerConfig()
require.NotNil(t, err)
require.Error(t, err)
require.Empty(t, cfg)
})

t.Run("with DOCKER_CONFIG env var", func(t *testing.T) {
t.Setenv("DOCKER_CONFIG", testDockerConfigDirPath)

cfg, err := getDockerConfig()
require.Nil(t, err)
require.NoError(t, err)
require.NotEmpty(t, cfg)

assert.Equal(t, 3, len(cfg.AuthConfigs))
assert.Len(t, cfg.AuthConfigs, 3)

authCfgs := cfg.AuthConfigs

Expand All @@ -82,10 +82,10 @@ func TestGetDockerConfig(t *testing.T) {
t.Setenv("DOCKER_CONFIG", testDockerConfigDirPath)

cfg, err := getDockerConfig()
require.Nil(t, err)
require.NoError(t, err)
require.NotEmpty(t, cfg)

assert.Equal(t, 1, len(cfg.AuthConfigs))
assert.Len(t, cfg.AuthConfigs, 1)

authCfgs := cfg.AuthConfigs

Expand All @@ -108,7 +108,7 @@ func TestGetDockerConfig(t *testing.T) {
}`)

registry, cfg, err := DockerImageAuth(context.Background(), exampleAuth+"/my/image:latest")
require.Nil(t, err)
require.NoError(t, err)
require.NotEmpty(t, cfg)

assert.Equal(t, exampleAuth, registry)
Expand All @@ -130,7 +130,7 @@ func TestGetDockerConfig(t *testing.T) {
}`)

registry, cfg, err := DockerImageAuth(context.Background(), imageReg+imagePath)
require.Nil(t, err)
require.NoError(t, err)
require.NotEmpty(t, cfg)

assert.Equal(t, imageReg, registry)
Expand Down Expand Up @@ -277,14 +277,14 @@ func TestCreateContainerFromPrivateRegistry(t *testing.T) {
ContainerRequest: req,
Started: true,
})
require.Nil(t, err)
require.NoError(t, err)
terminateContainerOnEnd(t, ctx, redisContainer)
}

func prepareLocalRegistryWithAuth(t *testing.T) {
ctx := context.Background()
wd, err := os.Getwd()
assert.NoError(t, err)
require.NoError(t, err)
// copyDirectoryToContainer {
req := ContainerRequest{
Image: "registry:2",
Expand Down Expand Up @@ -316,13 +316,13 @@ func prepareLocalRegistryWithAuth(t *testing.T) {
}

registryC, err := GenericContainer(ctx, genContainerReq)
assert.NoError(t, err)
require.NoError(t, err)

t.Cleanup(func() {
removeImageFromLocalCache(t, "localhost:5001/redis:5.0-alpine")
})
t.Cleanup(func() {
assert.NoError(t, registryC.Terminate(context.Background()))
require.NoError(t, registryC.Terminate(context.Background()))
})

_, cancel := context.WithCancel(context.Background())
Expand Down
20 changes: 10 additions & 10 deletions docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ func TestContainerStateAfterTermination(t *testing.T) {
}

state, err := nginx.State(ctx)
assert.Error(t, err, "expected error from container inspect.")
require.Error(t, err, "expected error from container inspect.")

assert.Nil(t, state, "expected nil container inspect.")
})
Expand All @@ -312,7 +312,7 @@ func TestContainerStateAfterTermination(t *testing.T) {
}

state, err := nginx.State(ctx)
assert.NoError(t, err, "unexpected error from container inspect before container termination.")
require.NoError(t, err, "unexpected error from container inspect before container termination.")

assert.NotNil(t, state, "unexpected nil container inspect before container termination.")

Expand All @@ -323,7 +323,7 @@ func TestContainerStateAfterTermination(t *testing.T) {
}

state, err = nginx.State(ctx)
assert.Error(t, err, "expected error from container inspect after container termination.")
require.Error(t, err, "expected error from container inspect after container termination.")

assert.NotNil(t, state, "unexpected nil container inspect after container termination.")
})
Expand Down Expand Up @@ -1261,7 +1261,7 @@ func TestContainerCustomPlatformImage(t *testing.T) {

terminateContainerOnEnd(t, ctx, c)

assert.Error(t, err)
require.Error(t, err)
})

t.Run("specific platform should be propagated", func(t *testing.T) {
Expand All @@ -1285,10 +1285,10 @@ func TestContainerCustomPlatformImage(t *testing.T) {
defer dockerCli.Close()

ctr, err := dockerCli.ContainerInspect(ctx, c.GetContainerID())
assert.NoError(t, err)
require.NoError(t, err)

img, _, err := dockerCli.ImageInspectWithRaw(ctx, ctr.Image)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, "linux", img.Os)
assert.Equal(t, "amd64", img.Architecture)
})
Expand Down Expand Up @@ -1478,7 +1478,7 @@ func TestDockerCreateContainerWithDirs(t *testing.T) {
hostDirName := "testdata"

abs, err := filepath.Abs(filepath.Join(".", hostDirName))
assert.Nil(t, err)
require.NoError(t, err)

tests := []struct {
name string
Expand Down Expand Up @@ -1998,7 +1998,7 @@ func TestDockerProviderFindContainerByName(t *testing.T) {
terminateContainerOnEnd(t, ctx, c2)

c, err := provider.findContainerByName(ctx, "test")
assert.NoError(t, err)
require.NoError(t, err)
require.NotNil(t, c)
assert.Contains(t, c.Names, c1Name)
}
Expand Down Expand Up @@ -2049,9 +2049,9 @@ func TestImageBuiltFromDockerfile_KeepBuiltImage(t *testing.T) {
require.NoError(t, err, "terminate container should not fail")
_, _, err = cli.ImageInspectWithRaw(ctx, containerImage)
if tt.keepBuiltImage {
assert.Nil(t, err, "image should still exist")
require.NoError(t, err, "image should still exist")
} else {
assert.NotNil(t, err, "image should not exist anymore")
require.Error(t, err, "image should not exist anymore")
}
})
}
Expand Down
6 changes: 3 additions & 3 deletions file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ func Test_IsDir(t *testing.T) {
t.Run(test.filepath, func(t *testing.T) {
result, err := isDir(test.filepath)
if test.err != nil {
assert.NotNil(t, err, "expected error")
require.Error(t, err, "expected error")
} else {
assert.Nil(t, err, "not expected error")
require.NoError(t, err, "not expected error")
}
assert.Equal(t, test.expected, result)
})
Expand All @@ -71,7 +71,7 @@ func Test_TarDir(t *testing.T) {
src := originalSrc
if test.abs {
absSrc, err := filepath.Abs(src)
require.Nil(t, err)
require.NoError(t, err)

src = absSrc
}
Expand Down
12 changes: 6 additions & 6 deletions from_dockerfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ func TestBuildImageFromDockerfile(t *testing.T) {
},
// }
})
assert.Nil(t, err)
require.NoError(t, err)
assert.Equal(t, "test-repo:test-tag", tag)

_, _, err = cli.ImageInspectWithRaw(ctx, tag)
assert.Nil(t, err)
require.NoError(t, err)

t.Cleanup(func() {
_, err := cli.ImageRemove(ctx, tag, types.ImageRemoveOptions{
Expand Down Expand Up @@ -69,11 +69,11 @@ func TestBuildImageFromDockerfile_NoRepo(t *testing.T) {
Repo: "test-repo",
},
})
assert.Nil(t, err)
require.NoError(t, err)
assert.True(t, strings.HasPrefix(tag, "test-repo:"))

_, _, err = cli.ImageInspectWithRaw(ctx, tag)
assert.Nil(t, err)
require.NoError(t, err)

t.Cleanup(func() {
_, err := cli.ImageRemove(ctx, tag, types.ImageRemoveOptions{
Expand Down Expand Up @@ -104,11 +104,11 @@ func TestBuildImageFromDockerfile_NoTag(t *testing.T) {
Tag: "test-tag",
},
})
assert.Nil(t, err)
require.NoError(t, err)
assert.True(t, strings.HasSuffix(tag, ":test-tag"))

_, _, err = cli.ImageInspectWithRaw(ctx, tag)
assert.Nil(t, err)
require.NoError(t, err)

t.Cleanup(func() {
_, err := cli.ImageRemove(ctx, tag, types.ImageRemoveOptions{
Expand Down
22 changes: 11 additions & 11 deletions internal/core/docker_host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func TestExtractDockerHost(t *testing.T) {
setupTestcontainersProperties(t, content)

socket, err := testcontainersHostFromProperties(context.Background())
require.Nil(t, err)
require.NoError(t, err)
assert.Equal(t, testRemoteHost, socket)
})

Expand All @@ -165,10 +165,10 @@ func TestExtractDockerHost(t *testing.T) {
tmpSocket := filepath.Join(tmpDir, "docker.sock")
t.Setenv("DOCKER_HOST", tmpSocket)
err := createTmpDockerSocket(tmpDir)
require.Nil(t, err)
require.NoError(t, err)

socket, err := dockerHostFromEnv(context.Background())
require.Nil(t, err)
require.NoError(t, err)
assert.Equal(t, tmpSocket, socket)
})

Expand All @@ -187,10 +187,10 @@ func TestExtractDockerHost(t *testing.T) {
tmpSocket := filepath.Join(tmpDir, "docker.sock")
t.Setenv("TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE", tmpSocket)
err := createTmpDockerSocket(tmpDir)
require.Nil(t, err)
require.NoError(t, err)

socket, err := dockerSocketOverridePath(context.Background())
require.Nil(t, err)
require.NoError(t, err)
assert.Equal(t, tmpSocket, socket)
})

Expand All @@ -208,7 +208,7 @@ func TestExtractDockerHost(t *testing.T) {
ctx := context.Background()

socket, err := dockerHostFromContext(context.WithValue(ctx, DockerHostContextKey, DockerSocketSchema+"/this/is/a/sample.sock"))
require.Nil(t, err)
require.NoError(t, err)
assert.Equal(t, "/this/is/a/sample.sock", socket)
})

Expand All @@ -232,7 +232,7 @@ func TestExtractDockerHost(t *testing.T) {
tmpSocket := setupDockerSocket(t)

socket, err := dockerSocketPath(context.Background())
require.Nil(t, err)
require.NoError(t, err)
assert.Equal(t, tmpSocket, socket)
})

Expand All @@ -243,7 +243,7 @@ func TestExtractDockerHost(t *testing.T) {
setupTestcontainersProperties(t, content)

socket, err := dockerHostFromProperties(context.Background())
require.Nil(t, err)
require.NoError(t, err)
assert.Equal(t, tmpSocket, socket)
})

Expand Down Expand Up @@ -426,7 +426,7 @@ func TestInAContainer(t *testing.T) {
f := filepath.Join(tmpDir, ".dockerenv-b")

testFile, err := os.Create(f)
assert.NoError(t, err)
require.NoError(t, err)
defer testFile.Close()

assert.True(t, inAContainer(f))
Expand Down Expand Up @@ -472,7 +472,7 @@ func setupDockerSocket(t *testing.T) string {
tmpDir := t.TempDir()
tmpSocket := filepath.Join(tmpDir, "docker.sock")
err := createTmpDockerSocket(filepath.Dir(tmpSocket))
require.Nil(t, err)
require.NoError(t, err)

DockerSocketPath = tmpSocket
DockerSocketPathWithSchema = tmpSchema + tmpSocket
Expand Down Expand Up @@ -503,7 +503,7 @@ func setupTestcontainersProperties(t *testing.T, content string) {
tmpDir := t.TempDir()
homeDir := filepath.Join(tmpDir, "home")
err := createTmpDir(homeDir)
require.Nil(t, err)
require.NoError(t, err)
t.Setenv("HOME", homeDir)
t.Setenv("USERPROFILE", homeDir) // Windows support

Expand Down
Loading

0 comments on commit ecf0a8e

Please sign in to comment.