forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversion_test.go
164 lines (134 loc) · 4.17 KB
/
version_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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package version_test
import (
"encoding/json"
"fmt"
"runtime"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/testutil"
"github.com/cosmos/cosmos-sdk/testutil/cmdtest"
"github.com/cosmos/cosmos-sdk/version"
)
func TestNewInfo(t *testing.T) {
info := version.NewInfo()
want := fmt.Sprintf(`:
git commit:
build tags:
%s`, fmt.Sprintf("go version %s %s/%s", runtime.Version(), runtime.GOOS, runtime.GOARCH))
require.Equal(t, want, info.String())
}
func TestInfo_String(t *testing.T) {
info := version.Info{
Name: "testapp",
AppName: "testappd",
Version: "1.0.0",
GitCommit: "1b78457135a4104bc3af97f20654d49e2ea87454",
BuildTags: "netgo,ledger",
GoVersion: "go version go1.14 linux/amd64",
CosmosSdkVersion: "0.42.5",
}
want := `testapp: 1.0.0
git commit: 1b78457135a4104bc3af97f20654d49e2ea87454
build tags: netgo,ledger
go version go1.14 linux/amd64`
require.Equal(t, want, info.String())
}
func TestCLI(t *testing.T) {
setVersionPackageVars(t)
sys := cmdtest.NewSystem()
sys.AddCommands(version.NewVersionCommand())
t.Run("no flags", func(t *testing.T) {
res := sys.MustRun(t, "version")
// Only prints the version, with a newline, to stdout.
require.Equal(t, testVersion+"\n", res.Stdout.String())
require.Empty(t, res.Stderr.String())
})
t.Run("--long flag", func(t *testing.T) {
res := sys.MustRun(t, "version", "--long")
out := res.Stdout.String()
lines := strings.Split(out, "\n")
require.Contains(t, lines, "name: testchain-app")
require.Contains(t, lines, "server_name: testchaind")
require.Contains(t, lines, `version: "3.14"`)
require.Contains(t, lines, "commit: abc123")
require.Contains(t, lines, "build_tags: mybuildtag")
require.Empty(t, res.Stderr.String())
})
t.Run("--output=json flag", func(t *testing.T) {
res := sys.MustRun(t, "version", "--output=json")
var info version.Info
require.NoError(t, json.Unmarshal(res.Stdout.Bytes(), &info))
// Assert against a couple fields that are difficult to predict in test
// without copying and pasting code.
require.NotEmpty(t, info.GoVersion)
// The SDK version appears to not be set during this test, so we'll ignore it here.
// Now clear out the non-empty fields, so we can compare against a fixed value.
info.GoVersion = ""
want := version.Info{
Name: testName,
AppName: testAppName,
Version: testVersion,
GitCommit: testCommit,
BuildTags: testBuildTags,
}
require.Equal(t, want, info)
require.Empty(t, res.Stderr.String())
})
t.Run("positional args rejected", func(t *testing.T) {
res := sys.Run("version", "foo")
require.Error(t, res.Err)
})
}
const (
testName = "testchain-app"
testAppName = "testchaind"
testVersion = "3.14"
testCommit = "abc123"
testBuildTags = "mybuildtag"
)
// setVersionPackageVars temporarily overrides the package variables in the version package
// so that we can assert meaningful output.
func setVersionPackageVars(t *testing.T) {
t.Helper()
var (
origName = version.Name
origAppName = version.AppName
origVersion = version.Version
origCommit = version.Commit
origBuildTags = version.BuildTags
)
t.Cleanup(func() {
version.Name = origName
version.AppName = origAppName
version.Version = origVersion
version.Commit = origCommit
version.BuildTags = origBuildTags
})
version.Name = testName
version.AppName = testAppName
version.Version = testVersion
version.Commit = testCommit
version.BuildTags = testBuildTags
}
func Test_runVersionCmd(t *testing.T) {
cmd := version.NewVersionCommand()
_, mockOut := testutil.ApplyMockIO(cmd)
cmd.SetArgs([]string{
fmt.Sprintf("--%s=''", flags.FlagOutput),
"--long=false",
})
require.NoError(t, cmd.Execute())
assert.Equal(t, "\n", mockOut.String())
mockOut.Reset()
cmd.SetArgs([]string{
fmt.Sprintf("--%s=json", flags.FlagOutput), "--long=true",
})
info := version.NewInfo()
stringInfo, err := json.Marshal(info)
require.NoError(t, err)
require.NoError(t, cmd.Execute())
assert.Equal(t, string(stringInfo)+"\n", mockOut.String())
}