github.com/grafana/pyroscope@v1.18.0/pkg/frontend/vcs/source/find_go_test.go (about)

     1  package source
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	giturl "github.com/kubescape/go-git-url"
     8  	"github.com/stretchr/testify/assert"
     9  
    10  	"github.com/grafana/pyroscope/pkg/frontend/vcs/client"
    11  	"github.com/grafana/pyroscope/pkg/frontend/vcs/config"
    12  )
    13  
    14  func Test_tryFindGoFile(t *testing.T) {
    15  	pyroscopeRepo, _ := giturl.NewGitURL("http://github.com/grafana/pyroscope")
    16  	tests := []struct {
    17  		name                  string
    18  		searchedPath          string
    19  		rootPath              string
    20  		repo                  giturl.IGitURL
    21  		clientMock            *mockVCSClient
    22  		attempts              int
    23  		expectedSearchedPaths []string
    24  		expectedError         error
    25  	}{
    26  		{
    27  			name:                  "happy case in root path",
    28  			searchedPath:          "/var/service1/src/main.go",
    29  			rootPath:              "",
    30  			repo:                  pyroscopeRepo,
    31  			clientMock:            newMockVCSClient().addFiles(newFile("main.go")),
    32  			attempts:              5,
    33  			expectedSearchedPaths: []string{"var/service1/src/main.go", "service1/src/main.go", "src/main.go", "main.go"},
    34  			expectedError:         nil,
    35  		},
    36  		{
    37  			name:                  "happy case in submodule",
    38  			searchedPath:          "/src/main.go",
    39  			rootPath:              "service/example",
    40  			repo:                  pyroscopeRepo,
    41  			clientMock:            newMockVCSClient().addFiles(newFile("service/example/main.go")),
    42  			attempts:              5,
    43  			expectedSearchedPaths: []string{"service/example/src/main.go", "service/example/main.go"},
    44  			expectedError:         nil,
    45  		},
    46  		{
    47  			name:                  "path with relative repository prefix",
    48  			searchedPath:          "github.com/grafana/pyroscope/main.go",
    49  			rootPath:              "",
    50  			repo:                  pyroscopeRepo,
    51  			clientMock:            newMockVCSClient().addFiles(newFile("main.go")),
    52  			attempts:              1,
    53  			expectedSearchedPaths: []string{"main.go"},
    54  			expectedError:         nil,
    55  		},
    56  		{
    57  			name:                  "path with absolute repository prefix",
    58  			searchedPath:          "/Users/pyroscope/git/github.com/grafana/pyroscope/main.go",
    59  			rootPath:              "",
    60  			repo:                  pyroscopeRepo,
    61  			clientMock:            newMockVCSClient().addFiles(newFile("main.go")),
    62  			attempts:              1,
    63  			expectedSearchedPaths: []string{"main.go"},
    64  			expectedError:         nil,
    65  		},
    66  		{
    67  			name:                  "not found, attempts exceeded",
    68  			searchedPath:          "/var/service1/src/main.go",
    69  			rootPath:              "",
    70  			repo:                  pyroscopeRepo,
    71  			clientMock:            newMockVCSClient().addFiles(newFile("main.go")),
    72  			attempts:              3,
    73  			expectedSearchedPaths: []string{"var/service1/src/main.go", "service1/src/main.go", "src/main.go"},
    74  			expectedError:         client.ErrNotFound,
    75  		},
    76  	}
    77  	for _, tt := range tests {
    78  		t.Run(tt.name, func(t *testing.T) {
    79  			ctx := context.Background()
    80  			sut := FileFinder{
    81  				file:     config.FileSpec{Path: tt.searchedPath},
    82  				rootPath: tt.rootPath,
    83  				ref:      defaultRef(""),
    84  				repo:     tt.repo,
    85  				client:   tt.clientMock,
    86  			}
    87  			_, err := sut.tryFindGoFile(ctx, tt.attempts)
    88  			assert.Equal(t, tt.expectedSearchedPaths, (*tt.clientMock).searchedSequence)
    89  			assert.Equal(t, tt.expectedError, err)
    90  		})
    91  	}
    92  }