From 390cec6eb47065029b5571f7d60153cdfc643cca Mon Sep 17 00:00:00 2001 From: James Couball Date: Thu, 9 Mar 2023 15:55:35 -0800 Subject: [PATCH] Correctly report command output when there is an error Signed-off-by: James Couball --- lib/git/failed_error.rb | 14 ++++++++------ tests/units/test_failed_error.rb | 6 +++--- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/git/failed_error.rb b/lib/git/failed_error.rb index 244ba2ca..27aa6ed9 100644 --- a/lib/git/failed_error.rb +++ b/lib/git/failed_error.rb @@ -14,18 +14,20 @@ module Git class FailedError < Git::GitExecuteError # Create a FailedError object # + # Since this gem redirects stderr to stdout, the stdout of the process is used. + # # @example # `exit 1` # set $? appropriately for this example - # result = Git::CommandLineResult.new(%w[git status], $?, '', "failed") + # result = Git::CommandLineResult.new(%w[git status], $?, 'stdout', 'stderr') # error = Git::FailedError.new(result) # error.message #=> - # "[\"git\", \"status\"]\nstatus: pid 89784 exit 1\nstderr: \"failed\"" + # "[\"git\", \"status\"]\nstatus: pid 89784 exit 1\noutput: \"stdout\"" # # @param result [Git::CommandLineResult] the result of the git command including # the git command, status, stdout, and stderr # def initialize(result) - super("#{result.git_cmd}\nstatus: #{result.status}\nstderr: #{result.stderr.inspect}") + super("#{result.git_cmd}\nstatus: #{result.status}\noutput: #{result.stdout.inspect}") @result = result end @@ -35,14 +37,14 @@ def initialize(result) # # @example # `exit 1` # set $? appropriately for this example - # result = Git::CommandLineResult.new(%w[git status], $?, '', "failed") + # result = Git::CommandLineResult.new(%w[git status], $?, 'stdout', 'stderr') # error = Git::FailedError.new(result) # error.result #=> # #, - # @stderr="failed", - # @stdout=""> + # @stderr="stderr", + # @stdout="stdout"> # # @return [Git::CommandLineResult] # diff --git a/tests/units/test_failed_error.rb b/tests/units/test_failed_error.rb index d3c5485f..4833c6df 100644 --- a/tests/units/test_failed_error.rb +++ b/tests/units/test_failed_error.rb @@ -3,7 +3,7 @@ class TestFailedError < Test::Unit::TestCase def test_initializer status = Struct.new(:to_s).new('pid 89784 exit 1') - result = Git::CommandLineResult.new(%w[git status], status, '', "failed") + result = Git::CommandLineResult.new(%w[git status], status, 'stdout', 'stderr') error = Git::FailedError.new(result) @@ -13,11 +13,11 @@ def test_initializer def test_message status = Struct.new(:to_s).new('pid 89784 exit 1') - result = Git::CommandLineResult.new(%w[git status], status, '', "failed") + result = Git::CommandLineResult.new(%w[git status], status, 'stdout', 'stderr') error = Git::FailedError.new(result) - expected_message = "[\"git\", \"status\"]\nstatus: pid 89784 exit 1\nstderr: \"failed\"" + expected_message = "[\"git\", \"status\"]\nstatus: pid 89784 exit 1\noutput: \"stdout\"" assert_equal(expected_message, error.message) end end