-
Notifications
You must be signed in to change notification settings - Fork 51
/
lint_test.go
88 lines (81 loc) · 2.34 KB
/
lint_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
package lib_test
import (
"bytes"
"errors"
"regexp"
"testing"
"github.com/yoheimuta/protolint/internal/setting_test"
"github.com/yoheimuta/protolint/lib"
)
func TestLint(t *testing.T) {
tests := []struct {
name string
inputArgs []string
wantStdoutRegex *regexp.Regexp
wantStderrRegex *regexp.Regexp
wantError error
}{
{
name: "no args",
wantStderrRegex: regexp.MustCompile(`[\S\s]*Usage:[\S\s]*protolint <command> \[arguments\][\S\s]*`),
wantError: lib.ErrInternalFailure,
},
{
name: "invalid args",
inputArgs: []string{
"-config_path",
setting_test.TestDataPath("lib", "not_exist.yaml"),
setting_test.TestDataPath("lib", "valid.proto"),
},
wantStderrRegex: regexp.MustCompile(`[\S\s]*not_exist.yaml: no such file or directory`),
wantError: lib.ErrInternalFailure,
},
{
name: "lint failures",
inputArgs: []string{
setting_test.TestDataPath("lib", "invalid.proto"),
},
wantStderrRegex: regexp.MustCompile(`[\S\s]*Found an incorrect indentation style[\S\s]*`),
wantError: lib.ErrLintFailure,
},
{
name: "lint success",
inputArgs: []string{
setting_test.TestDataPath("lib", "valid.proto"),
},
},
{
name: "lint success by specifying a config file",
inputArgs: []string{
"-config_path",
setting_test.TestDataPath("lib", ".protolint.yaml"),
setting_test.TestDataPath("lib", "invalid.proto"),
},
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
var stdout bytes.Buffer
var stderr bytes.Buffer
err := lib.Lint(test.inputArgs, &stdout, &stderr)
if !errors.Is(err, test.wantError) {
t.Errorf("got err %v, but want err %v", err, test.wantError)
}
if test.wantStdoutRegex != nil {
if !test.wantStdoutRegex.MatchString(stdout.String()) {
t.Errorf("got stdout %s, but want to match %v", stdout.String(), test.wantStdoutRegex)
}
} else if stdout.Len() > 0 {
t.Errorf("got stdout %s, but want empty stdout", stdout.String())
}
if test.wantStderrRegex != nil {
if !test.wantStderrRegex.MatchString(stderr.String()) {
t.Errorf("got stderr %s, but want to match %v", stderr.String(), test.wantStderrRegex)
}
} else if stderr.Len() > 0 {
t.Errorf("got stderr %s, but want empty stderr", stderr.String())
}
})
}
}