diff options
author | Kyrylo Silin <silin@kyrylo.org> | 2019-04-15 02:34:44 +0300 |
---|---|---|
committer | Kyrylo Silin <silin@kyrylo.org> | 2019-04-15 02:36:01 +0300 |
commit | f89d0eccacd7f6a20f9ee3ca9b47a5d90e0c06e3 (patch) | |
tree | 5ce380d497ba3fb9558e738203514ad7c28851ae | |
parent | 95d3a74e9b22c44ea48ce870171640f0471db09b (diff) | |
download | pry-f89d0eccacd7f6a20f9ee3ca9b47a5d90e0c06e3.tar.gz |
config: factor out 'system' to SystemCommandHandler
-rw-r--r-- | lib/pry.rb | 1 | ||||
-rw-r--r-- | lib/pry/config.rb | 9 | ||||
-rw-r--r-- | lib/pry/system_command_handler.rb | 15 | ||||
-rw-r--r-- | spec/system_command_handler_spec.rb | 34 |
4 files changed, 51 insertions, 8 deletions
@@ -22,6 +22,7 @@ require 'pry/editor' require 'pry/history' require 'pry/color_printer' require 'pry/exception_handler' +require 'pry/system_command_handler' Pry::Commands = Pry::CommandSet.new unless defined?(Pry::Commands) diff --git a/lib/pry/config.rb b/lib/pry/config.rb index 6fcf9920..be011505 100644 --- a/lib/pry/config.rb +++ b/lib/pry/config.rb @@ -34,14 +34,7 @@ class Pry # sessions. hooks: Pry::Hooks.default, pager: true, - - system: proc do |output, cmd, _| - next if system(cmd) - - output.puts 'Error: there was a problem executing system ' \ - "command: #{cmd}" - end, - + system: Pry::SystemCommandHandler.method(:default), color: Pry::Helpers::BaseHelpers.use_ansi_codes?, default_window_size: 5, editor: Pry::Editor.default, diff --git a/lib/pry/system_command_handler.rb b/lib/pry/system_command_handler.rb new file mode 100644 index 00000000..24bea2e3 --- /dev/null +++ b/lib/pry/system_command_handler.rb @@ -0,0 +1,15 @@ +class Pry + # @api private + # @since ?.?.? + module SystemCommandHandler + class << self + def default(output, command, _pry_instance) + return if Kernel.system(command) + + output.puts( + "Error: there was a problem executing system command: #{command}" + ) + end + end + end +end diff --git a/spec/system_command_handler_spec.rb b/spec/system_command_handler_spec.rb new file mode 100644 index 00000000..810fd606 --- /dev/null +++ b/spec/system_command_handler_spec.rb @@ -0,0 +1,34 @@ +require 'stringio' + +RSpec.describe Pry::SystemCommandHandler do + describe ".default" do + let(:output) { StringIO.new } + let(:pry_instance) { Pry.new } + + before { allow(Kernel).to receive(:system) } + + context "when command exists" do + before do + expect(Kernel).to receive(:system).with('test-command').and_return(true) + end + + it "executes the command without printing the warning" do + described_class.default(output, 'test-command', pry_instance) + expect(output.string).to be_empty + end + end + + context "when doesn't exist" do + before do + allow(Kernel).to receive(:system).with('test-command').and_return(nil) + end + + it "executes the command without printing the warning" do + described_class.default(output, 'test-command', pry_instance) + expect(output.string).to eq( + "Error: there was a problem executing system command: test-command\n" + ) + end + end + end +end |