-
Notifications
You must be signed in to change notification settings - Fork 4
/
cli_test.go
143 lines (130 loc) Β· 4.73 KB
/
cli_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package semv
import (
"bytes"
"testing"
)
func TestCLIRun(t *testing.T) {
ver := `git-semv version dev [none, unknown]
`
help := `
Usage: git-semv [--version] [--help] command <options>
Commands:
list Sorted versions
now, latest Latest version
major Next major version: vX.0.0
minor Next minor version: v0.X.0
patch Next patch version: v0.0.X
Options:
-p, --pre Pre-Release version indicates(ex: 0.0.1-rc.0)
-b, --build Build version indicates(ex: 0.0.1+3222d31.foo)
--build-name Specify build version name
-a, --all Include everything such as pre-release and build versions in list
-B, --bump Create tag and Push to origin
-x, --prefix Prefix for version and tag(default: v)
-h, --help Show this help message and exit
-v, --version Prints the version number
`
unknownFlag := "Error: unknown flag `unknown'\n"
unknownCmd := `Error: command is not available: unknown
`
semvers := `v8.8.8
v12.0.1
v12.344.0+20130313144700
v12.345.66
v12.345.67
`
semversWithPre := `v2.3.4-rc.2
v8.8.8
v12.0.1
v12.3.0-alpha
v12.3.0-alpha.0
v12.3.0-alpha.1
v12.3.0-alpha.1.beta
v12.3.0-beta
v12.3.0-beta.5
v12.3.0-rc
v12.344.0+20130313144700
v12.345.66
v12.345.67
v13.0.0-alpha.0
`
tests := []struct {
cmd []string
wantO []byte
wantE []byte
wantS int
}{
// list
{[]string{}, []byte(semvers), []byte(""), ExitOK},
{[]string{"list"}, []byte(semvers), []byte(""), ExitOK},
{[]string{"-a"}, []byte(semversWithPre), []byte(""), ExitOK},
{[]string{"--all"}, []byte(semversWithPre), []byte(""), ExitOK},
{[]string{"list", "-a"}, []byte(semversWithPre), []byte(""), ExitOK},
{[]string{"list", "--all"}, []byte(semversWithPre), []byte(""), ExitOK},
// now
{[]string{"now"}, []byte("v12.345.67\n"), []byte(""), ExitOK},
{[]string{"latest"}, []byte("v12.345.67\n"), []byte(""), ExitOK},
// next
{[]string{"major"}, []byte("v13.0.0\n"), []byte(""), ExitOK},
{[]string{"minor"}, []byte("v12.346.0\n"), []byte(""), ExitOK},
{[]string{"patch"}, []byte("v12.345.68\n"), []byte(""), ExitOK},
// pre
{[]string{"major", "-p"}, []byte("v13.0.0-alpha.1\n"), []byte(""), ExitOK},
{[]string{"major", "--pre"}, []byte("v13.0.0-alpha.1\n"), []byte(""), ExitOK},
{[]string{"minor", "--pre"}, []byte("v12.346.0-alpha.0\n"), []byte(""), ExitOK},
{[]string{"patch", "--pre"}, []byte("v12.345.68-alpha.0\n"), []byte(""), ExitOK},
{[]string{"major", "--pre-name", "rc"}, []byte("v13.0.0-rc.0\n"), []byte(""), ExitOK},
// build
{[]string{"major", "-b"}, []byte("v13.0.0+2f994ff.foobar\n"), []byte(""), ExitOK},
{[]string{"major", "--build"}, []byte("v13.0.0+2f994ff.foobar\n"), []byte(""), ExitOK},
{[]string{"minor", "--build"}, []byte("v12.346.0+2f994ff.foobar\n"), []byte(""), ExitOK},
{[]string{"patch", "--build"}, []byte("v12.345.68+2f994ff.foobar\n"), []byte(""), ExitOK},
{[]string{"major", "--build-name", "baz"}, []byte("v13.0.0+baz\n"), []byte(""), ExitOK},
// bump
{[]string{"major", "--bump"}, []byte("Bumped version to v13.0.0\n"), []byte(""), ExitOK},
// options
{[]string{"-h"}, []byte(help), []byte(""), ExitErr},
{[]string{"--help"}, []byte(help), []byte(""), ExitErr},
{[]string{"-v"}, []byte(""), []byte(ver), ExitOK},
{[]string{"--version"}, []byte(""), []byte(ver), ExitOK},
// unknown
{[]string{"--unknown=abc"}, []byte(""), []byte(unknownFlag), ExitErr},
{[]string{"unknown"}, []byte(help), []byte(unknownCmd), ExitErr},
}
tagCmder = MockedCmd{Out: mixed}
usernameCmder = MockedCmd{Out: "foobar\n", Err: ""}
latestCommitCmder = MockedCmd{Out: "2f994ff\n", Err: ""}
gitTagCmder = MockedCmd{Out: "", Err: ""}
gitPushTagCmder = MockedCmd{Out: "", Err: ""}
for i, tt := range tests {
out, err := new(bytes.Buffer), new(bytes.Buffer)
env := Env{Out: out, Err: err, Args: tt.cmd, Version: "dev", Commit: "none", Date: "unknown"}
cli := &cli{env: env}
status := cli.run()
if status != tt.wantS {
t.Errorf("test[%d]: status = %d; want %d", i, status, tt.wantS)
}
if !bytes.Equal(tt.wantO, out.Bytes()) {
t.Errorf("test[%d]: stdout = %s; want %s", i, out, tt.wantO)
}
if !bytes.Equal(tt.wantE, err.Bytes()) {
t.Errorf("test[%d]: stderr = %s; want %s", i, err, tt.wantE)
}
}
}
func TestRunCLI(t *testing.T) {
ver := "git-semv version dev [none, unknown]\n"
out, err := new(bytes.Buffer), new(bytes.Buffer)
args := []string{"-v"}
env := Env{Out: out, Err: err, Args: args, Version: "dev", Commit: "none", Date: "unknown"}
status := RunCLI(env)
if status != 0 {
t.Errorf("exit status = %d; want 0", status)
}
if !bytes.Equal([]byte(""), out.Bytes()) {
t.Errorf("output = %s; want empty", out)
}
if !bytes.Equal([]byte(ver), err.Bytes()) {
t.Errorf("err = %v; want %s", err, ver)
}
}