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

Make the RuboCop::Minitest::Test suite re-usable by gem consumers #278

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

Conversation

DougEdey
Copy link

@DougEdey DougEdey commented Dec 5, 2023

I've been writing custom cops for various jobs for a while now, but I've never had a consistent way to write tests for products that use minitest as the test suite (officially Rubocop only provides rspec helpers) , I started to look around to see what this Gem did and figured it would make sense to make rubocop-minitest provide a minitest class for others that write custom cops for their own code bases.

This does a couple of things

  1. Makes a new Rubocop::Minitest::Test class that includes the code from assert_offense (I don't feel like it needs to be a module), and it also uses declarative testing for people to use.
  2. I moved the "real" test suites in this gem to use the new base class, and kept the code blocks being tested as Minitest::Test
  3. I added declarative testing since all the test suites I've been in have it, where instead of using def test_something_does_something...end you can do test "something does something" do...end which makes it easier to read test names.

I tested this locally in an app of mine and it works great! So it can allow a consistent way for people to write custom cops and easily test them!


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • [-] Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

@exterm
Copy link

exterm commented Dec 5, 2023

This makes a lot of sense to me and I'd love to use it.

@Earlopain
Copy link
Contributor

If this is being exposed like this then I think it needs some documetation on how to pass global and cop specific config to a test.

See

def configure_enforced_style(style)
all_config = RuboCop::Minitest::CONFIG
cop_config = all_config['Minitest/GlobalExpectations']
cop_config = cop_config.merge('EnforcedStyle' => style)
all_config = all_config.merge('Minitest/GlobalExpectations' => cop_config)
config = RuboCop::Config.new(all_config)
@cop = RuboCop::Cop::Minitest::GlobalExpectations.new(config)
@preferred_method = style == :any ? :_ : style
end
end
and
def configure_max_assertions(max)
cop_config = RuboCop::Config.new('Minitest/MultipleAssertions' => { 'Max' => max })
@cop = RuboCop::Cop::Minitest::MultipleAssertions.new(cop_config)
end
end

@DougEdey
Copy link
Author

@Earlopain Updated the casing of RuboCop, updated the sample docs (both inline and README.md), handling the nil case for @cop

@DougEdey DougEdey changed the title Make the Rubocop::Minitest::Test suite re-usable by gem consumers Make the RuboCop::Minitest::Test suite re-usable by gem consumers Dec 13, 2023
@DougEdey
Copy link
Author

Squashed commits and confirmed CI is passing locally

@DougEdey
Copy link
Author

@koic 👋 Could I ask for a review?

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.

3 participants