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

Introduce config.dom_testing_default_html_version and use Rails::Dom::Testing to parse HTML in test helpers #48682

Conversation

flavorjones
Copy link
Member

@flavorjones flavorjones commented Jul 6, 2023

Motivation / Background

Some early users of Rails edge reported test breakage due to the change in #48523 that updated test helpers to use Nokogiri's HTML5 parser.

After discussing with Rafael, he suggested that we introduce a Rails config parameter so users may opt into this change at their convenience, essentially wrapping a config param around the changes in #48523.

This PR takes that idea a step further and groups all the test helpers from ActionView::TestCase, ActionDispatch::Assertions, and everything in Rails::Dom::Testing under a single config param:

config.dom_testing_default_html_version

As a result, by setting this config to either :html4 or :html5, a developer can control the HTML parser used by all the test helpers in Action View, Action Dispatch, and Rails::Dom::Testing; and some assertions support overriding that default for individual tests.

Detail

  • new config parameter config.dom_testing_default_html_version, which is injected into Dom::Rails::Testing via the railstie in Allow user to choose the HTML parser used rails-dom-testing#109
    • the value of that param is :html5 in Rails 7.1, and :html4 in earlier config versions
    • though, on JRuby, where Nokogiri::HTML5 is not supported, this will always be :html4
  • Action View and Dispatch test helpers use Rails::Dom::Testing.html_document or .html_document_fragment to ensure the desired HTML parser is used
    • Update dependency on rails-dom-testing to ~> 2.2

Additional information

Note that integration testing across gems like this is challenging, but I have actually fired up a Rails app using this PR and rails/rails-dom-testing#109 together, and it worked as expected.

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Changes that are unrelated should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

@flavorjones
Copy link
Member Author

I can't reproduce the failures in buildkite (cd railties; bin/test test/generators/app_generator_test.rb), not sure what's going on there.

@guilleiguaran
Copy link
Member

I've seen similar errors in the past when trying to point a gem to a git repo in the Gemfile, for some reason the gem can't be found when the app generators tests execute bundler

@flavorjones flavorjones force-pushed the flavorjones-use-rails-dom-testing-helpers-for-html-parsing branch from 119cdd7 to 22c8b34 Compare July 15, 2023 15:51
@flavorjones flavorjones force-pushed the flavorjones-use-rails-dom-testing-helpers-for-html-parsing branch 4 times, most recently from 70387c5 to a5462cd Compare August 3, 2023 14:01
which controls the HTML parser used by rails-dom-testing assertions.

The config parameter is set to :html5 in Rails 7.1 if the Nokogiri
HTML5 parser is supported.
Use the helpers introduced in rails-dom-testing 2.2.0 instead of
managing the HTML parsers as was done in rails#48523.

See also related rails#47144 / ad79ed0
@flavorjones flavorjones force-pushed the flavorjones-use-rails-dom-testing-helpers-for-html-parsing branch from a5462cd to 8a57ba8 Compare August 3, 2023 15:19
@flavorjones
Copy link
Member Author

@rafael I've updated this to use rails-dom-testing ~> 2.2, I think it's ready for full review.

@rafaelfranca rafaelfranca merged commit 94070e0 into rails:main Aug 3, 2023
9 checks passed
@flavorjones flavorjones deleted the flavorjones-use-rails-dom-testing-helpers-for-html-parsing branch August 3, 2023 21:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants