Skip to content

Commit cc5a516

Browse files
mmorel-35stevenh
andauthored
ci: enable perfsprint linter (testcontainers#2872)
Enable perfsprint linter for golangci-lint Signed-off-by: Matthieu MOREL <[email protected]> Co-authored-by: Steven Hartland <[email protected]>
1 parent 1819e70 commit cc5a516

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+116
-96
lines changed

.golangci.yml

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ linters:
88
- misspell
99
- nolintlint
1010
- nakedret
11+
- perfsprint
1112
- testifylint
1213
- thelper
1314
- usestdlibvars

docker.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func (c *DockerContainer) PortEndpoint(ctx context.Context, port nat.Port, proto
152152

153153
protoFull := ""
154154
if proto != "" {
155-
protoFull = fmt.Sprintf("%s://", proto)
155+
protoFull = proto + "://"
156156
}
157157

158158
return fmt.Sprintf("%s%s:%s", protoFull, host, outerPort.Port()), nil

docker_auth_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -284,11 +284,11 @@ func prepareLocalRegistryWithAuth(t *testing.T) string {
284284
},
285285
Files: []ContainerFile{
286286
{
287-
HostFilePath: fmt.Sprintf("%s/testdata/auth", wd),
287+
HostFilePath: wd + "/testdata/auth",
288288
ContainerFilePath: "/auth",
289289
},
290290
{
291-
HostFilePath: fmt.Sprintf("%s/testdata/data", wd),
291+
HostFilePath: wd + "/testdata/data",
292292
ContainerFilePath: "/data",
293293
},
294294
},

file_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"archive/tar"
77
"bytes"
88
"compress/gzip"
9+
"errors"
910
"fmt"
1011
"io"
1112
"os"
@@ -37,7 +38,7 @@ func Test_IsDir(t *testing.T) {
3738
{
3839
filepath: "foobar.doc",
3940
expected: false,
40-
err: fmt.Errorf("does not exist"),
41+
err: errors.New("does not exist"),
4142
},
4243
}
4344

internal/core/bootstrap.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package core
22

33
import (
44
"crypto/sha256"
5+
"encoding/hex"
56
"fmt"
67
"os"
78

@@ -89,7 +90,7 @@ func init() {
8990
return
9091
}
9192

92-
sessionID = fmt.Sprintf("%x", hasher.Sum(nil))
93+
sessionID = hex.EncodeToString(hasher.Sum(nil))
9394
}
9495

9596
func ProcessID() string {

internal/core/docker_host_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package core
22

33
import (
44
"context"
5-
"fmt"
5+
"errors"
66
"os"
77
"path/filepath"
88
"testing"
@@ -46,7 +46,7 @@ func testCallbackCheckPassing(_ context.Context, _ string) error {
4646
}
4747

4848
func testCallbackCheckError(_ context.Context, _ string) error {
49-
return fmt.Errorf("could not check the Docker host")
49+
return errors.New("could not check the Docker host")
5050
}
5151

5252
func mockCallbackCheck(t *testing.T, fn func(_ context.Context, _ string) error) {

internal/core/docker_rootless.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package core
33
import (
44
"context"
55
"errors"
6-
"fmt"
76
"net/url"
87
"os"
98
"path/filepath"
109
"runtime"
10+
"strconv"
1111
)
1212

1313
var (
@@ -144,7 +144,7 @@ func rootlessSocketPathFromHomeDesktopDir() (string, error) {
144144
// rootlessSocketPathFromRunDir returns the path to the rootless Docker socket from the /run/user/<uid>/docker.sock file.
145145
func rootlessSocketPathFromRunDir() (string, error) {
146146
uid := os.Getuid()
147-
f := filepath.Join(baseRunDir, "user", fmt.Sprintf("%d", uid), "docker.sock")
147+
f := filepath.Join(baseRunDir, "user", strconv.Itoa(uid), "docker.sock")
148148
if fileExists(f) {
149149
return f, nil
150150
}

internal/core/docker_rootless_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package core
22

33
import (
44
"context"
5-
"fmt"
65
"os"
76
"path/filepath"
7+
"strconv"
88
"testing"
99

1010
"github.com/stretchr/testify/assert"
@@ -161,7 +161,7 @@ func TestRootlessDockerSocketPath(t *testing.T) {
161161
})
162162

163163
uid := os.Getuid()
164-
runDir := filepath.Join(tmpDir, "user", fmt.Sprintf("%d", uid))
164+
runDir := filepath.Join(tmpDir, "user", strconv.Itoa(uid))
165165
err = createTmpDockerSocket(runDir)
166166
require.NoError(t, err)
167167

@@ -208,7 +208,7 @@ func setupRootlessNotFound(t *testing.T) {
208208

209209
baseRunDir = tmpDir
210210
uid := os.Getuid()
211-
runDir := filepath.Join(tmpDir, "run", "user", fmt.Sprintf("%d", uid))
211+
runDir := filepath.Join(tmpDir, "run", "user", strconv.Itoa(uid))
212212
err = createTmpDir(runDir)
213213
require.NoError(t, err)
214214
}

modules/chroma/chroma.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package chroma
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67

78
"github.com/testcontainers/testcontainers-go"
@@ -66,7 +67,7 @@ func (c *ChromaContainer) RESTEndpoint(ctx context.Context) (string, error) {
6667

6768
host, err := c.Host(ctx)
6869
if err != nil {
69-
return "", fmt.Errorf("failed to get container host")
70+
return "", errors.New("failed to get container host")
7071
}
7172

7273
return fmt.Sprintf("http://%s:%s", host, containerPort.Port()), nil

modules/cockroachdb/certs.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package cockroachdb
22

33
import (
44
"crypto/x509"
5-
"fmt"
5+
"errors"
66
"net"
77
"time"
88

@@ -28,7 +28,7 @@ func NewTLSConfig() (*TLSConfig, error) {
2828
ValidFor: time.Hour,
2929
})
3030
if caCert == nil {
31-
return nil, fmt.Errorf("failed to generate CA certificate")
31+
return nil, errors.New("failed to generate CA certificate")
3232
}
3333
// }
3434

@@ -42,7 +42,7 @@ func NewTLSConfig() (*TLSConfig, error) {
4242
Parent: caCert, // using the CA certificate as parent
4343
})
4444
if nodeCert == nil {
45-
return nil, fmt.Errorf("failed to generate node certificate")
45+
return nil, errors.New("failed to generate node certificate")
4646
}
4747
// }
4848

@@ -54,7 +54,7 @@ func NewTLSConfig() (*TLSConfig, error) {
5454
Parent: caCert, // using the CA certificate as parent
5555
})
5656
if clientCert == nil {
57-
return nil, fmt.Errorf("failed to generate client certificate")
57+
return nil, errors.New("failed to generate client certificate")
5858
}
5959

6060
return &TLSConfig{

modules/cockroachdb/cockroachdb.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"crypto/tls"
66
"crypto/x509"
77
"encoding/pem"
8+
"errors"
89
"fmt"
910
"net"
1011
"net/url"
@@ -18,7 +19,7 @@ import (
1819
"github.com/testcontainers/testcontainers-go/wait"
1920
)
2021

21-
var ErrTLSNotEnabled = fmt.Errorf("tls not enabled")
22+
var ErrTLSNotEnabled = errors.New("tls not enabled")
2223

2324
const (
2425
certsDir = "/tmp"
@@ -144,7 +145,7 @@ func addCmd(req *testcontainers.GenericContainerRequest, opts options) error {
144145
return fmt.Errorf("unsupported user %s with TLS, use %s", opts.User, defaultUser)
145146
}
146147
if opts.Password != "" {
147-
return fmt.Errorf("cannot use password authentication with TLS")
148+
return errors.New("cannot use password authentication with TLS")
148149
}
149150
}
150151

modules/compose/compose_api_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package compose
22

33
import (
44
"context"
5-
"fmt"
5+
"encoding/hex"
66
"hash/fnv"
77
"os"
88
"path/filepath"
@@ -642,7 +642,7 @@ func TestDockerComposeAPIVolumesDeletedOnDown(t *testing.T) {
642642

643643
volumeListFilters := filters.NewArgs()
644644
// the "mydata" identifier comes from the "testdata/docker-compose-volume.yml" file
645-
volumeListFilters.Add("name", fmt.Sprintf("%s_mydata", identifier))
645+
volumeListFilters.Add("name", identifier+"_mydata")
646646
volumeList, err := compose.dockerClient.VolumeList(ctx, volume.ListOptions{Filters: volumeListFilters})
647647
require.NoError(t, err, "compose.dockerClient.VolumeList()")
648648

@@ -690,7 +690,7 @@ func TestDockerComposeApiWithWaitForShortLifespanService(t *testing.T) {
690690
}
691691

692692
func testNameHash(name string) StackIdentifier {
693-
return StackIdentifier(fmt.Sprintf("%x", fnv.New32a().Sum([]byte(name))))
693+
return StackIdentifier(hex.EncodeToString(fnv.New32a().Sum([]byte(name))))
694694
}
695695

696696
// cleanup is a helper function that schedules the compose stack to be stopped when the test ends.

modules/dolt/dolt.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package dolt
33
import (
44
"context"
55
"database/sql"
6+
"errors"
67
"fmt"
78
"path/filepath"
89
"strings"
@@ -84,7 +85,7 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
8485
}
8586

8687
if len(password) == 0 && password == "" && !strings.EqualFold(rootUser, username) {
87-
return nil, fmt.Errorf("empty password can be used only with the root user")
88+
return nil, errors.New("empty password can be used only with the root user")
8889
}
8990

9091
container, err := testcontainers.GenericContainer(ctx, genericContainerReq)

modules/elasticsearch/elasticsearch.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"crypto/tls"
66
"crypto/x509"
7+
"errors"
78
"fmt"
89
"io"
910
"os"
@@ -212,7 +213,7 @@ func configurePassword(settings *Options, req *testcontainers.GenericContainerRe
212213

213214
if settings.Password != "" {
214215
if isOSS(req.Image) {
215-
return fmt.Errorf("it's not possible to activate security on Elastic OSS Image. Please switch to the default distribution.")
216+
return errors.New("it's not possible to activate security on Elastic OSS Image. Please switch to the default distribution.")
216217
}
217218

218219
if _, ok := req.Env["ELASTIC_PASSWORD"]; !ok {

modules/elasticsearch/version.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func isAtLeastVersion(image string, major int) bool {
2222
}
2323

2424
if !strings.HasPrefix(version, "v") {
25-
version = fmt.Sprintf("v%s", version)
25+
version = "v" + version
2626
}
2727

2828
if semver.IsValid(version) {

modules/gcloud/spanner_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func ExampleRunSpannerContainer() {
6363
// }
6464

6565
instanceOp, err := instanceAdmin.CreateInstance(ctx, &instancepb.CreateInstanceRequest{
66-
Parent: fmt.Sprintf("projects/%s", projectId),
66+
Parent: "projects/" + projectId,
6767
InstanceId: instanceId,
6868
Instance: &instancepb.Instance{
6969
DisplayName: instanceId,

modules/inbucket/inbucket.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (c *InbucketContainer) WebInterface(ctx context.Context) (string, error) {
4444
return "", err
4545
}
4646

47-
return fmt.Sprintf("http://%s", net.JoinHostPort(host, containerPort.Port())), nil
47+
return "http://" + net.JoinHostPort(host, containerPort.Port()), nil
4848
}
4949

5050
// Deprecated: use Run instead

modules/k3s/k3s.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ func (c *K3sContainer) LoadImages(ctx context.Context, images ...string) error {
220220
return fmt.Errorf("saving images %w", err)
221221
}
222222

223-
containerPath := fmt.Sprintf("/tmp/%s", filepath.Base(imagesTar.Name()))
223+
containerPath := "/tmp/" + filepath.Base(imagesTar.Name())
224224
err = c.Container.CopyFileToContainer(ctx, imagesTar.Name(), containerPath, 0x644)
225225
if err != nil {
226226
return fmt.Errorf("copying image to container %w", err)

modules/k6/k6.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func WithCache() testcontainers.CustomizeRequestOption {
143143
cacheVol := os.Getenv("TC_K6_BUILD_CACHE")
144144
// if no volume is provided, create one and ensure add labels for garbage collection
145145
if cacheVol == "" {
146-
cacheVol = fmt.Sprintf("k6-cache-%s", testcontainers.SessionID())
146+
cacheVol = "k6-cache-" + testcontainers.SessionID()
147147
volOptions = &mount.VolumeOptions{
148148
Labels: testcontainers.GenericLabels(),
149149
}

modules/kafka/kafka.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package kafka
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"math"
8+
"strconv"
79
"strings"
810

911
"github.com/docker/go-connections/nat"
@@ -58,7 +60,7 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
5860
"KAFKA_OFFSETS_TOPIC_NUM_PARTITIONS": "1",
5961
"KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR": "1",
6062
"KAFKA_TRANSACTION_STATE_LOG_MIN_ISR": "1",
61-
"KAFKA_LOG_FLUSH_INTERVAL_MESSAGES": fmt.Sprintf("%d", math.MaxInt),
63+
"KAFKA_LOG_FLUSH_INTERVAL_MESSAGES": strconv.Itoa(math.MaxInt),
6264
"KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS": "0",
6365
"KAFKA_NODE_ID": "1",
6466
"KAFKA_PROCESS_ROLES": "broker,controller",
@@ -203,7 +205,7 @@ func configureControllerQuorumVoters(req *testcontainers.GenericContainerRequest
203205
// which is available since version 7.0.0.
204206
func validateKRaftVersion(fqName string) error {
205207
if fqName == "" {
206-
return fmt.Errorf("image cannot be empty")
208+
return errors.New("image cannot be empty")
207209
}
208210

209211
image := fqName[:strings.LastIndex(fqName, ":")]
@@ -218,7 +220,7 @@ func validateKRaftVersion(fqName string) error {
218220

219221
// semver requires the version to start with a "v"
220222
if !strings.HasPrefix(version, "v") {
221-
version = fmt.Sprintf("v%s", version)
223+
version = "v" + version
222224
}
223225

224226
if semver.Compare(version, "v7.4.0") < 0 { // version < v7.4.0

modules/localstack/localstack.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func isLegacyMode(image string) bool {
2929
}
3030

3131
if !strings.HasPrefix(version, "v") {
32-
version = fmt.Sprintf("v%s", version)
32+
version = "v" + version
3333
}
3434

3535
if semver.IsValid(version) {
@@ -48,7 +48,7 @@ func isVersion2(image string) bool {
4848
}
4949

5050
if !strings.HasPrefix(version, "v") {
51-
version = fmt.Sprintf("v%s", version)
51+
version = "v" + version
5252
}
5353

5454
if semver.IsValid(version) {
@@ -82,7 +82,7 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
8282
ExposedPorts: []string{fmt.Sprintf("%d/tcp", defaultPort)},
8383
Env: map[string]string{},
8484
HostConfigModifier: func(hostConfig *container.HostConfig) {
85-
hostConfig.Binds = []string{fmt.Sprintf("%s:/var/run/docker.sock", dockerHost)}
85+
hostConfig.Binds = []string{dockerHost + ":/var/run/docker.sock"}
8686
},
8787
}
8888

0 commit comments

Comments
 (0)