Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
32 changes: 22 additions & 10 deletions lib/rubyshell/debugger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,39 @@ module Debugger
class << self
def run_wrapper(command, debug: nil)
if debug || RubyShell.debug?

time_one = Process.clock_gettime(Process::CLOCK_MONOTONIC)

result = yield
begin
result = yield
rescue RubyShell::CommandError => e
time_two = Process.clock_gettime(Process::CLOCK_MONOTONIC)
log_command(command, time_two - time_one, e.status, e.stdout, e.stderr)
raise

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In some situations on the past, i had some troubles with errors losing data when rescued.

This actual code will not give us problems, but. Lets add a test checking that when we have a command being rescued outside, with debug option true, the returned CommandError has the correct attributes. Like:

begin
  ls('error', _debug: true)
rescue => e
  puts e.command # => Correct command
  puts e.stdout # => Correct stdout
  # etc
end

end

time_two = Process.clock_gettime(Process::CLOCK_MONOTONIC)

RubyShell.log(<<~TEXT
\nExecuted: #{command.to_shell.chomp}
Duration: #{format("%.6f", time_two - time_one)}s
Pid: #{result.metadata[:exit_status].pid}
Exit code: #{result.metadata[:exit_status].to_i}
Stdout: #{result.to_s.inspect}
TEXT
)
log_command(command, time_two - time_one, result.metadata[:exit_status], result.to_s, "")

result
else
yield
end
end

private

def log_command(command, duration, status, stdout, stderr)
RubyShell.log(<<~TEXT
\nExecuted: #{command.to_shell.chomp}
Duration: #{format("%.6f", duration)}s
Pid: #{status.respond_to?(:pid) ? status.pid : ""}
Exit code: #{status.respond_to?(:exitstatus) ? status.exitstatus : status.to_i}
Stdout: #{stdout.inspect}
Stderr: #{stderr.inspect}
TEXT
)
end
end
end
end
2 changes: 1 addition & 1 deletion lib/rubyshell/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module RubyShell
class CommandError < StandardError
attr_reader :command, :status
attr_reader :command, :stdout, :stderr, :status

def initialize(command:, stdout: "", stderr: "", status: "", message: nil)
@command = command
Expand Down
16 changes: 16 additions & 0 deletions spec/debugger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@

expect(log_output.join).to include("Stdout: \"#{expected_output}\"")
end

it "logs the stderr" do
subject_method

expect(log_output.join).to include("Stderr: \"\"")
end
end

RSpec.shared_examples "a silent command" do
Expand Down Expand Up @@ -84,6 +90,16 @@ def subject_method
sh.echo("hello", _debug: true)
end
it_behaves_like "a logged command", "hello"

it "logs failed commands before reraising" do
expect do
sh.ruby("-e", "\"STDERR.write(%q{bad}); exit 1\"", _debug: true)
end.to raise_error(RubyShell::CommandError)

expect(log_output.join).to include("Exit code: 1")
expect(log_output.join).to include("Stdout: \"\"")
expect(log_output.join).to include("Stderr: \"bad\"")
end
end

context "when debug mode is enabled globally" do
Expand Down
Loading