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 Functionality For Multiple Devfile Name Variations #198

Merged
merged 2 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 6 additions & 2 deletions pkg/devfile/parser/util/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@
"fmt"
"net/http"
"os"
"strings"

"github.com/devfile/library/v2/pkg/util"
)

// Default filenames for create devfile to be used in mocks
const (
OutputDevfileYamlPath = "devfile.yaml"
)

type MockDevfileUtilsClient struct {
// Specify a valid git URL as an alias if using a localhost HTTP server in order to pass validation.
ParentURLAlias string
Expand Down Expand Up @@ -109,7 +113,7 @@
mockGitUrl := gc.MockGitURL
mockGitUrl.Token = gc.GitTestToken

if !mockGitUrl.IsFile || mockGitUrl.Revision == "" || !strings.Contains(mockGitUrl.Path, OutputDevfileYamlPath) {
if !mockGitUrl.IsFile || mockGitUrl.Revision == "" || !ValidateDevfileExistence((mockGitUrl.Path)) {

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

View check run for this annotation

Codecov / codecov/patch

pkg/devfile/parser/util/mock.go#L116

Added line #L116 was not covered by tests
return fmt.Errorf("error getting devfile from url: failed to retrieve %s", url+"/"+mockGitUrl.Path)
}

Expand Down
18 changes: 13 additions & 5 deletions pkg/devfile/parser/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@
"github.com/hashicorp/go-multierror"
)

// Default filenames for create devfile
const (
OutputDevfileYamlPath = "devfile.yaml"
)
// Contains common naming conventions for devfiles to look for when downloading resources
var DevfilePossibilities = [...]string{"devfile.yaml", "devfile.yml", ".devfile.yaml", ".devfile.yml"}
Jdubrick marked this conversation as resolved.
Show resolved Hide resolved

type DevfileUtilsClient struct {
}
Expand All @@ -52,7 +50,7 @@
return err
}

if !gitUrl.IsFile || gitUrl.Revision == "" || !strings.Contains(gitUrl.Path, OutputDevfileYamlPath) {
if !gitUrl.IsFile || gitUrl.Revision == "" || !ValidateDevfileExistence((gitUrl.Path)) {

Check warning on line 53 in pkg/devfile/parser/util/utils.go

View check run for this annotation

Codecov / codecov/patch

pkg/devfile/parser/util/utils.go#L53

Added line #L53 was not covered by tests
return fmt.Errorf("error getting devfile from url: failed to retrieve %s", url)
}

Expand Down Expand Up @@ -88,3 +86,13 @@

return nil
}

// ValidateDevfileExistence verifies if any of the naming possibilities for devfile are present in the url path
func ValidateDevfileExistence(path string) bool {
for _, devfile := range DevfilePossibilities {
if strings.Contains(path, devfile) {
return true
}
}
return false
}
48 changes: 48 additions & 0 deletions pkg/devfile/parser/util/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,51 @@ func TestDownloadInMemoryClient(t *testing.T) {
})
}
}

func TestValidateDevfileExistence(t *testing.T) {

tests := []struct {
name string
url string
wantErr bool
expectedValue bool
}{
{
name: "recognizes devfile.yaml",
url: "https://dummyurlpath/devfile/registry/main/stacks/python/3.0.0/devfile.yaml",
wantErr: false,
expectedValue: true,
},
{
name: "recognizes devfile.yml",
url: "https://dummyurlpath/devfile/registry/main/stacks/python/3.0.0/devfile.yml",
wantErr: false,
expectedValue: true,
},
{
name: "recognizes .devfile.yaml",
url: "https://dummyurlpath/devfile/registry/main/stacks/python/3.0.0/.devfile.yaml",
wantErr: false,
expectedValue: true,
},
{
name: "recognizes .devfile.yml",
url: "https://dummyurlpath/devfile/registry/main/stacks/python/3.0.0/.devfile.yml",
wantErr: false,
expectedValue: true,
},
{
name: "no valid devfile in path",
url: "https://dummyurlpath/devfile/registry/main/stacks/python/3.0.0/deploy.yaml",
wantErr: true,
expectedValue: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
res := ValidateDevfileExistence(tt.url)
assert.EqualValues(t, tt.expectedValue, res, "expected res = %t, got %t", tt.expectedValue, res)
})
}
}
Loading