Skip to content

Commit 7d5ab4e

Browse files
committed
Merge both mocks
1 parent b0f24e1 commit 7d5ab4e

File tree

2 files changed

+51
-46
lines changed

2 files changed

+51
-46
lines changed

pkg/frontend/vcs/source/find_go_test.go

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,14 @@ import (
1111
"github.com/grafana/pyroscope/pkg/frontend/vcs/config"
1212
)
1313

14-
type VCSClientMock struct {
15-
fileToFind string
16-
searchedSequence []string
17-
}
18-
19-
func (c *VCSClientMock) GetFile(ctx context.Context, req client.FileRequest) (client.File, error) {
20-
c.searchedSequence = append(c.searchedSequence, req.Path)
21-
if req.Path == c.fileToFind {
22-
return client.File{}, nil
23-
} else {
24-
return client.File{}, client.ErrNotFound
25-
}
26-
}
27-
2814
func Test_tryFindGoFile(t *testing.T) {
2915
pyroscopeRepo, _ := giturl.NewGitURL("http://github.com/grafana/pyroscope")
3016
tests := []struct {
3117
name string
3218
searchedPath string
3319
rootPath string
3420
repo giturl.IGitURL
35-
clientMock *VCSClientMock
21+
clientMock *mockVCSClient
3622
attempts int
3723
expectedSearchedPaths []string
3824
expectedError error
@@ -42,7 +28,7 @@ func Test_tryFindGoFile(t *testing.T) {
4228
searchedPath: "/var/service1/src/main.go",
4329
rootPath: "",
4430
repo: pyroscopeRepo,
45-
clientMock: &VCSClientMock{fileToFind: "/main.go"},
31+
clientMock: newMockVCSClient().addFiles(newFile("main.go")),
4632
attempts: 5,
4733
expectedSearchedPaths: []string{"var/service1/src/main.go", "service1/src/main.go", "src/main.go", "main.go"},
4834
expectedError: nil,
@@ -52,7 +38,7 @@ func Test_tryFindGoFile(t *testing.T) {
5238
searchedPath: "/src/main.go",
5339
rootPath: "service/example",
5440
repo: pyroscopeRepo,
55-
clientMock: &VCSClientMock{fileToFind: "service/example/main.go"},
41+
clientMock: newMockVCSClient().addFiles(newFile("service/example/main.go")),
5642
attempts: 5,
5743
expectedSearchedPaths: []string{"service/example/src/main.go", "service/example/main.go"},
5844
expectedError: nil,
@@ -62,7 +48,7 @@ func Test_tryFindGoFile(t *testing.T) {
6248
searchedPath: "github.com/grafana/pyroscope/main.go",
6349
rootPath: "",
6450
repo: pyroscopeRepo,
65-
clientMock: &VCSClientMock{fileToFind: "/main.go"},
51+
clientMock: newMockVCSClient().addFiles(newFile("main.go")),
6652
attempts: 1,
6753
expectedSearchedPaths: []string{"main.go"},
6854
expectedError: nil,
@@ -72,7 +58,7 @@ func Test_tryFindGoFile(t *testing.T) {
7258
searchedPath: "/Users/pyroscope/git/github.com/grafana/pyroscope/main.go",
7359
rootPath: "",
7460
repo: pyroscopeRepo,
75-
clientMock: &VCSClientMock{fileToFind: "/main.go"},
61+
clientMock: newMockVCSClient().addFiles(newFile("main.go")),
7662
attempts: 1,
7763
expectedSearchedPaths: []string{"main.go"},
7864
expectedError: nil,
@@ -82,7 +68,7 @@ func Test_tryFindGoFile(t *testing.T) {
8268
searchedPath: "/var/service1/src/main.go",
8369
rootPath: "",
8470
repo: pyroscopeRepo,
85-
clientMock: &VCSClientMock{fileToFind: "/main.go"},
71+
clientMock: newMockVCSClient().addFiles(newFile("main.go")),
8672
attempts: 3,
8773
expectedSearchedPaths: []string{"var/service1/src/main.go", "service1/src/main.go", "src/main.go"},
8874
expectedError: client.ErrNotFound,
@@ -94,6 +80,7 @@ func Test_tryFindGoFile(t *testing.T) {
9480
sut := FileFinder{
9581
file: config.FileSpec{Path: tt.searchedPath},
9682
rootPath: tt.rootPath,
83+
ref: defaultRef(""),
9784
repo: tt.repo,
9885
client: tt.clientMock,
9986
}

pkg/frontend/vcs/source/find_test.go

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"net/http"
88
"path/filepath"
9+
"sync"
910
"testing"
1011

1112
"github.com/go-kit/log"
@@ -17,49 +18,66 @@ import (
1718
"github.com/grafana/pyroscope/pkg/frontend/vcs/config"
1819
)
1920

20-
func newTestVCSClient(b testing.TB) *testVCSClient {
21-
return &testVCSClient{
21+
func newMockVCSClient() *mockVCSClient {
22+
return &mockVCSClient{
2223
files: make(map[client.FileRequest]client.File),
23-
t: b,
2424
}
2525
}
2626

27-
type testVCSClient struct {
28-
files map[client.FileRequest]client.File
29-
t testing.TB
27+
type mockFileResponse struct {
28+
request client.FileRequest
29+
content string
3030
}
3131

32-
func (m *testVCSClient) GetFile(ctx context.Context, req client.FileRequest) (client.File, error) {
33-
if file, ok := m.files[req]; ok {
34-
return file, nil
32+
func newFile(path string) mockFileResponse {
33+
return mockFileResponse{
34+
request: client.FileRequest{
35+
Path: path,
36+
},
37+
content: "# Content of " + path,
3538
}
36-
m.t.Logf("file not found: %+v", req)
37-
return client.File{}, client.ErrNotFound
3839
}
3940

40-
// mockFileResponse represents a file to be mocked
41-
type mockFileResponse struct {
42-
request client.FileRequest
43-
content string
41+
func (f *mockFileResponse) url() string {
42+
return fmt.Sprintf(
43+
"https://github.com/%s/%s/blob/%s/%s",
44+
f.request.Owner,
45+
f.request.Repo,
46+
f.request.Ref,
47+
f.request.Path,
48+
)
49+
}
50+
51+
type mockVCSClient struct {
52+
mtx sync.Mutex
53+
files map[client.FileRequest]client.File
54+
searchedSequence []string
55+
}
56+
57+
func (c *mockVCSClient) GetFile(ctx context.Context, req client.FileRequest) (client.File, error) {
58+
c.mtx.Lock()
59+
c.searchedSequence = append(c.searchedSequence, req.Path)
60+
file, ok := c.files[req]
61+
c.mtx.Unlock()
62+
if ok {
63+
return file, nil
64+
}
65+
return client.File{}, client.ErrNotFound
4466
}
4567

46-
// setupMockFiles is a shared function to setup mock responses for GetFile calls
47-
func (m *testVCSClient) addFiles(files ...mockFileResponse) {
68+
func (c *mockVCSClient) addFiles(files ...mockFileResponse) *mockVCSClient {
69+
c.mtx.Lock()
70+
defer c.mtx.Unlock()
4871
for _, file := range files {
4972
file.request.Owner = defaultOwner(file.request.Owner)
5073
file.request.Repo = defaultRepo(file.request.Repo)
5174
file.request.Ref = defaultRef(file.request.Ref)
52-
m.files[file.request] = client.File{
75+
c.files[file.request] = client.File{
5376
Content: file.content,
54-
URL: fmt.Sprintf(
55-
"https://github.com/%s/%s/blob/%s/%s",
56-
file.request.Owner,
57-
file.request.Repo,
58-
file.request.Ref,
59-
file.request.Path,
60-
),
77+
URL: file.url(),
6178
}
6279
}
80+
return c
6381
}
6482

6583
func defaultOwner(s string) string {
@@ -393,7 +411,7 @@ require (
393411
ctx := context.Background()
394412

395413
// Setup mock VCS client
396-
mockClient := newTestVCSClient(t)
414+
mockClient := newMockVCSClient()
397415

398416
// Populate pyroscopeYAML content into first mock file (if present)
399417
mockFiles := append(tt.mockFiles, mockFileResponse{

0 commit comments

Comments
 (0)