Skip to content

Commit

Permalink
Refactor Bundler::Dsl#check_rubygems_source_safety to improve readabi…
Browse files Browse the repository at this point in the history
…lity

`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.
  • Loading branch information
daniel-niknam authored and deivid-rodriguez committed Jul 26, 2021
1 parent b7523ad commit f3d7e94
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
16 changes: 11 additions & 5 deletions bundler/lib/bundler/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 " \
Expand Down
2 changes: 1 addition & 1 deletion bundler/spec/bundler/dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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. " \
Expand Down

0 comments on commit f3d7e94

Please sign in to comment.