From b9e304e7a4847644ef8f098641b9ce05820332d1 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Thu, 11 Jun 2020 09:13:55 +1000 Subject: [PATCH] Can stripComments and explodeAnchors for compare --- cmd/compare.go | 2 ++ cmd/compare_test.go | 49 ++++++++++++++++++++++++++++ examples/data1-no-comments.yaml | 4 +++ examples/sample2.yaml | 2 -- examples/simple-anchor-exploded.yaml | 5 +++ 5 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 examples/data1-no-comments.yaml delete mode 100644 examples/sample2.yaml create mode 100644 examples/simple-anchor-exploded.yaml diff --git a/cmd/compare.go b/cmd/compare.go index f11fa615..f6b3188f 100644 --- a/cmd/compare.go +++ b/cmd/compare.go @@ -31,6 +31,8 @@ yq x -d1 dataA.yaml dataB.yaml 'a.b.c' cmdCompare.PersistentFlags().StringVarP(&docIndex, "doc", "d", "0", "process document index number (0 based, * for all documents)") cmdCompare.PersistentFlags().StringVarP(&printMode, "printMode", "p", "v", "print mode (v (values, default), p (paths), pv (path and value pairs)") cmdCompare.PersistentFlags().StringVarP(&defaultValue, "defaultValue", "D", "", "default value printed when there are no results") + cmdCompare.PersistentFlags().BoolVarP(&stripComments, "stripComments", "", false, "strip comments out before comparing") + cmdCompare.PersistentFlags().BoolVarP(&explodeAnchors, "explodeAnchors", "X", false, "explode anchors") return cmdCompare } diff --git a/cmd/compare_test.go b/cmd/compare_test.go index 630c5500..903b1521 100644 --- a/cmd/compare_test.go +++ b/cmd/compare_test.go @@ -16,6 +16,55 @@ func TestCompareSameCmd(t *testing.T) { test.AssertResult(t, expectedOutput, result.Output) } +func TestCompareIgnoreCommentsCmd(t *testing.T) { + cmd := getRootCommand() + result := test.RunCmd(cmd, "compare --stripComments ../examples/data1.yaml ../examples/data1-no-comments.yaml") + if result.Error != nil { + t.Error(result.Error) + } + expectedOutput := `` + test.AssertResult(t, expectedOutput, result.Output) +} + +func TestCompareDontIgnoreCommentsCmd(t *testing.T) { + forceOsExit = false + cmd := getRootCommand() + result := test.RunCmd(cmd, "compare ../examples/data1.yaml ../examples/data1-no-comments.yaml") + + expectedOutput := `-a: simple # just the best ++a: simple + b: [1, 2] + c: + test: 1 +` + test.AssertResult(t, expectedOutput, result.Output) +} + +func TestCompareExplodeAnchorsCommentsCmd(t *testing.T) { + cmd := getRootCommand() + result := test.RunCmd(cmd, "compare --explodeAnchors ../examples/simple-anchor.yaml ../examples/simple-anchor-exploded.yaml") + if result.Error != nil { + t.Error(result.Error) + } + expectedOutput := `` + test.AssertResult(t, expectedOutput, result.Output) +} + +func TestCompareDontExplodeAnchorsCmd(t *testing.T) { + forceOsExit = false + cmd := getRootCommand() + result := test.RunCmd(cmd, "compare ../examples/simple-anchor.yaml ../examples/simple-anchor-exploded.yaml") + + expectedOutput := `-foo: &foo ++foo: + a: 1 + foobar: +- !!merge <<: *foo ++ a: 1 +` + test.AssertResult(t, expectedOutput, result.Output) +} + func TestCompareDifferentCmd(t *testing.T) { forceOsExit = false cmd := getRootCommand() diff --git a/examples/data1-no-comments.yaml b/examples/data1-no-comments.yaml new file mode 100644 index 00000000..b0f017b1 --- /dev/null +++ b/examples/data1-no-comments.yaml @@ -0,0 +1,4 @@ +a: simple +b: [1, 2] +c: + test: 1 diff --git a/examples/sample2.yaml b/examples/sample2.yaml deleted file mode 100644 index 3df90f8b..00000000 --- a/examples/sample2.yaml +++ /dev/null @@ -1,2 +0,0 @@ -b: - c: things \ No newline at end of file diff --git a/examples/simple-anchor-exploded.yaml b/examples/simple-anchor-exploded.yaml new file mode 100644 index 00000000..7cf0d51b --- /dev/null +++ b/examples/simple-anchor-exploded.yaml @@ -0,0 +1,5 @@ +foo: + a: 1 + +foobar: + a: 1 \ No newline at end of file