summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSimon Hausmann <simon.hausmann@qt.io>2019-09-26 11:05:11 +0200
committerSimon Hausmann <simon.hausmann@qt.io>2019-09-29 11:53:13 +0200
commit7540f36126aa2a7eea331be82f2f279c10732509 (patch)
tree80c343fcb83b635a904a0214e4694dd42dbe12f4 /src
parent9295b1aa712e5df5526699ed8e2851f49cc1da27 (diff)
Clean up dependencies.yaml generation
Centralize the conversion to the yaml string, as suggested by Aapo. Change-Id: I22f3738686f4c5bc88a1299f023f7816ed5d4763 Reviewed-by: Aapo Keskimolo <aapo.keskimolo@qt.io> Reviewed-by: Frederik Gladhorn <frederik.gladhorn@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/qtmoduleupdater/module.go23
-rw-r--r--src/qtmoduleupdater/module_test.go19
2 files changed, 24 insertions, 18 deletions
diff --git a/src/qtmoduleupdater/module.go b/src/qtmoduleupdater/module.go
index 7fa22beb..8f6aa4e6 100644
--- a/src/qtmoduleupdater/module.go
+++ b/src/qtmoduleupdater/module.go
@@ -86,6 +86,19 @@ func (depMap *YAMLDependenciesMap) MarshalYAML() (interface{}, error) {
return result, nil
}
+// ToString Converts the yaml dependencies map into its string representation, for storage
+// in the dependencies.yaml file, for example.
+func (depMap *YAMLDependencies) ToString() (string, error) {
+ output := &bytes.Buffer{}
+ encoder := yaml.NewEncoder(output)
+ if err := encoder.Encode(depMap); err != nil {
+ return "", fmt.Errorf("Error encoding YAML dependencies: %s", err)
+ }
+ encoder.Close()
+
+ return output.String(), nil
+}
+
//go:generate stringer -type=DependenciesUpdateResultEnum
// DependenciesUpdateResultEnum describes the different states after attempting to update the dependencies.yaml for a module.
@@ -389,12 +402,12 @@ func (module *Module) updateDependenciesForModule(availableModules map[string]*M
Path: "dependencies.yaml",
}
- yamlBuffer := &bytes.Buffer{}
- yamlEncoder := yaml.NewEncoder(yamlBuffer)
- yamlEncoder.Encode(*yamlObject)
- yamlEncoder.Close()
+ yamlStr, err := yamlObject.ToString()
+ if err != nil {
+ return dependenciesUpdateResult{}, fmt.Errorf("Internal error encoding yaml to string: %s", err)
+ }
- if err := index.HashObject(updatedIndexEntryForFile, yamlBuffer.Bytes()); err != nil {
+ if err := index.HashObject(updatedIndexEntryForFile, []byte(yamlStr)); err != nil {
return dependenciesUpdateResult{}, err
}
diff --git a/src/qtmoduleupdater/module_test.go b/src/qtmoduleupdater/module_test.go
index 38d341f6..20efc27e 100644
--- a/src/qtmoduleupdater/module_test.go
+++ b/src/qtmoduleupdater/module_test.go
@@ -28,12 +28,9 @@
package main
import (
- "bytes"
"testing"
"github.com/stretchr/testify/assert"
-
- yaml "gopkg.in/yaml.v2"
)
func TestModuleYamlMarshalling(t *testing.T) {
@@ -49,12 +46,10 @@ func TestModuleYamlMarshalling(t *testing.T) {
Required: true,
}
- output := &bytes.Buffer{}
- encoder := yaml.NewEncoder(output)
- encoder.Encode(&module)
- encoder.Close()
-
- yamlStr := output.String()
+ var yamlStr string
+ var err error
+ yamlStr, err = module.ToString()
+ assert.Nil(t, err, "Conversion to yaml string must succeed")
assert.Equal(t, `dependencies:
a:
@@ -84,10 +79,8 @@ func TestProposedUpdateFailsForModulesThatDependOnMoreThanQtBase(t *testing.T) {
yamlObject, err := qtSvg.maybePrepareUpdatedDependenciesYaml(availableModules)
assert.NotNil(t, yamlObject, "Yaml object must be defined for qtsvg")
- yamlStr := &bytes.Buffer{}
- encoder := yaml.NewEncoder(yamlStr)
- encoder.Encode(*yamlObject)
- encoder.Close()
+ yamlStr, err := yamlObject.ToString()
+ assert.Nil(t, err, "Conversion to yaml string must succeed")
assert.Nil(t, err, "It should be possible to create a new dependencies.yaml file for qtsvg")
assert.NotEqual(t, "", yamlStr, "Yaml string must not be empty for qtsvg")