From 767709fef5144a65f93a233949e4818617fdea87 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Fri, 12 Jun 2020 12:21:46 +1000 Subject: [PATCH] Fixed error flag --- cmd/read.go | 2 +- cmd/read_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/cmd/read.go b/cmd/read.go index 9039b907..8e68f77c 100644 --- a/cmd/read.go +++ b/cmd/read.go @@ -51,7 +51,7 @@ func readProperty(cmd *cobra.Command, args []string) error { matchingNodes, errorReadingStream := readYamlFile(args[0], path, updateAll, docIndexInt) - if exitStatus { + if exitStatus && len(matchingNodes) == 0 { cmd.SilenceUsage = true return errors.New("No matches found") } diff --git a/cmd/read_test.go b/cmd/read_test.go index 1906bfd9..f4990449 100644 --- a/cmd/read_test.go +++ b/cmd/read_test.go @@ -17,6 +17,30 @@ func TestReadCmd(t *testing.T) { test.AssertResult(t, "2\n", result.Output) } +func TestReadCmdWithExitStatus(t *testing.T) { + cmd := getRootCommand() + result := test.RunCmd(cmd, "read ../examples/sample.yaml b.c -e") + if result.Error != nil { + t.Error(result.Error) + } + test.AssertResult(t, "2\n", result.Output) +} + +func TestReadCmdWithExitStatusNotExist(t *testing.T) { + cmd := getRootCommand() + result := test.RunCmd(cmd, "read ../examples/sample.yaml caterpillar -e") + test.AssertResult(t, "No matches found", result.Error.Error()) +} + +func TestReadCmdNotExist(t *testing.T) { + cmd := getRootCommand() + result := test.RunCmd(cmd, "read ../examples/sample.yaml caterpillar") + if result.Error != nil { + t.Error(result.Error) + } + test.AssertResult(t, "", result.Output) +} + func TestReadUnwrapCmd(t *testing.T) { content := `b: 'frog' # my favourite`