From 9e88770cb13bf131514abf6604e7360b950872e2 Mon Sep 17 00:00:00 2001 From: Chris Patuzzo Date: Sun, 10 Sep 2017 12:34:12 +0100 Subject: [PATCH] Make a 'debug' command that replaces ./bin/console This may as well be a part of the tool as it could be useful to be able to jump into a console for various reasons once the tool is installed. It's not just useful during development. --- bin/console | 8 ------- lib/zz.rb | 1 + lib/zz/base.rb | 2 +- lib/zz/commands/debug.rb | 42 ++++++++++++++++++++++++++++++++++ spec/spec_helper.rb | 1 + spec/zz/commands/debug_spec.rb | 19 +++++++++++++++ 6 files changed, 64 insertions(+), 9 deletions(-) delete mode 100755 bin/console create mode 100644 lib/zz/commands/debug.rb create mode 100644 spec/zz/commands/debug_spec.rb diff --git a/bin/console b/bin/console deleted file mode 100755 index b160bc7..0000000 --- a/bin/console +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/env ruby - -$LOAD_PATH << File.expand_path("../lib", __dir__) - -require "zz" -require "pry" - -binding.pry diff --git a/lib/zz.rb b/lib/zz.rb index 54382ee..6326937 100644 --- a/lib/zz.rb +++ b/lib/zz.rb @@ -4,6 +4,7 @@ require "zz/help" require "zz/path" +require "zz/commands/debug" require "zz/commands/provision" require "zz/base" diff --git a/lib/zz/base.rb b/lib/zz/base.rb index e0f5e25..0ab98e3 100644 --- a/lib/zz/base.rb +++ b/lib/zz/base.rb @@ -1,5 +1,5 @@ module ZZ - COMMANDS = [Provision] + COMMANDS = [Debug, Provision] def self.execute(args) command_name = args.shift diff --git a/lib/zz/commands/debug.rb b/lib/zz/commands/debug.rb new file mode 100644 index 0000000..9985c67 --- /dev/null +++ b/lib/zz/commands/debug.rb @@ -0,0 +1,42 @@ +module ZZ + module Debug + class << self + def execute(args) + run_pry + rescue LoadError + run_irb + end + + def run_pry + require "pry" + ZZ.pry + end + + def run_irb + require "irb" + IRB.start + end + + def name + "debug" + end + + def summary + "runs an introspective debugger for zz" + end + + def description + [ + "This command starts a pry session in the context of the tuzz", + "automation tool and falls back to irb if pry is unavailable. It is", + "useful for debugging problems with the tool and for calling library", + "code in ways that weren't anticipated... or were they?", + ].join("\n ") + end + + def options + [] + end + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index cae0eb9..b841b3b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,4 +1,5 @@ require "rspec" +require "irb" require "pry" require "fileutils" require "zz" diff --git a/spec/zz/commands/debug_spec.rb b/spec/zz/commands/debug_spec.rb new file mode 100644 index 0000000..9d84fce --- /dev/null +++ b/spec/zz/commands/debug_spec.rb @@ -0,0 +1,19 @@ +RSpec.describe ZZ::Debug do + it "provides useful information about itself" do + expect(subject.name).to eq("debug") + expect(subject.summary).to eq("runs an introspective debugger for zz") + expect(subject.description).to match(/starts a pry session/) + end + + it "runs pry" do + expect(ZZ).to receive(:pry) + subject.execute([]) + end + + it "falls back to irb if pry is unavailable" do + allow(subject).to receive(:run_pry).and_raise(LoadError) + + expect(IRB).to receive(:start) + subject.execute([]) + end +end