diff options
author | Kyrylo Silin <silin@kyrylo.org> | 2019-04-14 13:27:25 +0300 |
---|---|---|
committer | Kyrylo Silin <silin@kyrylo.org> | 2019-04-14 13:27:25 +0300 |
commit | 5c3253339cee785ec04c4f4dc8b4fea889e969c6 (patch) | |
tree | 1f32d284af270cc1dbde4cfa2335ce9dae125e8e | |
parent | acf213773d6d6c589e3f97b78a0b04fa07eb249a (diff) | |
download | pry-5c3253339cee785ec04c4f4dc8b4fea889e969c6.tar.gz |
config: factor out default hook definition to Pry::Hooks
-rw-r--r-- | lib/pry/config.rb | 9 | ||||
-rw-r--r-- | lib/pry/hooks.rb | 10 | ||||
-rw-r--r-- | spec/hooks_spec.rb | 28 |
3 files changed, 39 insertions, 8 deletions
diff --git a/lib/pry/config.rb b/lib/pry/config.rb index 694d01b1..77e8373a 100644 --- a/lib/pry/config.rb +++ b/lib/pry/config.rb @@ -59,14 +59,7 @@ class Pry # The default hooks - display messages when beginning and ending Pry # sessions. - hooks: Pry::Hooks.new.add_hook( - :before_session, :default - ) do |_out, _target, pry_instance| - next if pry_instance.quiet? - - pry_instance.run_command('whereami --quiet') - end, - + hooks: Pry::Hooks.default, pager: true, system: proc do |output, cmd, _| diff --git a/lib/pry/hooks.rb b/lib/pry/hooks.rb index 04857d4a..38d70281 100644 --- a/lib/pry/hooks.rb +++ b/lib/pry/hooks.rb @@ -10,6 +10,16 @@ class Pry # puts "hello" # end class Hooks + def self.default + hooks = new + hooks.add_hook(:before_session, :default) do |_out, _target, pry_instance| + next if pry_instance.quiet? + + pry_instance.run_command('whereami --quiet') + end + hooks + end + def initialize @hooks = Hash.new { |h, k| h[k] = [] } end diff --git a/spec/hooks_spec.rb b/spec/hooks_spec.rb index dd0f7d60..5ee30b80 100644 --- a/spec/hooks_spec.rb +++ b/spec/hooks_spec.rb @@ -3,6 +3,34 @@ describe Pry::Hooks do @hooks = Pry::Hooks.new end + describe ".default" do + it "returns hooks with default before_session hook" do + hooks = described_class.default + expect(hooks.hook_exists?('before_session', :default)).to be_truthy + end + + context "when pry instance is quiet" do + let(:pry_instance) { Pry.new(quiet: true) } + + it "doesn't run the whereami command" do + expect(pry_instance).not_to receive(:run_command) + hooks = described_class.default + hooks.exec_hook(:before_session, StringIO.new, {}, pry_instance) + end + end + + context "when pry instance is not quiet" do + let(:pry_instance) { Pry.new(quiet: false) } + let(:output) { StringIO.new } + + it "runs the whereami command" do + expect(pry_instance).to receive(:run_command).with('whereami --quiet') + hooks = described_class.default + hooks.exec_hook(:before_session, StringIO.new, {}, pry_instance) + end + end + end + describe "adding a new hook" do it 'should not execute hook while adding it' do run = false |