Skip to content
This repository has been archived by the owner on Apr 14, 2021. It is now read-only.

Commit

Permalink
Add console command
Browse files Browse the repository at this point in the history
Console loads a new IRB session after loading the :default group, and
can optionally load another group that you pass as an argument, i.e.
`bundle console development`.
  • Loading branch information
Jacques Crocker authored and indirect committed Apr 9, 2010
1 parent 7e8553e commit be02cc3
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 5 deletions.
10 changes: 10 additions & 0 deletions lib/bundler/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,16 @@ def open(name)
end
end

desc "console [GROUP]", "Opens an IRB session with the bundle pre-loaded"
def console(group = nil)
require 'bundler/setup'
group ? Bundler.require(:default, group) : Bundler.require
ARGV.clear

require 'irb'
IRB.start
end

desc "version", "Prints the bundler's version information"
def version
Bundler.ui.info "Bundler version #{Bundler::VERSION}"
Expand Down
102 changes: 102 additions & 0 deletions spec/other/console_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
require File.expand_path('../../spec_helper', __FILE__)

describe "bundle console" do
before :each do
install_gemfile <<-G
source "file://#{gem_repo1}"
gem "rack"
gem "activesupport", :group => :test
gem "rack_middleware", :group => :development
G
end

it "starts IRB with the default group loaded" do
bundle "console" do |input|
input.puts("puts RACK")
input.puts("exit")
end
out.should include("0.9.1")
end

it "doesn't load any other groups" do
bundle "console" do |input|
input.puts("puts ACTIVESUPPORT")
input.puts("exit")
end
out.should include("NameError")
end

describe "when given a group" do
it "loads the given group" do
bundle "console test" do |input|
input.puts("puts ACTIVESUPPORT")
input.puts("exit")
end
out.should include("2.3.5")
end

it "loads the default group" do
bundle "console test" do |input|
input.puts("puts RACK")
input.puts("exit")
end
out.should include("0.9.1")
end

it "doesn't load other groups" do
bundle "console test" do |input|
input.puts("puts RACK_MIDDLEWARE")
input.puts("exit")
end
out.should include("NameError")
end
end

describe "when locked" do
before :each do
bundle :lock
end

it "starts IRB with the default group loaded" do
bundle :console do |input|
input.puts("puts RACK")
input.puts("exit")
end
out.should include("0.9.1")
end

it "doesn't load any other groups" do
bundle :console do |input|
input.puts("puts ACTIVESUPPORT")
input.puts("exit")
end
out.should include("NameError")
end

describe "and given a group" do
it "loads the given group" do
bundle "console test" do |input|
input.puts("puts ACTIVESUPPORT")
input.puts("exit")
end
out.should include("2.3.5")
end

it "loads the default group" do
bundle "console test" do |input|
input.puts("puts RACK")
input.puts("exit")
end
out.should include("0.9.1")
end

it "doesn't load other groups" do
bundle "console test" do |input|
input.puts("puts RACK_MIDDLEWARE")
input.puts("exit")
end
out.should include("NameError")
end
end
end
end
20 changes: 15 additions & 5 deletions spec/support/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,17 @@ def lib
def bundle(cmd, options = {})
expect_err = options.delete(:expect_err)
exit_status = options.delete(:exit_status)

env = (options.delete(:env) || {}).map{|k,v| "#{k}='#{v}' "}.join
args = options.map { |k,v| " --#{k} #{v}"}.join
gemfile = File.expand_path('../../../bin/bundle', __FILE__)
cmd = "#{env}#{Gem.ruby} -I#{lib} #{gemfile} #{cmd}#{args}"
exit_status ? sys_status(cmd) : sys_exec(cmd, expect_err)

if exit_status
sys_status(cmd)
else
sys_exec(cmd, expect_err){|i| yield i if block_given? }
end
end

def ruby(ruby, options = {})
Expand All @@ -58,10 +64,14 @@ def ruby(ruby, options = {})

def sys_exec(cmd, expect_err = false)
require "open3"
input, out, err = Open3.popen3(cmd)
@err = err.read.strip
puts @err if !expect_err && $show_err && !@err.empty?
@out = out.read.strip
@in, @out, @err = Open3.popen3(cmd)

yield @in if block_given?

@err = err.read_available_bytes.strip
@out = out.read_available_bytes.strip

puts @err unless expect_err || @err.empty? || !$show_err
@out
end

Expand Down
15 changes: 15 additions & 0 deletions spec/support/ruby_ext.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class IO
def read_available_bytes(chunk_size = 1024, select_timeout = 5)
buffer = []

while self.class.select([self], nil, nil, select_timeout)
begin
buffer << self.readpartial(chunk_size)
rescue(EOFError)
break
end
end

return buffer.join
end
end

0 comments on commit be02cc3

Please sign in to comment.