Skip to content

Commit

Permalink
Update merging and overriding tests
Browse files Browse the repository at this point in the history
Signed-off-by: Maysun J Faisal <maysunaneek@gmail.com>
  • Loading branch information
maysunfaisal committed Apr 19, 2021
1 parent 4c6cc6e commit 2e7a57f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 54 deletions.
30 changes: 5 additions & 25 deletions pkg/utils/overriding/merging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ import (

dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/stretchr/testify/assert"
yamlMachinery "k8s.io/apimachinery/pkg/util/yaml"
"sigs.k8s.io/yaml"
)

func mergingPatchTest(main, parent, expected []byte, expectedError string, plugins ...[]byte) func(t *testing.T) {
func mergingPatchTest(main, parent []byte, expectedTemplate dw.DevWorkspaceTemplateSpecContent, expectedError string, plugins ...[]byte) func(t *testing.T) {
return func(t *testing.T) {
result, err := MergeDevWorkspaceTemplateSpecBytes(main, parent, plugins...)
if err != nil {
Expand All @@ -24,21 +22,7 @@ func mergingPatchTest(main, parent, expected []byte, expectedError string, plugi
return
}

resultYaml, err := yaml.Marshal(result)
if err != nil {
t.Error(err)
}

expectedJson, err := yamlMachinery.ToJSON(expected)
if err != nil {
t.Error(err)
}
expectedYaml, err := yaml.JSONToYAML(expectedJson)
if err != nil {
t.Error(err)
}

assert.Equal(t, string(expectedYaml), string(resultYaml), "The two values should be the same.")
assert.Equal(t, &expectedTemplate, result, "The two values should be the same")
}
}

Expand Down Expand Up @@ -75,7 +59,7 @@ func TestMerging(t *testing.T) {
}
plugins = append(plugins, plugin)
}
result := []byte{}
var resultTemplate dw.DevWorkspaceTemplateSpecContent
resultError := ""
errorFile := filepath.Join(dirPath, "result-error.txt")
if _, err = os.Stat(errorFile); err == nil {
Expand All @@ -86,15 +70,11 @@ func TestMerging(t *testing.T) {
}
resultError = string(resultErrorBytes)
} else {
result, err = ioutil.ReadFile(filepath.Join(dirPath, "result.yaml"))
if err != nil {
t.Error(err)
return nil
}
readFileToStruct(t, filepath.Join(dirPath, "result.yaml"), &resultTemplate)
}
testName := filepath.Base(dirPath)

t.Run(testName, mergingPatchTest(main, parent, result, resultError, plugins...))
t.Run(testName, mergingPatchTest(main, parent, resultTemplate, resultError, plugins...))
}
return nil
})
Expand Down
34 changes: 5 additions & 29 deletions pkg/utils/overriding/overriding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
attributesPkg "github.com/devfile/api/v2/pkg/attributes"
"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/util/json"
yamlMachinery "k8s.io/apimachinery/pkg/util/yaml"
"sigs.k8s.io/yaml"
)

Expand Down Expand Up @@ -163,7 +161,7 @@ func TestBasicToplevelOverriding(t *testing.T) {
assert.Equal(t, expected, result, "The two values should be the same.")
}

func overridingPatchTest(original, patch, expected []byte, expectedError string) func(t *testing.T) {
func overridingPatchTest(original, patch []byte, expectedTemplate dw.DevWorkspaceTemplateSpecContent, expectedError string) func(t *testing.T) {
return func(t *testing.T) {
result, err := OverrideDevWorkspaceTemplateSpecBytes(original, patch)
if err != nil {
Expand All @@ -175,25 +173,7 @@ func overridingPatchTest(original, patch, expected []byte, expectedError string)
return
}

resultJson, err := json.Marshal(result)
if err != nil {
t.Error(err)
}
resultYaml, err := yaml.JSONToYAML(resultJson)
if err != nil {
t.Error(err)
}

expectedJson, err := yamlMachinery.ToJSON(expected)
if err != nil {
t.Error(err)
}
expectedYaml, err := yaml.JSONToYAML(expectedJson)
if err != nil {
t.Error(err)
}

assert.Equal(t, string(expectedYaml), string(resultYaml), "The two values should be the same.")
assert.Equal(t, &expectedTemplate, result, "The two values should be the same")
}
}

Expand All @@ -215,7 +195,7 @@ func TestOverridingPatches(t *testing.T) {
t.Error(err)
return nil
}
result := []byte{}
var resultTemplate dw.DevWorkspaceTemplateSpecContent
resultError := ""
errorFile := filepath.Join(dirPath, "result-error.txt")
if _, err = os.Stat(errorFile); err == nil {
Expand All @@ -226,15 +206,11 @@ func TestOverridingPatches(t *testing.T) {
}
resultError = string(resultErrorBytes)
} else {
result, err = ioutil.ReadFile(filepath.Join(dirPath, "result.yaml"))
if err != nil {
t.Error(err)
return nil
}
readFileToStruct(t, filepath.Join(dirPath, "result.yaml"), &resultTemplate)
}
testName := filepath.Base(dirPath)

t.Run(testName, overridingPatchTest(original, patch, result, resultError))
t.Run(testName, overridingPatchTest(original, patch, resultTemplate, resultError))
}
return nil
})
Expand Down

0 comments on commit 2e7a57f

Please sign in to comment.