Skip to content

Commit

Permalink
Merge pull request #191 from maysunfaisal/update-devfile-mock-util-1
Browse files Browse the repository at this point in the history
Add ability to use mock without passing in arguments
  • Loading branch information
maysunfaisal authored Nov 30, 2023
2 parents 38de98b + c1944d6 commit 20a0c91
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
17 changes: 14 additions & 3 deletions pkg/devfile/parser/util/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,20 @@ func (gc MockDevfileUtilsClient) DownloadInMemory(params util.HTTPRequestParams)

var mockGitUrl util.MockGitUrl

if util.IsGitProviderRepo(gc.MockGitURL.Host) {
mockGitUrl = gc.MockGitURL
mockGitUrl.Token = gc.GitTestToken
if gc.MockGitURL.Host != "" {
if util.IsGitProviderRepo(gc.MockGitURL.Host) {
mockGitUrl = gc.MockGitURL
mockGitUrl.Token = gc.GitTestToken
}
} else if params.URL != "" {
// Not all clients have the ability to pass in mock data
// So we should be adaptable and use the function params
// and mock the output
if util.IsGitProviderRepo(params.URL) {
gc.MockGitURL.Host = params.URL
mockGitUrl = gc.MockGitURL
mockGitUrl.Token = params.Token
}
}

return mockGitUrl.DownloadInMemoryWithClient(params, httpClient, gc.DownloadOptions)
Expand Down
12 changes: 9 additions & 3 deletions pkg/devfile/parser/util/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ func TestDownloadInMemoryClient(t *testing.T) {
wantErr: fmt.Sprintf(downloadErr, "https://"+RawGitHubHost+"/devfile/library/main/devfile.yaml"),
},
{
name: "Case 6: Input url is valid with a mock client",
client: MockDevfileUtilsClient{MockGitURL: util.MockGitUrl{Host: "https://github.com/devfile/library/blob/main/devfile.yaml"}, DownloadOptions: util.MockDownloadOptions{MockFile: "OK"}},
url: "https://github.com/devfile/library/blob/main/devfile.yaml",
name: "Case 6: Input url is valid with a mock client, dont use mock data during invocation",
client: MockDevfileUtilsClient{},
url: server.URL,
want: []byte{79, 75},
},
{
Expand All @@ -106,6 +106,12 @@ func TestDownloadInMemoryClient(t *testing.T) {
url: "https://github.com/devfile/library/blob/main/devfile.yaml",
wantErr: "failed to retrieve https://github.com/devfile/library/blob/main/devfile.yaml",
},
{
name: "Case 9: Input github url is valid with a mock client, dont use mock data during invocation",
client: MockDevfileUtilsClient{},
url: "https://raw.githubusercontent.com/maysunfaisal/OK/main/OK.txt",
want: []byte{79, 75},
},
}

for _, tt := range tests {
Expand Down
5 changes: 4 additions & 1 deletion pkg/util/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ CMD [ "waitress-serve", "--port=8081", "app:app"]

func (m MockGitUrl) DownloadInMemoryWithClient(params HTTPRequestParams, httpClient HTTPClient, options MockDownloadOptions) ([]byte, error) {

if m.GetToken() == "valid-token" || m.GetToken() == "" {
if m.GetToken() == "valid-token" {
switch {
case options.MockDevfile:
return []byte(mockDevfile), nil
Expand All @@ -249,6 +249,9 @@ func (m MockGitUrl) DownloadInMemoryWithClient(params HTTPRequestParams, httpCli
default:
return []byte(mockDevfile), nil
}
} else if m.GetToken() == "" {
// if no token is provided, assume normal operation
return DownloadInMemory(params)
}

return nil, fmt.Errorf("failed to retrieve %s", params.URL)
Expand Down

0 comments on commit 20a0c91

Please sign in to comment.