mirror of
https://github.com/mikefarah/yq.git
synced 2025-01-12 19:25:37 +00:00
test coverage and linting
This commit is contained in:
parent
4b3fbb878f
commit
64d1e58f97
@ -7,8 +7,8 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/mikefarah/yq/test"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func getRootCommand() *cobra.Command {
|
||||
|
@ -12,7 +12,7 @@ type JsonConverter interface {
|
||||
JsonToString(context interface{}) (string, error)
|
||||
}
|
||||
|
||||
type jsonConverter struct {}
|
||||
type jsonConverter struct{}
|
||||
|
||||
func NewJsonConverter() JsonConverter {
|
||||
return &jsonConverter{}
|
||||
|
@ -2,6 +2,7 @@ package marshal
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/mikefarah/yq/test"
|
||||
)
|
||||
|
||||
|
@ -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{}
|
||||
|
52
pkg/marshal/yaml_converter_test.go
Normal file
52
pkg/marshal/yaml_converter_test.go
Normal 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)
|
||||
}
|
@ -21,7 +21,7 @@ type navigator struct {
|
||||
}
|
||||
|
||||
func NewDataNavigator(l *logging.Logger) DataNavigator {
|
||||
return &navigator {
|
||||
return &navigator{
|
||||
log: l,
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"github.com/mikefarah/yq/test"
|
||||
logging "gopkg.in/op/go-logging.v1"
|
||||
)
|
||||
|
@ -7,8 +7,8 @@ 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
|
||||
}
|
||||
@ -19,7 +19,7 @@ type lib struct {
|
||||
}
|
||||
|
||||
func NewYqLib(l *logging.Logger) YqLib {
|
||||
return &lib {
|
||||
return &lib{
|
||||
navigator: NewDataNavigator(l),
|
||||
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
|
||||
|
@ -3,6 +3,7 @@ package yqlib
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/mikefarah/yq/test"
|
||||
logging "gopkg.in/op/go-logging.v1"
|
||||
)
|
||||
@ -54,8 +55,8 @@ 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) {
|
||||
@ -100,7 +101,10 @@ b: 2
|
||||
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"]))
|
||||
})
|
||||
|
||||
@ -121,7 +125,10 @@ b: 2
|
||||
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"]))
|
||||
})
|
||||
|
||||
@ -142,7 +149,10 @@ b: 2
|
||||
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"]))
|
||||
})
|
||||
|
||||
|
@ -4,7 +4,7 @@ type PathParser interface {
|
||||
ParsePath(path string) []string
|
||||
}
|
||||
|
||||
type parser struct {}
|
||||
type parser struct{}
|
||||
|
||||
func NewPathParser() PathParser {
|
||||
return &parser{}
|
||||
|
@ -2,6 +2,7 @@ package yqlib
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/mikefarah/yq/test"
|
||||
)
|
||||
|
||||
|
@ -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
3
yq.go
@ -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"
|
||||
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user