From 85e7249a2cae3c160e98624a27fea77abd0a476f Mon Sep 17 00:00:00 2001 From: Colby Swandale Date: Thu, 1 Mar 2018 22:18:33 +1100 Subject: keep init_gems_rb setting for creating gems.rb in bundle init --- lib/bundler/cli/init.rb | 6 ++++- lib/bundler/feature_flag.rb | 1 + lib/bundler/settings.rb | 1 + man/bundle-config.ronn | 2 ++ spec/commands/init_spec.rb | 65 ++++++++++++++++++++++++++++++++++++++++++--- 5 files changed, 70 insertions(+), 5 deletions(-) diff --git a/lib/bundler/cli/init.rb b/lib/bundler/cli/init.rb index f794ae1dbb..28e8f03d7f 100644 --- a/lib/bundler/cli/init.rb +++ b/lib/bundler/cli/init.rb @@ -36,7 +36,11 @@ module Bundler private def gemfile - @gemfile ||= @options[:gemsrb] ? "gems.rb" : "Gemfile" + @gemfile ||= generate_gems_rb? ? "gems.rb" : "Gemfile" + end + + def generate_gems_rb? + @options[:gemsrb] || Bundler.settings[:init_gems_rb] end end end diff --git a/lib/bundler/feature_flag.rb b/lib/bundler/feature_flag.rb index 5d22f70d7f..f7bb5d1cf8 100644 --- a/lib/bundler/feature_flag.rb +++ b/lib/bundler/feature_flag.rb @@ -40,6 +40,7 @@ module Bundler settings_flag(:error_on_stderr) { bundler_2_mode? } settings_flag(:forget_cli_options) { bundler_2_mode? } settings_flag(:global_gem_cache) { bundler_2_mode? } + settings_flag(:init_gems_rb) { bundler_2_mode? } settings_flag(:list_command) { bundler_2_mode? } settings_flag(:lockfile_uses_separate_rubygems_sources) { bundler_2_mode? } settings_flag(:only_update_to_newer_versions) { bundler_2_mode? } diff --git a/lib/bundler/settings.rb b/lib/bundler/settings.rb index 65be547f98..75e2d4dff2 100644 --- a/lib/bundler/settings.rb +++ b/lib/bundler/settings.rb @@ -36,6 +36,7 @@ module Bundler gem.mit global_gem_cache ignore_messages + init_gems_rb list_command lockfile_uses_separate_rubygems_sources major_deprecations diff --git a/man/bundle-config.ronn b/man/bundle-config.ronn index 4fa4c8d013..88ad827313 100644 --- a/man/bundle-config.ronn +++ b/man/bundle-config.ronn @@ -195,6 +195,8 @@ learn more about their operation in [bundle install(1)][bundle-install(1)]. * `ignore_messages` (`BUNDLE_IGNORE_MESSAGES`): When set, no post install messages will be printed. To silence a single gem, use dot notation like `ignore_messages.httparty true`. +* `init_gems_rb` (`BUNDLE_INIT_GEMS_RB`) + Generate a `gems.rb` instead of a `Gemfile` when running `bundle init`. * `jobs` (`BUNDLE_JOBS`): The number of gems Bundler can install in parallel. Defaults to 1. * `list_command` (`BUNDLE_LIST_COMMAND`) diff --git a/spec/commands/init_spec.rb b/spec/commands/init_spec.rb index b47136f9d9..505d232538 100644 --- a/spec/commands/init_spec.rb +++ b/spec/commands/init_spec.rb @@ -42,13 +42,19 @@ RSpec.describe "bundle init" do end context "when gemsrb option given" do - before do - create_file("gems.rb", <<-G) - gem "rails" - G + it "generates a gems.rb file" do + bundle! :init, :gemsrb => true + + expect(bundled_app("gems.rb")).to be_file end context "when gems.rb already exists" do + before do + create_file("gems.rb", <<-G) + gem "rails" + G + end + it "does not change existing gems.rb" do expect { bundle :init, :gemsrb => true }.not_to change { File.read(bundled_app("gems.rb")) } end @@ -61,6 +67,57 @@ RSpec.describe "bundle init" do context "when a gems.rb file exists in a parent directory" do let(:subdir) { "child_dir" } + before do + create_file("gems.rb", <<-G) + gem "rails" + G + end + + it "lets users generate a gems.rb in a child directory" do + FileUtils.mkdir bundled_app(subdir) + + Dir.chdir bundled_app(subdir) do + bundle! :init, :gemsrb => true + end + + expect(out).to include("Writing new gems.rb") + expect(bundled_app("#{subdir}/gems.rb")).to be_file + end + end + end + + context "when init_gems_rb setting is true" do + before { bundle! "config init_gems_rb true" } + + it "generates a gems.rb file" do + bundle :init + expect(bundled_app("gems.rb")).to be_file + end + + context "when gems.rb already exists" do + before do + create_file("gems.rb", <<-G) + gem "rails" + G + end + + it "does not change existing gems.rb" do + expect { bundle :init }.not_to change { File.read(bundled_app("gems.rb")) } + end + + it "notifies the user that an existing gems.rb already exists" do + bundle :init + expect(out).to include("gems.rb already exists") + end + end + + context "when a gems.rb file exists in a parent directory" do + let(:subdir) { "child_dir" } + before do + create_file("gems.rb", <<-G) + gem "rails" + G + end it "lets users generate a gems.rb in a child directory" do FileUtils.mkdir bundled_app(subdir) -- cgit v1.2.1