Skip to content

Commit

Permalink
Add fixture_paths configuration setting
Browse files Browse the repository at this point in the history
  • Loading branch information
jguecaimburu committed Apr 21, 2023
1 parent 1873102 commit f1afa0c
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 17 deletions.
6 changes: 6 additions & 0 deletions lib/generators/rspec/install/templates/spec/rails_helper.rb
Expand Up @@ -34,7 +34,13 @@
RSpec.configure do |config|
<% if RSpec::Rails::FeatureCheck.has_active_record? -%>
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
<% if ::Rails::VERSION::STRING < "7.1.0" -%>
config.fixture_path = Rails.root.join('spec/fixtures')
<% else -%>
config.fixture_paths = [
Rails.root.join('spec/fixtures')
]
<% end -%>
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
Expand Down
22 changes: 20 additions & 2 deletions lib/rspec/rails/configuration.rb
Expand Up @@ -57,7 +57,7 @@ def self.add_test_type_configurations(config)
end

# @private
def self.initialize_configuration(config) # rubocop:disable Metrics/MethodLength,Metrics/CyclomaticComplexity
def self.initialize_configuration(config) # rubocop:disable Metrics/MethodLength,Metrics/CyclomaticComplexity,Metrics/AbcSize,Metrics/PerceivedComplexity
config.backtrace_exclusion_patterns << /vendor\//
config.backtrace_exclusion_patterns << %r{lib/rspec/rails}

Expand All @@ -69,7 +69,13 @@ def self.initialize_configuration(config) # rubocop:disable Metrics/MethodLength
config.add_setting :use_transactional_fixtures, alias_with: :use_transactional_examples
config.add_setting :use_instantiated_fixtures
config.add_setting :global_fixtures
config.add_setting :fixture_path

if ::Rails::VERSION::STRING < "7.1.0"
config.add_setting :fixture_path
else
config.add_setting :fixture_paths
end

config.include RSpec::Rails::FixtureSupport, :use_fixtures

# We'll need to create a deprecated module in order to properly report to
Expand Down Expand Up @@ -157,6 +163,18 @@ def filter_rails_from_backtrace!
filter_gems_from_backtrace "activemodel", "activerecord",
"activesupport", "activejob"
end

# @deprecated TestFixtures#fixture_path is deprecated and will be removed in Rails 7.2
if ::Rails::VERSION::STRING >= "7.1.0"
def config.fixture_path=(path)
RSpec.deprecate(
"config.fixture_path = #{path.inspect}",
replacement: "config.fixture_paths = [#{path.inspect}]",
message: "Rails 7.1 has deprecated the singular fixture_path in favour of an array"
)
self.fixture_paths = Array(path)
end
end
end

add_test_type_configurations(config)
Expand Down
3 changes: 2 additions & 1 deletion lib/rspec/rails/fixture_support.rb
Expand Up @@ -23,10 +23,11 @@ def run_in_transaction?

# TestFixtures#fixture_path is deprecated and will be removed in Rails 7.2
if respond_to?(:fixture_paths=)
fixture_paths << RSpec.configuration.fixture_path
self.fixture_paths = RSpec.configuration.fixture_paths
else
self.fixture_path = RSpec.configuration.fixture_path
end

self.use_transactional_tests = RSpec.configuration.use_transactional_fixtures
self.use_instantiated_fixtures = RSpec.configuration.use_instantiated_fixtures

Expand Down
11 changes: 10 additions & 1 deletion spec/generators/rspec/install/install_generator_spec.rb
Expand Up @@ -15,6 +15,10 @@ def have_a_fixture_path
match(/^ config\.fixture_path = /m)
end

def have_fixture_paths
match(/^ config\.fixture_paths = /m)
end

def maintain_test_schema
match(/ActiveRecord::Migration\.maintain_test_schema!/m)
end
Expand Down Expand Up @@ -84,7 +88,11 @@ def filter_rails_from_backtrace

