From e050e082f7bcfa6cbd6fc5da88eb57bd5ee5e40a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20Barri=C3=A9?= Date: Tue, 19 Mar 2024 11:08:18 +0100 Subject: [PATCH 1/3] Make the dummy app bootable in test --- .gitignore | 4 +-- Gemfile | 1 + Gemfile.lock | 41 ++++++++++++++++++++++++ test/dummy/config/application.rb | 3 +- test/dummy/config/environments/test.rb | 5 --- test/dummy/config/initializers/assets.rb | 13 -------- 6 files changed, 44 insertions(+), 23 deletions(-) delete mode 100644 test/dummy/config/initializers/assets.rb diff --git a/.gitignore b/.gitignore index 5907f17..075c8d2 100644 --- a/.gitignore +++ b/.gitignore @@ -2,10 +2,8 @@ *.rbc capybara-*.html .rspec -/log /tmp -/db/*.sqlite3 -/db/*.sqlite3-journal +test/dummy/log /public/system /coverage/ /spec/tmp diff --git a/Gemfile b/Gemfile index 6cc6c14..39b75de 100644 --- a/Gemfile +++ b/Gemfile @@ -5,6 +5,7 @@ source "https://rubygems.org" gemspec gem "actionview" +gem "railties" gem "minitest" gem "mocha" gem "rake" diff --git a/Gemfile.lock b/Gemfile.lock index f13cf4f..57a56ac 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -12,6 +12,16 @@ PATH GEM remote: https://rubygems.org/ specs: + actionpack (7.1.3.2) + actionview (= 7.1.3.2) + activesupport (= 7.1.3.2) + nokogiri (>= 1.8.5) + racc + rack (>= 2.2.4) + rack-session (>= 1.0.1) + rack-test (>= 0.6.3) + rails-dom-testing (~> 2.2) + rails-html-sanitizer (~> 1.6) actionview (7.1.3.2) activesupport (= 7.1.3.2) builder (~> 3.1) @@ -41,6 +51,10 @@ GEM erubi (1.12.0) i18n (1.14.4) concurrent-ruby (~> 1.0) + io-console (0.7.2) + irb (1.12.0) + rdoc + reline (>= 0.4.2) json (2.7.1) language_server-protocol (3.17.0.3) loofah (2.22.0) @@ -65,7 +79,17 @@ GEM pry-byebug (3.10.1) byebug (~> 11.0) pry (>= 0.13, < 0.15) + psych (5.1.2) + stringio racc (1.7.3) + rack (3.0.9.1) + rack-session (2.0.0) + rack (>= 3.0.0) + rack-test (2.1.0) + rack (>= 1.3) + rackup (2.1.0) + rack (>= 3) + webrick (~> 1.8) rails-dom-testing (2.2.0) activesupport (>= 5.0.0) minitest @@ -73,11 +97,23 @@ GEM rails-html-sanitizer (1.6.0) loofah (~> 2.21) nokogiri (~> 1.14) + railties (7.1.3.2) + actionpack (= 7.1.3.2) + activesupport (= 7.1.3.2) + irb + rackup (>= 1.0.0) + rake (>= 12.2) + thor (~> 1.0, >= 1.2.2) + zeitwerk (~> 2.6) rainbow (3.1.1) rake (13.1.0) rake-compiler (1.2.7) rake + rdoc (6.6.2) + psych (>= 4.0.0) regexp_parser (2.9.0) + reline (0.4.3) + io-console (~> 0.5) rexml (3.2.6) rubocop (1.62.1) json (~> 2.3) @@ -99,9 +135,13 @@ GEM ruby_memcheck (2.3.0) nokogiri smart_properties (1.17.0) + stringio (3.1.0) + thor (1.3.1) tzinfo (2.0.6) concurrent-ruby (~> 1.0) unicode-display_width (2.5.0) + webrick (1.8.1) + zeitwerk (2.6.13) PLATFORMS ruby @@ -112,6 +152,7 @@ DEPENDENCIES minitest mocha pry-byebug + railties rake rake-compiler rubocop-shopify diff --git a/test/dummy/config/application.rb b/test/dummy/config/application.rb index 8fc0f90..020fdef 100644 --- a/test/dummy/config/application.rb +++ b/test/dummy/config/application.rb @@ -21,7 +21,6 @@ class Application < Rails::Application # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s] # config.i18n.default_locale = :de - # Do not swallow errors in after_commit/after_rollback callbacks. - config.active_record.raise_in_transactional_callbacks = true + config.action_view.annotate_rendered_view_with_filenames = true end end diff --git a/test/dummy/config/environments/test.rb b/test/dummy/config/environments/test.rb index 18c3311..ce066d7 100644 --- a/test/dummy/config/environments/test.rb +++ b/test/dummy/config/environments/test.rb @@ -28,11 +28,6 @@ # Disable request forgery protection in test environment. config.action_controller.allow_forgery_protection = false - # Tell Action Mailer not to deliver emails to the real world. - # The :test delivery method accumulates sent emails in the - # ActionMailer::Base.deliveries array. - config.action_mailer.delivery_method = :test - # Randomize the order test cases are executed. config.active_support.test_order = :random diff --git a/test/dummy/config/initializers/assets.rb b/test/dummy/config/initializers/assets.rb deleted file mode 100644 index 1be14d3..0000000 --- a/test/dummy/config/initializers/assets.rb +++ /dev/null @@ -1,13 +0,0 @@ -# frozen_string_literal: true - -# Be sure to restart your server when you modify this file. - -# Version of your assets, change this if you want to expire all your assets. -Rails.application.config.assets.version = "1.0" - -# Add additional assets to the asset load path -# Rails.application.config.assets.paths << Emoji.images_path - -# Precompile additional assets. -# application.js, application.css, and all non-JS/CSS in app/assets folder are already added. -# Rails.application.config.assets.precompile += %w( search.js ) From ea19da11eed363c55a895c7217f9fe99137f6e23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20Barri=C3=A9?= Date: Tue, 19 Mar 2024 11:12:50 +0100 Subject: [PATCH 2/3] Make sure annotate_rendered_view_with_filenames is configured Action View is setup to copy the configuration from the application after initialization is finished, using `ActiveSupport.on_load(:action_view)` to make sure it only happens when `ActionView::Base` is loaded. We need to add our on_load hook after that, so we're also putting it in an after_initialize. Typically apps load all Rails railties before requiring gems with Bundler, so our after_initialize should always run after the one from Action View. --- lib/better_html/railtie.rb | 2 ++ test/better_html/railtie_test.rb | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 test/better_html/railtie_test.rb diff --git a/lib/better_html/railtie.rb b/lib/better_html/railtie.rb index 8e25172..328a07a 100644 --- a/lib/better_html/railtie.rb +++ b/lib/better_html/railtie.rb @@ -6,7 +6,9 @@ module BetterHtml class Railtie < Rails::Railtie initializer "better_html.better_erb.initialization" do BetterHtml::BetterErb.prepend! + end + config.after_initialize do ActiveSupport.on_load(:action_view) do BetterHtml.config.annotate_rendered_view_with_filenames = ActionView::Base.annotate_rendered_view_with_filenames end diff --git a/test/better_html/railtie_test.rb b/test/better_html/railtie_test.rb new file mode 100644 index 0000000..592abb0 --- /dev/null +++ b/test/better_html/railtie_test.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +require "test_helper" +# setup dummy app +ENV["RAILS_ENV"] ||= "test" +require_relative "../dummy/config/environment" +# load railtie +require "better_html/railtie" + +module BetterHtml + class RailtieTest < ActiveSupport::TestCase + test "configuration is copied from ActionView" do + _ = ActionView::Base + assert BetterHtml.config.annotate_rendered_view_with_filenames + end + end +end From bcc021f91ecaa2d393fba6e64332e814472bca0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20Barri=C3=A9?= Date: Tue, 19 Mar 2024 11:24:42 +0100 Subject: [PATCH 3/3] Support actionview 6.0 --- gemfiles/Gemfile-rails-6-0 | 2 +- gemfiles/Gemfile-rails-6-1 | 2 +- lib/better_html/railtie.rb | 2 ++ test/better_html/railtie_test.rb | 8 +++++--- test/dummy/config/application.rb | 4 +++- 5 files changed, 12 insertions(+), 6 deletions(-) diff --git a/gemfiles/Gemfile-rails-6-0 b/gemfiles/Gemfile-rails-6-0 index 7263fd8..49909b4 100644 --- a/gemfiles/Gemfile-rails-6-0 +++ b/gemfiles/Gemfile-rails-6-0 @@ -5,7 +5,7 @@ source "https://rubygems.org" gemspec path: ".." gem "actionview", "~> 6.0.0" -gem "rake" +gem "railties" gem "rake-compiler" gem "minitest" gem "mocha" diff --git a/gemfiles/Gemfile-rails-6-1 b/gemfiles/Gemfile-rails-6-1 index 6746897..179bd88 100644 --- a/gemfiles/Gemfile-rails-6-1 +++ b/gemfiles/Gemfile-rails-6-1 @@ -5,7 +5,7 @@ source "https://rubygems.org" gemspec path: ".." gem "actionview", "~> 6.1.0" -gem "rake" +gem "railties" gem "rake-compiler" gem "minitest" gem "mocha" diff --git a/lib/better_html/railtie.rb b/lib/better_html/railtie.rb index 328a07a..1fd3fbb 100644 --- a/lib/better_html/railtie.rb +++ b/lib/better_html/railtie.rb @@ -10,6 +10,8 @@ class Railtie < Rails::Railtie config.after_initialize do ActiveSupport.on_load(:action_view) do + next unless ActionView::Base.respond_to?(:annotate_rendered_view_with_filenames) + BetterHtml.config.annotate_rendered_view_with_filenames = ActionView::Base.annotate_rendered_view_with_filenames end end diff --git a/test/better_html/railtie_test.rb b/test/better_html/railtie_test.rb index 592abb0..a18fcc6 100644 --- a/test/better_html/railtie_test.rb +++ b/test/better_html/railtie_test.rb @@ -9,9 +9,11 @@ module BetterHtml class RailtieTest < ActiveSupport::TestCase - test "configuration is copied from ActionView" do - _ = ActionView::Base - assert BetterHtml.config.annotate_rendered_view_with_filenames + if Rails::VERSION::STRING >= "6.1" + test "configuration is copied from ActionView" do + _ = ActionView::Base + assert BetterHtml.config.annotate_rendered_view_with_filenames + end end end end diff --git a/test/dummy/config/application.rb b/test/dummy/config/application.rb index 020fdef..03f7388 100644 --- a/test/dummy/config/application.rb +++ b/test/dummy/config/application.rb @@ -21,6 +21,8 @@ class Application < Rails::Application # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s] # config.i18n.default_locale = :de - config.action_view.annotate_rendered_view_with_filenames = true + if Rails::VERSION::STRING >= "6.1" + config.action_view.annotate_rendered_view_with_filenames = true + end end end