|
| 1 | +/* |
| 2 | + Copyright 2025 Docker Compose CLI authors |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package compose |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/docker/cli/cli/streams" |
| 24 | + "github.com/docker/compose/v2/internal" |
| 25 | + "github.com/docker/compose/v2/pkg/mocks" |
| 26 | + "go.uber.org/mock/gomock" |
| 27 | + "gotest.tools/v3/assert" |
| 28 | +) |
| 29 | + |
| 30 | +func TestVersionCommand(t *testing.T) { |
| 31 | + originalVersion := internal.Version |
| 32 | + defer func() { |
| 33 | + internal.Version = originalVersion |
| 34 | + }() |
| 35 | + internal.Version = "v9.9.9-test" |
| 36 | + |
| 37 | + tests := []struct { |
| 38 | + name string |
| 39 | + args []string |
| 40 | + want string |
| 41 | + }{ |
| 42 | + { |
| 43 | + name: "default", |
| 44 | + args: []string{}, |
| 45 | + want: "Docker Compose version v9.9.9-test\n", |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "short flag", |
| 49 | + args: []string{"--short"}, |
| 50 | + want: "9.9.9-test\n", |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "json flag", |
| 54 | + args: []string{"--format", "json"}, |
| 55 | + want: `{"version":"v9.9.9-test"}` + "\n", |
| 56 | + }, |
| 57 | + } |
| 58 | + |
| 59 | + for _, test := range tests { |
| 60 | + t.Run(test.name, func(t *testing.T) { |
| 61 | + ctrl := gomock.NewController(t) |
| 62 | + defer ctrl.Finish() |
| 63 | + |
| 64 | + buf := new(bytes.Buffer) |
| 65 | + cli := mocks.NewMockCli(ctrl) |
| 66 | + cli.EXPECT().Out().Return(streams.NewOut(buf)).AnyTimes() |
| 67 | + |
| 68 | + cmd := versionCommand(cli) |
| 69 | + cmd.SetArgs(test.args) |
| 70 | + err := cmd.Execute() |
| 71 | + assert.NilError(t, err) |
| 72 | + |
| 73 | + assert.Equal(t, test.want, buf.String()) |
| 74 | + }) |
| 75 | + } |
| 76 | +} |
0 commit comments