test coverage and linting

This commit is contained in:
Conor Nosal 2019-12-01 15:10:42 -05:00 committed by Mike Farah
parent 4b3fbb878f
commit 64d1e58f97
14 changed files with 115 additions and 47 deletions

View File

@ -7,8 +7,8 @@ import (
"strings"
"testing"
"github.com/spf13/cobra"
"github.com/mikefarah/yq/test"
"github.com/spf13/cobra"
)
func getRootCommand() *cobra.Command {

View File

@ -12,7 +12,7 @@ type JsonConverter interface {
JsonToString(context interface{}) (string, error)
}
type jsonConverter struct {}
type jsonConverter struct{}
func NewJsonConverter() JsonConverter {
return &jsonConverter{}

View File

@ -2,6 +2,7 @@ package marshal
import (
"testing"
"github.com/mikefarah/yq/test"
)

View File

@ -1,16 +1,17 @@
package marshal
import (
"strings"
yaml "github.com/mikefarah/yaml/v2"
errors "github.com/pkg/errors"
"strings"
)
type YamlConverter interface {
YamlToString(context interface{}, trimOutput bool) (string, error)
}
type yamlConverter struct {}
type yamlConverter struct{}
func NewYamlConverter() YamlConverter {
return &yamlConverter{}
@ -39,4 +40,4 @@ func (y *yamlConverter) marshalContext(context interface{}, trimOutput bool) (st
return strings.Trim(outStr, "\n "), nil
}
return outStr, nil
}
}

View File

@ -0,0 +1,52 @@
package marshal
import (
"testing"
"github.com/mikefarah/yq/test"
)
func TestYamlToString(t *testing.T) {
var raw = `b:
c: 2
`
var data = test.ParseData(raw)
got, _ := NewYamlConverter().YamlToString(data, false)
test.AssertResult(t, raw, got)
}
func TestYamlToString_withTrim(t *testing.T) {
var raw = `b:
c: 2`
var data = test.ParseData(raw)
got, _ := NewYamlConverter().YamlToString(data, true)
test.AssertResult(t, raw, got)
}
func TestYamlToString_withIntKey(t *testing.T) {
var raw = `b:
2: c
`
var data = test.ParseData(raw)
got, _ := NewYamlConverter().YamlToString(data, false)
test.AssertResult(t, raw, got)
}
func TestYamlToString_withBoolKey(t *testing.T) {
var raw = `b:
false: c
`
var data = test.ParseData(raw)
got, _ := NewYamlConverter().YamlToString(data, false)
test.AssertResult(t, raw, got)
}
func TestYamlToString_withArray(t *testing.T) {
var raw = `b:
- item: one
- item: two
`
var data = test.ParseData(raw)
got, _ := NewYamlConverter().YamlToString(data, false)
test.AssertResult(t, raw, got)
}

View File

@ -21,7 +21,7 @@ type navigator struct {
}
func NewDataNavigator(l *logging.Logger) DataNavigator {
return &navigator {
return &navigator{
log: l,
}
}

View File

@ -4,6 +4,7 @@ import (
"fmt"
"sort"
"testing"
"github.com/mikefarah/yq/test"
logging "gopkg.in/op/go-logging.v1"
)
@ -393,4 +394,4 @@ b:
result, _ := subject.DeleteChildValue(data, []string{"b", "0", "name"})
test.AssertResult(t, fmt.Sprintf("%v", expected), fmt.Sprintf("%v", result))
})
}
}

View File

