Skip to content

Commit

Permalink
golangci-lint: enable thelper linter (#2834)
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 Oct 18, 2024
1 parent 5e988ff commit b74984d
Show file tree
Hide file tree
Showing 26 changed files with 51 additions and 13 deletions.
17 changes: 4 additions & 13 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
linters:
enable:
- errcheck
- errorlint
- gci
- gocritic
- gofumpt
- misspell
- nolintlint
- nonamedreturns
- testifylint
- errcheck
- nolintlint
- thelper

linters-settings:
errorlint:
Expand All @@ -30,16 +31,6 @@ linters-settings:
disable:
- float-compare
- go-require
enable:
- bool-compare
- compares
- empty
- error-is-as
- error-nil
- expected-actual
- len
- require-error
- suite-dont-use-pkg
- suite-extra-assert-call
enable-all: true
run:
timeout: 5m
3 changes: 3 additions & 0 deletions docker_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ func TestBuildContainerFromDockerfile(t *testing.T) {

// removeImageFromLocalCache removes the image from the local cache
func removeImageFromLocalCache(t *testing.T, img string) {
t.Helper()
ctx := context.Background()

testcontainersClient, err := NewDockerClientWithOpts(ctx, client.WithVersion(daemonMaxVersion))
Expand Down Expand Up @@ -263,6 +264,7 @@ func TestCreateContainerFromPrivateRegistry(t *testing.T) {
}

func prepareLocalRegistryWithAuth(t *testing.T) string {
t.Helper()
ctx := context.Background()
wd, err := os.Getwd()
require.NoError(t, err)
Expand Down Expand Up @@ -359,6 +361,7 @@ func setAuthConfig(t *testing.T, host, username, password string) string {
// which can be used to connect to the local registry.
// This avoids the issues with localhost on WSL.
func localAddress(t *testing.T) string {
t.Helper()
if os.Getenv("WSL_DISTRO_NAME") == "" {
return "localhost"
}
Expand Down
2 changes: 2 additions & 0 deletions docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1345,6 +1345,7 @@ func TestContainerInspect_RawInspectIsCleanedOnStop(t *testing.T) {
}

func readHostname(tb testing.TB, containerId string) string {
tb.Helper()
containerClient, err := NewDockerClientWithOpts(context.Background())
if err != nil {
tb.Fatalf("Failed to create Docker client: %v", err)
Expand Down Expand Up @@ -1917,6 +1918,7 @@ func TestNetworkModeWithContainerReference(t *testing.T) {

// creates a temporary dir in which the files will be extracted. Then it will compare the bytes of each file in the source with the bytes from the copied-from-container file
func assertExtractedFiles(t *testing.T, ctx context.Context, container Container, hostFilePath string, containerFilePath string) {
t.Helper()
// create all copied files into a temporary dir
tmpDir := t.TempDir()

Expand Down
1 change: 1 addition & 0 deletions generic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ func TestGenericReusableContainerInSubprocess(t *testing.T) {
}

func createReuseContainerInSubprocess(t *testing.T) string {
t.Helper()
// force verbosity in subprocesses, so that the output is printed
cmd := exec.Command(os.Args[0], "-test.run=TestHelperContainerStarterProcess", "-test.v=true")
cmd.Env = append(os.Environ(), "GO_WANT_HELPER_PROCESS=1")
Expand Down
1 change: 1 addition & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const (
// unset environment variables to avoid side effects
// execute this function before each test
func resetTestEnv(t *testing.T) {
t.Helper()
t.Setenv("TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX", "")
t.Setenv("TESTCONTAINERS_RYUK_DISABLED", "")
t.Setenv("TESTCONTAINERS_RYUK_CONTAINER_PRIVILEGED", "")
Expand Down
5 changes: 5 additions & 0 deletions internal/core/docker_host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func testCallbackCheckError(_ context.Context, _ string) error {
}

func mockCallbackCheck(t *testing.T, fn func(_ context.Context, _ string) error) {
t.Helper()
oldCheck := dockerHostCheck
dockerHostCheck = fn
t.Cleanup(func() {
Expand Down Expand Up @@ -515,10 +516,12 @@ func createTmpDockerSocket(parent string) error {
// setupDockerHostNotFound sets up the environment for the test case where the DOCKER_HOST environment variable is
// already set (e.g. rootless docker) therefore we need to unset it before the test
func setupDockerHostNotFound(t *testing.T) {
t.Helper()
t.Setenv("DOCKER_HOST", "")
}

func setupDockerSocket(t *testing.T) string {
t.Helper()
t.Cleanup(func() {
DockerSocketPath = originalDockerSocketPath
DockerSocketPathWithSchema = originalDockerSocketPathWithSchema
Expand All @@ -536,6 +539,7 @@ func setupDockerSocket(t *testing.T) string {
}

func setupDockerSocketNotFound(t *testing.T) {
t.Helper()
t.Cleanup(func() {
DockerSocketPath = originalDockerSocketPath
DockerSocketPathWithSchema = originalDockerSocketPathWithSchema
Expand All @@ -548,6 +552,7 @@ func setupDockerSocketNotFound(t *testing.T) {
}

func setupTestcontainersProperties(t *testing.T, content string) {
t.Helper()
t.Cleanup(func() {
// reset the properties file after the test
config.Reset()
Expand Down
1 change: 1 addition & 0 deletions internal/core/docker_rootless_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ func TestRootlessDockerSocketPath(t *testing.T) {
}

func setupRootlessNotFound(t *testing.T) {
t.Helper()
t.Cleanup(func() {
baseRunDir = originalBaseRunDir
os.Setenv("XDG_RUNTIME_DIR", originalXDGRuntimeDir)
Expand Down
1 change: 1 addition & 0 deletions modulegen/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
)

func getTestRootContext(t *testing.T) context.Context {
t.Helper()
current, err := os.Getwd()
require.NoError(t, err)
return context.New(filepath.Dir(current))
Expand Down
8 changes: 8 additions & 0 deletions modulegen/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ func TestGenerateModule(t *testing.T) {

// assert content module file in the docs
func assertModuleDocContent(t *testing.T, module context.TestcontainersModule, moduleDocFile string) {
t.Helper()
content, err := os.ReadFile(moduleDocFile)
require.NoError(t, err)

Expand All @@ -383,6 +384,7 @@ func assertModuleDocContent(t *testing.T, module context.TestcontainersModule, m

// assert content module test
func assertExamplesTestContent(t *testing.T, module context.TestcontainersModule, examplesTestFile string) {
t.Helper()
content, err := os.ReadFile(examplesTestFile)
require.NoError(t, err)

Expand All @@ -402,6 +404,7 @@ func assertExamplesTestContent(t *testing.T, module context.TestcontainersModule

// assert content module test
func assertModuleTestContent(t *testing.T, module context.TestcontainersModule, exampleTestFile string) {
t.Helper()
content, err := os.ReadFile(exampleTestFile)
require.NoError(t, err)

Expand All @@ -413,6 +416,7 @@ func assertModuleTestContent(t *testing.T, module context.TestcontainersModule,

// assert content module
func assertModuleContent(t *testing.T, module context.TestcontainersModule, exampleFile string) {
t.Helper()
content, err := os.ReadFile(exampleFile)
require.NoError(t, err)

Expand All @@ -437,6 +441,7 @@ func assertModuleContent(t *testing.T, module context.TestcontainersModule, exam

// assert content GitHub workflow for the module
func assertModuleGithubWorkflowContent(t *testing.T, moduleWorkflowFile string) {
t.Helper()
content, err := os.ReadFile(moduleWorkflowFile)
require.NoError(t, err)

Expand All @@ -454,6 +459,7 @@ func assertModuleGithubWorkflowContent(t *testing.T, moduleWorkflowFile string)

// assert content go.mod
func assertGoModContent(t *testing.T, module context.TestcontainersModule, tcVersion string, goModFile string) {
t.Helper()
content, err := os.ReadFile(goModFile)
require.NoError(t, err)

Expand All @@ -465,6 +471,7 @@ func assertGoModContent(t *testing.T, module context.TestcontainersModule, tcVer

// assert content Makefile
func assertMakefileContent(t *testing.T, module context.TestcontainersModule, makefile string) {
t.Helper()
content, err := os.ReadFile(makefile)
require.NoError(t, err)

Expand All @@ -474,6 +481,7 @@ func assertMakefileContent(t *testing.T, module context.TestcontainersModule, ma

// assert content in the nav items from mkdocs.yml
func assertMkdocsNavItems(t *testing.T, module context.TestcontainersModule, originalConfig *mkdocs.Config, tmpCtx context.Context) {
t.Helper()
config, err := mkdocs.ReadConfig(tmpCtx.MkdocsConfigFile())
require.NoError(t, err)

Expand Down
1 change: 1 addition & 0 deletions modulegen/mkdocs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func TestNavItems(t *testing.T) {
}

func copyInitialMkdocsConfig(t *testing.T, tmpCtx context.Context) error {
t.Helper()
ctx := getTestRootContext(t)
return mkdocs.CopyConfig(ctx.MkdocsConfigFile(), tmpCtx.MkdocsConfigFile())
}
1 change: 1 addition & 0 deletions modules/artemis/artemis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func TestArtemis(t *testing.T) {
user: "artemis",
pass: "artemis",
hook: func(t *testing.T, container *artemis.Container) {
t.Helper()
expectQueue(t, container, "ArgsTestQueue")
},
},
Expand Down
2 changes: 2 additions & 0 deletions modules/clickhouse/clickhouse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ func TestClickHouseWithZookeeper(t *testing.T) {
}

func performReplicatedCRUD(t *testing.T, conn driver.Conn) ([]Test, error) {
t.Helper()
return backoff.RetryNotifyWithData(
func() ([]Test, error) {
err := conn.Exec(context.Background(), "CREATE TABLE replicated_test_table (id UInt64) ENGINE = ReplicatedMergeTree('/clickhouse/tables/{shard}/mdb.data_transfer_cp_cdc', '{replica}') PRIMARY KEY (id) ORDER BY (id) SETTINGS index_granularity = 8192;")
Expand Down Expand Up @@ -292,6 +293,7 @@ func performReplicatedCRUD(t *testing.T, conn driver.Conn) ([]Test, error) {
}

func performCRUD(t *testing.T, conn driver.Conn) ([]Test, error) {
t.Helper()
return backoff.RetryNotifyWithData(
func() ([]Test, error) {
err := conn.Exec(context.Background(), "create table if not exists test_table (id UInt64) engine = MergeTree PRIMARY KEY (id) ORDER BY (id) SETTINGS index_granularity = 8192;")
Expand Down
1 change: 1 addition & 0 deletions modules/compose/compose_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ func getFreePort(t *testing.T) int {
}

func writeTemplate(t *testing.T, templateFile string, port ...int) string {
t.Helper()
return writeTemplateWithSrvType(t, templateFile, "api", port...)
}

Expand Down
2 changes: 2 additions & 0 deletions modules/compose/compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ func TestLocalDockerComposeWithVolume(t *testing.T) {
}

func assertVolumeDoesNotExist(tb testing.TB, volumeName string) {
tb.Helper()
containerClient, err := testcontainers.NewDockerClientWithOpts(context.Background())
if err != nil {
tb.Fatalf("Failed to get provider: %v", err)
Expand All @@ -471,6 +472,7 @@ func assertContainerEnvironmentVariables(
present map[string]string,
absent map[string]string,
) {
tb.Helper()
containerClient, err := testcontainers.NewDockerClientWithOpts(context.Background())
if err != nil {
tb.Fatalf("Failed to get provider: %v", err)
Expand Down
1 change: 1 addition & 0 deletions modules/couchbase/couchbase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func TestEventingServiceWithCommunityContainer(t *testing.T) {
}

func testBucketUsage(t *testing.T, bucket *gocb.Bucket) {
t.Helper()
err := bucket.WaitUntilReady(5*time.Second, nil)
if err != nil {
t.Fatalf("could not connect bucket: %s", err)
Expand Down
1 change: 1 addition & 0 deletions modules/dolt/dolt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func TestDoltWithPublicRemoteCloneUrl(t *testing.T) {
}

func createTestCredsFile(t *testing.T) string {
t.Helper()
file, err := os.CreateTemp(t.TempDir(), "prefix")
if err != nil {
t.Fatal(err)
Expand Down
1 change: 1 addition & 0 deletions modules/kafka/consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type TestKafkaConsumer struct {
}

func NewTestKafkaConsumer(t *testing.T) (*TestKafkaConsumer, <-chan bool, <-chan bool, func()) {
t.Helper()
kc := &TestKafkaConsumer{
t: t,
ready: make(chan bool, 1),
Expand Down
1 change: 1 addition & 0 deletions modules/kafka/kafka_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func TestKafka_invalidVersion(t *testing.T) {
// assertAdvertisedListeners checks that the advertised listeners are set correctly:
// - The BROKER:// protocol is using the hostname of the Kafka container
func assertAdvertisedListeners(t *testing.T, container testcontainers.Container) {
t.Helper()
inspect, err := container.Inspect(context.Background())
require.NoError(t, err)

Expand Down
1 change: 1 addition & 0 deletions modules/mariadb/mariadb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ func TestMariaDBWithScripts(t *testing.T) {
}

func assertDataCanBeFetched(t *testing.T, ctx context.Context, container *mariadb.MariaDBContainer) {
t.Helper()
connectionString, err := container.ConnectionString(ctx)
require.NoError(t, err)
db, err := sql.Open("mysql", connectionString)
Expand Down
2 changes: 2 additions & 0 deletions modules/neo4j/neo4j_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ func setupNeo4j(ctx context.Context) (*neo4j.Neo4jContainer, error) {
}

func createDriver(t *testing.T, ctx context.Context, container *neo4j.Neo4jContainer) neo.DriverWithContext {
t.Helper()
// boltURL {
boltUrl, err := container.BoltUrl(ctx)
// }
Expand All @@ -182,6 +183,7 @@ func createDriver(t *testing.T, ctx context.Context, container *neo4j.Neo4jConta
}

func getContainerEnv(t *testing.T, ctx context.Context, container *neo4j.Neo4jContainer) string {
t.Helper()
exec, reader, err := container.Exec(ctx, []string{"env"})
if err != nil {
t.Fatalf("expected env to successfully run but did not: %s", err)
Expand Down
1 change: 1 addition & 0 deletions modules/ollama/ollama_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func TestOllama(t *testing.T) {
// For that, it checks if the response of the /api/tags endpoint
// contains the model name.
func assertLoadedModel(t *testing.T, c *ollama.OllamaContainer) {
t.Helper()
url, err := c.ConnectionString(context.Background())
require.NoError(t, err)

Expand Down
2 changes: 2 additions & 0 deletions modules/redis/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func TestRedisWithSnapshotting(t *testing.T) {
}

func assertSetsGets(t *testing.T, ctx context.Context, redisContainer *tcredis.RedisContainer, keyCount int) {
t.Helper()
// connectionString {
uri, err := redisContainer.ConnectionString(ctx)
// }
Expand All @@ -109,6 +110,7 @@ func assertSetsGets(t *testing.T, ctx context.Context, redisContainer *tcredis.R

client := redis.NewClient(options)
defer func(t *testing.T, ctx context.Context, client *redis.Client) {
t.Helper()
require.NoError(t, flushRedis(ctx, *client))
}(t, ctx, client)

Expand Down
2 changes: 2 additions & 0 deletions modules/valkey/valkey_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func TestValkeyWithSnapshotting(t *testing.T) {
}

func assertSetsGets(t *testing.T, ctx context.Context, valkeyContainer *tcvalkey.ValkeyContainer, keyCount int) {
t.Helper()
// connectionString {
uri, err := valkeyContainer.ConnectionString(ctx)
// }
Expand All @@ -95,6 +96,7 @@ func assertSetsGets(t *testing.T, ctx context.Context, valkeyContainer *tcvalkey
client, err := valkey.NewClient(options)
require.NoError(t, err)
defer func(t *testing.T, ctx context.Context, client *valkey.Client) {
t.Helper()
require.NoError(t, flushValkey(ctx, *client))
}(t, ctx, &client)

Expand Down
3 changes: 3 additions & 0 deletions port_forwarding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func TestExposeHostPorts(t *testing.T) {
}

func httpRequest(t *testing.T, c testcontainers.Container, port int) (int, string) {
t.Helper()
// wgetHostInternal {
code, reader, err := c.Exec(
context.Background(),
Expand All @@ -129,6 +130,7 @@ func httpRequest(t *testing.T, c testcontainers.Container, port int) (int, strin
}

func assertContainerHasHostAccess(t *testing.T, c testcontainers.Container, ports ...int) {
t.Helper()
for _, port := range ports {
code, response := httpRequest(t, c, port)
if code != 0 {
Expand All @@ -142,6 +144,7 @@ func assertContainerHasHostAccess(t *testing.T, c testcontainers.Container, port
}

func assertContainerHasNoHostAccess(t *testing.T, c testcontainers.Container, ports ...int) {
t.Helper()
for _, port := range ports {
_, response := httpRequest(t, c, port)

Expand Down
1 change: 1 addition & 0 deletions reaper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ func testContainerStop(t *testing.T) {

// testContainerTerminate tests terminating a container.
func testContainerTerminate(t *testing.T) {
t.Helper()
ctx := context.Background()

nginxA, err := GenericContainer(ctx, GenericContainerRequest{
Expand Down
Loading

0 comments on commit b74984d

Please sign in to comment.