Skip to content

Commit

Permalink
Make a 'debug' command that replaces ./bin/console
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tuzz committed Sep 10, 2017
1 parent 65a1108 commit 9e88770
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 9 deletions.
8 changes: 0 additions & 8 deletions bin/console

This file was deleted.

1 change: 1 addition & 0 deletions lib/zz.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require "zz/help"
require "zz/path"

require "zz/commands/debug"
require "zz/commands/provision"

require "zz/base"
2 changes: 1 addition & 1 deletion lib/zz/base.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module ZZ
COMMANDS = [Provision]
COMMANDS = [Debug, Provision]

def self.execute(args)
command_name = args.shift
Expand Down
42 changes: 42 additions & 0 deletions lib/zz/commands/debug.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "rspec"
require "irb"
require "pry"
require "fileutils"
require "zz"
Expand Down
19 changes: 19 additions & 0 deletions spec/zz/commands/debug_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 9e88770

Please sign in to comment.