Permalink
Browse files

Add console command

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...
1 parent 7e8553e commit be02cc3828cf2aeeb1ad98802728853f64a9aa2d @jacquescrocker jacquescrocker committed with indirect Apr 9, 2010
Showing with 142 additions and 5 deletions.
  1. +10 −0 lib/bundler/cli.rb
  2. +102 −0 spec/other/console_spec.rb
  3. +15 −5 spec/support/helpers.rb
  4. +15 −0 spec/support/ruby_ext.rb
View
@@ -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}"
View
@@ -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
View
@@ -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 = {})
@@ -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
View
@@ -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.