Skip to content

Commit 3e07b34

Browse files
committed
Switch accessor for fixture path in file upload support for 6.1
1 parent 93a2a70 commit 3e07b34

File tree

3 files changed

+51
-15
lines changed

3 files changed

+51
-15
lines changed

Changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
### Rails 6.1 Development
2+
[Full Changelog](https://github.com/rspec/rspec-rails/compare/main...rails-6-1-dev)
3+
4+
* Support new #file_fixture_path and new fixture test support code. (Jon Rowe, #2398)
5+
16
### Development
27
[Full Changelog](https://github.com/rspec/rspec-rails/compare/v4.0.1...main)
38

lib/rspec/rails/fixture_file_upload_support.rb

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,42 @@ module FixtureFileUploadSupport
66

77
private
88

9-
def rails_fixture_file_wrapper
10-
RailsFixtureFileWrapper.fixture_path = nil
11-
resolved_fixture_path =
12-
if respond_to?(:fixture_path) && !fixture_path.nil?
13-
fixture_path.to_s
14-
else
15-
(RSpec.configuration.fixture_path || '').to_s
16-
end
17-
RailsFixtureFileWrapper.fixture_path = File.join(resolved_fixture_path, '') unless resolved_fixture_path.strip.empty?
18-
RailsFixtureFileWrapper.instance
9+
# In Rails 6.2 fixture file path needs to be relative to `file_fixture_path` instead, this change
10+
# was brought in with a deprecation warning on 6.1. In Rails 6.2 expect to rework this to remove
11+
# the old accessor.
12+
if ::Rails.version.to_f >= 6.1
13+
def rails_fixture_file_wrapper
14+
RailsFixtureFileWrapper.file_fixture_path = nil
15+
resolved_fixture_path =
16+
if respond_to?(:file_fixture_path) && !file_fixture_path.nil?
17+
file_fixture_path.to_s
18+
else
19+
(RSpec.configuration.fixture_path || '').to_s
20+
end
21+
RailsFixtureFileWrapper.file_fixture_path = File.join(resolved_fixture_path, '') unless resolved_fixture_path.strip.empty?
22+
RailsFixtureFileWrapper.instance
23+
end
24+
else
25+
def rails_fixture_file_wrapper
26+
RailsFixtureFileWrapper.fixture_path = nil
27+
resolved_fixture_path =
28+
if respond_to?(:fixture_path) && !fixture_path.nil?
29+
fixture_path.to_s
30+
else
31+
(RSpec.configuration.fixture_path || '').to_s
32+
end
33+
RailsFixtureFileWrapper.fixture_path = File.join(resolved_fixture_path, '') unless resolved_fixture_path.strip.empty?
34+
RailsFixtureFileWrapper.instance
35+
end
1936
end
2037

2138
class RailsFixtureFileWrapper
2239
include ActionDispatch::TestProcess if defined?(ActionDispatch::TestProcess)
2340

41+
if ::Rails.version.to_f >= 6.1
42+
include ActiveSupport::Testing::FileFixtures
43+
end
44+
2445
class << self
2546
attr_accessor :fixture_path
2647

spec/rspec/rails/fixture_file_upload_support_spec.rb

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,43 @@ module RSpec::Rails
33
context 'with fixture path set in config' do
44
it 'resolves fixture file' do
55
RSpec.configuration.fixture_path = File.dirname(__FILE__)
6-
expect(fixture_file_upload_resolved('fixture_file_upload_support_spec.rb').run).to be true
6+
expect_to_pass fixture_file_upload_resolved('fixture_file_upload_support_spec.rb')
77
end
88

99
it 'resolves supports `Pathname` objects' do
1010
RSpec.configuration.fixture_path = Pathname(File.dirname(__FILE__))
11-
expect(fixture_file_upload_resolved('fixture_file_upload_support_spec.rb').run).to be true
11+
expect_to_pass fixture_file_upload_resolved('fixture_file_upload_support_spec.rb')
1212
end
1313
end
1414

1515
context 'with fixture path set in spec' do
1616
it 'resolves fixture file' do
17-
expect(fixture_file_upload_resolved('fixture_file_upload_support_spec.rb', File.dirname(__FILE__)).run).to be true
17+
expect_to_pass fixture_file_upload_resolved('fixture_file_upload_support_spec.rb', File.dirname(__FILE__))
1818
end
1919
end
2020

2121
context 'with fixture path not set' do
2222
it 'resolves fixture using relative path' do
2323
RSpec.configuration.fixture_path = nil
24-
expect(fixture_file_upload_resolved('spec/rspec/rails/fixture_file_upload_support_spec.rb').run).to be true
24+
expect_to_pass fixture_file_upload_resolved('spec/rspec/rails/fixture_file_upload_support_spec.rb')
2525
end
2626
end
2727

28+
def expect_to_pass(group)
29+
result = group.run(failure_reporter)
30+
failure_reporter.exceptions.map { |e| raise e }
31+
expect(result).to be true
32+
end
33+
2834
def fixture_file_upload_resolved(fixture_name, fixture_path = nil)
2935
RSpec::Core::ExampleGroup.describe do
3036
include RSpec::Rails::FixtureFileUploadSupport
3137

32-
self.fixture_path = fixture_path
38+
if ::Rails.version.to_f >= 6.1
39+
self.file_fixture_path = fixture_path
40+
else
41+
self.fixture_path = fixture_path
42+
end
3343

3444
it 'supports fixture file upload' do
3545
file = fixture_file_upload(fixture_name)

0 commit comments

Comments
 (0)