-
Notifications
You must be signed in to change notification settings - Fork 1
/
eagolint_test.go
123 lines (111 loc) · 4.09 KB
/
eagolint_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 eagolint_test
import (
"bytes"
"os"
"path/filepath"
"regexp"
"testing"
"github.com/Ullaakut/eagolint"
"io/ioutil"
)
func TestShouldSkipDirs(t *testing.T) {
skip, err := eagolint.ShouldSkip(".git", true, []string{".git"}, false, false)
if skip == false || err != filepath.SkipDir {
t.Errorf("Expected %t, %s got. %t, %s", true, filepath.SkipDir, skip, err)
}
skip, err = eagolint.ShouldSkip("dir", true, []string{".git"}, false, false)
if skip == false || err != nil {
t.Errorf("Expected %t, %v got. %t, %s", true, nil, skip, err)
}
}
func TestShouldSkipFiles(t *testing.T) {
t.Run("regular files", func(t *testing.T) {
binaryFilePath, _ := os.Executable()
tests := []struct {
path string
goOnly bool
skipTests bool
shouldSkip bool
err error
}{
{path: "eagolint.go", goOnly: false, skipTests: false, shouldSkip: false},
{path: "eagolint.go", goOnly: true, skipTests: false, shouldSkip: false},
{path: "eagolint.go", goOnly: false, skipTests: true, shouldSkip: false},
{path: "eagolint.go", goOnly: true, skipTests: true, shouldSkip: false},
{path: "README.md", goOnly: false, skipTests: false, shouldSkip: false},
{path: "README.md", goOnly: true, skipTests: false, shouldSkip: true},
{path: "README.md", goOnly: false, skipTests: true, shouldSkip: false},
{path: "README.md", goOnly: true, skipTests: true, shouldSkip: true},
{path: "eagolint_test.go", goOnly: false, skipTests: false, shouldSkip: false},
{path: "eagolint_test.go", goOnly: true, skipTests: false, shouldSkip: false},
{path: "eagolint_test.go", goOnly: false, skipTests: true, shouldSkip: true},
{path: "eagolint_test.go", goOnly: true, skipTests: true, shouldSkip: true},
{path: binaryFilePath, goOnly: false, skipTests: false, shouldSkip: true},
{path: binaryFilePath, goOnly: true, skipTests: false, shouldSkip: true},
{path: binaryFilePath, goOnly: false, skipTests: true, shouldSkip: true},
{path: binaryFilePath, goOnly: true, skipTests: true, shouldSkip: true},
}
for i, tc := range tests {
skip, err := eagolint.ShouldSkip(tc.path, false, []string{".git"}, tc.goOnly, tc.skipTests)
if skip != tc.shouldSkip || err != tc.err {
t.Errorf("%d) Expected %t, %v got %t, %s", i+1, tc.shouldSkip, tc.err, skip, err)
}
}
})
t.Run("file in skiplist", func(t *testing.T) {
skip, err := eagolint.ShouldSkip("file", false, []string{"file"}, false, false)
if skip != true || err != nil {
t.Errorf("Expected %t, %v got. %t, %s", true, nil, skip, err)
}
})
t.Run("error on file not found", func(t *testing.T) {
skip, err := eagolint.ShouldSkip("file", false, []string{".git"}, false, false)
if skip != true || err == nil {
t.Errorf("Expected %t, %v got. %t, %s", true, nil, skip, err)
}
})
}
func TestProcess(t *testing.T) {
content, err := ioutil.ReadFile("test_assets/bad_comments.go")
if err != nil {
t.Fatal(err)
}
b := &bytes.Buffer{}
err = eagolint.Process(bytes.NewBuffer(content), b, "file", nil)
if err != nil {
t.Errorf("Expected %v, got %s", nil, err)
}
expected := `file:6: double space typo in comment
file:8: double space typo in comment
file:12: missing punctuation at end of comment
file:14: missing punctuation at end of comment
file:20: double space typo in comment
file:21: double space typo in comment
file:25: missing punctuation at end of comment
file:31: double space typo in comment
`
if b.String() != expected {
t.Errorf("Expected %s, got %s", expected, b.String())
}
}
func TestProcessFile(t *testing.T) {
b := &bytes.Buffer{}
err := eagolint.ProcessFile(b, "eagolint_test.go", nil)
if err != nil {
t.Errorf("Expected %v, got %s", nil, err)
}
}
func TestProcessExclude(t *testing.T) {
lines := ` // TODO: fix
// FIXME: do something
// This is a non-excluded comment with issues`
b := &bytes.Buffer{}
exclude := regexp.MustCompile("TODO|FIXME")
expected := `file:3: missing punctuation at end of comment
file:3: double space typo in comment
`
_ = eagolint.Process(bytes.NewBufferString(lines), b, "file", exclude)
if b.String() != expected {
t.Errorf("Expected %s, got %s", expected, b.String())
}
}