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

Feature: Add Rails generator and rake task support for creating data migration test coverage #355

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

joshmfrankel
Copy link

Why

Implements: #148

I recently needed well-factored test coverage for a complex data migration, so I thought I'd take an attempt at implementing test suite support for both RSpec and Minitest.

What

  • Adds new Rake task rake data:tests:setup for optional help setting up new data migration tests
  • Adds new Config setting config.test_support_enabled which conditionally creates associated tests files upon rails g data_migration add_this_to_that. (Default: false)
  • Adds basic support for inferring test suite based on available helper file

I read the comment here: #162 (comment) regarding using Rails.configuration.generators.options[:rails]. That returns a very clear: Rails.configuration.generators.options[:rails][:test_framework] #=> :rspec. While testing Minitest and RSpec, I noticed that this comes from having the rspec-rails gem installed. It doesn't necessarily check for valid installation. I attempted a valid installation check by looking for the framework specific files test/test_helper.rb and spec/rails_helper.rb.

Resources

Also, kudos on a great gem! 💎

…ge on data migrations

* New Rake task `rake data:tests:setup`
* New Config setting `config.test_support = true/false` (false by default)
module Helpers
class InferTestSuiteType
def call
if File.exist?(Rails.root.join('spec', 'spec_helper.rb'))
Copy link
Author

Choose a reason for hiding this comment

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

We could perform add to the conditionals here for Rails.configuration.generators.options[:rails][:test_framework] == :rspec and Rails.configuration.generators.options[:rails][:test_framework] == :test_unit respectively to look for both installation and configuration.

module DataMigrate
module Tasks
class SetupTests
INJECTION_MATCHER = Regexp.new(/require_relative ["|']\.\.\/config\/environment["|']/)
Copy link
Author

Choose a reason for hiding this comment

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

Had mixed feeling on this. Basically, I want to find a target phrase in either the rails_helper.rb or test_helper.rb. It seemed best to inject the require loop https://github.com/ilyakatz/data-migrate/pull/355/files#diff-ffd91a3de47aa2b12e05f99c726f74f27c7181b3f2f987c24aa9af32f72ea2acR57 after the environment was loaded. Open to other suggestions.

The setup task is also optional. You could just individually require files per data migration test file.

def test_helper_file_path
case DataMigrate::Helpers::InferTestSuiteType.new.call
when :rspec
Rails.root.join('spec', 'rails_helper.rb')
Copy link
Author

Choose a reason for hiding this comment

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

rails_helper.rb contains the injection target BUT DataMigrate::Helpers::InferTestSuiteType checks spec_helper.rb (see: https://github.com/ilyakatz/data-migrate/pull/355/files#diff-e3eb6de888eae80ac87faa33be0419ff8abdc2bd7d14e23c243304834d53604eR5). I'm thinking these maybe should match in their conditional checks. Thoughts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant