-
Notifications
You must be signed in to change notification settings - Fork 8
/
app_test.go
56 lines (48 loc) · 1.3 KB
/
app_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
package cmd
import (
"bytes"
"os"
"strings"
"testing"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
easy "github.com/t-tomalak/logrus-easy-formatter"
)
func TestDebugFlag(t *testing.T) {
args, out := setup("--debug", "version")
Version("test-version", "test-commit", "test-date")
assert.Nil(t, App.Run(args))
assert.True(t, strings.Contains(out.String(), "level=info msg=\"goodhosts test-version@test-commit built on test-date\""))
// reset logrus for other tests
logrus.SetLevel(logrus.InfoLevel)
logrus.SetFormatter(&easy.Formatter{
LogFormat: "%msg%",
})
}
func TestQuiet(t *testing.T) {
args, out := setup("--quiet", "version")
assert.Nil(t, App.Run(args))
assert.Empty(t, out.String())
}
// Setup takes args slice and resets logrus and returns args to pass to App.Run
func setup(args ...string) ([]string, *bytes.Buffer) {
out := &bytes.Buffer{}
logrus.SetOutput(out)
a := os.Args[0:1]
a = append(a, args...)
return a, out
}
func read(t *testing.T, name, file string) func() {
err := os.WriteFile(name, []byte(file), 0440)
assert.Nil(t, err)
return func() {
assert.Nil(t, os.Remove(name))
}
}
func write(t *testing.T, name, file string) func() {
err := os.WriteFile(name, []byte(file), 0770)
assert.Nil(t, err)
return func() {
assert.Nil(t, os.Remove(name))
}
}