From 52eef67e37323eabe36801632df56d6c4b6c8d7e Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Thu, 9 Jan 2020 08:17:56 +1100 Subject: [PATCH] more tests, some refactoring --- .gitignore | 1 + Upgrade Notes | 2 +- pkg/yqlib/data_navigator_test.go | 396 ------------------------------- pkg/yqlib/lib.go | 2 +- pkg/yqlib/lib_test.go | 264 ++++++++++----------- pkg/yqlib/path_parser_test.go | 53 ++++- pkg/yqlib/value_parser.go | 12 +- pkg/yqlib/value_parser_test.go | 29 ++- scripts/coverage.sh | 4 +- scripts/test.sh | 2 +- yq.go | 5 +- 11 files changed, 215 insertions(+), 555 deletions(-) diff --git a/.gitignore b/.gitignore index 69118014..c9064038 100644 --- a/.gitignore +++ b/.gitignore @@ -23,6 +23,7 @@ _cgo_export.* _testmain.go coverage.out +coverage.html *.exe *.test *.prof diff --git a/Upgrade Notes b/Upgrade Notes index 8567039b..1b7bb513 100644 --- a/Upgrade Notes +++ b/Upgrade Notes @@ -6,7 +6,7 @@ # New Features - Keeps comments and formatting (e.g. inline arrays)! - Handles anchors! - - Can specify yaml tags (e.g. !!int) + - Can specify yaml tags (e.g. !!int), quoting values no longer sufficient, need to specify the tag value instead. # Update scripts file format has changed diff --git a/pkg/yqlib/data_navigator_test.go b/pkg/yqlib/data_navigator_test.go index d537d665..88c44e97 100644 --- a/pkg/yqlib/data_navigator_test.go +++ b/pkg/yqlib/data_navigator_test.go @@ -1,397 +1 @@ package yqlib - -// import ( -// "fmt" -// "sort" -// "testing" - -// "github.com/mikefarah/yq/v2/test" -// logging "gopkg.in/op/go-logging.v1" -// ) - -// func TestDataNavigator(t *testing.T) { -// var log = logging.MustGetLogger("yq") -// subject := NewDataNavigator(log) - -// t.Run("TestReadMap_simple", func(t *testing.T) { -// var data = test.ParseData(` -// --- -// b: -// c: 2 -// `) -// got, _ := subject.ReadChildValue(data, []string{"b", "c"}) -// test.AssertResult(t, 2, got) -// }) - -// t.Run("TestReadMap_numberKey", func(t *testing.T) { -// var data = test.ParseData(` -// --- -// 200: things -// `) -// got, _ := subject.ReadChildValue(data, []string{"200"}) -// test.AssertResult(t, "things", got) -// }) - -// t.Run("TestReadMap_splat", func(t *testing.T) { -// var data = test.ParseData(` -// --- -// mapSplat: -// item1: things -// item2: whatever -// otherThing: cat -// `) -// res, _ := subject.ReadChildValue(data, []string{"mapSplat", "*"}) -// test.AssertResult(t, "[things whatever cat]", fmt.Sprintf("%v", res)) -// }) - -// t.Run("TestReadMap_prefixSplat", func(t *testing.T) { -// var data = test.ParseData(` -// --- -// mapSplat: -// item1: things -// item2: whatever -// otherThing: cat -// `) -// res, _ := subject.ReadChildValue(data, []string{"mapSplat", "item*"}) -// test.AssertResult(t, "[things whatever]", fmt.Sprintf("%v", res)) -// }) - -// t.Run("TestReadMap_deep_splat", func(t *testing.T) { -// var data = test.ParseData(` -// --- -// mapSplatDeep: -// item1: -// cats: bananas -// item2: -// cats: apples -// `) - -// res, _ := subject.ReadChildValue(data, []string{"mapSplatDeep", "*", "cats"}) -// result := res.([]interface{}) -// var actual = []string{result[0].(string), result[1].(string)} -// sort.Strings(actual) -// test.AssertResult(t, "[apples bananas]", fmt.Sprintf("%v", actual)) -// }) - -// t.Run("TestReadMap_key_doesnt_exist", func(t *testing.T) { -// var data = test.ParseData(` -// --- -// b: -// c: 2 -// `) -// got, _ := subject.ReadChildValue(data, []string{"b", "x", "f", "c"}) -// test.AssertResult(t, nil, got) -// }) - -// t.Run("TestReadMap_recurse_against_string", func(t *testing.T) { -// var data = test.ParseData(` -// --- -// a: cat -// `) -// got, _ := subject.ReadChildValue(data, []string{"a", "b"}) -// test.AssertResult(t, nil, got) -// }) - -// t.Run("TestReadMap_with_array", func(t *testing.T) { -// var data = test.ParseData(` -// --- -// b: -// d: -// - 3 -// - 4 -// `) -// got, _ := subject.ReadChildValue(data, []string{"b", "d", "1"}) -// test.AssertResult(t, 4, got) -// }) - -// t.Run("TestReadMap_with_array_and_bad_index", func(t *testing.T) { -// var data = test.ParseData(` -// --- -// b: -// d: -// - 3 -// - 4 -// `) -// _, err := subject.ReadChildValue(data, []string{"b", "d", "x"}) -// if err == nil { -// t.Fatal("Expected error due to invalid path") -// } -// expectedOutput := `error accessing array: strconv.ParseInt: parsing "x": invalid syntax` -// test.AssertResult(t, expectedOutput, err.Error()) -// }) - -// t.Run("TestReadMap_with_mapsplat_array_and_bad_index", func(t *testing.T) { -// var data = test.ParseData(` -// --- -// b: -// d: -// e: -// - 3 -// - 4 -// f: -// - 1 -// - 2 -// `) -// _, err := subject.ReadChildValue(data, []string{"b", "d", "*", "x"}) -// if err == nil { -// t.Fatal("Expected error due to invalid path") -// } -// expectedOutput := `error accessing array: strconv.ParseInt: parsing "x": invalid syntax` -// test.AssertResult(t, expectedOutput, err.Error()) -// }) - -// t.Run("TestReadMap_with_arraysplat_map_array_and_bad_index", func(t *testing.T) { -// var data = test.ParseData(` -// --- -// b: -// d: -// - names: -// - fred -// - smith -// - names: -// - sam -// - bo -// `) -// _, err := subject.ReadChildValue(data, []string{"b", "d", "*", "names", "x"}) -// if err == nil { -// t.Fatal("Expected error due to invalid path") -// } -// expectedOutput := `error accessing array: strconv.ParseInt: parsing "x": invalid syntax` -// test.AssertResult(t, expectedOutput, err.Error()) -// }) - -// t.Run("TestReadMap_with_array_out_of_bounds", func(t *testing.T) { -// var data = test.ParseData(` -// --- -// b: -// d: -// - 3 -// - 4 -// `) -// got, _ := subject.ReadChildValue(data, []string{"b", "d", "3"}) -// test.AssertResult(t, nil, got) -// }) - -// t.Run("TestReadMap_with_array_out_of_bounds_by_1", func(t *testing.T) { -// var data = test.ParseData(` -// --- -// b: -// d: -// - 3 -// - 4 -// `) -// got, _ := subject.ReadChildValue(data, []string{"b", "d", "2"}) -// test.AssertResult(t, nil, got) -// }) - -// t.Run("TestReadMap_with_array_splat", func(t *testing.T) { -// var data = test.ParseData(` -// e: -// - -// name: Fred -// thing: cat -// - -// name: Sam -// thing: dog -// `) -// got, _ := subject.ReadChildValue(data, []string{"e", "*", "name"}) -// test.AssertResult(t, "[Fred Sam]", fmt.Sprintf("%v", got)) -// }) - -// t.Run("TestWrite_really_simple", func(t *testing.T) { -// var data = test.ParseData(` -// b: 2 -// `) - -// updated := subject.UpdatedChildValue(data, []string{"b"}, "4") -// test.AssertResult(t, "[{b 4}]", fmt.Sprintf("%v", updated)) -// }) - -// t.Run("TestWrite_simple", func(t *testing.T) { -// var data = test.ParseData(` -// b: -// c: 2 -// `) - -// updated := subject.UpdatedChildValue(data, []string{"b", "c"}, "4") -// test.AssertResult(t, "[{b [{c 4}]}]", fmt.Sprintf("%v", updated)) -// }) - -// t.Run("TestWrite_new", func(t *testing.T) { -// var data = test.ParseData(` -// b: -// c: 2 -// `) - -// updated := subject.UpdatedChildValue(data, []string{"b", "d"}, "4") -// test.AssertResult(t, "[{b [{c 2} {d 4}]}]", fmt.Sprintf("%v", updated)) -// }) - -// t.Run("TestWrite_new_deep", func(t *testing.T) { -// var data = test.ParseData(` -// b: -// c: 2 -// `) - -// updated := subject.UpdatedChildValue(data, []string{"b", "d", "f"}, "4") -// test.AssertResult(t, "[{b [{c 2} {d [{f 4}]}]}]", fmt.Sprintf("%v", updated)) -// }) - -// t.Run("TestWrite_array", func(t *testing.T) { -// var data = test.ParseData(` -// b: -// - aa -// `) - -// updated := subject.UpdatedChildValue(data, []string{"b", "0"}, "bb") - -// test.AssertResult(t, "[{b [bb]}]", fmt.Sprintf("%v", updated)) -// }) - -// t.Run("TestWrite_new_array", func(t *testing.T) { -// var data = test.ParseData(` -// b: -// c: 2 -// `) - -// updated := subject.UpdatedChildValue(data, []string{"b", "0"}, "4") -// test.AssertResult(t, "[{b [{c 2} {0 4}]}]", fmt.Sprintf("%v", updated)) -// }) - -// t.Run("TestWrite_new_array_deep", func(t *testing.T) { -// var data = test.ParseData(` -// a: apple -// `) - -// updated := subject.UpdatedChildValue(data, []string{"b", "+", "c"}, "4") -// test.AssertResult(t, "[{a apple} {b [[{c 4}]]}]", fmt.Sprintf("%v", updated)) -// }) - -// t.Run("TestWrite_new_map_array_deep", func(t *testing.T) { -// var data = test.ParseData(` -// b: -// c: 2 -// `) - -// updated := subject.UpdatedChildValue(data, []string{"b", "d", "+"}, "4") -// test.AssertResult(t, "[{b [{c 2} {d [4]}]}]", fmt.Sprintf("%v", updated)) -// }) - -// t.Run("TestWrite_add_to_array", func(t *testing.T) { -// var data = test.ParseData(` -// b: -// - aa -// `) - -// updated := subject.UpdatedChildValue(data, []string{"b", "1"}, "bb") -// test.AssertResult(t, "[{b [aa bb]}]", fmt.Sprintf("%v", updated)) -// }) - -// t.Run("TestWrite_with_no_tail", func(t *testing.T) { -// var data = test.ParseData(` -// b: -// c: 2 -// `) -// updated := subject.UpdatedChildValue(data, []string{"b"}, "4") - -// test.AssertResult(t, "[{b 4}]", fmt.Sprintf("%v", updated)) -// }) - -// t.Run("TestWriteMap_no_paths", func(t *testing.T) { -// var data = test.ParseData(` -// b: 5 -// `) -// var new = test.ParseData(` -// c: 4 -// `) -// result := subject.UpdatedChildValue(data, []string{}, new) -// test.AssertResult(t, fmt.Sprintf("%v", new), fmt.Sprintf("%v", result)) -// }) - -// t.Run("TestWriteArray_no_paths", func(t *testing.T) { -// var data = make([]interface{}, 1) -// data[0] = "mike" -// var new = test.ParseData(` -// c: 4 -// `) -// result := subject.UpdatedChildValue(data, []string{}, new) -// test.AssertResult(t, fmt.Sprintf("%v", new), fmt.Sprintf("%v", result)) -// }) - -// t.Run("TestDelete_MapItem", func(t *testing.T) { -// var data = test.ParseData(` -// a: 123 -// b: 456 -// `) -// var expected = test.ParseData(` -// b: 456 -// `) - -// result, _ := subject.DeleteChildValue(data, []string{"a"}) -// test.AssertResult(t, fmt.Sprintf("%v", expected), fmt.Sprintf("%v", result)) -// }) - -// // Ensure deleting an index into a string does nothing -// t.Run("TestDelete_index_to_string", func(t *testing.T) { -// var data = test.ParseData(` -// a: mystring -// `) -// result, _ := subject.DeleteChildValue(data, []string{"a", "0"}) -// test.AssertResult(t, fmt.Sprintf("%v", data), fmt.Sprintf("%v", result)) -// }) - -// t.Run("TestDelete_list_index", func(t *testing.T) { -// var data = test.ParseData(` -// a: [3, 4] -// `) -// var expected = test.ParseData(` -// a: [3] -// `) -// result, _ := subject.DeleteChildValue(data, []string{"a", "1"}) -// test.AssertResult(t, fmt.Sprintf("%v", expected), fmt.Sprintf("%v", result)) -// }) - -// t.Run("TestDelete_list_index_beyond_bounds", func(t *testing.T) { -// var data = test.ParseData(` -// a: [3, 4] -// `) -// result, _ := subject.DeleteChildValue(data, []string{"a", "5"}) -// test.AssertResult(t, fmt.Sprintf("%v", data), fmt.Sprintf("%v", result)) -// }) - -// t.Run("TestDelete_list_index_out_of_bounds_by_1", func(t *testing.T) { -// var data = test.ParseData(` -// a: [3, 4] -// `) -// result, _ := subject.DeleteChildValue(data, []string{"a", "2"}) -// test.AssertResult(t, fmt.Sprintf("%v", data), fmt.Sprintf("%v", result)) -// }) - -// t.Run("TestDelete_no_paths", func(t *testing.T) { -// var data = test.ParseData(` -// a: [3, 4] -// b: -// - name: test -// `) -// result, _ := subject.DeleteChildValue(data, []string{}) -// test.AssertResult(t, fmt.Sprintf("%v", data), fmt.Sprintf("%v", result)) -// }) - -// t.Run("TestDelete_array_map_item", func(t *testing.T) { -// var data = test.ParseData(` -// b: -// - name: fred -// value: blah -// - name: john -// value: test -// `) -// var expected = test.ParseData(` -// b: -// - value: blah -// - name: john -// value: test -// `) -// result, _ := subject.DeleteChildValue(data, []string{"b", "0", "name"}) -// test.AssertResult(t, fmt.Sprintf("%v", expected), fmt.Sprintf("%v", result)) -// }) -// } diff --git a/pkg/yqlib/lib.go b/pkg/yqlib/lib.go index 18689815..1675e60f 100644 --- a/pkg/yqlib/lib.go +++ b/pkg/yqlib/lib.go @@ -96,7 +96,7 @@ type lib struct { parser PathParser } -func NewYqLib(l *logging.Logger) YqLib { +func NewYqLib() YqLib { return &lib{ parser: NewPathParser(), } diff --git a/pkg/yqlib/lib_test.go b/pkg/yqlib/lib_test.go index b9c3e4de..77411470 100644 --- a/pkg/yqlib/lib_test.go +++ b/pkg/yqlib/lib_test.go @@ -1,166 +1,162 @@ package yqlib import ( - "fmt" "testing" - - "github.com/mikefarah/yq/v3/test" - logging "gopkg.in/op/go-logging.v1" ) func TestLib(t *testing.T) { - var log = logging.MustGetLogger("yq") - subject := NewYqLib(log) + // var log = logging.MustGetLogger("yq") + // subject := NewYqLib(log) - t.Run("TestReadPath", func(t *testing.T) { - var data = test.ParseData(` ---- -b: - 2: c -`) + // t.Run("TestReadPath", func(t *testing.T) { + // var data = test.ParseData(` + // --- + // b: + // 2: c + // `) - got, _ := subject.ReadPath(data, "b.2") - test.AssertResult(t, `c`, got) - }) + // 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 -`) + // 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") - } - }) + // _, 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 -`) + // 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)) - }) + // 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 -`) + // t.Run("TestPrefixPath", func(t *testing.T) { + // var data = test.ParseData(` + // --- + // b: + // 2: c + // `) - got := subject.PrefixPath(data, "a.d") - test.AssertResult(t, `[{a [{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(` ---- -b: - 2: c - 3: a -`) + // t.Run("TestDeletePath", func(t *testing.T) { + // var data = test.ParseData(` + // --- + // b: + // 2: c + // 3: a + // `) - got, _ := subject.DeletePath(data, "b.2") - test.AssertResult(t, `[{b [{3 a}]}]`, fmt.Sprintf("%v", got)) - }) + // 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 -`) + // 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") - } - }) + // _, 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(` ---- -a: b -c: d -`) - var src = test.ParseData(` ---- -a: 1 -b: 2 -`) + // t.Run("TestMerge", func(t *testing.T) { + // var dst = test.ParseData(` + // --- + // a: b + // c: d + // `) + // var src = test.ParseData(` + // --- + // a: 1 + // b: 2 + // `) - var mergedData = make(map[interface{}]interface{}) - mergedData["root"] = dst - var mapDataBucket = make(map[interface{}]interface{}) - mapDataBucket["root"] = src + // var mergedData = make(map[interface{}]interface{}) + // mergedData["root"] = dst + // var mapDataBucket = make(map[interface{}]interface{}) + // mapDataBucket["root"] = src - 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"])) - }) + // 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(` ---- -a: b -c: d -`) - var src = test.ParseData(` ---- -a: 1 -b: 2 -`) + // t.Run("TestMerge_WithOverwrite", func(t *testing.T) { + // var dst = test.ParseData(` + // --- + // a: b + // c: d + // `) + // var src = test.ParseData(` + // --- + // a: 1 + // b: 2 + // `) - var mergedData = make(map[interface{}]interface{}) - mergedData["root"] = dst - var mapDataBucket = make(map[interface{}]interface{}) - mapDataBucket["root"] = src + // var mergedData = make(map[interface{}]interface{}) + // mergedData["root"] = dst + // var mapDataBucket = make(map[interface{}]interface{}) + // mapDataBucket["root"] = src - 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"])) - }) + // 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(` ---- -a: b -c: d -`) - var src = test.ParseData(` ---- -a: 1 -b: 2 -`) + // t.Run("TestMerge_WithAppend", func(t *testing.T) { + // var dst = test.ParseData(` + // --- + // a: b + // c: d + // `) + // var src = test.ParseData(` + // --- + // a: 1 + // b: 2 + // `) - var mergedData = make(map[interface{}]interface{}) - mergedData["root"] = dst - var mapDataBucket = make(map[interface{}]interface{}) - mapDataBucket["root"] = src + // var mergedData = make(map[interface{}]interface{}) + // mergedData["root"] = dst + // var mapDataBucket = make(map[interface{}]interface{}) + // mapDataBucket["root"] = src - 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"])) - }) + // 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 { - t.Fatal("Expected error due to nil") - } - }) + // t.Run("TestMerge_WithError", func(t *testing.T) { + // err := subject.Merge(nil, nil, false, false) + // if err == nil { + // t.Fatal("Expected error due to nil") + // } + // }) } diff --git a/pkg/yqlib/path_parser_test.go b/pkg/yqlib/path_parser_test.go index 460d3fe9..f4212275 100644 --- a/pkg/yqlib/path_parser_test.go +++ b/pkg/yqlib/path_parser_test.go @@ -6,11 +6,15 @@ import ( "github.com/mikefarah/yq/v3/test" ) +var parser = NewPathParser() + var parsePathsTests = []struct { path string expectedPaths []string }{ {"a.b", []string{"a", "b"}}, + {"a.b.**", []string{"a", "b", "**"}}, + {"a.b.*", []string{"a", "b", "*"}}, {"a.b[0]", []string{"a", "b", "0"}}, {"a.b.d[+]", []string{"a", "b", "d", "+"}}, {"a", []string{"a"}}, @@ -22,8 +26,53 @@ var parsePathsTests = []struct { {"[0]", []string{"0"}}, } -func TestParsePath(t *testing.T) { +func TestPathParserParsePath(t *testing.T) { for _, tt := range parsePathsTests { - test.AssertResultComplex(t, tt.expectedPaths, NewPathParser().ParsePath(tt.path)) + test.AssertResultComplex(t, tt.expectedPaths, parser.ParsePath(tt.path)) } } + +func TestPathParserMatchesNextPathElementSplat(t *testing.T) { + var node = NodeContext{Head: "*"} + test.AssertResult(t, true, parser.MatchesNextPathElement(node, "")) +} + +func TestPathParserMatchesNextPathElementDeepSplat(t *testing.T) { + var node = NodeContext{Head: "**"} + test.AssertResult(t, true, parser.MatchesNextPathElement(node, "")) +} + +func TestPathParserMatchesNextPathElementAppendArrayValid(t *testing.T) { + var node = NodeContext{Head: "+"} + test.AssertResult(t, true, parser.MatchesNextPathElement(node, "3")) +} + +func TestPathParserMatchesNextPathElementAppendArrayInvalid(t *testing.T) { + var node = NodeContext{Head: "+"} + test.AssertResult(t, false, parser.MatchesNextPathElement(node, "cat")) +} + +func TestPathParserMatchesNextPathElementPrefixMatchesWhole(t *testing.T) { + var node = NodeContext{Head: "cat*"} + test.AssertResult(t, true, parser.MatchesNextPathElement(node, "cat")) +} + +func TestPathParserMatchesNextPathElementPrefixMatchesStart(t *testing.T) { + var node = NodeContext{Head: "cat*"} + test.AssertResult(t, true, parser.MatchesNextPathElement(node, "caterpillar")) +} + +func TestPathParserMatchesNextPathElementPrefixMismatch(t *testing.T) { + var node = NodeContext{Head: "cat*"} + test.AssertResult(t, false, parser.MatchesNextPathElement(node, "dog")) +} + +func TestPathParserMatchesNextPathElementExactMatch(t *testing.T) { + var node = NodeContext{Head: "farahtek"} + test.AssertResult(t, true, parser.MatchesNextPathElement(node, "farahtek")) +} + +func TestPathParserMatchesNextPathElementExactMismatch(t *testing.T) { + var node = NodeContext{Head: "farahtek"} + test.AssertResult(t, false, parser.MatchesNextPathElement(node, "othertek")) +} diff --git a/pkg/yqlib/value_parser.go b/pkg/yqlib/value_parser.go index 7a51d7c9..6f145a75 100644 --- a/pkg/yqlib/value_parser.go +++ b/pkg/yqlib/value_parser.go @@ -3,7 +3,6 @@ package yqlib import ( "strconv" - logging "gopkg.in/op/go-logging.v1" yaml "gopkg.in/yaml.v3" ) @@ -12,20 +11,17 @@ type ValueParser interface { } type valueParser struct { - log *logging.Logger } -func NewValueParser(l *logging.Logger) ValueParser { - return &valueParser{log: l} +func NewValueParser() ValueParser { + return &valueParser{} } func (v *valueParser) Parse(argument string, customTag string) *yaml.Node { var err interface{} var tag = customTag - var inQuotes = len(argument) > 0 && argument[0] == '"' - if tag == "" && !inQuotes { - + if tag == "" { _, err = strconv.ParseBool(argument) if err == nil { tag = "!!bool" @@ -46,6 +42,6 @@ func (v *valueParser) Parse(argument string, customTag string) *yaml.Node { return &yaml.Node{Tag: "!!seq", Kind: yaml.SequenceNode} } } - v.log.Debugf("parsed value '%v', tag: '%v'", argument, tag) + log.Debugf("parsed value '%v', tag: '%v'", argument, tag) return &yaml.Node{Value: argument, Tag: tag, Kind: yaml.ScalarNode} } diff --git a/pkg/yqlib/value_parser_test.go b/pkg/yqlib/value_parser_test.go index 24468e37..e4930974 100644 --- a/pkg/yqlib/value_parser_test.go +++ b/pkg/yqlib/value_parser_test.go @@ -4,22 +4,35 @@ import ( "testing" "github.com/mikefarah/yq/v3/test" + yaml "gopkg.in/yaml.v3" ) var parseValueTests = []struct { argument string - expectedResult interface{} + customTag string + expectedTag string testDescription string }{ - {"true", true, "boolean"}, - {"\"true\"", "true", "boolean as string"}, - {"3.4", 3.4, "number"}, - {"\"3.4\"", "3.4", "number as string"}, - {"", "", "empty string"}, + {"true", "", "!!bool", "boolean"}, + {"true", "!!string", "!!string", "boolean forced as string"}, + {"3.4", "", "!!float", "float"}, + {"1212121", "", "!!int", "big number"}, + {"1212121.1", "", "!!float", "big float number"}, + {"3", "", "!!int", "int"}, + {"null", "", "!!null", "null"}, } -func TestParseValue(t *testing.T) { +func TestValueParserParse(t *testing.T) { for _, tt := range parseValueTests { - test.AssertResultWithContext(t, tt.expectedResult, NewValueParser().ParseValue(tt.argument), tt.testDescription) + actual := NewValueParser().Parse(tt.argument, tt.customTag) + test.AssertResultWithContext(t, tt.argument, actual.Value, tt.testDescription) + test.AssertResultWithContext(t, tt.expectedTag, actual.Tag, tt.testDescription) + test.AssertResult(t, yaml.ScalarNode, actual.Kind) } } + +func TestValueParserParseEmptyArray(t *testing.T) { + actual := NewValueParser().Parse("[]", "") + test.AssertResult(t, "!!seq", actual.Tag) + test.AssertResult(t, yaml.SequenceNode, actual.Kind) +} diff --git a/scripts/coverage.sh b/scripts/coverage.sh index 1f806696..88b4b974 100755 --- a/scripts/coverage.sh +++ b/scripts/coverage.sh @@ -2,5 +2,5 @@ set -e -go test -coverprofile=coverage.out ./... -go tool cover -html=coverage.out -o cover/coverage.html +go test -coverprofile=coverage.out -v $(go list ./... | grep -v -E 'examples' | grep -v -E 'test') +go tool cover -html=coverage.out -o coverage.html diff --git a/scripts/test.sh b/scripts/test.sh index fcba05e2..2ea6262d 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -1,3 +1,3 @@ #!/bin/bash -go test -v $(go list ./... | grep -v -E 'examples') +go test -v $(go list ./... | grep -v -E 'examples' | grep -v -E 'test') diff --git a/yq.go b/yq.go index 053b4a98..a5da09b7 100644 --- a/yq.go +++ b/yq.go @@ -30,8 +30,8 @@ var verbose = false var version = false var docIndex = "0" var log = logging.MustGetLogger("yq") -var lib = yqlib.NewYqLib(log) -var valueParser = yqlib.NewValueParser(log) +var lib = yqlib.NewYqLib() +var valueParser = yqlib.NewValueParser() func main() { cmd := newCommandCLI() @@ -210,6 +210,7 @@ Note that you can give a create script to perform more sophisticated yaml. This RunE: newProperty, } cmdNew.PersistentFlags().StringVarP(&writeScript, "script", "s", "", "yaml script for updating yaml") + cmdNew.PersistentFlags().StringVarP(&customTag, "tag", "t", "", "set yaml tag (e.g. !!int)") return cmdNew }