Skip to content

Correctly report command output when there is an error #658

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions lib/git/failed_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 #=>
# #<Git::CommandLineResult:0x00000001046bd488
# @git_cmd=["git", "status"],
# @status=#<Process::Status: pid 89784 exit 1>,
# @stderr="failed",
# @stdout="">
# @stderr="stderr",
# @stdout="stdout">
#
# @return [Git::CommandLineResult]
#
Expand Down
6 changes: 3 additions & 3 deletions tests/units/test_failed_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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