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

Automatically add test/fixtures in engines to fixture_paths #48287

Merged
merged 1 commit into from May 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion activerecord/lib/active_record/fixtures.rb
Expand Up @@ -21,7 +21,8 @@ class FixtureClassNotFound < ActiveRecord::ActiveRecordError # :nodoc:
#
# They are stored in YAML files, one file per model, which are placed in the directories
# appointed by <tt>ActiveSupport::TestCase.fixture_paths=(path)</tt> (this is automatically
# configured for Rails, so you can just put your files in <tt><your-rails-app>/test/fixtures/</tt>).
# configured for Rails, so you can just put your files in <tt><your-rails-app>/test/fixtures/</tt>,
# or in the <tt>test/fixtures</tt> folder under any of your application's engines).
# The fixture file ends with the +.yml+ file extension, for example:
# <tt><your-rails-app>/test/fixtures/web_sites.yml</tt>).
#
Expand Down
5 changes: 5 additions & 0 deletions railties/CHANGELOG.md
@@ -1,3 +1,8 @@
* Add engine's `test/fixtures` path to `fixture_paths` in `on_load` hook if
path exists and is under the Rails application root.

*Chris Salzberg*

* `bin/rails app:template` now runs `bundle install` and any `after_bundle`
blocks after the template is executed.

Expand Down
9 changes: 9 additions & 0 deletions railties/lib/rails/engine.rb
Expand Up @@ -615,6 +615,15 @@ def load_seed
end
end

initializer :add_fixture_paths do
next if is_a?(Rails::Application)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably should just use this same initializer to register the application's fixtures.


fixtures = config.root.join("test", "fixtures")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should get from config.paths. It means we need to register this path, but we should not try to use the root because it disallow people from configuring this path.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked into paths but wasn't really sure if it fit the use case here.

if fixtures.exist? && fixtures.to_s.start_with?(Rails.root.to_s)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this check for the Rails.root.to_s? Is this to only get fixtures from engines embedded in the app?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this to only get fixtures from engines embedded in the app?

Yes, I found that if we don't do this, we include test fixtures from gem engines etc which I assumed we wouldn't want in typical usage. But I wasn't really sure, which is why I asked about this.

ActiveSupport.on_load(:active_record_fixtures) { self.fixture_paths |= ["#{fixtures}/"] }
end
end

initializer :prepend_helpers_path do |app|
if !isolated? || (app == self)
app.config.helpers_paths.unshift(*paths["app/helpers"].existent)
Expand Down
13 changes: 13 additions & 0 deletions railties/test/railties/engine_test.rb
Expand Up @@ -304,6 +304,19 @@ def index
assert_equal "Hi bukkits\n", response[2].body
end

test "adds its fixtures path to fixture_paths" do
@plugin.write "test/fixtures/bukkits.yml", ""

boot_rails

test_class = Class.new
test_class.singleton_class.attr_accessor :fixture_paths
test_class.fixture_paths = []
ActiveSupport.run_load_hooks(:active_record_fixtures, test_class)

assert_equal test_class.fixture_paths, ["#{Bukkits::Engine.root}/test/fixtures/"]
end

test "adds its mailer previews to mailer preview paths" do
@plugin.write "app/mailers/bukkit_mailer.rb", <<-RUBY
class BukkitMailer < ActionMailer::Base
Expand Down