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" "strings"
"testing" "testing"
"github.com/spf13/cobra"
"github.com/mikefarah/yq/test" "github.com/mikefarah/yq/test"
"github.com/spf13/cobra"
) )
func getRootCommand() *cobra.Command { func getRootCommand() *cobra.Command {

View File

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

View File

@ -1,9 +1,10 @@
package marshal package marshal
import ( import (
"strings"
yaml "github.com/mikefarah/yaml/v2" yaml "github.com/mikefarah/yaml/v2"
errors "github.com/pkg/errors" errors "github.com/pkg/errors"
"strings"
) )
type YamlConverter interface { type YamlConverter interface {

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

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"sort" "sort"
"testing" "testing"
"github.com/mikefarah/yq/test" "github.com/mikefarah/yq/test"
logging "gopkg.in/op/go-logging.v1" logging "gopkg.in/op/go-logging.v1"
) )

View File

@ -7,8 +7,8 @@ import (
type YqLib interface { type YqLib interface {
ReadPath(dataBucket interface{}, path string) (interface{}, error) ReadPath(dataBucket interface{}, path string) (interface{}, error)
WritePath(dataBucket interface{}, path string, value interface{}) (interface{}) WritePath(dataBucket interface{}, path string, value interface{}) interface{}
PrefixPath(dataBucket interface{}, prefix string) (interface{}) PrefixPath(dataBucket interface{}, prefix string) interface{}
DeletePath(dataBucket interface{}, path string) (interface{}, error) DeletePath(dataBucket interface{}, path string) (interface{}, error)
Merge(dst interface{}, src interface{}, overwrite bool, append bool) error Merge(dst interface{}, src interface{}, overwrite bool, append bool) error
} }
@ -30,12 +30,12 @@ func (l *lib) ReadPath(dataBucket interface{}, path string) (interface{}, error)
return l.navigator.ReadChildValue(dataBucket, paths) 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) var paths = l.parser.ParsePath(path)
return l.navigator.UpdatedChildValue(dataBucket, paths, value) 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) var paths = l.parser.ParsePath(prefix)
// Inverse order // Inverse order

View File

@ -3,6 +3,7 @@ package yqlib
import ( import (
"fmt" "fmt"
"testing" "testing"
"github.com/mikefarah/yq/test" "github.com/mikefarah/yq/test"
logging "gopkg.in/op/go-logging.v1" logging "gopkg.in/op/go-logging.v1"
) )
@ -54,8 +55,8 @@ b:
2: c 2: c
`) `)
got := subject.PrefixPath(data, "d") got := subject.PrefixPath(data, "a.d")
test.AssertResult(t, `[{d [{b [{2 c}]}]}]`, fmt.Sprintf("%v", got)) test.AssertResult(t, `[{a [{d [{b [{2 c}]}]}]}]`, fmt.Sprintf("%v", got))
}) })
t.Run("TestDeletePath", func(t *testing.T) { t.Run("TestDeletePath", func(t *testing.T) {
@ -100,7 +101,10 @@ b: 2
var mapDataBucket = make(map[interface{}]interface{}) var mapDataBucket = make(map[interface{}]interface{})
mapDataBucket["root"] = src 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"])) test.AssertResult(t, `[{a b} {c d}]`, fmt.Sprintf("%v", mergedData["root"]))
}) })
@ -121,7 +125,10 @@ b: 2
var mapDataBucket = make(map[interface{}]interface{}) var mapDataBucket = make(map[interface{}]interface{})
mapDataBucket["root"] = src 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"])) test.AssertResult(t, `[{a 1} {b 2}]`, fmt.Sprintf("%v", mergedData["root"]))
}) })
@ -142,7 +149,10 @@ b: 2
var mapDataBucket = make(map[interface{}]interface{}) var mapDataBucket = make(map[interface{}]interface{})
mapDataBucket["root"] = src 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"])) test.AssertResult(t, `[{a b} {c d} {a 1} {b 2}]`, fmt.Sprintf("%v", mergedData["root"]))
}) })

View File

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

View File

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

3
yq.go
View File

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

View File

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