Skip to content

Commit bbfec16

Browse files
committed
chore: add version flag test
1 parent 76f83d1 commit bbfec16

File tree

3 files changed

+169
-5
lines changed

3 files changed

+169
-5
lines changed

pkg/version/flag.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ package version
1010

1111
import (
1212
"fmt"
13+
"io"
1314
"os"
1415
"strconv"
16+
"strings"
1517

1618
flag "github.com/spf13/pflag"
1719
)
@@ -39,6 +41,16 @@ func (v *versionValue) Set(s string) error {
3941
*v = VersionRaw
4042
return nil
4143
}
44+
45+
if strings.HasPrefix(s, "v") {
46+
err := SetDynamicVersion(s)
47+
if err == nil {
48+
value, _ := strconv.Atoi(s)
49+
*v = versionValue(value)
50+
}
51+
return err
52+
}
53+
4254
boolVal, err := strconv.ParseBool(s)
4355
if boolVal {
4456
*v = VersionTrue
@@ -83,14 +95,20 @@ func AddFlags(fs *flag.FlagSet) {
8395
fs.AddFlag(flag.Lookup(versionFlagName))
8496
}
8597

98+
// variables for unit testing PrintAndExitIfRequested
99+
var (
100+
output = io.Writer(os.Stdout)
101+
exit = os.Exit
102+
)
103+
86104
// PrintAndExitIfRequested will check if the -version flag was passed
87105
// and, if so, print the version and exit.
88106
func PrintAndExitIfRequested(appName string) {
89107
if *versionFlag == VersionRaw {
90-
fmt.Printf("%s\n", Get().Text())
91-
os.Exit(0)
108+
fmt.Fprintf(output, "%s\n", Get().Text())
109+
exit(0)
92110
} else if *versionFlag == VersionTrue {
93-
fmt.Printf("%s %s\n", appName, Get().GitVersion)
94-
os.Exit(0)
111+
fmt.Fprintf(output, "%s %s\n", appName, Get().GitVersion)
112+
exit(0)
95113
}
96114
}

pkg/version/flag_test.go

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package version
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"strings"
7+
"testing"
8+
9+
flag "github.com/spf13/pflag"
10+
)
11+
12+
func TestVersionFlag(t *testing.T) {
13+
appName := "onex-test"
14+
initialFlagValue := *versionFlag
15+
initialVersion := Get()
16+
testcases := []struct {
17+
name string
18+
flags []string
19+
expectError string
20+
expectExit bool
21+
expectPrintVersion string
22+
expectGitVersion string
23+
}{
24+
{
25+
name: "no flag",
26+
flags: []string{},
27+
expectGitVersion: initialVersion.GitVersion,
28+
},
29+
{
30+
name: "false",
31+
flags: []string{"--version=false"},
32+
expectGitVersion: initialVersion.GitVersion,
33+
},
34+
35+
{
36+
name: "valueless",
37+
flags: []string{"--version"},
38+
expectGitVersion: initialVersion.GitVersion,
39+
expectExit: true,
40+
expectPrintVersion: appName + " " + initialVersion.GitVersion,
41+
},
42+
{
43+
name: "true",
44+
flags: []string{"--version=true"},
45+
expectGitVersion: initialVersion.GitVersion,
46+
expectExit: true,
47+
expectPrintVersion: appName + " " + initialVersion.GitVersion,
48+
},
49+
{
50+
name: "raw",
51+
flags: []string{"--version=raw"},
52+
expectGitVersion: initialVersion.GitVersion,
53+
expectExit: true,
54+
expectPrintVersion: fmt.Sprintf("%s", strings.TrimSpace(initialVersion.Text())),
55+
},
56+
{
57+
name: "truthy",
58+
flags: []string{"--version=T"},
59+
expectGitVersion: initialVersion.GitVersion,
60+
expectExit: true,
61+
expectPrintVersion: appName + " " + initialVersion.GitVersion,
62+
},
63+
{
64+
name: "override",
65+
flags: []string{"--version=v0.0.0-custom"},
66+
expectGitVersion: "v0.0.0-custom",
67+
},
68+
{
69+
name: "override and exit",
70+
flags: []string{"--version=v0.0.0-custom", "--version"},
71+
expectGitVersion: "v0.0.0-custom",
72+
expectExit: true,
73+
expectPrintVersion: appName + " v0.0.0-custom",
74+
},
75+
{
76+
name: "invalid override semver",
77+
flags: []string{"--version=vX"},
78+
expectError: `could not parse "vX"`,
79+
},
80+
{
81+
name: "invalid override major",
82+
flags: []string{"--version=v1.0.0"},
83+
expectError: `must match major/minor/patch`,
84+
},
85+
{
86+
name: "invalid override minor",
87+
flags: []string{"--version=v0.1.0"},
88+
expectError: `must match major/minor/patch`,
89+
},
90+
{
91+
name: "invalid override patch",
92+
flags: []string{"--version=v0.0.1"},
93+
expectError: `must match major/minor/patch`,
94+
},
95+
}
96+
97+
for _, tc := range testcases {
98+
t.Run(tc.name, func(t *testing.T) {
99+
100+
originalOutput := output
101+
originalExit := exit
102+
103+
outputBuffer := &bytes.Buffer{}
104+
output = outputBuffer
105+
exitCalled := false
106+
exit = func(code int) { exitCalled = true }
107+
108+
t.Cleanup(func() {
109+
output = originalOutput
110+
exit = originalExit
111+
*versionFlag = versionValue(initialFlagValue)
112+
err := SetDynamicVersion(initialVersion.GitVersion)
113+
if err != nil {
114+
t.Fatal(err)
115+
}
116+
})
117+
118+
fs := flag.NewFlagSet("test", flag.ContinueOnError)
119+
AddFlags(fs)
120+
err := fs.Parse(tc.flags)
121+
if tc.expectError != "" {
122+
if err == nil {
123+
t.Fatal("expected error, got none")
124+
}
125+
if !strings.Contains(err.Error(), tc.expectError) {
126+
t.Fatalf("expected error containing %q, got %q", tc.expectError, err.Error())
127+
}
128+
return
129+
} else if err != nil {
130+
t.Fatalf("unexpected parse error: %v", err)
131+
}
132+
133+
if e, a := tc.expectGitVersion, Get().GitVersion; e != a {
134+
t.Fatalf("gitversion: expected %v, got %v", e, a)
135+
}
136+
137+
PrintAndExitIfRequested(appName)
138+
if e, a := tc.expectExit, exitCalled; e != a {
139+
t.Fatalf("exit(): expected %v, got %v", e, a)
140+
}
141+
if e, a := tc.expectPrintVersion, strings.TrimSpace(outputBuffer.String()); e != a {
142+
t.Fatalf("print version: expected %v, got %v", e, a)
143+
}
144+
})
145+
}
146+
}

pkg/version/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func Get() Info {
8282
// These variables typically come from -ldflags settings and in
8383
// their absence fallback to the settings in pkg/version/base.go
8484
return Info{
85-
GitVersion: gitVersion,
85+
GitVersion: dynamicGitVersion.Load().(string),
8686
GitCommit: gitCommit,
8787
GitTreeState: gitTreeState,
8888
BuildDate: buildDate,

0 commit comments

Comments
 (0)