Skip to content

Commit 20a0c91

Browse files
authored
Merge pull request #191 from maysunfaisal/update-devfile-mock-util-1
Add ability to use mock without passing in arguments
2 parents 38de98b + c1944d6 commit 20a0c91

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

pkg/devfile/parser/util/mock.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,20 @@ func (gc MockDevfileUtilsClient) DownloadInMemory(params util.HTTPRequestParams)
4949

5050
var mockGitUrl util.MockGitUrl
5151

52-
if util.IsGitProviderRepo(gc.MockGitURL.Host) {
53-
mockGitUrl = gc.MockGitURL
54-
mockGitUrl.Token = gc.GitTestToken
52+
if gc.MockGitURL.Host != "" {
53+
if util.IsGitProviderRepo(gc.MockGitURL.Host) {
54+
mockGitUrl = gc.MockGitURL
55+
mockGitUrl.Token = gc.GitTestToken
56+
}
57+
} else if params.URL != "" {
58+
// Not all clients have the ability to pass in mock data
59+
// So we should be adaptable and use the function params
60+
// and mock the output
61+
if util.IsGitProviderRepo(params.URL) {
62+
gc.MockGitURL.Host = params.URL
63+
mockGitUrl = gc.MockGitURL
64+
mockGitUrl.Token = params.Token
65+
}
5566
}
5667

5768
return mockGitUrl.DownloadInMemoryWithClient(params, httpClient, gc.DownloadOptions)

pkg/devfile/parser/util/utils_test.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ func TestDownloadInMemoryClient(t *testing.T) {
8989
wantErr: fmt.Sprintf(downloadErr, "https://"+RawGitHubHost+"/devfile/library/main/devfile.yaml"),
9090
},
9191
{
92-
name: "Case 6: Input url is valid with a mock client",
93-
client: MockDevfileUtilsClient{MockGitURL: util.MockGitUrl{Host: "https://github.com/devfile/library/blob/main/devfile.yaml"}, DownloadOptions: util.MockDownloadOptions{MockFile: "OK"}},
94-
url: "https://github.com/devfile/library/blob/main/devfile.yaml",
92+
name: "Case 6: Input url is valid with a mock client, dont use mock data during invocation",
93+
client: MockDevfileUtilsClient{},
94+
url: server.URL,
9595
want: []byte{79, 75},
9696
},
9797
{
@@ -106,6 +106,12 @@ func TestDownloadInMemoryClient(t *testing.T) {
106106
url: "https://github.com/devfile/library/blob/main/devfile.yaml",
107107
wantErr: "failed to retrieve https://github.com/devfile/library/blob/main/devfile.yaml",
108108
},
109+
{
110+
name: "Case 9: Input github url is valid with a mock client, dont use mock data during invocation",
111+
client: MockDevfileUtilsClient{},
112+
url: "https://raw.githubusercontent.com/maysunfaisal/OK/main/OK.txt",
113+
want: []byte{79, 75},
114+
},
109115
}
110116

111117
for _, tt := range tests {

pkg/util/mock.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ CMD [ "waitress-serve", "--port=8081", "app:app"]
238238

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

241-
if m.GetToken() == "valid-token" || m.GetToken() == "" {
241+
if m.GetToken() == "valid-token" {
242242
switch {
243243
case options.MockDevfile:
244244
return []byte(mockDevfile), nil
@@ -249,6 +249,9 @@ func (m MockGitUrl) DownloadInMemoryWithClient(params HTTPRequestParams, httpCli
249249
default:
250250
return []byte(mockDevfile), nil
251251
}
252+
} else if m.GetToken() == "" {
253+
// if no token is provided, assume normal operation
254+
return DownloadInMemory(params)
252255
}
253256

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

0 commit comments

Comments
 (0)