Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to use mock without passing in arguments #191

Merged
merged 4 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@

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 != "" {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a test case for this block?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, tests updated.

// 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for this case, is it possible that gc.MockGitURL is nil?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its not a pointer, I dont think we should check for nil here. Its a regular struct assignment.

mockGitUrl = gc.MockGitURL
mockGitUrl.Token = params.Token
}

Check warning on line 65 in pkg/devfile/parser/util/mock.go

View check run for this annotation

Codecov / codecov/patch

pkg/devfile/parser/util/mock.go#L57-L65

Added lines #L57 - L65 were not covered by tests
}

return mockGitUrl.DownloadInMemoryWithClient(params, httpClient, gc.DownloadOptions)
Expand Down
4 changes: 2 additions & 2 deletions pkg/devfile/parser/util/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ func TestDownloadInMemoryClient(t *testing.T) {
},
{
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",
client: MockDevfileUtilsClient{MockGitURL: util.MockGitUrl{Host: server.URL}, DownloadOptions: util.MockDownloadOptions{MockFile: "OK"}},
url: server.URL,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are we able to test a gitURL?

This comment was marked as outdated.

This comment was marked as outdated.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since you add support for empty client, should add new unit test for your change, not editing the existing tests. the empty client case you added, needs a gitURL?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add a Github URL

want: []byte{79, 75},
},
{
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 @@

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

if m.GetToken() == "valid-token" || m.GetToken() == "" {
if m.GetToken() == "valid-token" {

Check warning on line 241 in pkg/util/mock.go

View check run for this annotation

Codecov / codecov/patch

pkg/util/mock.go#L241

Added line #L241 was not covered by tests
switch {
case options.MockDevfile:
return []byte(mockDevfile), nil
Expand All @@ -249,6 +249,9 @@
default:
return []byte(mockDevfile), nil
}
} else if m.GetToken() == "" {
// if no token is provided, assume normal operation
return DownloadInMemory(params)

Check warning on line 254 in pkg/util/mock.go

View check run for this annotation

Codecov / codecov/patch

pkg/util/mock.go#L252-L254

Added lines #L252 - L254 were not covered by tests
}

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