From 6c345b3755e73335bf753c034232cb6f22afacd6 Mon Sep 17 00:00:00 2001 From: Eng Zer Jun Date: Tue, 3 Oct 2023 01:36:56 +0800 Subject: [PATCH] refactor(cmd/compose/run): remove redundant `len` check From the Go specification [1]: "1. For a nil slice, the number of iterations is 0." `len` returns 0 if the slice is nil [2]. Therefore, checking `len(v) > 0` before a loop is unnecessary. [1]: https://go.dev/ref/spec#For_range [2]: https://pkg.go.dev/builtin#len Signed-off-by: Eng Zer Jun --- cmd/compose/run.go | 18 ++++++++---------- pkg/e2e/compose_run_test.go | 10 +++++++++- pkg/e2e/fixtures/run-test/ports.yaml | 6 ++++++ 3 files changed, 23 insertions(+), 11 deletions(-) create mode 100644 pkg/e2e/fixtures/run-test/ports.yaml diff --git a/cmd/compose/run.go b/cmd/compose/run.go index a3004eb63..aa54b5c65 100644 --- a/cmd/compose/run.go +++ b/cmd/compose/run.go @@ -80,11 +80,10 @@ func (options runOptions) apply(project *types.Project) error { target.Tty = !options.noTty target.StdinOpen = options.interactive + + // --service-ports and --publish are incompatible if !options.servicePorts { target.Ports = []types.ServicePortConfig{} - } - if len(options.publish) > 0 { - target.Ports = []types.ServicePortConfig{} for _, p := range options.publish { config, err := types.ParsePortConfig(p) if err != nil { @@ -93,14 +92,13 @@ func (options runOptions) apply(project *types.Project) error { target.Ports = append(target.Ports, config...) } } - if len(options.volumes) > 0 { - for _, v := range options.volumes { - volume, err := loader.ParseVolume(v) - if err != nil { - return err - } - target.Volumes = append(target.Volumes, volume) + + for _, v := range options.volumes { + volume, err := loader.ParseVolume(v) + if err != nil { + return err } + target.Volumes = append(target.Volumes, volume) } for i, s := range project.Services { diff --git a/pkg/e2e/compose_run_test.go b/pkg/e2e/compose_run_test.go index e330bd43d..b074e6a80 100644 --- a/pkg/e2e/compose_run_test.go +++ b/pkg/e2e/compose_run_test.go @@ -96,10 +96,18 @@ func TestLocalComposeRun(t *testing.T) { }) t.Run("compose run --publish", func(t *testing.T) { - c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/compose.yaml", "run", "--publish", "8081:80", "-d", "back", + c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/ports.yaml", "run", "--publish", "8081:80", "-d", "back", "/bin/sh", "-c", "sleep 1") res := c.RunDockerCmd(t, "ps") assert.Assert(t, strings.Contains(res.Stdout(), "8081->80/tcp"), res.Stdout()) + assert.Assert(t, !strings.Contains(res.Stdout(), "8082->80/tcp"), res.Stdout()) + }) + + t.Run("compose run --service-ports", func(t *testing.T) { + c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/ports.yaml", "run", "--service-ports", "-d", "back", + "/bin/sh", "-c", "sleep 1") + res := c.RunDockerCmd(t, "ps") + assert.Assert(t, strings.Contains(res.Stdout(), "8082->80/tcp"), res.Stdout()) }) t.Run("compose run orphan", func(t *testing.T) { diff --git a/pkg/e2e/fixtures/run-test/ports.yaml b/pkg/e2e/fixtures/run-test/ports.yaml new file mode 100644 index 000000000..58f320f53 --- /dev/null +++ b/pkg/e2e/fixtures/run-test/ports.yaml @@ -0,0 +1,6 @@ +version: '3.8' +services: + back: + image: alpine + ports: + - 8082:80