forked from reviewdog/reviewdog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reviewdog_test.go
174 lines (150 loc) · 4.81 KB
/
reviewdog_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
165
166
167
168
169
170
171
172
173
174
package reviewdog
import (
"context"
"os"
"strings"
"testing"
"github.com/reviewdog/errorformat"
"github.com/reviewdog/reviewdog/filter"
"github.com/reviewdog/reviewdog/parser"
)
var _ CommentService = &testWriter{}
type testWriter struct {
FakePost func(c *Comment) error
}
func (s *testWriter) Post(_ context.Context, c *Comment) error {
return s.FakePost(c)
}
func ExampleReviewdog() {
difftext := `diff --git a/golint.old.go b/golint.new.go
index 34cacb9..a727dd3 100644
--- a/golint.old.go
+++ b/golint.new.go
@@ -2,6 +2,12 @@ package test
var V int
+var NewError1 int
+
// invalid func comment
func F() {
}
+
+// invalid func comment2
+func F2() {
+}
`
lintresult := `golint.new.go:3:5: exported var V should have comment or be unexported
golint.new.go:5:5: exported var NewError1 should have comment or be unexported
golint.new.go:7:1: comment on exported function F should be of the form "F ..."
golint.new.go:11:1: comment on exported function F2 should be of the form "F2 ..."
`
efm, _ := errorformat.NewErrorformat([]string{`%f:%l:%c: %m`})
p := parser.NewErrorformatParser(efm)
c := NewRawCommentWriter(os.Stdout)
d := NewDiffString(difftext, 1)
app := NewReviewdog("tool name", p, c, d, filter.ModeAdded, false)
app.Run(context.Background(), strings.NewReader(lintresult))
// Unordered output:
// golint.new.go:5:5: exported var NewError1 should have comment or be unexported
// golint.new.go:11:1: comment on exported function F2 should be of the form "F2 ..."
}
func TestReviewdog_Run_clean_path(t *testing.T) {
difftext := `diff --git a/golint.old.go b/golint.new.go
index 34cacb9..a727dd3 100644
--- a/golint.old.go
+++ b/golint.new.go
@@ -2,6 +2,12 @@ package test
var V int
+var NewError1 int
+
// invalid func comment
func F() {
}
+
+// invalid func comment2
+func F2() {
+}
`
lintresult := `./golint.new.go:3:5: exported var V should have comment or be unexported
./golint.new.go:5:5: exported var NewError1 should have comment or be unexported
./golint.new.go:7:1: comment on exported function F should be of the form "F ..."
./golint.new.go:11:1: comment on exported function F2 should be of the form "F2 ..."
`
want := "golint.new.go"
c := &testWriter{
FakePost: func(c *Comment) error {
if got := c.Result.Diagnostic.GetLocation().GetPath(); got != want {
t.Errorf("path: got %v, want %v", got, want)
}
return nil
},
}
efm, _ := errorformat.NewErrorformat([]string{`%f:%l:%c: %m`})
p := parser.NewErrorformatParser(efm)
d := NewDiffString(difftext, 1)
app := NewReviewdog("tool name", p, c, d, filter.ModeAdded, false)
app.Run(context.Background(), strings.NewReader(lintresult))
}
func TestReviewdog_Run_returns_nil_if_fail_on_error_not_passed_and_some_errors_found(t *testing.T) {
difftext := `diff --git a/golint.old.go b/golint.new.go
index 34cacb9..a727dd3 100644
--- a/golint.old.go
+++ b/golint.new.go
@@ -2,6 +2,12 @@ package test
var V int
+var NewError1 int
+
// invalid func comment
func F() {
}
+
+// invalid func comment2
+func F2() {
+}
`
lintresult := `golint.new.go:3:5: exported var V should have comment or be unexported
golint.new.go:5:5: exported var NewError1 should have comment or be unexported
golint.new.go:7:1: comment on exported function F should be of the form "F ..."
golint.new.go:11:1: comment on exported function F2 should be of the form "F2 ..."
`
c := NewRawCommentWriter(os.Stdout)
efm, _ := errorformat.NewErrorformat([]string{`%f:%l:%c: %m`})
p := parser.NewErrorformatParser(efm)
d := NewDiffString(difftext, 1)
app := NewReviewdog("tool name", p, c, d, filter.ModeAdded, false)
err := app.Run(context.Background(), strings.NewReader(lintresult))
if err != nil {
t.Errorf("No errors expected, but got %v", err)
}
}
func TestReviewdog_Run_returns_error_if_fail_on_error_passed_and_some_errors_found(t *testing.T) {
difftext := `diff --git a/golint.old.go b/golint.new.go
index 34cacb9..a727dd3 100644
--- a/golint.old.go
+++ b/golint.new.go
@@ -2,6 +2,12 @@ package test
var V int
+var NewError1 int
+
// invalid func comment
func F() {
}
+
+// invalid func comment2
+func F2() {
+}
`
lintresult := `golint.new.go:3:5: exported var V should have comment or be unexported
golint.new.go:5:5: exported var NewError1 should have comment or be unexported
golint.new.go:7:1: comment on exported function F should be of the form "F ..."
golint.new.go:11:1: comment on exported function F2 should be of the form "F2 ..."
`
c := NewRawCommentWriter(os.Stdout)
efm, _ := errorformat.NewErrorformat([]string{`%f:%l:%c: %m`})
p := parser.NewErrorformatParser(efm)
d := NewDiffString(difftext, 1)
app := NewReviewdog("tool name", p, c, d, filter.ModeAdded, true)
err := app.Run(context.Background(), strings.NewReader(lintresult))
if err != nil && err.Error() != "input data has violations" {
t.Errorf("'input data has violations' expected, but got %v", err)
}
}