Fixed compare output, added tests

This commit is contained in:
Mike Farah 2020-02-03 15:35:00 +11:00
parent 25293a6894
commit 14ac791eaf
3 changed files with 55 additions and 15 deletions

View File

@ -94,6 +94,54 @@ func TestReadCmd(t *testing.T) {
test.AssertResult(t, "2", result.Output) 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) { func TestValidateCmd(t *testing.T) {
cmd := getRootCommand() cmd := getRootCommand()
result := test.RunCmd(cmd, "validate ../examples/sample.yaml b.c") result := test.RunCmd(cmd, "validate ../examples/sample.yaml b.c")

View File

@ -3,6 +3,7 @@ package cmd
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"strings"
"github.com/kylelemons/godebug/diff" "github.com/kylelemons/godebug/diff"
"github.com/mikefarah/yq/v3/pkg/yqlib" "github.com/mikefarah/yq/v3/pkg/yqlib"
@ -75,6 +76,7 @@ func compareDocuments(cmd *cobra.Command, args []string) error {
return errorDoingThings 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 return nil
} }

View File

@ -1,14 +1,4 @@
deep1: a: "simple" # just the best
hostA: b: [1, 3]
value: 1234 c:
notRelevant: test: 1
value: bananas
hostB:
value: 5678
deep2:
hostC:
value: 1234
notRelevant:
value: bananas
hostD:
value: 5678