From 14ac791eafae4edf564cfc0605e7c6f1dbbf4b71 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Mon, 3 Feb 2020 15:35:00 +1100 Subject: [PATCH] Fixed compare output, added tests --- cmd/commands_test.go | 48 ++++++++++++++++++++++++++++++++++++++++++++ cmd/compare.go | 4 +++- examples/data3.yaml | 18 ++++------------- 3 files changed, 55 insertions(+), 15 deletions(-) diff --git a/cmd/commands_test.go b/cmd/commands_test.go index f74d677e..4e8ba72d 100644 --- a/cmd/commands_test.go +++ b/cmd/commands_test.go @@ -94,6 +94,54 @@ func TestReadCmd(t *testing.T) { test.AssertResult(t, "2", result.Output) } +func TestCompareCmd(t *testing.T) { + cmd := getRootCommand() + result := test.RunCmd(cmd, "compare ../examples/data1.yaml ../examples/data3.yaml") + if result.Error != nil { + t.Error(result.Error) + } + expectedOutput := `-a: simple # just the best +-b: [1, 2] ++a: "simple" # just the best ++b: [1, 3] + c: + test: 1 +` + test.AssertResult(t, expectedOutput, result.Output) +} + +func TestComparePrettyCmd(t *testing.T) { + cmd := getRootCommand() + result := test.RunCmd(cmd, "compare -P ../examples/data1.yaml ../examples/data3.yaml") + if result.Error != nil { + t.Error(result.Error) + } + expectedOutput := ` a: simple # just the best + b: + - 1 +-- 2 ++- 3 + c: + test: 1 +` + test.AssertResult(t, expectedOutput, result.Output) +} + +func TestComparePathsCmd(t *testing.T) { + cmd := getRootCommand() + result := test.RunCmd(cmd, "compare -P -ppv ../examples/data1.yaml ../examples/data3.yaml **") + if result.Error != nil { + t.Error(result.Error) + } + expectedOutput := ` a: simple # just the best + b.[0]: 1 +-b.[1]: 2 ++b.[1]: 3 + c.test: 1 +` + test.AssertResult(t, expectedOutput, result.Output) +} + func TestValidateCmd(t *testing.T) { cmd := getRootCommand() result := test.RunCmd(cmd, "validate ../examples/sample.yaml b.c") diff --git a/cmd/compare.go b/cmd/compare.go index 9c0bbf68..310bb0b1 100644 --- a/cmd/compare.go +++ b/cmd/compare.go @@ -3,6 +3,7 @@ package cmd import ( "bufio" "bytes" + "strings" "github.com/kylelemons/godebug/diff" "github.com/mikefarah/yq/v3/pkg/yqlib" @@ -75,6 +76,7 @@ func compareDocuments(cmd *cobra.Command, args []string) error { return errorDoingThings } - cmd.Print(diff.Diff(dataBufferA.String(), dataBufferB.String())) + cmd.Print(diff.Diff(strings.TrimSuffix(dataBufferA.String(), "\n"), strings.TrimSuffix(dataBufferB.String(), "\n"))) + cmd.Print("\n") return nil } diff --git a/examples/data3.yaml b/examples/data3.yaml index 0aba4f10..22e8056a 100644 --- a/examples/data3.yaml +++ b/examples/data3.yaml @@ -1,14 +1,4 @@ -deep1: - hostA: - value: 1234 - notRelevant: - value: bananas - hostB: - value: 5678 -deep2: - hostC: - value: 1234 - notRelevant: - value: bananas - hostD: - value: 5678 +a: "simple" # just the best +b: [1, 3] +c: + test: 1 \ No newline at end of file