diff --git a/lib/rubyshell.rb b/lib/rubyshell.rb index fc30d06..c97ade9 100644 --- a/lib/rubyshell.rb +++ b/lib/rubyshell.rb @@ -27,7 +27,7 @@ def debug=(value) def debug(value = true) # rubocop:disable Style/OptionalBooleanParameter previous_value = @debug_mode - @debug_mode = value + @debug_mode = value.nil? ? @debug_mode : value result = yield diff --git a/spec/debugger_spec.rb b/spec/debugger_spec.rb index 44633c7..900f202 100644 --- a/spec/debugger_spec.rb +++ b/spec/debugger_spec.rb @@ -7,154 +7,100 @@ end end - describe ".run_wrapper" do - let(:log_output) { [] } - let(:logger) { double("Logger", info: nil) } - - before do - allow(logger).to receive(:info) { |msg| log_output << msg } - allow(RubyShell).to receive(:logger).and_return(logger) + RSpec.shared_examples "a logged command" do |expected_output| + it "returns the command output" do + expect(subject_method).to eq(expected_output) end - after do - RubyShell.debug = false + it "logs the command executed" do + subject_method + + expect(log_output.join).to include("Executed: echo #{expected_output}") end - context "when debug mode is disabled" do - def subject_method - sh.echo("hello") - end + it "logs the duration" do + subject_method - it "returns the command output" do - expect(subject_method).to eq("hello") - end + expect(log_output.join).to match(/Duration: \d+\.\d+s/) + end - it "does not log anything" do - subject_method + it "logs the pid" do + subject_method - expect(log_output).to be_empty - end + expect(log_output.join).to match(/Pid: \d+/) end - context "when debug mode is enabled via block" do - def subject_method - sh(debug: true) { echo("hello") } - end + it "logs the exit code" do + subject_method - it "returns the command output" do - expect(subject_method).to eq("hello") - end + expect(log_output.join).to include("Exit code: 0") + end - it "logs the command executed" do - subject_method + it "logs the stdout" do + subject_method - expect(log_output.join).to include("Executed: echo hello") - end + expect(log_output.join).to include("Stdout: \"#{expected_output}\"") + end + end - it "logs the duration" do - subject_method + RSpec.shared_examples "a silent command" do + it "returns the command output" do + expect(subject_method).to eq("hello") + end - expect(log_output.join).to match(/Duration: \d+\.\d+s/) - end + it "does not log anything" do + subject_method + expect(log_output).to be_empty + end + end - it "logs the pid" do - subject_method + describe ".run_wrapper" do + let(:log_output) { [] } + let(:logger) { double("Logger", info: nil) } - expect(log_output.join).to match(/Pid: \d+/) - end + before do + allow(logger).to receive(:info) { |msg| log_output << msg } + allow(RubyShell).to receive(:logger).and_return(logger) + end - it "logs the exit code" do - subject_method + after { RubyShell.debug = false } - expect(log_output.join).to include("Exit code: 0") + context "when debug mode is disabled" do + def subject_method + sh.echo("hello") end + it_behaves_like "a silent command" + end - it "logs the stdout" do - subject_method - - expect(log_output.join).to include('Stdout: "hello"') + context "when debug mode is enabled via block" do + def subject_method + sh(debug: true) { echo("hello") } end + it_behaves_like "a logged command", "hello" end context "when debug mode is enabled via command option" do def subject_method sh.echo("hello", _debug: true) end - - it "returns the command output" do - expect(subject_method).to eq("hello") - end - - it "logs the command executed" do - subject_method - - expect(log_output.join).to include("Executed: echo hello") - end - - it "logs the duration" do - subject_method - - expect(log_output.join).to match(/Duration: \d+\.\d+s/) - end - - it "logs the pid" do - subject_method - - expect(log_output.join).to match(/Pid: \d+/) - end - - it "logs the exit code" do - subject_method - - expect(log_output.join).to include("Exit code: 0") - end - - it "logs the stdout" do - subject_method - - expect(log_output.join).to include('Stdout: "hello"') - end + it_behaves_like "a logged command", "hello" end context "when debug mode is enabled globally" do before { RubyShell.debug = true } - def subject_method - sh.echo("world") - end - - it "returns the command output" do - expect(subject_method).to eq("world") + context "when command is used as a method" do + def subject_method + sh.echo("world") + end + it_behaves_like "a logged command", "world" end - it "logs the command executed" do - subject_method - - expect(log_output.join).to include("Executed: echo world") - end - - it "logs the duration" do - subject_method - - expect(log_output.join).to match(/Duration: \d+\.\d+s/) - end - - it "logs the pid" do - subject_method - - expect(log_output.join).to match(/Pid: \d+/) - end - - it "logs the exit code" do - subject_method - - expect(log_output.join).to include("Exit code: 0") - end - - it "logs the stdout" do - subject_method - - expect(log_output.join).to include('Stdout: "world"') + context "when command is executed in a block" do + def subject_method + sh { echo("world") } + end + it_behaves_like "a logged command", "world" end end end