Skip to content

Commit b9dff4e

Browse files
committed
Add a default-off option to use Pry, favoring IRB
1 parent c406729 commit b9dff4e

File tree

4 files changed

+54
-30
lines changed

4 files changed

+54
-30
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ gemspec
55

66
gem "rspec", "~> 3.0"
77
gem "standardrb"
8+
gem "pry"

bin/rack-console

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,24 @@ options = {}
88
OptionParser.new do |opts|
99
opts.banner = "USAGE: rack-console [options]"
1010

11-
opts.on("-c", "--config [RACKUP_FILE]", "Specify a rackup config file") do |config|
11+
opts.on("-c", "--config [RACKUP_FILE]", "Specify a rackup config file (default: ./config.ru)") do |config|
1212
options[:config] = config
1313
end
1414

15-
opts.on("-e", "--environment [ENVIRONMENT]", "Specify the Rack environment") do |environment|
15+
opts.on("-e", "--environment [ENVIRONMENT]", "Specify the Rack environment (default: development)") do |environment|
1616
options[:environment] = environment
1717
end
1818

19+
opts.on("-I", "--include [PATHS]", "Add paths (colon-separated) to the $LOAD_PATH") do |paths|
20+
options[:include] = paths.split(":")
21+
end
22+
1923
opts.on("-r", "--require [LIBRARY]", "Require a file or library before the Rack console loads") do |library|
2024
options[:require] = library
2125
end
2226

23-
opts.on("-I", "--include [PATHS]", "Add paths (colon-separated) to the $LOAD_PATH") do |paths|
24-
options[:include] = paths.split(":")
27+
opts.on("-P", "--[no-]pry", "Use Pry if available or explicitly disable it") do |pry|
28+
options[:pry] = pry
2529
end
2630

2731
opts.on("-v", "--version", "Print version and exit") do |v|

lib/rack/console.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def initialize(options = {})
1616
@environment = options[:environment]
1717
@include = options[:include]
1818
@require = options[:require]
19+
@use_pry = !!options[:pry]
1920
end
2021

2122
def start
@@ -32,7 +33,7 @@ def start
3233
main.instance_variable_set(:@app, Rack::Console::Session.new(app))
3334
main.extend(Rack::Console::Methods)
3435

35-
if Gem::Specification.find_all_by_name("pry").any?
36+
if Gem::Specification.find_all_by_name("pry").any? && @use_pry
3637
require "pry"
3738
Pry.start
3839
else

spec/rack/console_spec.rb

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,63 @@
11
require "rack/console"
22
require "irb"
3+
require "pry"
34

45
describe Rack::Console do
56
before do
67
@old_pwd = Dir.pwd
78
Dir.chdir File.expand_path("../../support", __FILE__)
89

9-
expect(IRB).to receive(:start)
10+
allow(IRB).to receive(:start)
11+
allow(Pry).to receive(:start)
1012
end
1113

12-
it "defaults to a config.ru file in the current working directory" do
13-
expect(Rack::Builder).to receive(:parse_file)
14-
.with("config.ru")
15-
.and_call_original
14+
context "when using IRB" do
15+
before do
16+
expect(IRB).to receive(:start)
17+
expect(Pry).not_to receive(:start)
18+
end
1619

17-
Rack::Console.new.start
18-
end
20+
it "defaults to a config.ru file in the current working directory" do
21+
expect(Rack::Builder).to receive(:parse_file)
22+
.with("config.ru")
23+
.and_call_original
1924

20-
it "accepts the --config option to override the location of config.ru" do
21-
Dir.chdir @old_pwd
22-
expect(Rack::Builder).to receive(:parse_file)
23-
.with("spec/support/config.ru")
24-
.and_call_original
25+
Rack::Console.new.start
26+
end
2527

26-
Rack::Console.new(config: "spec/support/config.ru").start
27-
end
28+
it "accepts the --config option to override the location of config.ru" do
29+
Dir.chdir @old_pwd
30+
expect(Rack::Builder).to receive(:parse_file)
31+
.with("spec/support/config.ru")
32+
.and_call_original
2833

29-
it "accepts the --require option to require a file or library" do
30-
Rack::Console.new(require: "json").start
31-
expect { JSON }.not_to raise_error
32-
end
34+
Rack::Console.new(config: "spec/support/config.ru").start
35+
end
3336

34-
it "accepts the --require option to add paths to $LOAD_PATH" do
35-
Rack::Console.new(include: ["lib", "spec"]).start
36-
expect($LOAD_PATH).to include("lib")
37-
expect($LOAD_PATH).to include("spec")
37+
it "accepts the --require option to require a file or library" do
38+
Rack::Console.new(require: "json").start
39+
expect { JSON }.not_to raise_error
40+
end
41+
42+
it "accepts the --require option to add paths to $LOAD_PATH" do
43+
Rack::Console.new(include: ["lib", "spec"]).start
44+
expect($LOAD_PATH).to include("lib")
45+
expect($LOAD_PATH).to include("spec")
46+
end
47+
48+
it "accepts the --environment option to set the environment" do
49+
Rack::Console.new(environment: "production").start
50+
expect(ENV["RACK_ENV"]).to eq("production")
51+
end
3852
end
3953

40-
it "accepts an argument to set the environment" do
41-
Rack::Console.new(environment: "production").start
42-
expect(ENV["RACK_ENV"]).to eq("production")
54+
context "when using Pry" do
55+
it "accepts the --pry option to enable Pry" do
56+
expect(IRB).not_to receive(:start)
57+
expect(Pry).to receive(:start)
58+
59+
Rack::Console.new(pry: true).start
60+
end
4361
end
4462

4563
describe "preamble message" do

0 commit comments

Comments
 (0)