Skip to content

Commit 2ba5c46

Browse files
committed
Set custom version
1 parent f215973 commit 2ba5c46

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

version/version.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package version
22

33
import (
4+
"fmt"
5+
"os"
46
"strings"
57

68
"github.com/blang/semver/v4"
@@ -12,9 +14,25 @@ var (
1214
binaryVersion string
1315
binarySHA string
1416
binaryBuildDate string
17+
customVersion string
1518
)
1619

20+
func SetVersion(version string) {
21+
_, err := semver.Parse(version)
22+
23+
if err != nil {
24+
fmt.Fprintf(os.Stderr, "Unable to parse version: %s\n", err)
25+
os.Exit(1)
26+
}
27+
28+
customVersion = version
29+
}
30+
1731
func VersionString() string {
32+
if customVersion != "" {
33+
return customVersion
34+
}
35+
1836
// Remove the "v" prefix from the binary in case it is present
1937
binaryVersion = strings.TrimPrefix(binaryVersion, "v")
2038
versionString, err := semver.Make(binaryVersion)

version/version_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package version_test
22

33
import (
4+
"os"
5+
"os/exec"
6+
47
"code.cloudfoundry.org/cli/version"
58
. "github.com/onsi/ginkgo/v2"
69
. "github.com/onsi/gomega"
@@ -13,5 +16,36 @@ var _ = Describe("Version", func() {
1316
Expect(version.VersionString()).To(Equal("0.0.0-unknown-version"))
1417
})
1518
})
19+
20+
When("a custom version is set", func() {
21+
It("returns the custom version", func() {
22+
version.SetVersion("1.2.3")
23+
Expect(version.VersionString()).To(Equal("1.2.3"))
24+
})
25+
})
26+
})
27+
28+
Describe("SetVersion", func() {
29+
It("sets the version for valid semver versions", func() {
30+
version.SetVersion("1.2.3")
31+
Expect(version.VersionString()).To(Equal("1.2.3"))
32+
})
33+
34+
It("exits with status code 1 when given an invalid semver", func() {
35+
if os.Getenv("TEST_EXIT") == "1" {
36+
version.SetVersion("not-a-semver")
37+
Fail("Expected process to exit but it didn't")
38+
}
39+
40+
cmd := exec.Command(os.Args[0], "-ginkgo.focus=exits with status code 1 when given an invalid semver")
41+
cmd.Env = append(os.Environ(), "TEST_EXIT=1")
42+
43+
err := cmd.Run()
44+
Expect(err).To(HaveOccurred())
45+
46+
exitErr, ok := err.(*exec.ExitError)
47+
Expect(ok).To(BeTrue())
48+
Expect(exitErr.ExitCode()).To(Equal(1))
49+
})
1650
})
1751
})

0 commit comments

Comments
 (0)