Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to Rails 7.1's file_fixture_upload naming approach #2718

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion example_app_generator/generate_stuff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
Original file line number Diff line number Diff line change
@@ -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

This file was deleted.

2 changes: 1 addition & 1 deletion lib/rspec/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
4 changes: 2 additions & 2 deletions lib/rspec/rails/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down
92 changes: 92 additions & 0 deletions spec/rspec/rails/file_fixture_upload_support_spec.rb
Original file line number Diff line number Diff line change
@@ -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
74 changes: 0 additions & 74 deletions spec/rspec/rails/fixture_file_upload_support_spec.rb

This file was deleted.