From 10fb9743f55354ceb72e445fed67b16555d879a2 Mon Sep 17 00:00:00 2001 From: Samuel Giddins Date: Tue, 13 Jun 2017 15:48:21 -0500 Subject: Add a compatibility guard that prints friendly errors on bundler 2+ --- lib/bundler.rb | 1 + lib/bundler/compatibility_guard.rb | 14 ++++++++++++++ lib/bundler/inline.rb | 1 + lib/bundler/shared_helpers.rb | 1 + spec/other/compatibility_guard_spec.rb | 23 +++++++++++++++++++++++ 5 files changed, 40 insertions(+) create mode 100644 lib/bundler/compatibility_guard.rb create mode 100644 spec/other/compatibility_guard_spec.rb diff --git a/lib/bundler.rb b/lib/bundler.rb index 2da2db29c9..a587f1e430 100644 --- a/lib/bundler.rb +++ b/lib/bundler.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true +require "bundler/compatibility_guard" require "bundler/vendored_fileutils" require "pathname" diff --git a/lib/bundler/compatibility_guard.rb b/lib/bundler/compatibility_guard.rb new file mode 100644 index 0000000000..6e40a119d1 --- /dev/null +++ b/lib/bundler/compatibility_guard.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +require "rubygems" +require "bundler/version" + +if Bundler::VERSION.split(".").first.to_i >= 2 + if Gem::Version.new(Object::RUBY_VERSION) < Gem::Version.new("2.0.0") + abort "Bundler 2 requires Ruby 2+. Either install bundler 1 or update to a supported Ruby version." + end + + if Gem::Version.new(Gem::VERSION) < Gem::Version.new("2.0.0") + abort "Bundler 2 requires RubyGems 2+. Either install bundler 1 or update to a supported RubyGems version." + end +end diff --git a/lib/bundler/inline.rb b/lib/bundler/inline.rb index 8462dd9cc1..f5e3a8c157 100644 --- a/lib/bundler/inline.rb +++ b/lib/bundler/inline.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true +require "bundler/compatibility_guard" # Allows for declaring a Gemfile inline in a ruby script, optionally installing # any gems that aren't already installed on the user's system. diff --git a/lib/bundler/shared_helpers.rb b/lib/bundler/shared_helpers.rb index 5d3956abc1..bc4c1c1d88 100644 --- a/lib/bundler/shared_helpers.rb +++ b/lib/bundler/shared_helpers.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true +require "bundler/compatibility_guard" require "pathname" require "rubygems" diff --git a/spec/other/compatibility_guard_spec.rb b/spec/other/compatibility_guard_spec.rb new file mode 100644 index 0000000000..abe70741a1 --- /dev/null +++ b/spec/other/compatibility_guard_spec.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +RSpec.describe "bundler compatibility guard" do + context "when the bundler version is 2+" do + before { simulate_bundler_version "2.0.a" } + + context "when running on Ruby < 2", :ruby => "< 2.a" do + it "raises a friendly error" do + bundle :version + expect(err).to eq("Bundler 2 requires Ruby 2+. Either install bundler 1 or update to a supported Ruby version.") + end + end + + context "when running on RubyGems < 2" do + before { simulate_rubygems_version "1.3.6" } + + it "raises a friendly error" do + bundle :version + expect(err).to eq("Bundler 2 requires RubyGems 2+. Either install bundler 1 or update to a supported RubyGems version.") + end + end + end +end -- cgit v1.2.1