Small increase in test coverage
Improving test coverage Change-Id: I0e4da35f24b16d143cf0e4045182ae7d93d13b1c
This commit is contained in:
parent
f8f6f8be27
commit
3101e642ce
1
go.sum
1
go.sum
@ -478,6 +478,7 @@ github.com/spf13/viper v1.0.2 h1:Ncr3ZIuJn322w2k1qmzXDnkLAdQMlJqBa9kfAH+irso=
|
||||
github.com/spf13/viper v1.0.2/go.mod h1:A8kyI5cUJhb8N+3pkfONlcEcZbueH6nhAm0Fq7SrnBM=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.2.0 h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48=
|
||||
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
|
||||
github.com/stretchr/testify v0.0.0-20151208002404-e3a8ff8ce365/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
|
||||
|
@ -6,6 +6,13 @@ import (
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
const (
|
||||
// DotYamlSeparator yaml separator
|
||||
DotYamlSeparator = "...\n"
|
||||
// DashYamlSeparator yaml separator
|
||||
DashYamlSeparator = "---\n"
|
||||
)
|
||||
|
||||
// WriteOut dumps any yaml competible document to writer, adding yaml separator `---`
|
||||
// at the beginning of the document, and `...` at the end
|
||||
func WriteOut(dst io.Writer, src interface{}) error {
|
||||
@ -15,7 +22,7 @@ func WriteOut(dst io.Writer, src interface{}) error {
|
||||
return err
|
||||
}
|
||||
// add separator for each document
|
||||
_, err = dst.Write([]byte("---\n"))
|
||||
_, err = dst.Write([]byte(DashYamlSeparator))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -24,7 +31,7 @@ func WriteOut(dst io.Writer, src interface{}) error {
|
||||
return err
|
||||
}
|
||||
// add separator for each document
|
||||
_, err = dst.Write([]byte("...\n"))
|
||||
_, err = dst.Write([]byte(DotYamlSeparator))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package yaml_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"regexp"
|
||||
"testing"
|
||||
"time"
|
||||
@ -13,6 +14,25 @@ import (
|
||||
utilyaml "opendev.org/airship/airshipctl/pkg/util/yaml"
|
||||
)
|
||||
|
||||
// FakeYaml Used to emulate yaml
|
||||
type FakeYaml struct {
|
||||
Key string
|
||||
}
|
||||
|
||||
// FakeErrorDashWriter fake object to simulate errors of writer
|
||||
type FakeErrorDashWriter struct {
|
||||
Err error
|
||||
Match string
|
||||
}
|
||||
|
||||
func (f *FakeErrorDashWriter) Write(b []byte) (int, error) {
|
||||
if string(b) == f.Match {
|
||||
// arbitrary error from io package
|
||||
return 0, f.Err
|
||||
}
|
||||
return len(b), nil
|
||||
}
|
||||
|
||||
func TestWriteOut(t *testing.T) {
|
||||
|
||||
// Create some object, that can be marshaled into yaml
|
||||
@ -53,3 +73,26 @@ func TestWriteOut(t *testing.T) {
|
||||
// Compare original object with reverse marshaled
|
||||
assert.Equal(t, ob, &rob)
|
||||
}
|
||||
|
||||
func TestWriteOutErrorsWrongYaml(t *testing.T) {
|
||||
src := make(chan int)
|
||||
var b bytes.Buffer
|
||||
assert.Error(t, utilyaml.WriteOut(&b, src))
|
||||
}
|
||||
|
||||
func TestWriteOutErrorsErrorWriter(t *testing.T) {
|
||||
// Some easy to match yaml
|
||||
fakeYaml := FakeYaml{Key: "value"}
|
||||
fakeWriter := &FakeErrorDashWriter{Err: io.ErrUnexpectedEOF}
|
||||
|
||||
strings := []string{
|
||||
utilyaml.DotYamlSeparator,
|
||||
utilyaml.DashYamlSeparator,
|
||||
"Key: value\n",
|
||||
}
|
||||
for _, str := range strings {
|
||||
fakeWriter.Match = str
|
||||
assert.Error(t, utilyaml.WriteOut(fakeWriter, fakeYaml))
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user