-
-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathversion_test.go
More file actions
126 lines (113 loc) · 2.58 KB
/
version_test.go
File metadata and controls
126 lines (113 loc) · 2.58 KB
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
package main
import (
"bytes"
"runtime/debug"
"testing"
"github.com/alecthomas/kong"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.abhg.dev/testing/stub"
)
func TestVersionFlag(t *testing.T) {
defer stub.Func(&_generateBuildReport, "commithash timestamp")()
var (
exitCode int
stdout bytes.Buffer
)
_ = versionFlag(true).BeforeReset(&kong.Kong{
Stdout: &stdout,
Exit: func(code int) {
exitCode = code
},
})
assert.Zero(t, exitCode)
assert.Contains(t, stdout.String(), "git-spice "+_version)
assert.Contains(t, stdout.String(), "(commithash timestamp)")
}
func TestVersionCmd(t *testing.T) {
defer stub.Value(&_version, "v1.2.3")()
t.Run("Default", func(t *testing.T) {
var stdout bytes.Buffer
err := new(versionCmd).Run(&kong.Kong{
Stdout: &stdout,
})
require.NoError(t, err)
assert.Contains(t, stdout.String(), "git-spice v1.2.3")
})
t.Run("Short", func(t *testing.T) {
var stdout bytes.Buffer
err := (&versionCmd{Short: true}).Run(&kong.Kong{
Stdout: &stdout,
})
require.NoError(t, err)
assert.Equal(t, "v1.2.3\n", stdout.String())
})
}
func TestGenerateBuildReport(t *testing.T) {
tests := []struct {
name string
give *debug.BuildInfo
want string
}{
{
name: "NoBuildInfo",
give: &debug.BuildInfo{},
},
{
name: "Revision",
give: &debug.BuildInfo{
Settings: []debug.BuildSetting{
{Key: "vcs.revision", Value: "commithash"},
},
},
want: "commithash",
},
{
name: "RevisionDirty",
give: &debug.BuildInfo{
Settings: []debug.BuildSetting{
{Key: "vcs.revision", Value: "commithash"},
{Key: "vcs.modified", Value: "true"},
},
},
want: "commithash-dirty",
},
{
name: "TimeOnly",
give: &debug.BuildInfo{
Settings: []debug.BuildSetting{
{Key: "vcs.time", Value: "timestamp"},
},
},
want: "timestamp",
},
{
name: "RevisionAndTime",
give: &debug.BuildInfo{
Settings: []debug.BuildSetting{
{Key: "vcs.revision", Value: "commithash"},
{Key: "vcs.time", Value: "timestamp"},
},
},
want: "commithash timestamp",
},
{
name: "RevisionDirtyAndTime",
give: &debug.BuildInfo{
Settings: []debug.BuildSetting{
{Key: "vcs.revision", Value: "commithash"},
{Key: "vcs.modified", Value: "true"},
{Key: "vcs.time", Value: "timestamp"},
},
},
want: "commithash-dirty timestamp",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer stub.Func(&_debugReadBuildInfo, tt.give, true)()
got := _generateBuildReport()
assert.Equal(t, tt.want, got)
})
}
}