Skip to content

Commit

Permalink
Golangci-lint for all go-modules (testcontainers#1498)
Browse files Browse the repository at this point in the history
* move golangci-lint to ci-test-go.yml

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

* checkout before setup-go

* cleanup .golangci.yml

* define cache-dependency-path for actions/setup-go

* remove static-analysis from required status check

* apply gofumpt formatting

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

---------

Signed-off-by: Matthieu MOREL <[email protected]>
  • Loading branch information
mmorel-35 authored Aug 21, 2023
1 parent 7021689 commit 520b0ac
Show file tree
Hide file tree
Showing 103 changed files with 324 additions and 276 deletions.
2 changes: 1 addition & 1 deletion .github/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ branches:
# Required. Require branches to be up to date before merging.
strict: true
# Required. The list of status checks to require in order to merge into this branch
contexts: ["static-analysis", "test (1.x, ubuntu-latest) / ./ubuntu-latest/1.x"]
contexts: ["test (1.x, ubuntu-latest) / ./ubuntu-latest/1.x"]
# Required. Enforce all configured restrictions for administrators. Set to true to enforce required status checks for repository administrators. Set to null to disable.
enforce_admins: false
# Prevent merge commits from being pushed to matching branches
Expand Down
27 changes: 24 additions & 3 deletions .github/workflows/ci-test-go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ on:
default: false
description: "Disable the ryuk container for the test."

permissions:
contents: read
# Optional: allow read access to pull request. Use with `only-new-issues` option.
# pull-requests: read

jobs:
test-go-project:
name: "${{ inputs.project-directory }}/${{ inputs.platform }}/${{ inputs.go-version }}"
Expand All @@ -48,14 +53,30 @@ jobs:
if: ${{ inputs.rootless-docker }}
run: sudo rm -rf /var/run/docker.sock

- name: Check out code into the Go module directory
uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: ${{ inputs.go-version }}
go-version: '${{ inputs.go-version }}'
cache-dependency-path: '${{ inputs.project-directory }}/go.sum'
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v3
- name: golangci-lint
# TODO: Remove each example/module once it passes the golangci-lint
if: ${{ inputs.platform == 'ubuntu-latest' && inputs.go-version == '1.20.x' && !contains(fromJSON('["examples/cockroachdb", "examples/spanner", "examples/toxiproxy", "modules/compose", "modules/pulsar", "modules/redis"]'), inputs.project-directory) }}
uses: golangci/golangci-lint-action@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
# Optional: working directory, useful for monorepos
working-directory: ${{ inputs.project-directory }}
# Optional: golangci-lint command line arguments.
args: --verbose
# Optional: if set to true then the all caching functionality will be complete disabled,
# takes precedence over all other caching options.
skip-cache: true

- name: modVerify
working-directory: ./${{ inputs.project-directory }}
Expand Down
51 changes: 0 additions & 51 deletions .github/workflows/golangci-lint.yml

This file was deleted.

9 changes: 4 additions & 5 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
linters:
enable:
- gci
- gofmt
- gofumpt
- misspell


linters-settings:
gci:
sections:
- standard
- default
- prefix(github.com/testcontainters)
- standard
- default
- prefix(github.com/testcontainers)

run:
timeout: 5m
1 change: 1 addition & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/testcontainers/testcontainers-go/internal/config"
)

Expand Down
1 change: 1 addition & 0 deletions container.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/docker/docker/api/types/registry"
"github.com/docker/docker/pkg/archive"
"github.com/docker/go-connections/nat"

tcexec "github.com/testcontainers/testcontainers-go/exec"
"github.com/testcontainers/testcontainers-go/internal/testcontainersdocker"
"github.com/testcontainers/testcontainers-go/wait"
Expand Down
20 changes: 11 additions & 9 deletions container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (
"time"

"github.com/stretchr/testify/assert"

"github.com/testcontainers/testcontainers-go/wait"
)

