From f3d7e946ee48030c4785070562687b6ef1641b4f Mon Sep 17 00:00:00 2001 From: Daniel Niknam Date: Sun, 25 Jul 2021 00:42:24 +1000 Subject: [PATCH] Refactor Bundler::Dsl#check_rubygems_source_safety to improve readability `check_rubygems_source_safety` is responsible for: 1. if there are multiple global sources - for bundle 3.x raise an error - for bundle 2.x print a warning 2. print a warning if there is no explicit global source The second responsibility was added recently and now the logic could be extracted to improve readability. Conditions are still live in the `check_rubygems_source_safety` method since we don't want to call both functions always and that would help us achieve that. --- bundler/lib/bundler/dsl.rb | 16 +++++++++++----- bundler/spec/bundler/dsl_spec.rb | 2 +- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/bundler/lib/bundler/dsl.rb b/bundler/lib/bundler/dsl.rb index 4e38df1ce9e9..3517a109ed58 100644 --- a/bundler/lib/bundler/dsl.rb +++ b/bundler/lib/bundler/dsl.rb @@ -448,14 +448,20 @@ def check_path_source_safety def check_rubygems_source_safety if @sources.implicit_global_source? - Bundler::SharedHelpers.major_deprecation 2, "This Gemfile does not include an explicit global source. " \ - "Not using an explicit global source may result in a different lockfile being generated depending on " \ - "the gems you have installed locally before bundler is run." \ - "Instead, define a global source in your Gemfile like this: source \"https://rubygems.org\"." + implicit_global_source_warning + elsif @sources.aggregate_global_source? + multiple_global_source_warning end + end - return unless @sources.aggregate_global_source? + def implicit_global_source_warning + Bundler::SharedHelpers.major_deprecation 2, "This Gemfile does not include an explicit global source. " \ + "Not using an explicit global source may result in a different lockfile being generated depending on " \ + "the gems you have installed locally before bundler is run." \ + "Instead, define a global source in your Gemfile like this: source \"https://rubygems.org\"." + end + def multiple_global_source_warning if Bundler.feature_flag.bundler_3_mode? msg = "This Gemfile contains multiple primary sources. " \ "Each source after the first must include a block to indicate which gems " \ diff --git a/bundler/spec/bundler/dsl_spec.rb b/bundler/spec/bundler/dsl_spec.rb index cfc4a7855da0..e6cd43ab59d6 100644 --- a/bundler/spec/bundler/dsl_spec.rb +++ b/bundler/spec/bundler/dsl_spec.rb @@ -245,7 +245,7 @@ describe "#check_primary_source_safety" do context "when a global source is not defined implicitly" do it "will raise a major deprecation warning" do - not_a_global_source = double("not-a-global-source", no_remotes?: true, multiple_remotes?: false) + not_a_global_source = double("not-a-global-source", :no_remotes? => true) allow(Bundler::Source::Rubygems).to receive(:new).and_return(not_a_global_source) warning = "This Gemfile does not include an explicit global source. " \