@ -7,21 +7,21 @@ import (
type YqLib interface {
ReadPath(dataBucket interface{}, path string) (interface{}, error)
WritePath(dataBucket interface{}, path string, value interface{}) (interface{})
PrefixPath(dataBucket interface{}, prefix string) (interface{})
WritePath(dataBucket interface{}, path string, value interface{}) interface{}
PrefixPath(dataBucket interface{}, prefix string) interface{}
DeletePath(dataBucket interface{}, path string) (interface{}, error)
Merge(dst interface{}, src interface{}, overwrite bool, append bool) error
}
type lib struct {
navigator DataNavigator
parser PathParser
parser PathParser
}
func NewYqLib(l *logging.Logger) YqLib {
return &lib {
return &lib{
navigator: NewDataNavigator(l),
parser: NewPathParser(),
parser: NewPathParser(),
}
}
@ -30,12 +30,12 @@ func (l *lib) ReadPath(dataBucket interface{}, path string) (interface{}, error)
return l.navigator.ReadChildValue(dataBucket, paths)
}
func (l *lib) WritePath(dataBucket interface{}, path string, value interface{}) (interface{}) {
func (l *lib) WritePath(dataBucket interface{}, path string, value interface{}) interface{} {
var paths = l.parser.ParsePath(path)
return l.navigator.UpdatedChildValue(dataBucket, paths, value)
}
func (l *lib) PrefixPath(dataBucket interface{}, prefix string) (interface{}) {
func (l *lib) PrefixPath(dataBucket interface{}, prefix string) interface{} {
var paths = l.parser.ParsePath(prefix)
// Inverse order
@ -65,4 +65,4 @@ func (l *lib) Merge(dst interface{}, src interface{}, overwrite bool, append boo
return mergo.Merge(dst, src, mergo.WithAppendSlice)
}
return mergo.Merge(dst, src)
}
}

View File

@ -3,6 +3,7 @@ package yqlib
import (
"fmt"
"testing"
"github.com/mikefarah/yq/test"
logging "gopkg.in/op/go-logging.v1"
)
@ -18,46 +19,46 @@ func TestLib(t *testing.T) {
b:
2: c
`)
got, _ := subject.ReadPath(data, "b.2")
test.AssertResult(t, `c`, got)
})
t.Run("TestReadPath_WithError", func(t *testing.T) {
var data = test.ParseData(`
---
b:
- c
`)
_, err := subject.ReadPath(data, "b.[a]")
if err == nil {
t.Fatal("Expected error due to invalid path")
}
})
t.Run("TestWritePath", func(t *testing.T) {
var data = test.ParseData(`
---
b:
2: c
`)
got := subject.WritePath(data, "b.3", "a")
test.AssertResult(t, `[{b [{2 c} {3 a}]}]`, fmt.Sprintf("%v", got))
})
t.Run("TestPrefixPath", func(t *testing.T) {
var data = test.ParseData(`
---
b:
2: c
`)
got := subject.PrefixPath(data, "d")
test.AssertResult(t, `[{d [{b [{2 c}]}]}]`, fmt.Sprintf("%v", got))
got := subject.PrefixPath(data, "a.d")
test.AssertResult(t, `[{a [{d [{b [{2 c}]}]}]}]`, fmt.Sprintf("%v", got))
})
t.Run("TestDeletePath", func(t *testing.T) {
var data = test.ParseData(`
---
@ -65,24 +66,24 @@ b:
2: c
3: a
`)
got, _ := subject.DeletePath(data, "b.2")
test.AssertResult(t, `[{b [{3 a}]}]`, fmt.Sprintf("%v", got))
})
t.Run("TestDeletePath_WithError", func(t *testing.T) {
var data = test.ParseData(`
---
b:
- c
`)
_, err := subject.DeletePath(data, "b.[a]")
if err == nil {
t.Fatal("Expected error due to invalid path")
}
})
t.Run("TestMerge", func(t *testing.T) {
var dst = test.ParseData(`
---
@ -94,16 +95,19 @@ c: d
a: 1
b: 2
`)
var mergedData = make(map[interface{}]interface{})
mergedData["root"] = dst
var mapDataBucket = make(map[interface{}]interface{})
mapDataBucket["root"] = src
subject.Merge(&mergedData, mapDataBucket, false, false)
err := subject.Merge(&mergedData, mapDataBucket, false, false)
if err != nil {
t.Fatal("Unexpected error")
}
test.AssertResult(t, `[{a b} {c d}]`, fmt.Sprintf("%v", mergedData["root"]))
})
t.Run("TestMerge_WithOverwrite", func(t *testing.T) {
var dst = test.ParseData(`
---
@ -115,16 +119,19 @@ c: d
a: 1
b: 2
`)
var mergedData = make(map[interface{}]interface{})
mergedData["root"] = dst
var mapDataBucket = make(map[interface{}]interface{})
mapDataBucket["root"] = src
subject.Merge(&mergedData, mapDataBucket, true, false)
err := subject.Merge(&mergedData, mapDataBucket, true, false)
if err != nil {
t.Fatal("Unexpected error")
}
test.AssertResult(t, `[{a 1} {b 2}]`, fmt.Sprintf("%v", mergedData["root"]))
})
t.Run("TestMerge_WithAppend", func(t *testing.T) {
var dst = test.ParseData(`
---
@ -136,16 +143,19 @@ c: d
a: 1
b: 2
`)
var mergedData = make(map[interface{}]interface{})
mergedData["root"] = dst
var mapDataBucket = make(map[interface{}]interface{})
mapDataBucket["root"] = src
subject.Merge(&mergedData, mapDataBucket, false, true)
err := subject.Merge(&mergedData, mapDataBucket, false, true)
if err != nil {
t.Fatal("Unexpected error")
}
test.AssertResult(t, `[{a b} {c d} {a 1} {b 2}]`, fmt.Sprintf("%v", mergedData["root"]))
})
t.Run("TestMerge_WithError", func(t *testing.T) {
err := subject.Merge(nil, nil, false, false)
if err == nil {
@ -153,4 +163,4 @@ b: 2
}
})
}
}

View File

@ -4,7 +4,7 @@ type PathParser interface {
ParsePath(path string) []string
}
type parser struct {}
type parser struct{}
func NewPathParser() PathParser {
return &parser{}

View File

@ -2,6 +2,7 @@ package yqlib
import (
"testing"
"github.com/mikefarah/yq/test"
)
@ -25,4 +26,4 @@ func TestParsePath(t *testing.T) {
for _, tt := range parsePathsTests {
test.AssertResultComplex(t, tt.expectedPaths, NewPathParser().ParsePath(tt.path))
}
}
}

View File

@ -2,5 +2,5 @@
set -e
go test -coverprofile=coverage.out
go test -coverprofile=coverage.out ./...
go tool cover -html=coverage.out -o cover/coverage.html

3
yq.go
View File

@ -9,8 +9,9 @@ import (
"reflect"
"strconv"
"strings"
"github.com/mikefarah/yq/pkg/yqlib"
"github.com/mikefarah/yq/pkg/marshal"
"github.com/mikefarah/yq/pkg/yqlib"
errors "github.com/pkg/errors"

View File

@ -4,8 +4,9 @@ import (
"fmt"
"runtime"
"testing"
"github.com/mikefarah/yq/test"
"github.com/mikefarah/yq/pkg/marshal"
"github.com/mikefarah/yq/test"
)
var parseValueTests = []struct {