forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepos_test.go
205 lines (184 loc) · 5.36 KB
/
repos_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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package backend
import (
"bytes"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"reflect"
"testing"
"github.com/inconshreveable/log15"
"github.com/sourcegraph/sourcegraph/cmd/frontend/db"
"github.com/sourcegraph/sourcegraph/cmd/frontend/internal/inventory"
"github.com/sourcegraph/sourcegraph/cmd/frontend/types"
"github.com/sourcegraph/sourcegraph/internal/api"
"github.com/sourcegraph/sourcegraph/internal/rcache"
"github.com/sourcegraph/sourcegraph/internal/repoupdater"
"github.com/sourcegraph/sourcegraph/internal/repoupdater/protocol"
"github.com/sourcegraph/sourcegraph/internal/vcs/git"
"github.com/sourcegraph/sourcegraph/internal/vcs/util"
)
func TestReposService_Get(t *testing.T) {
var s repos
ctx := testContext()
wantRepo := &types.Repo{ID: 1, Name: "github.com/u/r"}
calledGet := db.Mocks.Repos.MockGet_Return(t, wantRepo)
repo, err := s.Get(ctx, 1)
if err != nil {
t.Fatal(err)
}
if !*calledGet {
t.Error("!calledGet")
}
// Should not be called because mock GitHub has same data as mock DB.
if !reflect.DeepEqual(repo, wantRepo) {
t.Errorf("got %+v, want %+v", repo, wantRepo)
}
}
func TestReposService_List(t *testing.T) {
var s repos
ctx := testContext()
wantRepos := []*types.Repo{
{Name: "r1"},
{Name: "r2"},
}
calledList := db.Mocks.Repos.MockList(t, "r1", "r2")
repos, err := s.List(ctx, db.ReposListOptions{})
if err != nil {
t.Fatal(err)
}
if !*calledList {
t.Error("!calledList")
}
if !reflect.DeepEqual(repos, wantRepos) {
t.Errorf("got %+v, want %+v", repos, wantRepos)
}
}
func TestRepos_Add(t *testing.T) {
var s repos
ctx := testContext()
const repoName = "my/repo"
calledRepoLookup := false
repoupdater.MockRepoLookup = func(args protocol.RepoLookupArgs) (*protocol.RepoLookupResult, error) {
calledRepoLookup = true
if args.Repo != repoName {
t.Errorf("got %q, want %q", args.Repo, repoName)
}
return &protocol.RepoLookupResult{
Repo: &protocol.RepoInfo{Name: repoName, Description: "d"},
}, nil
}
defer func() { repoupdater.MockRepoLookup = nil }()
if err := s.Add(ctx, repoName); err != nil {
t.Fatal(err)
}
if !calledRepoLookup {
t.Error("!calledRepoLookup")
}
}
type gitObjectInfo string
func (oid gitObjectInfo) OID() git.OID {
var v git.OID
copy(v[:], []byte(oid))
return v
}
func TestReposGetInventory(t *testing.T) {
var s repos
ctx := testContext()
const (
wantRepo = "a"
wantCommitID = "cccccccccccccccccccccccccccccccccccccccc"
wantRootOID = "oid-root"
)
repoupdater.MockRepoLookup = func(args protocol.RepoLookupArgs) (*protocol.RepoLookupResult, error) {
if args.Repo != wantRepo {
t.Errorf("got %q, want %q", args.Repo, wantRepo)
}
return &protocol.RepoLookupResult{Repo: &protocol.RepoInfo{Name: wantRepo}}, nil
}
defer func() { repoupdater.MockRepoLookup = nil }()
git.Mocks.Stat = func(commit api.CommitID, path string) (os.FileInfo, error) {
if commit != wantCommitID {
t.Errorf("got commit %q, want %q", commit, wantCommitID)
}
return &util.FileInfo{Name_: path, Mode_: os.ModeDir, Sys_: gitObjectInfo(wantRootOID)}, nil
}
git.Mocks.ReadDir = func(commit api.CommitID, name string, recurse bool) ([]os.FileInfo, error) {
if commit != wantCommitID {
t.Errorf("got commit %q, want %q", commit, wantCommitID)
}
switch name {
case "":
return []os.FileInfo{
&util.FileInfo{Name_: "a", Mode_: os.ModeDir, Sys_: gitObjectInfo("oid-a")},
&util.FileInfo{Name_: "b.go", Size_: 12},
}, nil
case "a":
return []os.FileInfo{&util.FileInfo{Name_: "a/c.m", Size_: 24}}, nil
default:
panic("unhandled mock ReadDir " + name)
}
}
git.Mocks.NewFileReader = func(commit api.CommitID, name string) (io.ReadCloser, error) {
if commit != wantCommitID {
t.Errorf("got commit %q, want %q", commit, wantCommitID)
}
var data []byte
switch name {
case "b.go":
data = []byte("package main")
case "a/c.m":
data = []byte("@interface X:NSObject {}")
default:
panic("unhandled mock ReadFile " + name)
}
return ioutil.NopCloser(bytes.NewReader(data)), nil
}
defer git.ResetMocks()
tests := []struct {
useEnhancedLanguageDetection bool
want *inventory.Inventory
}{
{
useEnhancedLanguageDetection: false,
want: &inventory.Inventory{
Languages: []inventory.Lang{
{Name: "Go", TotalBytes: 0, TotalLines: 0},
{Name: "Limbo", TotalBytes: 0, TotalLines: 0}, // obviously incorrect, but this is how the pre-enhanced lang detection worked
},
},
},
{
useEnhancedLanguageDetection: true,
want: &inventory.Inventory{
Languages: []inventory.Lang{
{Name: "Go", TotalBytes: 12, TotalLines: 1},
{Name: "Objective-C", TotalBytes: 24, TotalLines: 1},
},
},
},
}
for _, test := range tests {
t.Run(fmt.Sprintf("useEnhancedLanguageDetection=%v", test.useEnhancedLanguageDetection), func(t *testing.T) {
rcache.SetupForTest(t)
orig := useEnhancedLanguageDetection
useEnhancedLanguageDetection = test.useEnhancedLanguageDetection
defer func() { useEnhancedLanguageDetection = orig }() // reset
inv, err := s.GetInventory(ctx, &types.Repo{Name: wantRepo}, wantCommitID, false)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(inv, test.want) {
t.Errorf("got %#v\nwant %#v", inv, test.want)
}
})
}
}
func TestMain(m *testing.M) {
flag.Parse()
if !testing.Verbose() {
log15.Root().SetHandler(log15.DiscardHandler())
}
os.Exit(m.Run())
}