From 93a2a70266ab9c020547a9c986d732323c105230 Mon Sep 17 00:00:00 2001 From: Jon Rowe Date: Thu, 15 Oct 2020 10:46:27 +0100 Subject: [PATCH 01/23] Setup rails 6.1 builds --- .travis.yml | 16 ++++++++++++++++ Gemfile | 2 +- Gemfile-rails-dependencies | 2 +- Rakefile | 11 +++++++---- 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index 843352ae6e..a88f9ffd4f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -38,6 +38,22 @@ script: "script/run_build 2>&1" matrix: include: + # Rails 6.1 builds >= 2.5 + - rvm: ruby-3.0.0-preview1 + env: RAILS_VERSION='master' + allow_failure: true + - rvm: 2.7.1 + env: RAILS_VERSION='master' + - rvm: 2.6.6 + env: RAILS_VERSION='master' + - rvm: 2.5.8 + env: RAILS_VERSION='master' + jdk: oraclejdk11 + env: + - RAILS_VERSION='master' + - JRUBY_OPT=--dev + - JAVA_OPTS="--add-opens java.base/sun.nio.ch=org.jruby.dist --add-opens java.base/java.io=org.jruby.dist --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.security=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.security.cert=ALL-UNNAMED --add-opens=java.base/java.util.zip=ALL-UNNAMED --add-opens=java.base/java.lang.reflect=ALL-UNNAMED --add-opens=java.base/java.util.regex=ALL-UNNAMED --add-opens=java.base/java.net=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/javax.crypto=ALL-UNNAMED --add-opens=java.management/sun.management=ALL-UNNAMED" + # Rails 6.0 builds >= 2.5.0 - rvm: jruby-head jdk: oraclejdk11 diff --git a/Gemfile b/Gemfile index a7f2cf8eb5..e249430262 100644 --- a/Gemfile +++ b/Gemfile @@ -26,7 +26,7 @@ MAJOR = case RAILS_VERSION when /5-2-stable/ 5 - when /stable/, nil, false, '' + when /master/, /stable/, nil, false, '' 6 else /(\d+)[\.|-]\d+/.match(RAILS_VERSION).captures.first.to_i diff --git a/Gemfile-rails-dependencies b/Gemfile-rails-dependencies index 5b3ebda404..03f75abfb2 100644 --- a/Gemfile-rails-dependencies +++ b/Gemfile-rails-dependencies @@ -1,7 +1,7 @@ version_file = File.expand_path("../.rails-version", __FILE__) case version = ENV['RAILS_VERSION'] || (File.exist?(version_file) && File.read(version_file).chomp) || '' -when /main/ +when /master/ gem "rails", :git => "https://github.com/rails/rails.git" gem "arel", :git => "https://github.com/rails/arel.git" gem "journey", :git => "https://github.com/rails/journey.git" diff --git a/Rakefile b/Rakefile index f68cb64a6f..ac5a7b39c3 100644 --- a/Rakefile +++ b/Rakefile @@ -27,10 +27,13 @@ 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/].tr('-', '.') - if version == "main" || version.nil? - version = Float::INFINITY - end + string_version = ENV.fetch("RAILS_VERSION", "~> 6.0.0") + version = + if string_version == "master" || string_version.nil? + Float::INFINITY + else + string_version[/\d[\.-]\d/].tr('-', '.') + end tags = [] if version.to_f >= 5.1 From 3e07b342ed820236643d267442a09eb083a99ed6 Mon Sep 17 00:00:00 2001 From: Jon Rowe Date: Thu, 15 Oct 2020 11:46:13 +0100 Subject: [PATCH 02/23] Switch accessor for fixture path in file upload support for 6.1 --- Changelog.md | 5 +++ .../rails/fixture_file_upload_support.rb | 41 ++++++++++++++----- .../rails/fixture_file_upload_support_spec.rb | 20 ++++++--- 3 files changed, 51 insertions(+), 15 deletions(-) diff --git a/Changelog.md b/Changelog.md index 8672ffd1a4..b282cf44b0 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,3 +1,8 @@ +### Rails 6.1 Development +[Full Changelog](https://github.com/rspec/rspec-rails/compare/main...rails-6-1-dev) + +* Support new #file_fixture_path and new fixture test support code. (Jon Rowe, #2398) + ### Development [Full Changelog](https://github.com/rspec/rspec-rails/compare/v4.0.1...main) diff --git a/lib/rspec/rails/fixture_file_upload_support.rb b/lib/rspec/rails/fixture_file_upload_support.rb index bcbc733d74..ef4989c653 100644 --- a/lib/rspec/rails/fixture_file_upload_support.rb +++ b/lib/rspec/rails/fixture_file_upload_support.rb @@ -6,21 +6,42 @@ module FixtureFileUploadSupport private - def rails_fixture_file_wrapper - RailsFixtureFileWrapper.fixture_path = nil - resolved_fixture_path = - if respond_to?(:fixture_path) && !fixture_path.nil? - fixture_path.to_s - else - (RSpec.configuration.fixture_path || '').to_s - end - RailsFixtureFileWrapper.fixture_path = File.join(resolved_fixture_path, '') unless resolved_fixture_path.strip.empty? - RailsFixtureFileWrapper.instance + # In Rails 6.2 fixture file path needs to be relative to `file_fixture_path` instead, this change + # was brought in with a deprecation warning on 6.1. In Rails 6.2 expect to rework this to remove + # the old accessor. + if ::Rails.version.to_f >= 6.1 + def rails_fixture_file_wrapper + RailsFixtureFileWrapper.file_fixture_path = nil + resolved_fixture_path = + if respond_to?(:file_fixture_path) && !file_fixture_path.nil? + file_fixture_path.to_s + else + (RSpec.configuration.fixture_path || '').to_s + end + RailsFixtureFileWrapper.file_fixture_path = File.join(resolved_fixture_path, '') unless resolved_fixture_path.strip.empty? + RailsFixtureFileWrapper.instance + end + else + def rails_fixture_file_wrapper + RailsFixtureFileWrapper.fixture_path = nil + resolved_fixture_path = + if respond_to?(:fixture_path) && !fixture_path.nil? + fixture_path.to_s + else + (RSpec.configuration.fixture_path || '').to_s + end + RailsFixtureFileWrapper.fixture_path = File.join(resolved_fixture_path, '') unless resolved_fixture_path.strip.empty? + RailsFixtureFileWrapper.instance + end end class RailsFixtureFileWrapper include ActionDispatch::TestProcess if defined?(ActionDispatch::TestProcess) + if ::Rails.version.to_f >= 6.1 + include ActiveSupport::Testing::FileFixtures + end + class << self attr_accessor :fixture_path diff --git a/spec/rspec/rails/fixture_file_upload_support_spec.rb b/spec/rspec/rails/fixture_file_upload_support_spec.rb index d7be688d19..af074704dd 100644 --- a/spec/rspec/rails/fixture_file_upload_support_spec.rb +++ b/spec/rspec/rails/fixture_file_upload_support_spec.rb @@ -3,33 +3,43 @@ module RSpec::Rails context 'with fixture path set in config' do it 'resolves fixture file' do RSpec.configuration.fixture_path = File.dirname(__FILE__) - expect(fixture_file_upload_resolved('fixture_file_upload_support_spec.rb').run).to be true + expect_to_pass fixture_file_upload_resolved('fixture_file_upload_support_spec.rb') end it 'resolves supports `Pathname` objects' do RSpec.configuration.fixture_path = Pathname(File.dirname(__FILE__)) - expect(fixture_file_upload_resolved('fixture_file_upload_support_spec.rb').run).to be true + expect_to_pass fixture_file_upload_resolved('fixture_file_upload_support_spec.rb') end end context 'with fixture path set in spec' do it 'resolves fixture file' do - expect(fixture_file_upload_resolved('fixture_file_upload_support_spec.rb', File.dirname(__FILE__)).run).to be true + expect_to_pass fixture_file_upload_resolved('fixture_file_upload_support_spec.rb', File.dirname(__FILE__)) end end context 'with fixture path not set' do it 'resolves fixture using relative path' do RSpec.configuration.fixture_path = nil - expect(fixture_file_upload_resolved('spec/rspec/rails/fixture_file_upload_support_spec.rb').run).to be true + expect_to_pass fixture_file_upload_resolved('spec/rspec/rails/fixture_file_upload_support_spec.rb') end end + def expect_to_pass(group) + result = group.run(failure_reporter) + failure_reporter.exceptions.map { |e| raise e } + expect(result).to be true + end + def fixture_file_upload_resolved(fixture_name, fixture_path = nil) RSpec::Core::ExampleGroup.describe do include RSpec::Rails::FixtureFileUploadSupport - self.fixture_path = fixture_path + if ::Rails.version.to_f >= 6.1 + self.file_fixture_path = fixture_path + else + self.fixture_path = fixture_path + end it 'supports fixture file upload' do file = fixture_file_upload(fixture_name) From 5081f475d4261df6c4f66d1946fc1e61f51dbace Mon Sep 17 00:00:00 2001 From: Jon Rowe Date: Fri, 16 Oct 2020 10:01:23 +0100 Subject: [PATCH 03/23] Fix controller spec on Rails 6.1 The #config option is removed from activerecord/lib/active_record/test_fixtures.rb --- .../rspec/rails/example/controller_example_group_spec.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/spec/rspec/rails/example/controller_example_group_spec.rb b/spec/rspec/rails/example/controller_example_group_spec.rb index 329626191b..56b53049d1 100644 --- a/spec/rspec/rails/example/controller_example_group_spec.rb +++ b/spec/rspec/rails/example/controller_example_group_spec.rb @@ -36,7 +36,14 @@ def my_helper "other_value" end end - config.include mod + + # Rails 6.1 removes config from ./activerecord/lib/active_record/test_fixtures.rb + if respond_to?(:config) + config.include mod + else + ActiveRecord::Base.include mod + end + group.class_exec do let(:my_helper) { "my_value" } end From 62b835038a3cda02e890db8abce13b4077206dad Mon Sep 17 00:00:00 2001 From: Jon Rowe Date: Fri, 16 Oct 2020 10:06:24 +0100 Subject: [PATCH 04/23] Fix view specs on Rails 6.1 --- spec/rspec/rails/example/view_example_group_spec.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/spec/rspec/rails/example/view_example_group_spec.rb b/spec/rspec/rails/example/view_example_group_spec.rb index f46f7feb04..9b4dafcc74 100644 --- a/spec/rspec/rails/example/view_example_group_spec.rb +++ b/spec/rspec/rails/example/view_example_group_spec.rb @@ -13,6 +13,7 @@ module ::Namespaced; module ThingsHelper; end; end group = RSpec::Core::ExampleGroup.describe('things/show.html.erb') do def self.helper(*); end # Stub method end + allow(group).to receive(:helper) expect(group).to receive(:helper).with(ThingsHelper) group.class_exec do include ViewExampleGroup @@ -23,6 +24,7 @@ def self.helper(*); end # Stub method group = RSpec::Core::ExampleGroup.describe('namespaced/things/show.html.erb') do def self.helper(*); end # Stub method end + allow(group).to receive(:helper) expect(group).to receive(:helper).with(Namespaced::ThingsHelper) group.class_exec do include ViewExampleGroup @@ -66,6 +68,7 @@ module ::ApplicationHelper; end group = RSpec::Core::ExampleGroup.describe('bars/new.html.erb') do def self.helper(*); end # Stub method end + allow(group).to receive(:helper) expect(group).to receive(:helper).with(ApplicationHelper) group.class_exec do include ViewExampleGroup From 61a34d3ef7d827b6ea1b236582d2bce18ed4b6f1 Mon Sep 17 00:00:00 2001 From: Jon Rowe Date: Fri, 30 Oct 2020 16:26:28 +0000 Subject: [PATCH 05/23] Update expected rspec versions --- rspec-rails.gemspec | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rspec-rails.gemspec b/rspec-rails.gemspec index f23b732435..237093112d 100644 --- a/rspec-rails.gemspec +++ b/rspec-rails.gemspec @@ -46,10 +46,10 @@ Gem::Specification.new do |s| # get released. %w[core expectations mocks support].each do |name| if RSpec::Rails::Version::STRING =~ /pre/ # prerelease builds - expected_rspec_version = "3.10.0.pre" + expected_rspec_version = "3.11.0.pre" s.add_runtime_dependency "rspec-#{name}", "= #{expected_rspec_version}" else - expected_rspec_version = "3.9.0" + expected_rspec_version = "3.10.0" s.add_runtime_dependency "rspec-#{name}", "~> #{expected_rspec_version.split(".")[0..1].join(".")}" end end From 80c82f312ed78d706468a6c5ef7ef5530e7a55d7 Mon Sep 17 00:00:00 2001 From: Jon Rowe Date: Fri, 30 Oct 2020 16:26:28 +0000 Subject: [PATCH 06/23] Update expected rspec versions --- rspec-rails.gemspec | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rspec-rails.gemspec b/rspec-rails.gemspec index f23b732435..237093112d 100644 --- a/rspec-rails.gemspec +++ b/rspec-rails.gemspec @@ -46,10 +46,10 @@ Gem::Specification.new do |s| # get released. %w[core expectations mocks support].each do |name| if RSpec::Rails::Version::STRING =~ /pre/ # prerelease builds - expected_rspec_version = "3.10.0.pre" + expected_rspec_version = "3.11.0.pre" s.add_runtime_dependency "rspec-#{name}", "= #{expected_rspec_version}" else - expected_rspec_version = "3.9.0" + expected_rspec_version = "3.10.0" s.add_runtime_dependency "rspec-#{name}", "~> #{expected_rspec_version.split(".")[0..1].join(".")}" end end From 8658247154dd402c49743e9f2c5fb89d5798a501 Mon Sep 17 00:00:00 2001 From: Phil Pirozhkov Date: Sat, 31 Oct 2020 23:41:08 +0300 Subject: [PATCH 07/23] Merge pull request #2399 from ThHareau/feat/ticket-2375 Improve controller template when no action --- lib/generators/rspec/controller/templates/request_spec.rb | 7 ++++++- .../rspec/controller/controller_generator_spec.rb | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/generators/rspec/controller/templates/request_spec.rb b/lib/generators/rspec/controller/templates/request_spec.rb index 42b4b8e01b..75c5fc129a 100644 --- a/lib/generators/rspec/controller/templates/request_spec.rb +++ b/lib/generators/rspec/controller/templates/request_spec.rb @@ -1,7 +1,12 @@ require 'rails_helper' RSpec.describe "<%= class_name.pluralize %>", <%= type_metatag(:request) %> do -<% namespaced_path = regular_class_path.join('/') %> +<% namespaced_path = regular_class_path.join('/') -%> +<% if actions.empty? -%> + describe "GET /index" do + pending "add some examples (or delete) #{__FILE__}" + end +<% end -%> <% for action in actions -%> describe "GET /<%= action %>" do it "returns http success" do diff --git a/spec/generators/rspec/controller/controller_generator_spec.rb b/spec/generators/rspec/controller/controller_generator_spec.rb index 8516703f23..4143af50fe 100644 --- a/spec/generators/rspec/controller/controller_generator_spec.rb +++ b/spec/generators/rspec/controller/controller_generator_spec.rb @@ -17,6 +17,7 @@ it { is_expected.to exist } it { is_expected.to contain(/require 'rails_helper'/) } it { is_expected.to contain(/^RSpec.describe "Posts", #{type_metatag(:request)}/) } + it { is_expected.to contain('pending') } end end From dee0214f580231d8616df15840c9ec819bf44dd2 Mon Sep 17 00:00:00 2001 From: Jon Rowe Date: Sun, 1 Nov 2020 11:25:00 +0000 Subject: [PATCH 08/23] Changelog for #2399 --- Changelog.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Changelog.md b/Changelog.md index b282cf44b0..06fa41d24c 100644 --- a/Changelog.md +++ b/Changelog.md @@ -14,6 +14,8 @@ Enhancements: (Eloy Espinaco, Luka Lüdicke, #2355, #2356, #2378) * Generated scaffold now includes engine route helpers when inside a mountable engine. (Andrew W. Lee, #2372) +* Improve request spec "controller" scafold when no action is specified. + (Thomas Hareau, #2399) Bug Fixes: From 9cf2b1ff8656677406f73eff8984930b121d4bf8 Mon Sep 17 00:00:00 2001 From: Jon Rowe Date: Tue, 17 Nov 2020 08:36:10 +0000 Subject: [PATCH 09/23] Add exception for rspec ci --- rspec-rails.gemspec | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/rspec-rails.gemspec b/rspec-rails.gemspec index 237093112d..0f67a2c4f9 100644 --- a/rspec-rails.gemspec +++ b/rspec-rails.gemspec @@ -45,7 +45,9 @@ Gem::Specification.new do |s| # that we want. These will need to change from time to time as new RSpecs # get released. %w[core expectations mocks support].each do |name| - if RSpec::Rails::Version::STRING =~ /pre/ # prerelease builds + if ENV['RSPEC_CI'] + s.add_runtime_dependency "rspec-#{name}", "= 4.0.0.pre" + elsif RSpec::Rails::Version::STRING =~ /pre/ # prerelease builds expected_rspec_version = "3.11.0.pre" s.add_runtime_dependency "rspec-#{name}", "= #{expected_rspec_version}" else From b01ebbec47709a2ba023e2d85c1238e627b0dd5a Mon Sep 17 00:00:00 2001 From: Jon Rowe Date: Mon, 23 Nov 2020 09:30:23 +0000 Subject: [PATCH 10/23] Merge pull request #2404 from rspec/move-to-github-actions Move MRI build to Github Actions --- .github/workflows/ci.yml | 108 ++++++++++++++++++ .travis.yml | 64 ++--------- .../matchers/have_broadcasted_matcher.feature | 10 +- .../rails/example/mailbox_example_group.rb | 2 +- .../rspec/scaffold/scaffold_generator_spec.rb | 13 ++- 5 files changed, 129 insertions(+), 68 deletions(-) create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000000..ac2b0bd7b4 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,108 @@ +name: RSpec CI +on: + push: + branches: + - 'main' + - '*-maintenance' + - '*-dev' + pull_request: + branches: + - '*' +jobs: + test: + name: 'Ruby: ${{ matrix.ruby }}, Rails: ${{ matrix.env.RAILS_VERSION }}' + runs-on: ubuntu-20.04 + strategy: + fail-fast: false + matrix: + include: + # Rails 6.1 builds >= 2.5 + - ruby: ruby-3.0.0-preview1 + allow_failure: true + env: + RAILS_VERSION: 'master' + - ruby: 2.7.1 + env: + RAILS_VERSION: 'master' + - ruby: 2.6.6 + env: + RAILS_VERSION: 'master' + - ruby: 2.5.8 + env: + RAILS_VERSION: 'master' + + # Rails 6.0 builds >= 2.5.0 + - ruby: 3.0.0-preview1 + env: + RAILS_VERSION: '~> 6.0.0' + - ruby: 2.7 + env: + RAILS_VERSION: '~> 6.0.0' + - ruby: 2.6 + env: + RAILS_VERSION: '~> 6.0.0' + - ruby: 2.5 + env: + RAILS_VERSION: '~> 6.0.0' + # Rails 5.2 builds >= 2.2.2 + - ruby: 2.6.6 + env: + RAILS_VERSION: '~> 5.2.0' + - ruby: 2.5.8 + env: + RAILS_VERSION: '~> 5.2.0' + - ruby: 2.4.10 + env: + RAILS_VERSION: '~> 5.2.0' + - ruby: 2.3.8 + env: + RAILS_VERSION: '~> 5.2.0' + - ruby: 2.2.10 + allow_failure: true + env: + RAILS_VERSION: '~> 5.2.0' + - ruby: 2.2.10 + env: + RAILS_VERSION: '5-2-stable' + # Rails 5.1 Builds >= 2.2.2 + - ruby: 2.6.6 + env: + RAILS_VERSION: '~> 5.1.0' + - ruby: 2.5.8 + env: + RAILS_VERSION: '~> 5.1.0' + - ruby: 2.4.10 + env: + RAILS_VERSION: '~> 5.1.0' + - ruby: 2.3.8 + env: + RAILS_VERSION: '~> 5.1.0' + - ruby: 2.2.10 + env: + RAILS_VERSION: '~> 5.1.0' + # Rails 5.0 Builds >= 2.2.2 + - ruby: 2.6.6 + env: + RAILS_VERSION: '~> 5.0.0' + - ruby: 2.5.8 + env: + RAILS_VERSION: '~> 5.0.0' + - ruby: 2.4.10 + env: + RAILS_VERSION: '~> 5.0.0' + - ruby: 2.3.8 + env: + RAILS_VERSION: '~> 5.0.0' + - ruby: 2.2.10 + env: + RAILS_VERSION: '~> 5.0.0' + env: ${{ matrix.env }} + steps: + - uses: actions/checkout@v2 + - uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ matrix.ruby }} + - run: script/clone_all_rspec_repos + - run: bundle install --binstubs + - run: script/run_build + continue-on-error: ${{ matrix.allow_failure || false }} diff --git a/.travis.yml b/.travis.yml index a88f9ffd4f..1d48088034 100644 --- a/.travis.yml +++ b/.travis.yml @@ -39,16 +39,7 @@ script: "script/run_build 2>&1" matrix: include: # Rails 6.1 builds >= 2.5 - - rvm: ruby-3.0.0-preview1 - env: RAILS_VERSION='master' - allow_failure: true - - rvm: 2.7.1 - env: RAILS_VERSION='master' - - rvm: 2.6.6 - env: RAILS_VERSION='master' - - rvm: 2.5.8 - env: RAILS_VERSION='master' - jdk: oraclejdk11 + - jdk: oraclejdk11 env: - RAILS_VERSION='master' - JRUBY_OPT=--dev @@ -61,59 +52,18 @@ matrix: - RAILS_VERSION='~> 6.0.0' - JRUBY_OPT=--dev - JAVA_OPTS="--add-opens java.base/sun.nio.ch=org.jruby.dist --add-opens java.base/java.io=org.jruby.dist --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.security=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.security.cert=ALL-UNNAMED --add-opens=java.base/java.util.zip=ALL-UNNAMED --add-opens=java.base/java.lang.reflect=ALL-UNNAMED --add-opens=java.base/java.util.regex=ALL-UNNAMED --add-opens=java.base/java.net=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/javax.crypto=ALL-UNNAMED --add-opens=java.management/sun.management=ALL-UNNAMED" - - rvm: ruby-3.0.0-preview1 - env: RAILS_VERSION='~> 6.0.0' - allow_failure: true - - rvm: 2.7.1 - env: RAILS_VERSION='~> 6.0.0' - - rvm: 2.6.6 - env: RAILS_VERSION='~> 6.0.0' - - rvm: 2.5.8 - env: RAILS_VERSION='~> 6.0.0' - # Rails 5.2 builds >= 2.2.2 - rvm: jruby-head jdk: oraclejdk11 env: - RAILS_VERSION='~> 5.2.0' - JRUBY_OPT=--dev - JAVA_OPTS="--add-opens java.base/sun.nio.ch=org.jruby.dist --add-opens java.base/java.io=org.jruby.dist --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.security=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.security.cert=ALL-UNNAMED --add-opens=java.base/java.util.zip=ALL-UNNAMED --add-opens=java.base/java.lang.reflect=ALL-UNNAMED --add-opens=java.base/java.util.regex=ALL-UNNAMED --add-opens=java.base/java.net=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/javax.crypto=ALL-UNNAMED --add-opens=java.management/sun.management=ALL-UNNAMED" - - rvm: 2.6.6 - env: RAILS_VERSION='~> 5.2.0' - - rvm: 2.5.8 - env: RAILS_VERSION='~> 5.2.0' - - rvm: 2.4.10 - 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 - env: RAILS_VERSION='~> 5.1.0' - - rvm: 2.5.8 - env: RAILS_VERSION='~> 5.1.0' - - rvm: 2.4.10 - 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 - env: RAILS_VERSION='~> 5.0.0' - - rvm: 2.5.8 - env: RAILS_VERSION='~> 5.0.0' - - rvm: 2.4.10 - 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 + +branches: + only: + - master + - /^\d+-\d+-maintenance$/ + - /^\d+-\d+-dev$/ diff --git a/features/matchers/have_broadcasted_matcher.feature b/features/matchers/have_broadcasted_matcher.feature index ecacadafe9..8b639326ac 100644 --- a/features/matchers/have_broadcasted_matcher.feature +++ b/features/matchers/have_broadcasted_matcher.feature @@ -16,7 +16,7 @@ Feature: have_broadcasted matcher it "matches with stream name" do expect { ActionCable.server.broadcast( - "notifications", text: 'Hello!' + "notifications", { text: "Hello!" } ) }.to have_broadcasted_to("notifications") end @@ -34,7 +34,7 @@ Feature: have_broadcasted matcher it "matches with message" do expect { ActionCable.server.broadcast( - "notifications", text: 'Hello!' + "notifications", { text: "Hello!" } ) }.to have_broadcasted_to("notifications").with(text: 'Hello!') end @@ -52,7 +52,7 @@ Feature: have_broadcasted matcher it "matches with message" do expect { ActionCable.server.broadcast( - "notifications", text: 'Hello!', user_id: 12 + "notifications", { text: 'Hello!', user_id: 12 } ) }.to have_broadcasted_to("notifications").with(a_hash_including(text: 'Hello!')) end @@ -70,7 +70,7 @@ Feature: have_broadcasted matcher it "matches with message" do expect { ActionCable.server.broadcast( - "notifications", text: 'Hello!', user_id: 12 + "notifications", { text: 'Hello!', user_id: 12 } ) }.to have_broadcasted_to("notifications").with { |data| expect(data['user_id']).to eq 12 @@ -90,7 +90,7 @@ Feature: have_broadcasted matcher it "matches with stream name" do expect { ActionCable.server.broadcast( - "notifications", text: 'Hello!' + "notifications", { text: 'Hello!' } ) }.to broadcast_to("notifications") end diff --git a/lib/rspec/rails/example/mailbox_example_group.rb b/lib/rspec/rails/example/mailbox_example_group.rb index 24cd125b60..de85acdf43 100644 --- a/lib/rspec/rails/example/mailbox_example_group.rb +++ b/lib/rspec/rails/example/mailbox_example_group.rb @@ -13,7 +13,7 @@ module MailboxExampleGroup def self.create_inbound_email(arg) case arg when Hash - create_inbound_email_from_mail(arg) + create_inbound_email_from_mail(**arg) else create_inbound_email_from_source(arg.to_s) end diff --git a/spec/generators/rspec/scaffold/scaffold_generator_spec.rb b/spec/generators/rspec/scaffold/scaffold_generator_spec.rb index a56b5858ea..d8417519f1 100644 --- a/spec/generators/rspec/scaffold/scaffold_generator_spec.rb +++ b/spec/generators/rspec/scaffold/scaffold_generator_spec.rb @@ -1,8 +1,10 @@ # Generators are not automatically loaded by Rails require 'generators/rspec/scaffold/scaffold_generator' require 'support/generators' +require 'rspec/support/spec/in_sub_process' RSpec.describe Rspec::Generators::ScaffoldGenerator, type: :generator do + include RSpec::Support::InSubProcess setup_default_destination describe 'standard request specs' do @@ -41,12 +43,13 @@ end describe 'in an engine' do - before do - allow_any_instance_of(::Rails::Generators::NamedBase).to receive(:mountable_engine?).and_return(true) - run_generator %w[posts --request_specs] + it 'generates files with Engine url_helpers' do + in_sub_process do + allow_any_instance_of(::Rails::Generators::NamedBase).to receive(:mountable_engine?).and_return(true) + run_generator %w[posts --request_specs] + is_expected.to contain('Engine.routes.url_helpers') + end end - - it { is_expected.to contain('Engine.routes.url_helpers') } end end From e83c996b0764f5bec0736ea45e9f6deedc3f1f4e Mon Sep 17 00:00:00 2001 From: Benoit Tigeot Date: Wed, 2 Dec 2020 12:55:50 +0100 Subject: [PATCH 11/23] Add 6.1.0 to matrix --- .github/workflows/ci.yml | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ac2b0bd7b4..db17fd9242 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -16,23 +16,40 @@ jobs: fail-fast: false matrix: include: - # Rails 6.1 builds >= 2.5 - - ruby: ruby-3.0.0-preview1 + # Rails Master builds >= 2.5 + - ruby: ruby-3.0.0-preview2 allow_failure: true env: RAILS_VERSION: 'master' - ruby: 2.7.1 + allow_failure: true env: RAILS_VERSION: 'master' - ruby: 2.6.6 + allow_failure: true env: RAILS_VERSION: 'master' - ruby: 2.5.8 + allow_failure: true env: RAILS_VERSION: 'master' + # Rails 6.1.0 builds >= 2.5 + - ruby: ruby-3.0.0-preview2 + env: + RAILS_VERSION: '~> 6.1.0' + - ruby: 2.7.1 + env: + RAILS_VERSION: '~> 6.1.0' + - ruby: 2.6.6 + env: + RAILS_VERSION: '~> 6.1.0' + - ruby: 2.5.8 + env: + RAILS_VERSION: '~> 6.1.0' + # Rails 6.0 builds >= 2.5.0 - - ruby: 3.0.0-preview1 + - ruby: 3.0.0-preview2 env: RAILS_VERSION: '~> 6.0.0' - ruby: 2.7 From a90e52f1177205b1d32d9d3e997d8c947fed218c Mon Sep 17 00:00:00 2001 From: Benoit Tigeot Date: Wed, 4 Nov 2020 00:46:59 +0100 Subject: [PATCH 12/23] Use mailbox_for when available https://github.com/rails/rails/pull/36181 --- lib/rspec/rails/matchers/action_mailbox.rb | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/rspec/rails/matchers/action_mailbox.rb b/lib/rspec/rails/matchers/action_mailbox.rb index c5392cb225..de50c69143 100644 --- a/lib/rspec/rails/matchers/action_mailbox.rb +++ b/lib/rspec/rails/matchers/action_mailbox.rb @@ -22,11 +22,20 @@ def initialize(message) @inbound_email = create_inbound_email(message) end - def matches?(mailbox) - @mailbox = mailbox - @receiver = ApplicationMailbox.router.send(:match_to_mailbox, inbound_email) + if defined?(::ApplicationMailbox) && ::ApplicationMailbox.router.respond_to?(:mailbox_for) + def matches?(mailbox) + @mailbox = mailbox + @receiver = ApplicationMailbox.router.mailbox_for(inbound_email) - @receiver == @mailbox + @receiver == @mailbox + end + else + def matches?(mailbox) + @mailbox = mailbox + @receiver = ApplicationMailbox.router.send(:match_to_mailbox, inbound_email) + + @receiver == @mailbox + end end def failure_message @@ -41,7 +50,7 @@ def failure_message_when_negated "expected #{describe_inbound_email} not to route to #{mailbox}" end - private + private attr_reader :inbound_email, :mailbox, :receiver From d61f35b0d46e7e77e22c36d092c823a995f08f80 Mon Sep 17 00:00:00 2001 From: Benoit Tigeot Date: Sun, 1 Nov 2020 14:47:43 +0100 Subject: [PATCH 13/23] Skip test for deprecated support of relative paths to `render file:` --- features/controller_specs/anonymous_controller.feature | 2 ++ 1 file changed, 2 insertions(+) diff --git a/features/controller_specs/anonymous_controller.feature b/features/controller_specs/anonymous_controller.feature index 7892b4dfac..9baf73fb8d 100644 --- a/features/controller_specs/anonymous_controller.feature +++ b/features/controller_specs/anonymous_controller.feature @@ -101,6 +101,8 @@ Feature: anonymous controller When I run `rspec spec` Then the examples should all pass + # Deprecated support removed in https://github.com/rails/rails/commit/d52d7739468153bd6cb7c629f60bd5cd7ebea3eb + @rails_pre_6 Scenario: Specify error handling in `ApplicationController` with render :file Given a file named "spec/controllers/application_controller_spec.rb" with: """ruby From b5337fa5cf373ef169ce014d0303106f0db5e01c Mon Sep 17 00:00:00 2001 From: Jon Rowe Date: Fri, 11 Dec 2020 21:05:13 +0000 Subject: [PATCH 14/23] Replace deprecated parent method with module_parent --- .../generate_action_mailer_specs.rb | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/example_app_generator/generate_action_mailer_specs.rb b/example_app_generator/generate_action_mailer_specs.rb index 13a1f0a4c0..06833fc1a5 100644 --- a/example_app_generator/generate_action_mailer_specs.rb +++ b/example_app_generator/generate_action_mailer_specs.rb @@ -14,15 +14,20 @@ end end CODE - gsub_file 'config/initializers/action_mailer.rb', - /ExampleApp/, - Rails.application.class.parent.to_s + + rails_parent = + if Rails.version.to_f >= 6.0 + Rails.application.class.module_parent.to_s + else + Rails.application.class.parent.to_s + end + + gsub_file 'config/initializers/action_mailer.rb', /ExampleApp/, rails_parent copy_file 'spec/support/default_preview_path' chmod 'spec/support/default_preview_path', 0755 - gsub_file 'spec/support/default_preview_path', - /ExampleApp/, - Rails.application.class.parent.to_s + gsub_file 'spec/support/default_preview_path', /ExampleApp/, rails_parent + if skip_active_record? comment_lines 'spec/support/default_preview_path', /active_record/ comment_lines 'spec/support/default_preview_path', /active_storage/ From 7f2a6f49270dd68ee381cba82b738fe291b99f47 Mon Sep 17 00:00:00 2001 From: Jon Rowe Date: Sun, 27 Dec 2020 12:33:16 +0000 Subject: [PATCH 15/23] Update ci.yml --- .github/workflows/ci.yml | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 50935d026f..c606beb045 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -17,39 +17,40 @@ jobs: matrix: include: # Rails Master builds >= 2.5 - - ruby: ruby-3.0.0-preview2 + - ruby: 3.0 allow_failure: true env: RAILS_VERSION: 'master' - - ruby: 2.7.1 + - ruby: 2.7 allow_failure: true env: RAILS_VERSION: 'master' - - ruby: 2.6.6 + - ruby: 2.6 allow_failure: true env: RAILS_VERSION: 'master' - - ruby: 2.5.8 + - ruby: 2.5 allow_failure: true env: RAILS_VERSION: 'master' # Rails 6.1.0 builds >= 2.5 - - ruby: ruby-3.0.0-preview2 + - ruby: 3.0 allow_failure: true env: RAILS_VERSION: '~> 6.1.0' - - ruby: 2.7.1 + - ruby: 2.7 env: RAILS_VERSION: '~> 6.1.0' - - ruby: 2.6.6 + - ruby: 2.6 env: RAILS_VERSION: '~> 6.1.0' - - ruby: 2.5.8 + - ruby: 2.5 env: RAILS_VERSION: '~> 6.1.0' + # Rails 6.0 builds >= 2.5.0 - - ruby: 3.0.0-preview2 + - ruby: 3.0 env: RAILS_VERSION: '~> 6.0.0' - ruby: 2.7 From 040fecc98894de42a433d94a15d19f07434b77f8 Mon Sep 17 00:00:00 2001 From: Phil Pirozhkov Date: Thu, 14 Jan 2021 10:01:26 +0300 Subject: [PATCH 16/23] Add testing snippets Snippet is a self-contained example that defines the configuration, Rails project code and specs to run. Snippets: - allow for clean separation between different snippets - work quite fast - reuse the already installed gems that specs and Cukes use - do not litter - do not depend on other parts of the build In theory snippets retain the ability to use generators and arbitrary commands, but it makes the case under test less evident. Co-authored-by: Benoit Tigeot --- BUILD_DETAIL.md | 15 +++++++ Changelog.md | 1 + Gemfile | 21 +--------- Gemfile-sqlite-dependencies | 20 ++++++++++ script/run_build | 2 + script/run_snippets.sh | 13 ++++++ snippets/use_active_record_false.rb | 61 +++++++++++++++++++++++++++++ 7 files changed, 113 insertions(+), 20 deletions(-) create mode 100644 Gemfile-sqlite-dependencies create mode 100755 script/run_snippets.sh create mode 100644 snippets/use_active_record_false.rb diff --git a/BUILD_DETAIL.md b/BUILD_DETAIL.md index c2bea9ddf5..fc5b269986 100644 --- a/BUILD_DETAIL.md +++ b/BUILD_DETAIL.md @@ -66,6 +66,21 @@ $ bundle exec cucumber $ bin/cucumber ``` +## Snippets + +RSpec Rails uses snippets, self-contained examples that are used to cover +cases and regressions that don't need a full-blown example application to +reproduce. + +Snippets reuse the already installed gems, and don't attempt to install gem +versions that are not on the system already to prevent version mismatches. + +Run with: + +``` +$ script/run_snippets.sh +``` + ## YARD documentation RSpec uses [YARD](https://yardoc.org/) for API documentation on the [rspec.info site](https://rspec.info/). diff --git a/Changelog.md b/Changelog.md index 06fa41d24c..90410b3a49 100644 --- a/Changelog.md +++ b/Changelog.md @@ -16,6 +16,7 @@ Enhancements: (Andrew W. Lee, #2372) * Improve request spec "controller" scafold when no action is specified. (Thomas Hareau, #2399) +* Introduce testing snippets concept (Phil Pirozhkov, Benoit Tigeot, #2423) Bug Fixes: diff --git a/Gemfile b/Gemfile index a438462dc1..8d74d1b894 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,4 @@ source "https://rubygems.org" -version_file = File.expand_path('.rails-version', __dir__) -RAILS_VERSION = ENV['RAILS_VERSION'] || (File.exist?(version_file) && File.read(version_file).chomp) || "" gemspec @@ -22,28 +20,11 @@ end gem 'capybara' -MAJOR = - case RAILS_VERSION - when /5-2-stable/ - 5 - when /master/, /stable/, nil, false, '' - 6 - else - /(\d+)[\.|-]\d+/.match(RAILS_VERSION).captures.first.to_i - end - -if MAJOR >= 6 - # sqlite3 is an optional, unspecified, dependency and Rails 6.0 only supports `~> 1.4` - gem 'sqlite3', '~> 1.4', platforms: [:ruby] -else - # Similarly, Rails 5.0 only supports '~> 1.3.6'. Rails 5.1-5.2 support '~> 1.3', '>= 1.3.6' - gem 'sqlite3', '~> 1.3.6', platforms: [:ruby] -end - # Until 1.13.2 is released due to Rubygems usage gem 'ffi', '~> 1.12.0' custom_gemfile = File.expand_path('Gemfile-custom', __dir__) eval_gemfile custom_gemfile if File.exist?(custom_gemfile) +eval_gemfile 'Gemfile-sqlite-dependencies' eval_gemfile 'Gemfile-rails-dependencies' diff --git a/Gemfile-sqlite-dependencies b/Gemfile-sqlite-dependencies new file mode 100644 index 0000000000..ace683988a --- /dev/null +++ b/Gemfile-sqlite-dependencies @@ -0,0 +1,20 @@ +version_file = File.expand_path('.rails-version', __dir__) +RAILS_VERSION = ENV['RAILS_VERSION'] || (File.exist?(version_file) && File.read(version_file).chomp) || "" + +MAJOR = + case RAILS_VERSION + when /5-2-stable/ + 5 + when /master/, /stable/, nil, false, '' + 6 + else + /(\d+)[\.|-]\d+/.match(RAILS_VERSION).captures.first.to_i + end + +if MAJOR >= 6 +# sqlite3 is an optional, unspecified, dependency and Rails 6.0 only supports `~> 1.4` + gem 'sqlite3', '~> 1.4', platforms: [:ruby] +else +# Similarly, Rails 5.0 only supports '~> 1.3.6'. Rails 5.1-5.2 support '~> 1.3', '>= 1.3.6' + gem 'sqlite3', '~> 1.3.6', platforms: [:ruby] +end diff --git a/script/run_build b/script/run_build index e20840e3ca..b7e542bfba 100755 --- a/script/run_build +++ b/script/run_build @@ -20,6 +20,8 @@ fi fold "cukes" run_cukes +fold "snippets" script/run_snippets.sh + if documentation_enforced; then fold "doc check" check_documentation_coverage fi diff --git a/script/run_snippets.sh b/script/run_snippets.sh new file mode 100755 index 0000000000..45146e0277 --- /dev/null +++ b/script/run_snippets.sh @@ -0,0 +1,13 @@ +#!/bin/bash +set -e + +( + cd snippets + # This is required to load `bundle/inline` + unset RUBYOPT + for snippet in *.rb; + do + echo Running $snippet + ruby $snippet + done +) diff --git a/snippets/use_active_record_false.rb b/snippets/use_active_record_false.rb new file mode 100644 index 0000000000..2965223084 --- /dev/null +++ b/snippets/use_active_record_false.rb @@ -0,0 +1,61 @@ +if __FILE__ =~ /^snippets/ + fail "Snippets are supposed to be run from their own directory to avoid side " \ + "effects as e.g. the root `Gemfile`, or `spec/spec_helpers.rb` to be " \ + "loaded by the root `.rspec`." +end + +# We opt-out from using RubyGems, but `bundler/inline` requires it +require 'rubygems' + +require "bundler/inline" + +# We pass `false` to `gemfile` to skip the installation of gems, +# because it may install versions that would conflict with versions +# from the main `Gemfile.lock`. +gemfile(false) do + source "https://rubygems.org" + + git_source(:github) { |repo| "https://github.com/#{repo}.git" } + + # Those Gemfiles carefully pick the right versions depending on + # settings in the ENV, `.rails-version` and `maintenance-branch`. + Dir.chdir('..') do + eval_gemfile 'Gemfile-sqlite-dependencies' + # This Gemfile expects `maintenance-branch` file to be present + # in the current directory. + eval_gemfile 'Gemfile-rspec-dependencies' + # This Gemfile expects `.rails-version` file + eval_gemfile 'Gemfile-rails-dependencies' + end + + gem "rspec-rails", path: "../" +end + +# Run specs at exit +require "rspec/autorun" + +# This snippet describes the case when ActiveRecord is loaded, but +# `use_active_record` is set to `false` in RSpec configuration. + +# Initialization +require "active_record/railtie" +require "rspec/rails" + +# This connection will do for database-independent bug reports +ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:") + +# RSpec configuration +RSpec.configure do |config| + config.use_active_record = false +end + +# Rails project code +class Foo +end + +# Rails project specs +RSpec.describe Foo do + it 'does not not break' do + Foo + end +end From 23c42e72004527f6561eb19be73ecf76f2f8c174 Mon Sep 17 00:00:00 2001 From: Phil Pirozhkov Date: Wed, 23 Dec 2020 20:08:55 +0300 Subject: [PATCH 17/23] Fix WrongScopeError when use_active_record = false Fixes #2417 --- Changelog.md | 1 + lib/rspec/rails/fixture_support.rb | 14 +++++++------- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Changelog.md b/Changelog.md index 90410b3a49..8395f7743c 100644 --- a/Changelog.md +++ b/Changelog.md @@ -26,6 +26,7 @@ Bug Fixes: (Phil Pirozhkov, Jon Rowe, #2353, #2354) * Remove old #fixture_path feature detection code which broke under newer Rails. (Koen Punt, Jon Rowe, #2370) +* Fix an error when `use_active_record` is `false` (Phil Pirozhkov, #2423) ### 4.0.1 / 2020-05-16 [Full Changelog](https://github.com/rspec/rspec-rails/compare/v4.0.0...v4.0.1) diff --git a/lib/rspec/rails/fixture_support.rb b/lib/rspec/rails/fixture_support.rb index 2161b115c6..02786d3c29 100644 --- a/lib/rspec/rails/fixture_support.rb +++ b/lib/rspec/rails/fixture_support.rb @@ -9,6 +9,13 @@ module FixtureSupport include RSpec::Rails::MinitestAssertionAdapter include ActiveRecord::TestFixtures + if ::Rails.version.to_f >= 6.1 + # @private return the example name for TestFixtures + def name + @example + end + end + included do if RSpec.configuration.use_active_record? include Fixtures @@ -50,13 +57,6 @@ def proxy_method_warning_if_called_in_before_context_scope(method_name) end end end - - if ::Rails.version.to_f >= 6.1 - # @private return the example name for TestFixtures - def name - @example - end - end end end end From 1cedd987edefa72c82be4b343bdb4b630c435d1b Mon Sep 17 00:00:00 2001 From: Benoit Tigeot Date: Fri, 11 Dec 2020 00:34:54 +0100 Subject: [PATCH 18/23] Add a regression test for a removed method in 6.1 This method has been moved in https://github.com/rails/rails/commit/3cece0b6574c496605df055a2ebf77177f5b6e7f We do not include ActiveSupport::Testing::Assertion. This resulted in failures when `perform_enqueued_jobs do` was called from specs. Method was added back in https://github.com/rails/rails/pull/40780, and was released in Rails 6.1.1 --- spec/rspec/rails/matchers/active_job_spec.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/spec/rspec/rails/matchers/active_job_spec.rb b/spec/rspec/rails/matchers/active_job_spec.rb index 896cd7db1e..02c3132016 100644 --- a/spec/rspec/rails/matchers/active_job_spec.rb +++ b/spec/rspec/rails/matchers/active_job_spec.rb @@ -702,4 +702,16 @@ def self.name; "LoggingJob"; end }.to raise_error(/expected to perform exactly 1 jobs, but performed 0/) end end + + describe 'Active Job test helpers' do + include ActiveJob::TestHelper + + it 'does not raise that "assert_nothing_raised" is undefined' do + expect { + perform_enqueued_jobs do + :foo + end + }.to_not raise_error + end + end end From 9398cc87b7f44eee5aec5b2d6e6d6ce0c6d76112 Mon Sep 17 00:00:00 2001 From: Phil Pirozhkov Date: Thu, 18 Feb 2021 10:51:32 +0300 Subject: [PATCH 19/23] Remove Rails 7 builds against Ruby < 2.7 There won't be Rails 6.2. Rails master is tracking 7.0. 7.0 is dropping support for 2.5, 2.6. activesupport-7.0.0.alpha requires ruby version >= 2.7.0, which is incompatible See https://github.com/rspec/rspec-rails/pull/2461/checks?check_run_id=1922949773 --- .github/workflows/ci.yml | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c606beb045..fa880114d9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -16,7 +16,7 @@ jobs: fail-fast: false matrix: include: - # Rails Master builds >= 2.5 + # Rails Master (7.0) builds >= 2.7 - ruby: 3.0 allow_failure: true env: @@ -25,16 +25,8 @@ jobs: allow_failure: true env: RAILS_VERSION: 'master' - - ruby: 2.6 - allow_failure: true - env: - RAILS_VERSION: 'master' - - ruby: 2.5 - allow_failure: true - env: - RAILS_VERSION: 'master' - # Rails 6.1.0 builds >= 2.5 + # Rails 6.1 builds >= 2.5 - ruby: 3.0 allow_failure: true env: From 8edb6608d474b1559452df8ff602b4ed8711cff0 Mon Sep 17 00:00:00 2001 From: Benoit Tigeot Date: Wed, 17 Feb 2021 23:22:11 +0100 Subject: [PATCH 20/23] Add snippets for method collision on :name and :method_name --- snippets/avoid_fixture_name_collision.rb | 57 ++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 snippets/avoid_fixture_name_collision.rb diff --git a/snippets/avoid_fixture_name_collision.rb b/snippets/avoid_fixture_name_collision.rb new file mode 100644 index 0000000000..8f1768acc8 --- /dev/null +++ b/snippets/avoid_fixture_name_collision.rb @@ -0,0 +1,57 @@ +if __FILE__ =~ /^snippets/ + fail "Snippets are supposed to be run from their own directory to avoid side " \ + "effects as e.g. the root `Gemfile`, or `spec/spec_helpers.rb` to be " \ + "loaded by the root `.rspec`." +end + +# We opt-out from using RubyGems, but `bundler/inline` requires it +require 'rubygems' + +require "bundler/inline" + +# We pass `false` to `gemfile` to skip the installation of gems, +# because it may install versions that would conflict with versions +# from the main `Gemfile.lock`. +gemfile(false) do + source "https://rubygems.org" + + git_source(:github) { |repo| "https://github.com/#{repo}.git" } + + # Those Gemfiles carefully pick the right versions depending on + # settings in the ENV, `.rails-version` and `maintenance-branch`. + Dir.chdir('..') do + eval_gemfile 'Gemfile-sqlite-dependencies' + # This Gemfile expects `maintenance-branch` file to be present + # in the current directory. + eval_gemfile 'Gemfile-rspec-dependencies' + # This Gemfile expects `.rails-version` file + eval_gemfile 'Gemfile-rails-dependencies' + end + + gem "rspec-rails", path: "../" +end + +# Run specs at exit +require "rspec/autorun" + +require "rails" +require "active_record/railtie" +require "rspec/rails" + +# This connection will do for database-independent bug reports +ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:") + +RSpec.configure do |config| + config.use_transactional_fixtures = true +end + +RSpec.describe 'Foo' do + subject { true } + + # Rails 6.1 and after + let(:name) { raise "Should never raise" } + # Before Rails 6.1 + let(:method_name) { raise "Should never raise" } + + it { is_expected.to be_truthy } +end From 4aa5e0b4ad70dda0e243f15ac78d729b2a6694fa Mon Sep 17 00:00:00 2001 From: Benoit Tigeot Date: Wed, 17 Feb 2021 23:30:09 +0100 Subject: [PATCH 21/23] Prevent collisions on let :name and :method_name in rspec-rails examples Fix: https://github.com/rspec/rspec-rails/issues/2451 --- lib/rspec/rails/fixture_support.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/rspec/rails/fixture_support.rb b/lib/rspec/rails/fixture_support.rb index 02786d3c29..1a7221e829 100644 --- a/lib/rspec/rails/fixture_support.rb +++ b/lib/rspec/rails/fixture_support.rb @@ -9,11 +9,11 @@ module FixtureSupport include RSpec::Rails::MinitestAssertionAdapter include ActiveRecord::TestFixtures - if ::Rails.version.to_f >= 6.1 - # @private return the example name for TestFixtures - def name - @example - end + # @private prevent ActiveSupport::TestFixtures to start a DB transaction. + # Monkey patched to avoid collisions with 'let(:name)' in Rails 6.1 and after + # and let(:method_name) before Rails 6.1. + def run_in_transaction? + use_transactional_tests && !self.class.uses_transaction?(@example) end included do From ee760f2611e5c8f44494381ebe8546cceb09fcde Mon Sep 17 00:00:00 2001 From: Benoit Tigeot Date: Thu, 18 Feb 2021 21:52:40 +0100 Subject: [PATCH 22/23] @example resolve to nil, self: RSpec::ExampleGroups::ObservesACallToName --- lib/rspec/rails/fixture_support.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rspec/rails/fixture_support.rb b/lib/rspec/rails/fixture_support.rb index 1a7221e829..12acfdfbd6 100644 --- a/lib/rspec/rails/fixture_support.rb +++ b/lib/rspec/rails/fixture_support.rb @@ -13,7 +13,7 @@ module FixtureSupport # Monkey patched to avoid collisions with 'let(:name)' in Rails 6.1 and after # and let(:method_name) before Rails 6.1. def run_in_transaction? - use_transactional_tests && !self.class.uses_transaction?(@example) + use_transactional_tests && !self.class.uses_transaction?(self) end included do From 5874755a3e6043a0dada6d24c7414728900f9401 Mon Sep 17 00:00:00 2001 From: Jon Rowe Date: Sat, 20 Feb 2021 09:18:55 +0000 Subject: [PATCH 23/23] Changelog for #2461 --- Changelog.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Changelog.md b/Changelog.md index 8395f7743c..4c50085608 100644 --- a/Changelog.md +++ b/Changelog.md @@ -3,6 +3,11 @@ * Support new #file_fixture_path and new fixture test support code. (Jon Rowe, #2398) +Bug Fixes: + +* Prevent collisions with `let(:name)` for Rails 6.1 and `let(:method_name)` on older + Rails. (Benoit Tigeot, #2461) + ### Development [Full Changelog](https://github.com/rspec/rspec-rails/compare/v4.0.1...main)