forked from hound-search/hound
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_test.go
139 lines (117 loc) · 2.66 KB
/
index_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
package index
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"testing"
)
const (
url = "https://www.etsy.com/"
rev = "r420"
)
func thisDir() string {
_, file, _, _ := runtime.Caller(0)
return filepath.Dir(file)
}
func buildIndex(url, rev string) (*IndexRef, error) {
dir, err := ioutil.TempDir(os.TempDir(), "hound")
if err != nil {
return nil, err
}
var opt IndexOptions
return Build(&opt, dir, thisDir(), url, rev)
}
func TestSearch(t *testing.T) {
// Build an index
ref, err := buildIndex(url, rev)
if err != nil {
t.Fatal(err)
}
defer ref.Remove() //nolint
// Make sure the metadata in the ref is good.
if ref.Rev != rev {
t.Fatalf("expected rev of %s, got %s", rev, ref.Rev)
}
if ref.Url != url {
t.Fatalf("expected url of %s got %s", url, ref.Url)
}
// Make sure the ref can be opened.
idx, err := ref.Open()
if err != nil {
t.Fatal(err)
}
defer idx.Close()
// Make sure we can carry out a search
if _, err := idx.Search("5a1c0dac2d9b3ea4085b30dd14375c18eab993d5", &SearchOptions{}); err != nil {
t.Fatal(err)
}
}
func TestSearchWithLimits(t *testing.T) {
// Build an index
ref, err := buildIndex(url, rev)
if err != nil {
t.Fatal(err)
}
defer ref.Remove() //nolint
// Make sure the ref can be opened.
idx, err := ref.Open()
if err != nil {
t.Fatal(err)
}
defer idx.Close()
// Make sure we can carry out a search within result limits
expectedMatches := 100
var debugBuf bytes.Buffer
if results, err := idx.Search("8365a", &SearchOptions{MaxResults: 100}); err != nil {
t.Fatal(err)
} else {
totalMatches := 0
for _, fileWithMatch := range results.Matches {
totalMatches += len(fileWithMatch.Matches)
for _, match := range fileWithMatch.Matches {
fmt.Fprintf(&debugBuf, "file: %v, line no: %d\n", fileWithMatch.Filename, match.LineNumber)
}
}
if totalMatches != expectedMatches {
t.Error(debugBuf.String())
t.Fatalf("expected %d matches, got %d matches", expectedMatches, totalMatches)
}
}
}
func TestRemove(t *testing.T) {
ref, err := buildIndex(url, rev)
if err != nil {
t.Fatal(err)
}
if err := ref.Remove(); err != nil {
t.Fatal(err)
}
if _, err := os.Stat(ref.Dir()); err == nil {
t.Fatalf("Remove did not delete directory: %s", ref.Dir())
}
}
func TestRead(t *testing.T) {
ref, err := buildIndex(url, rev)
if err != nil {
t.Fatal(err)
}
defer ref.Remove() //nolint
r, err := Read(ref.Dir())
if err != nil {
t.Fatal(err)
}
if r.Url != url {
t.Fatalf("expected url of %s, got %s", url, r.Url)
}
if r.Rev != rev {
t.Fatalf("expected rev of %s, got %s", rev, r.Rev)
}
idx, err := r.Open()
if err != nil {
t.Fatal(err)
}
defer idx.Close()
}