specify "with default fixture path" do
run_generator
expect(rails_helper).to have_a_fixture_path
if ::Rails::VERSION::STRING < "7.1.0"
expect(rails_helper).to have_a_fixture_path
else
expect(rails_helper).to have_fixture_paths
end
end

specify "with transactional fixtures" do
Expand Down Expand Up @@ -127,6 +135,7 @@ def filter_rails_from_backtrace
specify "without fixture path" do
run_generator
expect(rails_helper).not_to have_a_fixture_path
expect(rails_helper).not_to have_fixture_paths
end

specify "without transactional fixtures" do
Expand Down
51 changes: 41 additions & 10 deletions spec/rspec/rails/configuration_spec.rb
Expand Up @@ -78,7 +78,11 @@

include_examples "adds setting", :global_fixtures

include_examples "adds setting", :fixture_path
if ::Rails::VERSION::STRING < "7.1.0"
include_examples "adds setting", :fixture_path
else
include_examples "adds setting", :fixture_paths
end

include_examples "adds setting", :rendering_views

Expand Down Expand Up @@ -157,22 +161,49 @@ def in_inferring_type_from_location_environment
include_examples "infers type from location", :feature, "spec/features"
end

it "fixture support is included with metadata `:use_fixtures`" do
in_sub_process do
RSpec.configuration.global_fixtures = [:foo]
RSpec.configuration.fixture_path = "custom/path"
if ::Rails::VERSION::STRING < "7.1.0"
it "fixture support is included with metadata `:use_fixtures`" do
in_sub_process do
RSpec.configuration.global_fixtures = [:foo]
RSpec.configuration.fixture_path = "custom/path"

group = RSpec.describe("Arbitrary Description", :use_fixtures)
group = RSpec.describe("Arbitrary Description", :use_fixtures)

if ::Rails::VERSION::STRING < '7.1.0'
expect(group).to respond_to(:fixture_path)
expect(group.fixture_path).to eq("custom/path")
else

expect(group.new.respond_to?(:foo, true)).to be(true)
end
end
else
it "fixture support is included with metadata `:use_fixtures` and fixture_paths configured" do
in_sub_process do
RSpec.configuration.global_fixtures = [:foo]
RSpec.configuration.fixture_paths = ["custom/path", "other/custom/path"]

group = RSpec.describe("Arbitrary Description", :use_fixtures)

expect(group).to respond_to(:fixture_paths)
expect(group.fixture_paths).to include("custom/path")
expect(group.fixture_paths).to eq(["custom/path", "other/custom/path"])

expect(group.new.respond_to?(:foo, true)).to be(true)
end
end

it "fixture support is included with metadata `:use_fixtures` and deprecated fixture_path configured" do
in_sub_process do
expect(RSpec).to receive(:deprecate)

RSpec.configuration.global_fixtures = [:foo]
RSpec.configuration.fixture_path = "custom/path"

expect(group.new.respond_to?(:foo, true)).to be(true)
group = RSpec.describe("Arbitrary Description", :use_fixtures)

expect(group).to respond_to(:fixture_paths)
expect(group.fixture_paths).to eq(["custom/path"])

expect(group.new.respond_to?(:foo, true)).to be(true)
end
end
end

Expand Down
11 changes: 8 additions & 3 deletions spec/rspec/rails/fixture_support_spec.rb
@@ -1,14 +1,19 @@
module RSpec::Rails
RSpec.describe FixtureSupport do
context "with use_transactional_fixtures set to false" do
it "still supports fixture_path" do
it "still supports fixture_path and fixture_paths" do
allow(RSpec.configuration).to receive(:use_transactional_fixtures) { false }
group = RSpec::Core::ExampleGroup.describe do
include FixtureSupport
end

expect(group).to respond_to(:fixture_path)
expect(group).to respond_to(:fixture_path=)
if ::Rails::VERSION::STRING < "7.1.0"
expect(group).to respond_to(:fixture_path)
expect(group).to respond_to(:fixture_path=)
else
expect(group).to respond_to(:fixture_paths)
expect(group).to respond_to(:fixture_paths=)
end
end
end

Expand Down

0 comments on commit f1afa0c

Please sign in to comment.