From 27227a8824da65c8136406ddafc67215486ccdca Mon Sep 17 00:00:00 2001 From: Milas Bowman Date: Tue, 9 Aug 2022 16:43:58 -0400 Subject: [PATCH] lint: add `nolintlint` and clean up `nolint` directives (#9738) Signed-off-by: Milas Bowman --- .golangci.yml | 1 + cmd/formatter/logs.go | 2 +- pkg/compose/build_classic.go | 5 +++-- pkg/compose/container.go | 8 ++++---- pkg/compose/dependencies_test.go | 32 +++++++++++++++++--------------- pkg/compose/logs.go | 7 ++++--- pkg/e2e/compose_test.go | 2 +- pkg/e2e/framework.go | 4 ++-- pkg/e2e/volumes_test.go | 2 -- pkg/progress/tty.go | 2 -- pkg/utils/writer_test.go | 15 ++++++++------- 11 files changed, 41 insertions(+), 39 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 434b92875..ea8f78c8a 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -18,6 +18,7 @@ linters: - lll - misspell - nakedret + - nolintlint - staticcheck - structcheck - typecheck diff --git a/cmd/formatter/logs.go b/cmd/formatter/logs.go index b5bc37361..74ac6e056 100644 --- a/cmd/formatter/logs.go +++ b/cmd/formatter/logs.go @@ -89,7 +89,7 @@ func (l *logConsumer) Log(container, service, message string) { } p := l.getPresenter(container) for _, line := range strings.Split(message, "\n") { - fmt.Fprintf(l.writer, "%s%s\n", p.prefix, line) //nolint:errcheck + fmt.Fprintf(l.writer, "%s%s\n", p.prefix, line) } } diff --git a/pkg/compose/build_classic.go b/pkg/compose/build_classic.go index a6cc2a3fa..e362ae247 100644 --- a/pkg/compose/build_classic.go +++ b/pkg/compose/build_classic.go @@ -29,7 +29,6 @@ import ( "github.com/compose-spec/compose-go/types" buildx "github.com/docker/buildx/build" "github.com/docker/cli/cli/command/image/build" - "github.com/docker/compose/v2/pkg/api" dockertypes "github.com/docker/docker/api/types" "github.com/docker/docker/cli" "github.com/docker/docker/pkg/archive" @@ -40,6 +39,8 @@ import ( "github.com/docker/docker/pkg/urlutil" "github.com/hashicorp/go-multierror" "github.com/pkg/errors" + + "github.com/docker/compose/v2/pkg/api" ) func (s *composeService) doBuildClassic(ctx context.Context, project *types.Project, opts map[string]buildx.Options) (map[string]string, error) { @@ -65,7 +66,7 @@ func (s *composeService) doBuildClassic(ctx context.Context, project *types.Proj return nameDigests, errs } -// nolint: gocyclo +//nolint:gocyclo func (s *composeService) doBuildClassicSimpleImage(ctx context.Context, options buildx.Options) (string, error) { var ( buildCtx io.ReadCloser diff --git a/pkg/compose/container.go b/pkg/compose/container.go index 954aed727..004ac4167 100644 --- a/pkg/compose/container.go +++ b/pkg/compose/container.go @@ -30,13 +30,13 @@ const ( // ContainerRunning running status ContainerRunning = "running" // ContainerRemoving removing status - ContainerRemoving = "removing" //nolint + ContainerRemoving = "removing" // ContainerPaused paused status - ContainerPaused = "paused" //nolint + ContainerPaused = "paused" // ContainerExited exited status - ContainerExited = "exited" //nolint + ContainerExited = "exited" // ContainerDead dead status - ContainerDead = "dead" //nolint + ContainerDead = "dead" ) var _ io.ReadCloser = ContainerStdout{} diff --git a/pkg/compose/dependencies_test.go b/pkg/compose/dependencies_test.go index 5d5871a54..61bf0fd9c 100644 --- a/pkg/compose/dependencies_test.go +++ b/pkg/compose/dependencies_test.go @@ -21,7 +21,7 @@ import ( "testing" "github.com/compose-spec/compose-go/types" - "gotest.tools/v3/assert" + "github.com/stretchr/testify/require" ) var project = types.Project{ @@ -45,25 +45,27 @@ var project = types.Project{ } func TestInDependencyUpCommandOrder(t *testing.T) { - order := make(chan string) - //nolint:errcheck, unparam - go InDependencyOrder(context.TODO(), &project, func(ctx context.Context, config string) error { - order <- config + ctx, cancel := context.WithCancel(context.Background()) + t.Cleanup(cancel) + + var order []string + err := InDependencyOrder(ctx, &project, func(ctx context.Context, service string) error { + order = append(order, service) return nil }) - assert.Equal(t, <-order, "test3") - assert.Equal(t, <-order, "test2") - assert.Equal(t, <-order, "test1") + require.NoError(t, err, "Error during iteration") + require.Equal(t, []string{"test3", "test2", "test1"}, order) } func TestInDependencyReverseDownCommandOrder(t *testing.T) { - order := make(chan string) - //nolint:errcheck, unparam - go InReverseDependencyOrder(context.TODO(), &project, func(ctx context.Context, config string) error { - order <- config + ctx, cancel := context.WithCancel(context.Background()) + t.Cleanup(cancel) + + var order []string + err := InReverseDependencyOrder(ctx, &project, func(ctx context.Context, service string) error { + order = append(order, service) return nil }) - assert.Equal(t, <-order, "test1") - assert.Equal(t, <-order, "test2") - assert.Equal(t, <-order, "test3") + require.NoError(t, err, "Error during iteration") + require.Equal(t, []string{"test1", "test2", "test3"}, order) } diff --git a/pkg/compose/logs.go b/pkg/compose/logs.go index 7d26c815a..e8a16e780 100644 --- a/pkg/compose/logs.go +++ b/pkg/compose/logs.go @@ -21,11 +21,12 @@ import ( "io" "strings" - "github.com/docker/compose/v2/pkg/api" - "github.com/docker/compose/v2/pkg/utils" "github.com/docker/docker/api/types" "github.com/docker/docker/pkg/stdcopy" "golang.org/x/sync/errgroup" + + "github.com/docker/compose/v2/pkg/api" + "github.com/docker/compose/v2/pkg/utils" ) func (s *composeService) Logs(ctx context.Context, projectName string, consumer api.LogConsumer, options api.LogOptions) error { @@ -95,7 +96,7 @@ func (s *composeService) logContainers(ctx context.Context, consumer api.LogCons if err != nil { return err } - defer r.Close() //nolint errcheck + defer r.Close() //nolint:errcheck name := getContainerNameWithoutProject(c) w := utils.GetWriter(func(line string) { diff --git a/pkg/e2e/compose_test.go b/pkg/e2e/compose_test.go index f2ac2ad18..d1e613c4d 100644 --- a/pkg/e2e/compose_test.go +++ b/pkg/e2e/compose_test.go @@ -134,7 +134,7 @@ func TestDownComposefileInParentFolder(t *testing.T) { tmpFolder, err := os.MkdirTemp("fixtures/simple-composefile", "test-tmp") assert.NilError(t, err) - defer os.Remove(tmpFolder) // nolint: errcheck + defer os.Remove(tmpFolder) //nolint:errcheck projectName := filepath.Base(tmpFolder) res := c.RunDockerComposeCmd(t, "--project-directory", tmpFolder, "up", "-d") diff --git a/pkg/e2e/framework.go b/pkg/e2e/framework.go index 66a6233a7..6b1534c24 100644 --- a/pkg/e2e/framework.go +++ b/pkg/e2e/framework.go @@ -172,12 +172,12 @@ func CopyFile(t testing.TB, sourceFile string, destinationFile string) { src, err := os.Open(sourceFile) require.NoError(t, err, "Failed to open source file: %s") - //nolint: errcheck + //nolint:errcheck defer src.Close() dst, err := os.OpenFile(destinationFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o755) require.NoError(t, err, "Failed to open destination file: %s", destinationFile) - //nolint: errcheck + //nolint:errcheck defer dst.Close() _, err = io.Copy(dst, src) diff --git a/pkg/e2e/volumes_test.go b/pkg/e2e/volumes_test.go index 604b3ba6c..76a9094b9 100644 --- a/pkg/e2e/volumes_test.go +++ b/pkg/e2e/volumes_test.go @@ -50,7 +50,6 @@ func TestLocalComposeVolume(t *testing.T) { t.Run("check container volume specs", func(t *testing.T) { res := c.RunDockerCmd(t, "inspect", "compose-e2e-volume-nginx2-1", "--format", "{{ json .Mounts }}") output := res.Stdout() - //nolint assert.Assert(t, strings.Contains(output, `"Destination":"/usr/src/app/node_modules","Driver":"local","Mode":"z","RW":true,"Propagation":""`), output) assert.Assert(t, strings.Contains(output, `"Destination":"/myconfig","Mode":"","RW":false,"Propagation":"rprivate"`), output) }) @@ -68,7 +67,6 @@ func TestLocalComposeVolume(t *testing.T) { t.Run("check container bind-mounts specs", func(t *testing.T) { res := c.RunDockerCmd(t, "inspect", "compose-e2e-volume-nginx-1", "--format", "{{ json .Mounts }}") output := res.Stdout() - //nolint assert.Assert(t, strings.Contains(output, `"Type":"bind"`)) assert.Assert(t, strings.Contains(output, `"Destination":"/usr/share/nginx/html"`)) }) diff --git a/pkg/progress/tty.go b/pkg/progress/tty.go index 0dc95b906..09338fb83 100644 --- a/pkg/progress/tty.go +++ b/pkg/progress/tty.go @@ -164,14 +164,12 @@ func (w *ttyWriter) print() { continue } line := lineText(event, "", terminalWidth, statusPadding, runtime.GOOS != "windows") - //nolint: errcheck fmt.Fprint(w.out, line) numLines++ for _, v := range w.eventIDs { ev := w.events[v] if ev.ParentID == event.ID { line := lineText(ev, " ", terminalWidth, statusPadding, runtime.GOOS != "windows") - //nolint: errcheck fmt.Fprint(w.out, line) numLines++ } diff --git a/pkg/utils/writer_test.go b/pkg/utils/writer_test.go index 7eedb844f..7383a3e87 100644 --- a/pkg/utils/writer_test.go +++ b/pkg/utils/writer_test.go @@ -22,18 +22,19 @@ import ( "gotest.tools/v3/assert" ) +//nolint:errcheck func TestSplitWriter(t *testing.T) { var lines []string w := GetWriter(func(line string) { lines = append(lines, line) }) - w.Write([]byte("h")) //nolint: errcheck - w.Write([]byte("e")) //nolint: errcheck - w.Write([]byte("l")) //nolint: errcheck - w.Write([]byte("l")) //nolint: errcheck - w.Write([]byte("o")) //nolint: errcheck - w.Write([]byte("\n")) //nolint: errcheck - w.Write([]byte("world!\n")) //nolint: errcheck + w.Write([]byte("h")) + w.Write([]byte("e")) + w.Write([]byte("l")) + w.Write([]byte("l")) + w.Write([]byte("o")) + w.Write([]byte("\n")) + w.Write([]byte("world!\n")) assert.DeepEqual(t, lines, []string{"hello", "world!"}) }