From c2ab26a7f0e3b3f1e2d992d1968ed3cf717dd885 Mon Sep 17 00:00:00 2001 From: Jon Rowe Date: Sat, 16 May 2020 14:54:31 +0100 Subject: [PATCH] Add back Ruby 2.2 support for Rails 5 (#2332) * Add extra debugging tool for specs expected to pass * Add back Ruby 2.2 support for Rails 5 --- .travis.yml | 9 ++++++++ Gemfile | 13 +++++++++-- Rakefile | 2 +- example_app_generator/generate_app.rb | 4 +++- .../rails/example/mailer_example_group.rb | 4 ++-- script/update_rubygems_and_install_bundler | 10 ++++++-- .../rails/example/view_example_group_spec.rb | 20 +++++++++++++--- spec/support/group_failure_formatter.rb | 23 +++++++++++++++++++ 8 files changed, 74 insertions(+), 11 deletions(-) create mode 100644 spec/support/group_failure_formatter.rb diff --git a/.travis.yml b/.travis.yml index 2491eaeff7..c65a0c1e95 100644 --- a/.travis.yml +++ b/.travis.yml @@ -74,6 +74,11 @@ matrix: env: RAILS_VERSION='~> 5.2.0' - rvm: 2.3.8 env: RAILS_VERSION='~> 5.2.0' + - rvm: 2.2.10 + env: RAILS_VERSION='~> 5.2.0' + allow_failure: true + - rvm: 2.2.10 + env: RAILS_VERSION='5-2-stable' # Rails 5.1 Builds >= 2.2.2 - rvm: 2.6.6 @@ -84,6 +89,8 @@ matrix: env: RAILS_VERSION='~> 5.1.0' - rvm: 2.3.8 env: RAILS_VERSION='~> 5.1.0' + - rvm: 2.2.10 + env: RAILS_VERSION='~> 5.1.0' # Rails 5.0 Builds >= 2.2.2 - rvm: 2.6.6 @@ -94,5 +101,7 @@ matrix: env: RAILS_VERSION='~> 5.0.0' - rvm: 2.3.8 env: RAILS_VERSION='~> 5.0.0' + - rvm: 2.2.10 + env: RAILS_VERSION='~> 5.0.0' fast_finish: true diff --git a/Gemfile b/Gemfile index 07d5f4c98d..ac7c392be7 100644 --- a/Gemfile +++ b/Gemfile @@ -22,6 +22,9 @@ case RAILS_VERSION when /master/ MAJOR = 6 MINOR = 0 +when /5-2-stable/ + MAJOR = 5 + MINOR = 2 when /stable/ MAJOR = 6 MINOR = 0 @@ -46,7 +49,11 @@ gem 'rake', '~> 12' gem 'mime-types', "~> 3" -gem 'capybara', '>= 2.13', '< 4.0', require: false +if RUBY_VERSION.to_f == 2.2 + gem 'capybara', '~> 3.1.0' +else + gem 'capybara', '>= 2.13', '< 4.0', require: false +end if MAJOR < 6 gem 'nokogiri', '1.9.1' @@ -56,7 +63,9 @@ end gem "rubyzip", '~> 1.2' -gem 'rubocop', '~> 0.80.1' +if RUBY_VERSION.to_f >= 2.3 + gem 'rubocop', '~> 0.80.1' +end custom_gemfile = File.expand_path('Gemfile-custom', __dir__) eval_gemfile custom_gemfile if File.exist?(custom_gemfile) diff --git a/Rakefile b/Rakefile index 5a0b085066..f17ba175a4 100644 --- a/Rakefile +++ b/Rakefile @@ -27,7 +27,7 @@ RSpec::Core::RakeTask.new(:spec) do |t| end Cucumber::Rake::Task.new(:cucumber) do |t| - version = ENV.fetch("RAILS_VERSION", "~> 6.0.0")[/\d[\.-]\d/] + version = ENV.fetch("RAILS_VERSION", "~> 6.0.0")[/\d[\.-]\d/].tr('-', '.') if version == "master" || version.nil? version = Float::INFINITY end diff --git a/example_app_generator/generate_app.rb b/example_app_generator/generate_app.rb index b1f16d7d52..a69d856eac 100644 --- a/example_app_generator/generate_app.rb +++ b/example_app_generator/generate_app.rb @@ -36,7 +36,9 @@ end if Rails::VERSION::STRING >= "5.1.0" - if RUBY_VERSION < "2.4" + if RUBY_VERSION < "2.3" + gsub_file "Gemfile", /.*capybara.*/, "gem 'capybara', '~> 3.1.0'" + elsif RUBY_VERSION < "2.4" gsub_file "Gemfile", /.*capybara.*/, "gem 'capybara', '~> 3.15.0'" end if Rails::VERSION::STRING >= "5.2.0" diff --git a/lib/rspec/rails/example/mailer_example_group.rb b/lib/rspec/rails/example/mailer_example_group.rb index 874c773cf7..fd86fe9427 100644 --- a/lib/rspec/rails/example/mailer_example_group.rb +++ b/lib/rspec/rails/example/mailer_example_group.rb @@ -21,8 +21,8 @@ module MailerExampleGroup included do include ::Rails.application.routes.url_helpers - options = ::Rails.configuration.action_mailer.default_url_options - options&.each { |key, value| default_url_options[key] = value } + options = ::Rails.configuration.action_mailer.default_url_options || {} + options.each { |key, value| default_url_options[key] = value } end # Class-level DSL for mailer specs. diff --git a/script/update_rubygems_and_install_bundler b/script/update_rubygems_and_install_bundler index c5372151e4..2ecbbf294a 100755 --- a/script/update_rubygems_and_install_bundler +++ b/script/update_rubygems_and_install_bundler @@ -5,5 +5,11 @@ set -e source script/functions.sh -gem update --no-document --system -gem install --no-document bundler +if is_ruby_23_plus; then + gem update --no-document --system + gem install --no-document bundler +else + echo "Warning installing older versions of Rubygems / Bundler" + gem update --system '2.7.8' + gem install bundler -v '1.17.3' +fi diff --git a/spec/rspec/rails/example/view_example_group_spec.rb b/spec/rspec/rails/example/view_example_group_spec.rb index de8e595acd..d16002ff60 100644 --- a/spec/rspec/rails/example/view_example_group_spec.rb +++ b/spec/rspec/rails/example/view_example_group_spec.rb @@ -1,3 +1,5 @@ +require 'support/group_failure_formatter' + module RSpec::Rails RSpec.describe ViewExampleGroup do it_behaves_like "an rspec-rails example group mixin", :view, @@ -234,7 +236,18 @@ def _view; end # Stub method expect(view_spec.view).to eq(view) end - it 'is accessible to hooks' do + if RUBY_VERSION <= "2.3.0" && ENV["RAILS_VERSION"] !~ /stable/ && ::Rails.version.to_f == 5.2 + pending_only_on_ruby_22_rails_52 = """ + Rails 5.2.4.2 has a syntax error in ActionDispatch::Request::Session. + (A &. usage which does not work in 2.2.10) + It has been fixed but not released, this spec will not pass until that + has been released. + """ + else + pending_only_on_ruby_22_rails_52 = false + end + + it 'is accessible to hooks', pending: pending_only_on_ruby_22_rails_52 do with_isolated_config do run_count = 0 RSpec.configuration.before(:each, type: :view) do @@ -242,9 +255,10 @@ def _view; end # Stub method run_count += 1 end group = RSpec::Core::ExampleGroup.describe 'a view', type: :view do - specify { true } + specify { expect(true).to eq true } end - group.run + group.run(failure_reporter) + expect(failure_reporter.exceptions).to eq [] expect(run_count).to eq 1 end end diff --git a/spec/support/group_failure_formatter.rb b/spec/support/group_failure_formatter.rb new file mode 100644 index 0000000000..8692a1bd3a --- /dev/null +++ b/spec/support/group_failure_formatter.rb @@ -0,0 +1,23 @@ +module RSpec::Rails::TestSupport + class FailureReporter + def initialize + @exceptions = [] + end + attr_reader :exceptions + + def example_failed(example) + @exceptions << example.exception + end + + def method_missing(name, *_args, &_block) + end + end + + def failure_reporter + @failure_reporter ||= FailureReporter.new + end +end + +RSpec.configure do |config| + config.include RSpec::Rails::TestSupport +end