Skip to content

Commit cab2c2a

Browse files
suwakeindeloof
authored andcommitted
Refactoring of redundant condition checks
Signed-off-by: keitosuwahara <[email protected]>
1 parent 1946de5 commit cab2c2a

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

pkg/api/labels.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
package api
1818

1919
import (
20-
"fmt"
21-
2220
"github.com/hashicorp/go-version"
2321

2422
"github.com/docker/compose/v2/internal"
@@ -65,9 +63,7 @@ var ComposeVersion string
6563
func init() {
6664
v, err := version.NewVersion(internal.Version)
6765
if err == nil {
68-
segments := v.Segments()
69-
if len(segments) > 2 {
70-
ComposeVersion = fmt.Sprintf("%d.%d.%d", segments[0], segments[1], segments[2])
71-
}
66+
// We are only interested in the core version (e.g., "1.2.3") from the version string.
67+
ComposeVersion = v.Core().String()
7268
}
7369
}

pkg/api/labels_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Copyright 2020 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 api
18+
19+
import (
20+
"testing"
21+
22+
"github.com/docker/compose/v2/internal"
23+
"github.com/hashicorp/go-version"
24+
"gotest.tools/v3/assert"
25+
)
26+
27+
func TestComposeVersionInitialization(t *testing.T) {
28+
v, err := version.NewVersion(internal.Version)
29+
if err != nil {
30+
assert.Equal(t, "", ComposeVersion, "ComposeVersion should be empty for a non-semver internal version (e.g. 'devel')")
31+
} else {
32+
expected := v.Core().String()
33+
assert.Equal(t, expected, ComposeVersion, "ComposeVersion should be the core of internal.Version")
34+
}
35+
}

0 commit comments

Comments
 (0)