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_file_fixture_upload_spec.rb b/example_app_generator/no_active_record/spec/verify_file_fixture_upload_spec.rb new file mode 100644 index 0000000000..01dd8ab5c7 --- /dev/null +++ b/example_app_generator/no_active_record/spec/verify_file_fixture_upload_spec.rb @@ -0,0 +1,20 @@ +require 'rails_helper' + +RSpec.describe 'Example App', :use_fixtures, type: :model do + if ::Rails::VERSION::STRING < "7.1.0" + it 'supports fixture_file_upload' do + file = fixture_file_upload(__FILE__) + expect(file.read).to match(/RSpec\.describe 'Example App'/im) + end + else + it 'supports file_fixture_upload' do + file = file_fixture_upload(__FILE__) + expect(file.read).to match(/RSpec\.describe 'Example App'/im) + end + + it 'supports fixture_file_upload' do + file = fixture_file_upload(__FILE__) + expect(file.read).to match(/RSpec\.describe 'Example App'/im) + end + end +end 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_fixture_file_upload_spec.rb deleted file mode 100644 index 2a96e28374..0000000000 --- a/example_app_generator/no_active_record/spec/verify_fixture_file_upload_spec.rb +++ /dev/null @@ -1,8 +0,0 @@ -require 'rails_helper' - -RSpec.describe 'Example App', :use_fixtures, type: :model do - it 'supports fixture file upload' do - file = fixture_file_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 70% rename from lib/rspec/rails/fixture_file_upload_support.rb rename to lib/rspec/rails/file_fixture_upload_support.rb index c7c4262ffd..803888935c 100644 --- a/lib/rspec/rails/fixture_file_upload_support.rb +++ b/lib/rspec/rails/file_fixture_upload_support.rb @@ -1,16 +1,20 @@ module RSpec module Rails # @private - module FixtureFileUploadSupport - delegate :fixture_file_upload, to: :rails_fixture_file_wrapper + module FileFixtureUploadSupport + if ::Rails::VERSION::STRING < "7.1.0" + delegate :fixture_file_upload, to: :rails_file_fixture_wrapper + else + delegate :file_fixture_upload, :fixture_file_upload, to: :rails_file_fixture_wrapper + end 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 +23,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/file_fixture_upload_support_spec.rb b/spec/rspec/rails/file_fixture_upload_support_spec.rb new file mode 100644 index 0000000000..662531e675 --- /dev/null +++ b/spec/rspec/rails/file_fixture_upload_support_spec.rb @@ -0,0 +1,92 @@ +module RSpec::Rails + 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 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 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 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 file_fixture_upload_resolved('spec/rspec/rails/file_fixture_upload_support_spec.rb') + end + end + + def file_fixture_upload_resolved(fixture_name, file_fixture_path = nil) + RSpec::Core::ExampleGroup.describe do + 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 FileFixtureUploadSupport/im) + end + 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 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 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 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 file_fixture_upload_resolved('spec/rspec/rails/file_fixture_upload_support_spec.rb') + end + end + + def file_fixture_upload_resolved(fixture_name, file_fixture_path = nil) + RSpec::Core::ExampleGroup.describe do + include RSpec::Rails::FileFixtureUploadSupport + + self.file_fixture_path = file_fixture_path + + it 'supports file_fixture_upload' do + file = file_fixture_upload(fixture_name) + expect(file.read).to match(/describe FileFixtureUploadSupport/im) + end + + it 'supports fixture_file_upload' do + file = fixture_file_upload(fixture_name) + expect(file.read).to match(/describe FileFixtureUploadSupport/im) + end + end + 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 + end +end diff --git a/spec/rspec/rails/fixture_file_upload_support_spec.rb b/spec/rspec/rails/fixture_file_upload_support_spec.rb deleted file mode 100644 index 2a139fe0ce..0000000000 --- a/spec/rspec/rails/fixture_file_upload_support_spec.rb +++ /dev/null @@ -1,74 +0,0 @@ -module RSpec::Rails - RSpec.describe FixtureFileUploadSupport 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') - 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') - 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__)) - 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') - 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') - 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') - 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__)) - 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') - end - 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, file_fixture_path = nil) - RSpec::Core::ExampleGroup.describe do - include RSpec::Rails::FixtureFileUploadSupport - - 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) - end - end - end - end -end