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

improve error message #70

Merged
merged 1 commit into from
Mar 11, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
30 changes: 11 additions & 19 deletions pkg/devfile/parser/context/apiVersion.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,27 @@ func (d *DevfileCtx) SetDevfileAPIVersion() error {
return errors.Wrapf(err, "failed to decode devfile json")
}

var apiVer string

// Get "apiVersion" value from map for devfile V1
apiVersion, okApi := r["apiVersion"]

// Get "schemaVersion" value from map for devfile V2
schemaVersion, okSchema := r["schemaVersion"]
var devfilePath string
if d.GetAbsPath() != "" {
devfilePath = d.GetAbsPath()
} else if d.GetURL() != "" {
devfilePath = d.GetURL()
}

if okApi {
apiVer = apiVersion.(string)
// apiVersion cannot be empty
if apiVer == "" {
return fmt.Errorf("apiVersion in devfile cannot be empty")
}

} else if okSchema {
apiVer = schemaVersion.(string)
if okSchema {
// SchemaVersion cannot be empty
if schemaVersion.(string) == "" {
return fmt.Errorf("schemaVersion in devfile cannot be empty")
return fmt.Errorf("schemaVersion in devfile: %s cannot be empty", devfilePath)
}
} else {
return fmt.Errorf("apiVersion or schemaVersion not present in devfile")

return fmt.Errorf("schemaVersion not present in devfile: %s", devfilePath)
}

// Successful
d.apiVersion = apiVer
klog.V(4).Infof("devfile apiVersion: '%s'", d.apiVersion)
d.apiVersion = schemaVersion.(string)
klog.V(4).Infof("devfile schemaVersion: '%s'", d.apiVersion)
return nil
}

Expand Down
46 changes: 24 additions & 22 deletions pkg/devfile/parser/context/apiVersion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,46 @@ import (
func TestSetDevfileAPIVersion(t *testing.T) {

const (
apiVersion = "2.0.0"
validJson = `{"apiVersion": "2.0.0"}`
emptyJson = "{}"
emptyApiVersionJson = `{"apiVersion": ""}`
schemaVersion = "2.0.0"
validJson = `{"schemaVersion": "2.0.0"}`
emptyJson = "{}"
emptySchemaVersionJson = `{"schemaVersion": ""}`
devfilePath = "/testpath/devfile.yaml"
devfileURL = "http://server/devfile.yaml"
)

// test table
tests := []struct {
name string
rawJson []byte
want string
wantErr error
name string
devfileCtx DevfileCtx
want string
wantErr error
}{
{
name: "valid apiVersion",
rawJson: []byte(validJson),
want: apiVersion,
wantErr: nil,
name: "valid schemaVersion",
devfileCtx: DevfileCtx{rawContent: []byte(validJson), absPath: devfilePath},
want: schemaVersion,
wantErr: nil,
},
{
name: "apiVersion not present",
rawJson: []byte(emptyJson),
want: "",
wantErr: fmt.Errorf("apiVersion or schemaVersion not present in devfile"),
name: "schemaVersion not present",
devfileCtx: DevfileCtx{rawContent: []byte(emptyJson), absPath: devfilePath},
want: "",
wantErr: fmt.Errorf("schemaVersion not present in devfile: %s", devfilePath),
},
{
name: "apiVersion empty",
rawJson: []byte(emptyApiVersionJson),
want: "",
wantErr: fmt.Errorf("apiVersion in devfile cannot be empty"),
name: "schemaVersion empty",
devfileCtx: DevfileCtx{rawContent: []byte(emptySchemaVersionJson), url: devfileURL},
want: "",
wantErr: fmt.Errorf("schemaVersion in devfile: %s cannot be empty", devfileURL),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

// new devfile context object
d := DevfileCtx{rawContent: tt.rawJson}
d := tt.devfileCtx

// SetDevfileAPIVersion
gotErr := d.SetDevfileAPIVersion()
Expand All @@ -66,7 +68,7 @@ func TestSetDevfileAPIVersion(t *testing.T) {
func TestGetApiVersion(t *testing.T) {

const (
apiVersion = "1.0.0"
apiVersion = "2.0.0"
)

t.Run("get apiVersion", func(t *testing.T) {
Expand Down