diff --git a/cmd/commands_test.go b/cmd/commands_test.go index e26b974..5ad441d 100644 --- a/cmd/commands_test.go +++ b/cmd/commands_test.go @@ -94,12 +94,21 @@ func TestReadCmd(t *testing.T) { test.AssertResult(t, "2\n", result.Output) } -func TestCompareCmd(t *testing.T) { +func TestCompareSameCmd(t *testing.T) { cmd := getRootCommand() - result := test.RunCmd(cmd, "compare ../examples/data1.yaml ../examples/data3.yaml") + result := test.RunCmd(cmd, "compare ../examples/data1.yaml ../examples/data1.yaml") if result.Error != nil { t.Error(result.Error) } + expectedOutput := `` + test.AssertResult(t, expectedOutput, result.Output) +} + +func TestCompareDifferentCmd(t *testing.T) { + forceOsExit = false + cmd := getRootCommand() + result := test.RunCmd(cmd, "compare ../examples/data1.yaml ../examples/data3.yaml") + expectedOutput := `-a: simple # just the best -b: [1, 2] +a: "simple" # just the best @@ -111,6 +120,7 @@ func TestCompareCmd(t *testing.T) { } func TestComparePrettyCmd(t *testing.T) { + forceOsExit = false cmd := getRootCommand() result := test.RunCmd(cmd, "compare -P ../examples/data1.yaml ../examples/data3.yaml") if result.Error != nil { @@ -128,6 +138,7 @@ func TestComparePrettyCmd(t *testing.T) { } func TestComparePathsCmd(t *testing.T) { + forceOsExit = false cmd := getRootCommand() result := test.RunCmd(cmd, "compare -P -ppv ../examples/data1.yaml ../examples/data3.yaml **") if result.Error != nil { diff --git a/cmd/compare.go b/cmd/compare.go index a6f03a5..f11fa61 100644 --- a/cmd/compare.go +++ b/cmd/compare.go @@ -3,6 +3,7 @@ package cmd import ( "bufio" "bytes" + "os" "strings" "github.com/kylelemons/godebug/diff" @@ -11,6 +12,9 @@ import ( "github.com/spf13/cobra" ) +// turn off for unit tests :( +var forceOsExit = true + func createCompareCmd() *cobra.Command { var cmdCompare = &cobra.Command{ Use: "compare [yaml_file_a] [yaml_file_b]", @@ -70,7 +74,14 @@ func compareDocuments(cmd *cobra.Command, args []string) error { return errorDoingThings } - cmd.Print(diff.Diff(strings.TrimSuffix(dataBufferA.String(), "\n"), strings.TrimSuffix(dataBufferB.String(), "\n"))) - cmd.Print("\n") + diffString := diff.Diff(strings.TrimSuffix(dataBufferA.String(), "\n"), strings.TrimSuffix(dataBufferB.String(), "\n")) + + if len(diffString) > 1 { + cmd.Print(diffString) + cmd.Print("\n") + if forceOsExit { + os.Exit(1) + } + } return nil }