11package version_test
22
33import (
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