-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcli_test.go
123 lines (106 loc) · 3.8 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
package main
import (
"bytes"
"fmt"
"os"
"testing"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
func TestCliListSuccess(t *testing.T) {
var out = &bytes.Buffer{}
log.SetOutput(out)
dir, rm := mockGoPackageDir(t, "TestCliListSuccess")
defer rm()
// Change working dir to test dir
err := os.Chdir(dir)
assert.NoError(t, err)
cases := []struct {
inArgs []string
outputWantNoColor []string
}{
{
[]string{"list"},
[]string{`level=info msg="Found License" license=BSD-3-Clause package=github.com/fake/package`, `level=info msg="Found License" license=BSD-3-Clause package=github.com/fake/nested/inside/a/package`},
},
{
[]string{"ls"},
[]string{`level=info msg="Found License" license=BSD-3-Clause package=github.com/fake/nested/inside/a/package`, `level=info msg="Found License" license=BSD-3-Clause package=github.com/fake/nested/inside/a/package`},
},
}
for _, c := range cases {
_, err = newCli().ParseArgs(append(c.inArgs, "--no-color"))
assert.NoError(t, err)
for _, want := range c.outputWantNoColor {
assert.Contains(t, out.String(), want)
}
out.Reset()
}
}
func TestCliQuiet(t *testing.T) {
initial := log.GetLevel()
defer log.SetLevel(initial)
err := setQuiet()
assert.NoError(t, err)
after := log.GetLevel()
assert.Equal(t, log.ErrorLevel, after)
}
func TestCliCheck(t *testing.T) {
var out = &bytes.Buffer{}
log.SetOutput(out)
dir, rm := mockGoPackageDir(t, "TestCliCheck")
defer rm()
// Change working dir to test dir
err := os.Chdir(dir)
assert.NoError(t, err)
cases := []struct {
inArgs []string
outputWantNoColor []string
err []error
}{
{
[]string{"check"},
[]string{`level=info msg="Found Approved license" license=BSD-3-Clause package=github.com/fake/package`, `level=info msg="Found Approved license" license=BSD-3-Clause package=github.com/fake/nested/inside/a/package`},
[]error{nil},
},
{
[]string{"check", "-f", ".wwhrd-ex.yml"},
[]string{`level=warning msg="Found exceptioned package" license=BSD-3-Clause package=github.com/fake/package`, `level=warning msg="Found exceptioned package" license=BSD-3-Clause package=github.com/fake/nested/inside/a/package`},
[]error{nil},
},
{
[]string{"check", "-f", ".wwhrd-exwc.yml"},
[]string{`level=warning msg="Found exceptioned package" license=BSD-3-Clause package=github.com/fake/package`, `level=warning msg="Found exceptioned package" license=BSD-3-Clause package=github.com/fake/nested/inside/a/package`},
[]error{nil},
},
{
[]string{"check", "-f", ".wwhrd-bl.yml"},
[]string{`level=error msg="Found Non-Approved license" license=BSD-3-Clause package=github.com/fake/package`, `level=error msg="Found Non-Approved license" license=BSD-3-Clause package=github.com/fake/nested/inside/a/package`},
[]error{fmt.Errorf("Non-Approved license found")},
},
{
[]string{"check", "-f", "NONEXISTENT"},
[]string{""},
[]error{
fmt.Errorf("can't read config file: stat NONEXISTENT: no such file or directory"),
fmt.Errorf("can't read config file: GetFileAttributesEx NONEXISTENT: The system cannot find the file specified."),
fmt.Errorf("can't read config file: CreateFile NONEXISTENT: The system cannot find the file specified."),
fmt.Errorf("can't read config file: FindFirstFile NONEXISTENT: The system cannot find the file specified."),
},
},
{
[]string{"check", "-f", ".wwhrd-botched.yml"},
[]string{""},
[]error{fmt.Errorf(`can't read config file: yaml: unmarshal errors:
line 2: cannot unmarshal !!str ` + "`whiteli...`" + ` into main.Config`)},
},
}
for _, c := range cases {
_, err = newCli().ParseArgs(append(c.inArgs, "--no-color"))
assert.Contains(t, c.err, err)
for _, want := range c.outputWantNoColor {
assert.Contains(t, out.String(), want)
}
out.Reset()
}
}