Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion bundler/lib/bundler/dependency.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ def initialize(name, version, options = {}, &blk)
type = options["type"] || :runtime
super(name, version, type)

@autorequire = nil
@groups = Array(options["group"] || :default).map(&:to_sym)
@source = options["source"]
@path = options["path"]
Expand Down
43 changes: 19 additions & 24 deletions bundler/lib/bundler/runtime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,35 +50,30 @@ def require(*groups)
Plugin.hook(Plugin::Events::GEM_BEFORE_REQUIRE_ALL, dependencies)

dependencies.each do |dep|
required_file = nil
Plugin.hook(Plugin::Events::GEM_BEFORE_REQUIRE, dep)

begin
# Loop through all the specified autorequires for the
# dependency. If there are none, use the dependency's name
# as the autorequire.
Array(dep.autorequire || dep.name).each do |file|
# Allow `require: true` as an alias for `require: <name>`
file = dep.name if file == true
required_file = file
begin
Kernel.require file
rescue RuntimeError => e
raise e if e.is_a?(LoadError) # we handle this a little later
# Loop through all the specified autorequires for the
# dependency. If there are none, use the dependency's name
# as the autorequire.
Array(dep.autorequire || dep.name).each do |file|
# Allow `require: true` as an alias for `require: <name>`
file = dep.name if file == true
required_file = file
begin
Kernel.require required_file
rescue LoadError => e
if dep.autorequire.nil? && e.path == required_file
if required_file.include?("-")
required_file = required_file.tr("-", "/")
retry
end
else
raise Bundler::GemRequireError.new e,
"There was an error while trying to load the gem '#{file}'."
end
end
rescue LoadError => e
raise if dep.autorequire || e.path != required_file

if dep.autorequire.nil? && dep.name.include?("-")
begin
namespaced_file = dep.name.tr("-", "/")
Kernel.require namespaced_file
rescue LoadError => e
raise if e.path != namespaced_file
end
rescue RuntimeError => e
raise Bundler::GemRequireError.new e,
"There was an error while trying to load the gem '#{file}'."
end
end

Expand Down
174 changes: 101 additions & 73 deletions bundler/spec/commands/console_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,106 +36,134 @@ def __pry__
RUBY
end
end

install_gemfile <<-G
source "https://gem.repo2"
gem "myrack"
gem "activesupport", :group => :test
gem "myrack_middleware", :group => :development
G
end

it "starts IRB with the default group loaded" do
bundle "console" do |input, _, _|
input.puts("puts MYRACK")
input.puts("exit")
end
expect(out).to include("0.9.1")
end
context "when the library has an unrelated error" do
before do
build_lib "loadfuuu", "1.0.0" do |s|
s.write "lib/loadfuuu.rb", "require_relative 'loadfuuu/bar'"
s.write "lib/loadfuuu/bar.rb", "require 'not-in-bundle'"
end

it "uses IRB as default console" do
bundle "console" do |input, _, _|
input.puts("__FILE__")
input.puts("exit")
install_gemfile <<-G
source "https://gem.repo1"
path "#{lib_path}" do
gem "loadfuuu", require: true
end
G
end
expect(out).to include("(irb)")
end

it "starts another REPL if configured as such" do
install_gemfile <<-G
source "https://gem.repo2"
gem "pry"
G
bundle "config set console pry"
it "does not show the bug report template" do
bundle("console", raise_on_error: false) do |input, _, _|
input.puts("exit")
end

bundle "console" do |input, _, _|
input.puts("__method__")
input.puts("exit")
expect(err).not_to include("ERROR REPORT TEMPLATE")
end
expect(out).to include(":__pry__")
end

it "falls back to IRB if the other REPL isn't available" do
bundle "config set console pry"
# make sure pry isn't there
context "when the library does not have any errors" do
before do
install_gemfile <<-G
source "https://gem.repo2"
gem "myrack"
gem "activesupport", :group => :test
gem "myrack_middleware", :group => :development
G
end

bundle "console" do |input, _, _|
input.puts("__FILE__")
input.puts("exit")
it "starts IRB with the default group loaded" do
bundle "console" do |input, _, _|
input.puts("puts MYRACK")
input.puts("exit")
end
expect(out).to include("0.9.1")
end
expect(out).to include("(irb)")
end

it "doesn't load any other groups" do
bundle "console" do |input, _, _|
input.puts("puts ACTIVESUPPORT")
input.puts("exit")
it "uses IRB as default console" do
bundle "console" do |input, _, _|
input.puts("__FILE__")
input.puts("exit")
end
expect(out).to include("(irb)")
end
expect(out).to include("NameError")
end

describe "when given a group" do
it "loads the given group" do
bundle "console test" do |input, _, _|
input.puts("puts ACTIVESUPPORT")
it "starts another REPL if configured as such" do
install_gemfile <<-G
source "https://gem.repo2"
gem "pry"
G
bundle "config set console pry"