func Test_ContainerValidation(t *testing.T) {

type ContainerValidationTestCase struct {
Name string
ExpectedError error
Expand Down Expand Up @@ -71,13 +71,14 @@ func Test_ContainerValidation(t *testing.T) {
for _, testCase := range testTable {
t.Run(testCase.Name, func(t *testing.T) {
err := testCase.ContainerRequest.Validate()
if err == nil && testCase.ExpectedError == nil {
switch {
case err == nil && testCase.ExpectedError == nil:
return
} else if err == nil && testCase.ExpectedError != nil {
case err == nil && testCase.ExpectedError != nil:
t.Errorf("did not receive expected error: %s", testCase.ExpectedError.Error())
} else if err != nil && testCase.ExpectedError == nil {
case err != nil && testCase.ExpectedError == nil:
t.Errorf("received unexpected error: %s", err.Error())
} else if err.Error() != testCase.ExpectedError.Error() {
case err.Error() != testCase.ExpectedError.Error():
t.Errorf("errors mismatch: %s != %s", err.Error(), testCase.ExpectedError.Error())
}
})
Expand Down Expand Up @@ -155,7 +156,7 @@ func Test_BuildImageWithContexts(t *testing.T) {
for _, f := range files {
header := tar.Header{
Name: f.Name,
Mode: 0777,
Mode: 0o777,
Size: int64(len(f.Contents)),
Typeflag: tar.TypeReg,
Format: tar.FormatGNU,
Expand Down Expand Up @@ -271,13 +272,14 @@ func Test_BuildImageWithContexts(t *testing.T) {
ContainerRequest: req,
Started: true,
})
if testCase.ExpectedError != nil && err != nil {
switch {
case testCase.ExpectedError != nil && err != nil:
if testCase.ExpectedError.Error() != err.Error() {
t.Fatalf("unexpected error: %s, was expecting %s", err.Error(), testCase.ExpectedError.Error())
}
} else if err != nil {
case err != nil:
t.Fatal(err)
} else {
default:
terminateContainerOnEnd(t, ctx, c)
}
})
Expand Down
4 changes: 2 additions & 2 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/google/uuid"
"github.com/moby/term"
specs "github.com/opencontainers/image-spec/specs-go/v1"

tcexec "github.com/testcontainers/testcontainers-go/exec"
"github.com/testcontainers/testcontainers-go/internal"
"github.com/testcontainers/testcontainers-go/internal/testcontainersdocker"
Expand Down Expand Up @@ -316,7 +317,6 @@ func (c *DockerContainer) inspectContainer(ctx context.Context) (*types.Containe
// Logs will fetch both STDOUT and STDERR from the current container. Returns a
// ReadCloser and leaves it up to the caller to extract what it wants.
func (c *DockerContainer) Logs(ctx context.Context) (io.ReadCloser, error) {

const streamHeaderSize = 8

options := types.ContainerLogsOptions{
Expand All @@ -334,7 +334,7 @@ func (c *DockerContainer) Logs(ctx context.Context) (io.ReadCloser, error) {
r := bufio.NewReader(rc)

go func() {
var lineStarted = true
lineStarted := true
for err == nil {
line, isPrefix, err := r.ReadLine()

Expand Down
1 change: 1 addition & 0 deletions docker_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/cpuguy83/dockercfg"
"github.com/docker/docker/api/types/registry"

"github.com/testcontainers/testcontainers-go/internal/testcontainersdocker"
)

Expand Down
1 change: 1 addition & 0 deletions docker_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/docker/docker/client"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/testcontainers/testcontainers-go/internal/testcontainersdocker"
"github.com/testcontainers/testcontainers-go/wait"
)
Expand Down
1 change: 1 addition & 0 deletions docker_exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

"github.com/stretchr/testify/require"

tcexec "github.com/testcontainers/testcontainers-go/exec"
)

Expand Down
14 changes: 6 additions & 8 deletions docker_mounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ package testcontainers

import "github.com/docker/docker/api/types/mount"

var (
mountTypeMapping = map[MountType]mount.Type{
MountTypeBind: mount.TypeBind,
MountTypeVolume: mount.TypeVolume,
MountTypeTmpfs: mount.TypeTmpfs,
MountTypePipe: mount.TypeNamedPipe,
}
)
var mountTypeMapping = map[MountType]mount.Type{
MountTypeBind: mount.TypeBind,
MountTypeVolume: mount.TypeVolume,
MountTypeTmpfs: mount.TypeTmpfs,
MountTypePipe: mount.TypeNamedPipe,
}

// BindMounter can optionally be implemented by mount sources
// to support advanced scenarios based on mount.BindOptions
Expand Down
3 changes: 2 additions & 1 deletion docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/docker/go-units"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/testcontainers/testcontainers-go/internal/config"
"github.com/testcontainers/testcontainers-go/internal/testcontainersdocker"
"github.com/testcontainers/testcontainers-go/wait"
Expand Down Expand Up @@ -2190,7 +2191,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) {
// create all copied files into a temporary dir
tmpDir := filepath.Join(t.TempDir())
tmpDir := t.TempDir()

// compare the bytes of each file in the source with the bytes from the copied-from-container file
srcFiles, err := os.ReadDir(hostFilePath)
Expand Down
3 changes: 1 addition & 2 deletions examples/bigtable/bigtable.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import (
"context"
"fmt"

"github.com/testcontainers/testcontainers-go/wait"

"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)

// bigtableContainer represents the bigtable container type used in the module
Expand Down
5 changes: 3 additions & 2 deletions examples/bigtable/bigtable_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package bigtable

import (
"cloud.google.com/go/bigtable"
"context"
"testing"

"cloud.google.com/go/bigtable"
"google.golang.org/api/option"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"testing"
)

const (
Expand Down
1 change: 1 addition & 0 deletions examples/cockroachdb/cockroachdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

_ "github.com/jackc/pgx/v4/stdlib"

"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)
Expand Down
5 changes: 3 additions & 2 deletions examples/datastore/datastore_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package datastore

import (
"cloud.google.com/go/datastore"
"context"
"testing"

"cloud.google.com/go/datastore"
"google.golang.org/api/option"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"testing"
)

type Task struct {
Expand Down
3 changes: 1 addition & 2 deletions examples/firestore/firestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import (
"context"
"fmt"

"github.com/testcontainers/testcontainers-go/wait"

"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)

// firestoreContainer represents the firestore container type used in the module
Expand Down
9 changes: 5 additions & 4 deletions examples/firestore/firestore_test.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
package firestore

import (
"cloud.google.com/go/firestore"
"context"
"testing"

"cloud.google.com/go/firestore"
"google.golang.org/api/option"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"testing"
)

type Person struct {
Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
}

type emulatorCreds struct {
}
type emulatorCreds struct{}

func (ec emulatorCreds) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
return map[string]string{"authorization": "Bearer owner"}, nil
}

func (ec emulatorCreds) RequireTransportSecurity() bool {
return false
}
Expand Down
2 changes: 1 addition & 1 deletion examples/nats/nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func runContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize
return nil, err
}

uri:= fmt.Sprintf("nats://%s:%s", hostIP, mappedPort.Port())
uri := fmt.Sprintf("nats://%s:%s", hostIP, mappedPort.Port())

return &natsContainer{Container: container, URI: uri}, nil
}
1 change: 0 additions & 1 deletion examples/nats/nats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,3 @@ func TestNats(t *testing.T) {
t.Fatalf("expected message to be 'hello', got '%s'", msg.Data)
}
}

Loading

0 comments on commit 520b0ac

Please sign in to comment.