From d3e1c4bfe8b44883ecf64943dc699012a33aab30 Mon Sep 17 00:00:00 2001 From: Alex Robbin Date: Sat, 2 Dec 2023 21:41:24 -0500 Subject: [PATCH] migrate to Rails 7.1's `file_fixture_upload` naming approach rails/rails#48857 renamed `fixture_file_upload` to `file_fixture_upload` to reduce "confusion and surprise". With this change, `file_fixture_upload` now works in RSpec, with `fixture_file_upload` still functional to maintain backwards compatibility. --- .rubocop_todo.yml | 2 +- example_app_generator/generate_stuff.rb | 2 +- ....rb => verify_file_fixture_upload_spec.rb} | 4 +-- lib/rspec/rails.rb | 2 +- lib/rspec/rails/configuration.rb | 4 +-- ...port.rb => file_fixture_upload_support.rb} | 14 +++++----- ...rb => file_fixture_upload_support_spec.rb} | 28 +++++++++---------- 7 files changed, 28 insertions(+), 28 deletions(-) rename example_app_generator/no_active_record/spec/{verify_fixture_file_upload_spec.rb => verify_file_fixture_upload_spec.rb} (66%) rename lib/rspec/rails/{fixture_file_upload_support.rb => file_fixture_upload_support.rb} (76%) rename spec/rspec/rails/{fixture_file_upload_support_spec.rb => file_fixture_upload_support_spec.rb} (58%) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index d7f5923875..a387554207 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -116,7 +116,7 @@ Lint/SuppressedException: # AllowedPatterns: (?-mix:(exactly|at_least|at_most)\(\d+\)\.times) Lint/UnreachableLoop: Exclude: - - 'spec/rspec/rails/fixture_file_upload_support_spec.rb' + - 'spec/rspec/rails/file_fixture_upload_support_spec.rb' - 'spec/rspec/rails/fixture_support_spec.rb' # Offense count: 12 diff --git a/example_app_generator/generate_stuff.rb b/example_app_generator/generate_stuff.rb index af074104fa..999bb9ec88 100644 --- a/example_app_generator/generate_stuff.rb +++ b/example_app_generator/generate_stuff.rb @@ -48,7 +48,7 @@ def setup_tasks def final_tasks copy_file 'spec/verify_no_active_record_spec.rb' copy_file 'spec/verify_no_fixture_setup_spec.rb' - copy_file 'spec/verify_fixture_file_upload_spec.rb' + copy_file 'spec/verify_file_fixture_upload_spec.rb' end def skip_active_record? diff --git a/example_app_generator/no_active_record/spec/verify_fixture_file_upload_spec.rb b/example_app_generator/no_active_record/spec/verify_file_fixture_upload_spec.rb similarity index 66% rename from example_app_generator/no_active_record/spec/verify_fixture_file_upload_spec.rb rename to example_app_generator/no_active_record/spec/verify_file_fixture_upload_spec.rb index 2a96e28374..7a66a5a3da 100644 --- a/example_app_generator/no_active_record/spec/verify_fixture_file_upload_spec.rb +++ b/example_app_generator/no_active_record/spec/verify_file_fixture_upload_spec.rb @@ -1,8 +1,8 @@ require 'rails_helper' RSpec.describe 'Example App', :use_fixtures, type: :model do - it 'supports fixture file upload' do - file = fixture_file_upload(__FILE__) + it 'supports file_fixture_upload' do + file = file_fixture_upload(__FILE__) expect(file.read).to match(/RSpec\.describe 'Example App'/im) end end diff --git a/lib/rspec/rails.rb b/lib/rspec/rails.rb index 2aa1f5b412..01bb579334 100644 --- a/lib/rspec/rails.rb +++ b/lib/rspec/rails.rb @@ -10,7 +10,7 @@ require 'rspec/rails/matchers' require 'rspec/rails/fixture_support' require 'rspec/rails/file_fixture_support' -require 'rspec/rails/fixture_file_upload_support' +require 'rspec/rails/file_fixture_upload_support' require 'rspec/rails/example' require 'rspec/rails/vendor/capybara' require 'rspec/rails/configuration' diff --git a/lib/rspec/rails/configuration.rb b/lib/rspec/rails/configuration.rb index c21ed2e97d..f669111385 100644 --- a/lib/rspec/rails/configuration.rb +++ b/lib/rspec/rails/configuration.rb @@ -90,8 +90,8 @@ def self.initialize_configuration(config) # rubocop:disable Metrics/MethodLength config.add_setting :file_fixture_path, default: 'spec/fixtures/files' config.include RSpec::Rails::FileFixtureSupport - # Add support for fixture_path on fixture_file_upload - config.include RSpec::Rails::FixtureFileUploadSupport + # Add support for fixture_path on file_fixture_upload + config.include RSpec::Rails::FileFixtureUploadSupport # This allows us to expose `render_views` as a config option even though it # breaks the convention of other options by using `render_views` as a diff --git a/lib/rspec/rails/fixture_file_upload_support.rb b/lib/rspec/rails/file_fixture_upload_support.rb similarity index 76% rename from lib/rspec/rails/fixture_file_upload_support.rb rename to lib/rspec/rails/file_fixture_upload_support.rb index c7c4262ffd..976c67875d 100644 --- a/lib/rspec/rails/fixture_file_upload_support.rb +++ b/lib/rspec/rails/file_fixture_upload_support.rb @@ -1,16 +1,16 @@ module RSpec module Rails # @private - module FixtureFileUploadSupport - delegate :fixture_file_upload, to: :rails_fixture_file_wrapper + module FileFixtureUploadSupport + delegate :file_fixture_upload, :fixture_file_upload, to: :rails_file_fixture_wrapper private # In Rails 7.0 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 7.0 expect to rework this to remove # the old accessor. - def rails_fixture_file_wrapper - RailsFixtureFileWrapper.file_fixture_path = nil + def rails_file_fixture_wrapper + RailsFileFixtureWrapper.file_fixture_path = nil resolved_fixture_path = if respond_to?(:file_fixture_path) && !file_fixture_path.nil? file_fixture_path.to_s @@ -19,11 +19,11 @@ def rails_fixture_file_wrapper 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 + RailsFileFixtureWrapper.file_fixture_path = File.join(resolved_fixture_path, '') unless resolved_fixture_path.strip.empty? + RailsFileFixtureWrapper.instance end - class RailsFixtureFileWrapper + class RailsFileFixtureWrapper include ActionDispatch::TestProcess if defined?(ActionDispatch::TestProcess) include ActiveSupport::Testing::FileFixtures diff --git a/spec/rspec/rails/fixture_file_upload_support_spec.rb b/spec/rspec/rails/file_fixture_upload_support_spec.rb similarity index 58% rename from spec/rspec/rails/fixture_file_upload_support_spec.rb rename to spec/rspec/rails/file_fixture_upload_support_spec.rb index 2a139fe0ce..aad8b8f6fe 100644 --- a/spec/rspec/rails/fixture_file_upload_support_spec.rb +++ b/spec/rspec/rails/file_fixture_upload_support_spec.rb @@ -1,53 +1,53 @@ module RSpec::Rails - RSpec.describe FixtureFileUploadSupport do + RSpec.describe FileFixtureUploadSupport do if ::Rails::VERSION::STRING < "7.1.0" context 'with fixture path set in config' do it 'resolves fixture file' do RSpec.configuration.fixture_path = File.dirname(__FILE__) - expect_to_pass fixture_file_upload_resolved('fixture_file_upload_support_spec.rb') + expect_to_pass file_fixture_upload_resolved('file_fixture_upload_support_spec.rb') end it 'resolves supports `Pathname` objects' do RSpec.configuration.fixture_path = Pathname(File.dirname(__FILE__)) - expect_to_pass fixture_file_upload_resolved('fixture_file_upload_support_spec.rb') + expect_to_pass file_fixture_upload_resolved('file_fixture_upload_support_spec.rb') end end context 'with fixture path set in spec' do it 'resolves fixture file' do - expect_to_pass fixture_file_upload_resolved('fixture_file_upload_support_spec.rb', File.dirname(__FILE__)) + expect_to_pass file_fixture_upload_resolved('file_fixture_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_to_pass fixture_file_upload_resolved('spec/rspec/rails/fixture_file_upload_support_spec.rb') + expect_to_pass file_fixture_upload_resolved('spec/rspec/rails/file_fixture_upload_support_spec.rb') end end else context 'with fixture paths set in config' do it 'resolves fixture file' do RSpec.configuration.fixture_paths = [File.dirname(__FILE__)] - expect_to_pass fixture_file_upload_resolved('fixture_file_upload_support_spec.rb') + expect_to_pass file_fixture_upload_resolved('file_fixture_upload_support_spec.rb') end it 'resolves supports `Pathname` objects' do RSpec.configuration.fixture_paths = [Pathname(File.dirname(__FILE__))] - expect_to_pass fixture_file_upload_resolved('fixture_file_upload_support_spec.rb') + expect_to_pass file_fixture_upload_resolved('file_fixture_upload_support_spec.rb') end end context 'with fixture path set in spec' do it 'resolves fixture file' do - expect_to_pass fixture_file_upload_resolved('fixture_file_upload_support_spec.rb', File.dirname(__FILE__)) + expect_to_pass file_fixture_upload_resolved('file_fixture_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_to_pass fixture_file_upload_resolved('spec/rspec/rails/fixture_file_upload_support_spec.rb') + expect_to_pass file_fixture_upload_resolved('spec/rspec/rails/file_fixture_upload_support_spec.rb') end end end @@ -58,15 +58,15 @@ def expect_to_pass(group) expect(result).to be true end - def fixture_file_upload_resolved(fixture_name, file_fixture_path = nil) + def file_fixture_upload_resolved(fixture_name, file_fixture_path = nil) RSpec::Core::ExampleGroup.describe do - include RSpec::Rails::FixtureFileUploadSupport + include RSpec::Rails::FileFixtureUploadSupport self.file_fixture_path = file_fixture_path - it 'supports fixture file upload' do - file = fixture_file_upload(fixture_name) - expect(file.read).to match(/describe FixtureFileUploadSupport/im) + it 'supports file_fixture_upload' do + file = file_fixture_upload(fixture_name) + expect(file.read).to match(/describe FileFixtureUploadSupport/im) end end end