bundle "console" do |input, _, _|
input.puts("__method__")
input.puts("exit")
end
expect(out).to include("2.3.5")
expect(out).to include(":__pry__")
end

it "loads the default group" do
bundle "console test" do |input, _, _|
input.puts("puts MYRACK")
it "falls back to IRB if the other REPL isn't available" do
bundle "config set console pry"
# make sure pry isn't there

bundle "console" do |input, _, _|
input.puts("__FILE__")
input.puts("exit")
end
expect(out).to include("0.9.1")
expect(out).to include("(irb)")
end

it "doesn't load other groups" do
bundle "console test" do |input, _, _|
input.puts("puts MYRACK_MIDDLEWARE")
it "doesn't load any other groups" do
bundle "console" do |input, _, _|
input.puts("puts ACTIVESUPPORT")
input.puts("exit")
end
expect(out).to include("NameError")
end
end

it "performs an automatic bundle install" do
gemfile <<-G
source "https://gem.repo2"
gem "myrack"
gem "activesupport", :group => :test
gem "myrack_middleware", :group => :development
gem "foo"
G

bundle "config set auto_install 1"
bundle :console do |input, _, _|
input.puts("puts 'hello'")
input.puts("exit")
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
expect(out).to include("2.3.5")
end

it "loads the default group" do
bundle "console test" do |input, _, _|
input.puts("puts MYRACK")
input.puts("exit")
end
expect(out).to include("0.9.1")
end

it "doesn't load other groups" do
bundle "console test" do |input, _, _|
input.puts("puts MYRACK_MIDDLEWARE")
input.puts("exit")
end
expect(out).to include("NameError")
end
end

it "performs an automatic bundle install" do
gemfile <<-G
source "https://gem.repo2"
gem "myrack"
gem "activesupport", :group => :test
gem "myrack_middleware", :group => :development
gem "foo"
G

bundle "config set auto_install 1"
bundle :console do |input, _, _|
input.puts("puts 'hello'")
input.puts("exit")
end
expect(out).to include("Installing foo 1.0")
expect(out).to include("hello")
expect(the_bundle).to include_gems "foo 1.0"
end
expect(out).to include("Installing foo 1.0")
expect(out).to include("hello")
expect(the_bundle).to include_gems "foo 1.0"
end
end
53 changes: 14 additions & 39 deletions bundler/spec/runtime/require_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,9 @@
end
G

load_error_run <<-R, "fail"
Bundler.require
R
run "Bundler.require", raise_on_error: false

expect(err_without_deprecations).to eq("ZOMG LOAD ERROR")
expect(err_without_deprecations).to include("cannot load such file -- fail")
end

it "displays a helpful message if the required gem throws an error" do
Expand Down Expand Up @@ -155,16 +153,9 @@
end
G

cmd = <<-RUBY
begin
Bundler.require
rescue LoadError => e
warn "ZOMG LOAD ERROR: \#{e.message}"
end
RUBY
run(cmd)
run "Bundler.require", raise_on_error: false

expect(err_without_deprecations).to eq("ZOMG LOAD ERROR: cannot load such file -- load-bar")
expect(err_without_deprecations).to include("cannot load such file -- load-bar")
end

describe "with namespaced gems" do
Expand Down Expand Up @@ -215,10 +206,9 @@
end
G

load_error_run <<-R, "jquery-rails"
Bundler.require
R
expect(err_without_deprecations).to eq("ZOMG LOAD ERROR")
run "Bundler.require", raise_on_error: false

expect(err_without_deprecations).to include("cannot load such file -- jquery-rails")
end

it "handles the case where regex fails" do
Expand All @@ -233,16 +223,9 @@
end
G

cmd = <<-RUBY
begin
Bundler.require
rescue LoadError => e
warn "ZOMG LOAD ERROR" if e.message.include?("Could not open library 'libfuuu-1.0'")
end
RUBY
run(cmd)
run "Bundler.require", raise_on_error: false

expect(err_without_deprecations).to eq("ZOMG LOAD ERROR")
expect(err_without_deprecations).to include("libfuuu-1.0").and include("cannot open shared object file")
end

it "doesn't swallow the error when the library has an unrelated error" do
Expand All @@ -257,16 +240,9 @@
end
G

cmd = <<-RUBY
begin
Bundler.require
rescue LoadError => e
warn "ZOMG LOAD ERROR: \#{e.message}"
end
RUBY
run(cmd)
run "Bundler.require", raise_on_error: false

expect(err_without_deprecations).to eq("ZOMG LOAD ERROR: cannot load such file -- load-bar")
expect(err_without_deprecations).to include("cannot load such file -- load-bar")
end
end

Expand Down Expand Up @@ -375,10 +351,9 @@ def self.two
gem "busted_require"
G

load_error_run <<-R, "no_such_file_omg"
Bundler.require
R
expect(err_without_deprecations).to eq("ZOMG LOAD ERROR")
run "Bundler.require", raise_on_error: false

expect(err_without_deprecations).to include("cannot load such file -- no_such_file_omg")
end
end
end
Expand Down
Loading