-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsif_test.go
145 lines (126 loc) · 3.32 KB
/
sif_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
package sif
import (
"fmt"
"testing"
)
func TestScanFileLineNumbers(t *testing.T) {
var scanFileTests = []struct {
filename string
pattern string
lineNumbers []int
}{
{"python.txt", "better", []int{3, 4, 5, 6, 7, 8, 17, 18}},
{"golang.txt", "interface", []int{5, 7}},
{"subdir/hello.go", "World", []int{6}},
}
for _, tc := range scanFileTests {
s, err := New(tc.pattern, Options{false})
if err != nil {
t.Fatalf("Error on create sif %s", err)
}
filename := fmt.Sprintf("./_tests/%s", tc.filename)
fm, err := s.ScanFile(filename)
if err != nil {
t.Fatalf("Error on scan file %s: %s", tc.filename, err)
}
if fm == nil {
t.Fatalf("Does not found pattern %s in file %s", tc.pattern, tc.filename)
}
for i, match := range fm.Matches {
expected := tc.lineNumbers[i]
actual := match.Line
if expected != actual {
t.Errorf("expected %d, got %d", expected, actual)
}
}
}
}
func TestScanFileCaseInsensitive(t *testing.T) {
s, err := New("world", Options{CaseInsensitive: true})
if err != nil {
t.Fatalf("Error on create sif %s", err)
}
fm, err := s.ScanFile("./_tests/subdir/hello.go")
if err != nil {
t.Fatalf("Error on scan file %s", err)
}
if fm == nil {
t.Fatal("Does not found pattern in file")
}
if fm.Matches[0].Line != 6 {
t.Errorf("expected 6, got %d", fm.Matches[0].Line)
}
}
func TestDontPanicWithInvalidPatterns(t *testing.T) {
_, err := New("[", Options{false})
if err == nil {
t.Fatalf("Expected error for an invalid pattern")
}
}
func TestScanFileText(t *testing.T) {
s, err := New("import", Options{false})
if err != nil {
t.Fatalf("Error on create sif %s", err)
}
expected := s.pattern.ReplaceAllStringFunc(`import "fmt"`, bgYellow)
fm, err := s.ScanFile("./_tests/subdir/hello.go")
if err != nil {
t.Fatalf("Error on scan file %s", err)
}
if len(fm.Matches) != 1 {
t.Errorf("expected 1, got %d", len(fm.Matches))
}
if fm.Matches[0].Text != expected {
t.Errorf("expected %s, got %s", expected, fm.Matches[0].Text)
}
}
func TestScanDir(t *testing.T) {
expectedFiles := []string{"golang.txt", "subdir/hello.go"}
s, err := New("fmt", Options{false})
if err != nil {
t.Fatalf("Error on create sif %s", err)
}
files, err := s.ScanDir("./_tests")
if err != nil {
t.Fatalf("Error on scan dir %s", err)
}
if len(files) != 2 {
t.Errorf("expected 2, got %d", len(files))
}
for i, f := range files {
expected := fmt.Sprintf("_tests/%s", expectedFiles[i])
if f.Name != expected {
t.Errorf("expected %s, got %s", expected, f.Name)
}
}
}
func TestScan(t *testing.T) {
var scanTests = []struct {
pattern string
path string
expectedFiles []string
}{
{"fmt", "golang.txt", []string{"golang.txt"}},
{"fmt", "", []string{"golang.txt", "subdir/hello.go"}},
}
for _, tc := range scanTests {
s, err := New(tc.pattern, Options{false})
if err != nil {
t.Fatalf("Error on create sif %s", err)
}
path := fmt.Sprintf("_tests/%s", tc.path)
fs, err := s.Scan(path)
if err != nil {
t.Fatalf("Error on scan path %s", err)
}
if len(fs) != len(tc.expectedFiles) {
t.Errorf("expected %d, got %d", len(tc.expectedFiles), len(fs))
}
for i, f := range fs {
expected := fmt.Sprintf("_tests/%s", tc.expectedFiles[i])
if f.Name != expected {
t.Errorf("expected %s, got %s", expected, f.Name)
}
}
}
}