forked from sourcegraph/src-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_test.go
133 lines (116 loc) · 3.41 KB
/
search_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
package main
import (
"bytes"
"encoding/json"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"github.com/grafana/regexp"
)
var _ = func() bool {
// Disable colordiff when testing because its output various from system to system(!)
os.Setenv("COLORDIFF", "false")
isTest = true
return true
}()
func TestSearchOutput(t *testing.T) {
type testT struct {
input *searchResultsImproved
want *string
}
tests := map[string]*testT{}
dataDir := "testdata/search_formatting"
infos, err := os.ReadDir(dataDir)
if err != nil {
t.Fatal(err)
}
for _, f := range infos {
path := filepath.Join(dataDir, f.Name())
if strings.HasSuffix(f.Name(), ".got.txt") {
os.Remove(path)
}
isTestInput := strings.HasSuffix(f.Name(), ".test.json")
isTestResult := strings.HasSuffix(f.Name(), ".want.txt")
if !isTestInput && !isTestResult {
continue
}
testName := strings.TrimSuffix(f.Name(), ".test.json")
testName = strings.TrimSuffix(testName, ".want.txt")
if _, ok := tests[testName]; !ok {
tests[testName] = &testT{}
}
data, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
if isTestInput {
if err := json.Unmarshal(data, &tests[testName].input); err != nil {
t.Fatal(err)
}
} else {
tmp := string(data)
tests[testName].want = &tmp
}
}
for testName, tst := range tests {
if tst.input == nil {
t.Fatalf("mytest.want.txt exists, but mytest.test.json file doesn't")
if tst.want == nil {
t.Fatalf("test is missing a .want.txt file, please create an empty one")
}
}
if tst.want == nil {
// Create the initial (empty) .want.txt file.
wantFile := filepath.Join(dataDir, testName+".want.txt")
if err := os.WriteFile(wantFile, nil, 0600); err != nil {
t.Fatal(err)
}
tmp := ""
tst.want = &tmp
}
}
for testName, tst := range tests {
t.Run(testName, func(t *testing.T) {
tmpl, err := parseTemplate(searchResultsTemplate)
if err != nil {
t.Fatal(err)
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, tst.input); err != nil {
t.Fatal(err)
}
got := buf.String()
normalizeTimeAgo(&got)
normalizeTimeAgo(tst.want)
if got != *tst.want {
t.Logf("'%s.want.txt' does not match '%s.got.txt'", testName, testName)
gotFile := filepath.Join(dataDir, testName+".got.txt")
wantFile := filepath.Join(dataDir, testName+".want.txt")
err := os.WriteFile(gotFile, []byte(got), 0600)
if err != nil {
t.Fatal(err)
}
cmd := exec.Command("git", "diff", "--no-index", wantFile, gotFile)
out, _ := cmd.CombinedOutput()
t.Fatalf("\n%s\nTo accept these changes, run:\n$ mv %s %s", string(out), gotFile, wantFile)
}
})
}
}
var nTimeAgoPattern = regexp.MustCompile(`(\d+|N) (months?|years?|time) ago`)
// normalizeTimeAgo makes tests not depend on the current time.
func normalizeTimeAgo(s *string) {
*s = nTimeAgoPattern.ReplaceAllString(*s, "N time ago")
}
func TestBuildVersionHasNewSearchInterface(t *testing.T) {
buildWithoutSearchInterface := "24568_2018-11-30_429039d"
buildWithSearchInterface := "25391_2018-12-12_ffbd6a3"
if buildVersionHasNewSearchInterface(buildWithoutSearchInterface) {
t.Errorf("Build version is before the new generic search interface was merged. Expected false, but got true.")
}
if !buildVersionHasNewSearchInterface(buildWithSearchInterface) {
t.Errorf("Build version is after the new generic search interface was merged. Expected true, but got false.")
}
}