forked from clio/ten_years_rails
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move bundle_report rails compatibility logic into a class (#137)
* Move bundle_report rails compatibility logic into a class I extracted it into a class because I think it will be easier to use the `incompatible_gems_by_state` data there. Based on the code review * Add incompatible with new compatible versions * Update changelog * Capture stdout to assert Co-authored-by: Ariel <[email protected]> --------- Co-authored-by: Ariel <[email protected]>
- Loading branch information
Showing
9 changed files
with
159 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
lib/next_rails/bundle_report/rails_version_compatibility.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
class NextRails::BundleReport::RailsVersionCompatibility | ||
def initialize(gems: NextRails::GemInfo.all, options: {}) | ||
@gems = gems | ||
@options = options | ||
end | ||
|
||
def generate | ||
erb_output | ||
end | ||
|
||
def incompatible_gems_by_state | ||
@incompatible_gems_by_state ||= begin | ||
incompatible_gems.each { |gem| gem.find_latest_compatible(rails_version: rails_version) } | ||
incompatible_gems.group_by { |gem| gem.state(rails_version) } | ||
end | ||
end | ||
|
||
private | ||
|
||
def erb_output | ||
template = <<-ERB | ||
<% if incompatible_gems_by_state[:found_compatible] -%> | ||
<%= Rainbow("=> Incompatible with Rails #{rails_version} (with new versions that are compatible):").white.bold %> | ||
<%= Rainbow("These gems will need to be upgraded before upgrading to Rails #{rails_version}.").italic %> | ||
<% incompatible_gems_by_state[:found_compatible].each do |gem| -%> | ||
<%= gem_header(gem) %> - upgrade to <%= gem.latest_compatible_version.version %> | ||
<% end -%> | ||
<% end -%> | ||
<% if incompatible_gems_by_state[:incompatible] -%> | ||
<%= Rainbow("=> Incompatible with Rails #{rails_version} (with no new compatible versions):").white.bold %> | ||
<%= Rainbow("These gems will need to be removed or replaced before upgrading to Rails #{rails_version}.").italic %> | ||
<% incompatible_gems_by_state[:incompatible].each do |gem| -%> | ||
<%= gem_header(gem) %> - new version, <%= gem.latest_version.version %>, is not compatible with Rails #{rails_version} | ||
<% end -%> | ||
<% end -%> | ||
<% if incompatible_gems_by_state[:no_new_version] -%> | ||
<%= Rainbow("=> Incompatible with Rails #{rails_version} (with no new versions):").white.bold %> | ||
<%= Rainbow("These gems will need to be upgraded by us or removed before upgrading to Rails #{rails_version}.").italic %> | ||
<%= Rainbow("This list is likely to contain internal gems, like Cuddlefish.").italic %> | ||
<% incompatible_gems_by_state[:no_new_version].each do |gem| -%> | ||
<%= gem_header(gem) %> - new version not found | ||
<% end -%> | ||
<% end -%> | ||
<%= Rainbow(incompatible_gems.length.to_s).red %> gems incompatible with Rails <%= rails_version %> | ||
ERB | ||
|
||
erb_version = ERB.version | ||
if erb_version =~ /erb.rb \[([\d\.]+) .*\]/ | ||
erb_version = $1 | ||
end | ||
|
||
if Gem::Version.new(erb_version) < Gem::Version.new("2.2") | ||
ERB.new(template, nil, "-").result(binding) | ||
else | ||
ERB.new(template, trim_mode: "-").result(binding) | ||
end | ||
end | ||
|
||
def gem_header(_gem) | ||
header = Rainbow("#{_gem.name} #{_gem.version}").bold | ||
header << Rainbow(" (loaded from git)").magenta if _gem.sourced_from_git? | ||
header | ||
end | ||
|
||
def incompatible_gems | ||
@incompatible_gems ||= @gems.reject do |gem| | ||
gem.compatible_with_rails?(rails_version: rails_version) || (!include_rails_gems && gem.from_rails?) | ||
end.sort_by { |gem| gem.name } | ||
end | ||
|
||
def rails_version | ||
@options[:rails_version] | ||
end | ||
|
||
def include_rails_gems | ||
@options[:include_rails_gems] | ||
end | ||
end |
40 changes: 40 additions & 0 deletions
40
spec/next_rails/bundle_report/rails_version_compatibility_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
RSpec.describe NextRails::BundleReport::RailsVersionCompatibility do | ||
describe "#generate" do | ||
it "returns non incompatible gems" do | ||
output = NextRails::BundleReport::RailsVersionCompatibility.new(options: { rails_version: 7.0 }).generate | ||
expect(output).to match "gems incompatible with Rails 7.0" | ||
end | ||
|
||
it "returns incompatible with compatible versions" do | ||
next_rails_version = 7.1 | ||
specification = Gem::Specification.new do |s| | ||
s.name = "audited" | ||
s.version = "5.1.0" | ||
s.add_dependency "rails", ">= 5.0", "< 7.1" | ||
end | ||
audited = NextRails::GemInfo.new(specification) | ||
gems = [audited] | ||
|
||
allow_any_instance_of(described_class).to receive(:incompatible_gems_by_state) | ||
.and_return({ found_compatible: gems }) | ||
|
||
allow(audited).to receive(:latest_compatible_version).and_return(Gem::Version.new("5.8.0")) | ||
|
||
output = | ||
NextRails::BundleReport::RailsVersionCompatibility.new( | ||
gems: gems, | ||
options: { rails_version: next_rails_version, include_rails_gems: false } | ||
).generate | ||
|
||
expect(output).to include("Incompatible with Rails 7.1 (with new versions that are compatible):") | ||
expect(output).to include("These gems will need to be upgraded before upgrading to Rails 7.1.") | ||
expect(output).to include("- upgrade to 5.8.0") | ||
expect(output).to include("gems incompatible with Rails 7.1") | ||
end | ||
end | ||
end | ||
|
1 change: 0 additions & 1 deletion
1
spec/next_rails/bundle_report/ruby_version_compatibility_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters