forked from alecthomas/gometalinter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linters_test.go
176 lines (154 loc) · 5.11 KB
/
linters_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
175
176
package main
import (
"reflect"
"regexp"
"runtime"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNewLinterWithCustomLinter(t *testing.T) {
config := LinterConfig{
Command: "/usr/bin/custom",
Pattern: "path",
}
linter, err := NewLinter("thename", config)
require.NoError(t, err)
assert.Equal(t, functionName(partitionPathsAsDirectories), functionName(linter.LinterConfig.PartitionStrategy))
assert.Equal(t, "(?m:path)", linter.regex.String())
assert.Equal(t, "thename", linter.Name)
assert.Equal(t, config.Command, linter.Command)
}
func TestGetLinterByName(t *testing.T) {
config := LinterConfig{
Command: "maligned",
Pattern: "path",
InstallFrom: "./install/path",
PartitionStrategy: partitionPathsAsDirectories,
IsFast: true,
}
overrideConfig := getLinterByName(config.Command, config)
assert.Equal(t, config.Command, overrideConfig.Command)
assert.Equal(t, config.Pattern, overrideConfig.Pattern)
assert.Equal(t, config.InstallFrom, overrideConfig.InstallFrom)
assert.Equal(t, functionName(config.PartitionStrategy), functionName(overrideConfig.PartitionStrategy))
assert.Equal(t, config.IsFast, overrideConfig.IsFast)
}
func TestValidateLinters(t *testing.T) {
originalConfig := *config
defer func() { config = &originalConfig }()
config = &Config{
Enable: []string{"_dummylinter_"},
}
err := validateLinters(lintersFromConfig(config), config)
require.Error(t, err, "expected unknown linter error for _dummylinter_")
config = &Config{
Enable: defaultEnabled(),
}
err = validateLinters(lintersFromConfig(config), config)
require.NoError(t, err)
}
func TestLinter_test(t *testing.T) {
exampleOutput := `--- FAIL: TestHello (0.00s)
other_test.go:11:
Error Trace: other_test.go:11
Error: Not equal:
expected: "This is not"
actual : "equal to this"
Diff:
--- Expected
+++ Actual
@@ -1 +1 @@
-This is not
+equal to this
Test: TestHello
other_test.go:12: this should fail
other_test.go:13: fail again
other_test.go:14: last fail
other_test.go:15:
other_test.go:16:
require.go:1159:
Error Trace: other_test.go:17
Error: Should be true
Test: TestHello
FAIL
FAIL test 0.003s`
pattern := regexp.MustCompile(defaultLinters["test"].Pattern)
matches := pattern.FindAllStringSubmatch(exampleOutput, -1)
var errors []map[string]string
for _, match := range matches {
m := make(map[string]string)
for i, name := range pattern.SubexpNames() {
if i != 0 && name != "" {
m[name] = string(match[i])
}
}
errors = append(errors, m)
}
// Assert expected errors
assert.Equal(t, "other_test.go", errors[0]["path"])
assert.Equal(t, "12", errors[0]["line"])
assert.Equal(t, "this should fail", errors[0]["message"])
assert.Equal(t, "other_test.go", errors[1]["path"])
assert.Equal(t, "13", errors[1]["line"])
assert.Equal(t, "fail again", errors[1]["message"])
assert.Equal(t, "other_test.go", errors[2]["path"])
assert.Equal(t, "14", errors[2]["line"])
assert.Equal(t, "last fail", errors[2]["message"])
assert.Equal(t, "other_test.go", errors[3]["path"])
assert.Equal(t, "15", errors[3]["line"])
assert.Equal(t, " ", errors[3]["message"])
// Go metalinter does not support errors without a message as there is little or no output to parse
// E.g. t.Fail() or t.Error("")
// assert.Equal(t, "other_test.go", errors[5]["path"])
// assert.Equal(t, "15", errors[5]["line"])
// assert.Equal(t, "", errors[5]["message"])
}
func TestLinter_testify(t *testing.T) {
exampleOutput := `--- FAIL: TestHello (0.00s)
other_test.go:11:
Error Trace: other_test.go:11
Error: Not equal:
expected: "This is not"
actual : "equal to this"
Diff:
--- Expected
+++ Actual
@@ -1 +1 @@
-This is not
+equal to this
Test: TestHello
other_test.go:12: this should fail
other_test.go:13: fail again
other_test.go:14: last fail
other_test.go:15:
other_test.go:16:
require.go:1159:
Error Trace: other_test.go:17
Error: Should be true
Test: TestHello
FAIL
FAIL test 0.003s`
pattern := regexp.MustCompile(defaultLinters["testify"].Pattern)
matches := pattern.FindAllStringSubmatch(exampleOutput, -1)
var errors []map[string]string
for _, match := range matches {
m := make(map[string]string)
for i, name := range pattern.SubexpNames() {
if i != 0 && name != "" {
m[name] = string(match[i])
}
}
errors = append(errors, m)
}
// Assert expected errors
assert.Equal(t, "other_test.go", errors[0]["path"])
assert.Equal(t, "11", errors[0]["line"])
assert.Equal(t, "Not equal", errors[0]["message"])
assert.Equal(t, "other_test.go", errors[1]["path"])
assert.Equal(t, "17", errors[1]["line"])
assert.Equal(t, "Should be true", errors[1]["message"])
}
func functionName(i interface{}) string {
return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
}