-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathvert_test.go
128 lines (115 loc) · 2.7 KB
/
vert_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
package main
import (
"bytes"
"testing"
"github.com/urfave/cli"
)
type mockContext struct {
bools map[string]bool
args cli.Args
}
func (c *mockContext) Args() cli.Args {
return c.args
}
func (c *mockContext) Bool(name string) bool {
return c.bools[name]
}
func TestGit2semver(t *testing.T) {
tests := map[string]string{
"v1.10.0-123-g0239788": "1.10.0+123.g0239788",
"1.10.0-123-g0239788": "1.10.0+123.g0239788",
}
for in, expect := range tests {
out, err := git2semver(in)
if err != nil {
t.Errorf("Failed to parse %s: %s", in, err)
continue
}
if out.String() != expect {
t.Errorf("Expected %q, got %q", expect, out.String())
}
}
// And test a failure
in := "fatal: No names found, cannot describe anything."
out, err := git2semver(in)
if err == nil {
t.Errorf("Expected version parse to fail for %q", in)
}
if out != nil {
t.Errorf("Expected version to be nil for %q", in)
}
}
func TestRun(t *testing.T) {
var b bytes.Buffer
c := &mockContext{
args: cli.Args{">=1.0.0", "1.0.0", "1.1.1", "1.2.3", "1.0.1", "0.9.0"},
bools: map[string]bool{
"failed": false,
"sort": true,
},
}
// Set the package defaults
stdout = &b
stderr = &b
tests := []struct {
args cli.Args
bools map[string]bool
out string
code int
}{
// Base case.
{
args: cli.Args{"v1.0.0", "1.0.0"},
bools: map[string]bool{"failed": false, "sort": false},
code: 0,
out: "1.0.0\n",
},
// One failure, four passes, sorted.
{
args: cli.Args{">=1.0.0", "1.0.0", "1.1.1", "1.2.3", "1.0.1", "0.9.0"},
bools: map[string]bool{"failed": false, "sort": true},
code: 1,
out: "1.0.0\n1.0.1\n1.1.1\n1.2.3\n",
},
// One failure, four passes, unsorted.
{
args: cli.Args{">=1.0.0", "1.0.0", "1.1.1", "1.2.3", "1.0.1", "0.9.0"},
bools: map[string]bool{"failed": false, "sort": false},
code: 1,
out: "1.0.0\n1.1.1\n1.2.3\n1.0.1\n",
},
// One failure, print failures.
{
args: cli.Args{">=1.0.0", "1.0.0", "1.1.1", "1.2.3", "1.0.1", "0.9.0"},
bools: map[string]bool{"failed": true, "sort": true},
code: 1,
out: "0.9.0\n",
},
// Two failures, sorted.
{
args: cli.Args{">=1.0.0", "0.1", "v0.9.0"},
bools: map[string]bool{"failed": true, "sort": true},
code: 2,
out: "0.1.0\n0.9.0\n",
},
// Convert git tag
{
args: cli.Args{">1", "v1.10.0-123-g0239788"},
bools: map[string]bool{"git": true},
code: 0,
out: "1.10.0+123.g0239788\n",
},
}
for _, tt := range tests {
c.args = tt.args
c.bools = tt.bools
res := run(c)
if res != tt.code {
t.Errorf("Expected code %d, got %d", tt.code, res)
}
if b.String() != tt.out {
t.Errorf("Expected:%s\nGot:%s", tt.out, b.String())
}
b.Reset()
}
}