Git Product home page Git Product logo

minitest-style-guide's Introduction

RuboCop Logo


Ruby Style Guide Gem Version Actions Status Test Coverage Maintainability Discord

Role models are important.
-- Officer Alex J. Murphy / RoboCop

RuboCop is a Ruby static code analyzer (a.k.a. linter) and code formatter. Out of the box it will enforce many of the guidelines outlined in the community Ruby Style Guide. Apart from reporting the problems discovered in your code, RuboCop can also automatically fix many of them for you.

RuboCop is extremely flexible and most aspects of its behavior can be tweaked via various configuration options.


Patreon OpenCollective OpenCollective Tidelift

Working on RuboCop is often fun, but it also requires a great deal of time and energy.

Please consider financially supporting its ongoing development.

Installation

RuboCop's installation is pretty standard:

$ gem install rubocop

If you'd rather install RuboCop using bundler, add a line for it in your Gemfile (but set the require option to false, as it is a standalone tool):

gem 'rubocop', require: false

RuboCop is stable between minor versions, both in terms of API and cop configuration. We aim to ease the maintenance of RuboCop extensions and the upgrades between RuboCop releases. All big changes are reserved for major releases. To prevent an unwanted RuboCop update you might want to use a conservative version lock in your Gemfile:

gem 'rubocop', '~> 1.65', require: false

See our versioning policy for further details.

Quickstart

Just type rubocop in a Ruby project's folder and watch the magic happen.

$ cd my/cool/ruby/project
$ rubocop

You can also use this magic in your favorite editor with RuboCop's built-in LSP server.

Documentation

You can read a lot more about RuboCop in its official docs.

Compatibility

RuboCop officially supports the following runtime Ruby implementations:

  • MRI 2.7+
  • JRuby 9.4+

Targets Ruby 2.0+ code analysis.

See the compatibility documentation for further details.

Readme Badge

If you use RuboCop in your project, you can include one of these badges in your readme to let people know that your code is written following the community Ruby Style Guide.

Ruby Style Guide

Ruby Style Guide

Here are the Markdown snippets for the two badges:

[![Ruby Style Guide](https://img.shields.io/badge/code_style-rubocop-brightgreen.svg)](https://github.com/rubocop/rubocop)

[![Ruby Style Guide](https://img.shields.io/badge/code_style-community-brightgreen.svg)](https://rubystyle.guide)

Team

Here's a list of RuboCop's core developers:

See the team page for more details.

Logo

RuboCop's logo was created by Dimiter Petrov. You can find the logo in various formats here.

The logo is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.

Contributors

Here's a list of all the people who have contributed to the development of RuboCop.

I'm extremely grateful to each and every one of them!

If you'd like to contribute to RuboCop, please take the time to go through our short contribution guidelines.

Converting more of the Ruby Style Guide into RuboCop cops is our top priority right now. Writing a new cop is a great way to dive into RuboCop!

Of course, bug reports and suggestions for improvements are always welcome. GitHub pull requests are even better! :-)

Funding

While RuboCop is free software and will always be, the project would benefit immensely from some funding. Raising a monthly budget of a couple of thousand dollars would make it possible to pay people to work on certain complex features, fund other development related stuff (e.g. hardware, conference trips) and so on. Raising a monthly budget of over $5000 would open the possibility of someone working full-time on the project which would speed up the pace of development significantly.

We welcome both individual and corporate sponsors! We also offer a wide array of funding channels to account for your preferences (although currently Open Collective is our preferred funding platform).

If you're working in a company that's making significant use of RuboCop we'd appreciate it if you suggest to your company to become a RuboCop sponsor.

You can support the development of RuboCop via GitHub Sponsors, Patreon, PayPal, Open Collective and Tidelift .

Note: If doing a sponsorship in the form of donation is problematic for your company from an accounting standpoint, we'd recommend the use of Tidelift, where you can get a support-like subscription instead.

Open Collective for Individuals

Support us with a monthly donation and help us continue our activities. [Become a backer]

Open Collective for Organizations

Become a sponsor and get your logo on our README on GitHub with a link to your site. [Become a sponsor]

Changelog

RuboCop's changelog is available here.

Copyright

Copyright (c) 2012-2024 Bozhidar Batsov. See LICENSE.txt for further details.

minitest-style-guide's People

Contributors

abhaynikam avatar andyw8 avatar bbatsov avatar dependabot[bot] avatar duduribeiro avatar fatkodima avatar ignacio-chiazzo avatar koic avatar tejasbubane avatar uaitt avatar ydah avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

minitest-style-guide's Issues

Guides for assertion matcher.

Following are the assertion for which guides are still remaining. I am planning to work on them today and Raise PRs for each. Please do let me know if there is any objection or concern regarding adding any of the matchers or the way I work. 😄

FYI, I haven't used few of the matchers at all. I would be trying them on sample application and then raise the PRs 🙏

  • assert_operator/ refute_operator
  • assert_output/ refute_output
  • assert_predicate/ refute_predicate
  • assert_raises/ refute_raises
  • assert_respond_to/ refute_respond_to
  • assert_same / refute_same
  • assert_send/ refute_send #15 (comment)
  • assert_silent / refute_silent
  • assert_throws / refute_throws

cc/ @koic @bbatsov

Mocha guidance

To start off the Mocha section (#37) I would propose these as recommended configuration settings:

 Mocha.configure do |c|
   c.stubbing_method_on_nil = :prevent
   c.stubbing_non_existent_method = :prevent
   c.stubbing_non_public_method = :prevent
 end

The first two can prevent subtle bugs which may cause false passes.

The third encourages better design by avoiding tests having knowledge of private implementation details.

https://mocha.jamesmead.org/Mocha/Configuration.html

Minitest/RefuteInDelta (& Minitest/AssertInDelta?) not safe for autocorrect

.rubocop.yml

---
require:
  - rubocop-minitest

AllCops:
  NewCops: enable

test/test_minitest_assert_in_delta.rb

# frozen_string_literal: true

require 'minitest/autorun'

# test
class AssertInDeltaTest < Minitest::Test
  def test_assert_in_delta_cop
    refute_equal(4.2, 420_001 / 100_000.0)
  end
end

$ ruby test/test_minitest_assert_in_delta.rb
=> pass

$ rubocop

Offenses:

test/test_minitest_assert_in_delta.rb:10:5: C: [Correctable] Minitest/RefuteInDelta: Prefer using refute_in_delta(4.2, 420_001 / 100_000.0).
    refute_equal(4.2, 420_001 / 100_000.0)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected, 1 offense autocorrectable

$ rubocop -a
=> autocorrected

$ ruby test/test_minitest_assert_in_delta.rb
=> fail

Fails because the default delta value for refute_in_delta is 0.001. As Rubocop can never know the user's intended delta value this cop is not safe for autocorrect.

Don't know whether you'd consider Minitest/RefuteInDelta unsafe also, as if you replace the assertion in the above example with assert_equal, autocorrect turns a failing test into one that passes..

I'd also suggest the suggestion text for both cops use the good code examples where an explicit delta value is provided.

Any conventions for naming individual tests?

Are there any good conventions for naming individual tests?

I'm used to rspec with describe "#method" and then a context "when blah blah" and then it "does the thing", which is a pretty easy kind of format to follow.

But I'm trying out minitest and I'm not sure how to do naming for def test_does_the_thing_when_blah

Feature Request : Enforce consistency

Is your feature request related to a problem? Please describe.

Something I've noticed in our repositories at work, is that we often end up with a mixture of the various checks inside a single test-suite :

  • _()
  • value()
  • expect()

Combined with the various checks like assert, must, refute, should.
I know several of them are being deprecated by minitest itself however.

Describe the solution you'd like

I'm wondering whether we can write a Cop, that takes a specific configuration to say that all checks need to be done with one of the three lines above, and should be followed by the required syntax, being must, should or other plugin.

Specifying files should end in `_test` or start with `test_`

We were looking for a rubocop that enforced filenaming for minitest files, something similar to this one available when using RSpec:
https://www.rubydoc.info/gems/rubocop-rspec/1.0/RuboCop/Cop/RSpec/FileName

There is no cop like this in the out-of-the-box minitest cop repo:
https://github.com/rubocop-hq/rubocop-minitest

The minitest cop repo specifies that the listed cops are based on the recommendations in this style guide. While it would be easy for us to write a custom cop in our project to enforce the desired naming, I wonder if a more general change might be wanted? Before submitting a PR: How do contributors feel about adding a file naming recommendation to the style guide and in turn creating a file name enforcement cop?

Recommend passing a failure_message when calling `assert` with a single argument

Developers should be encouraged to supply a failure message when making single assertions.

In these cases, the code might be readable:

assert page.has_content?("Widget added to cart")

…but the test failure message doesn't give any info other than making developers go back to the code to see the error:

Expected false to be truthy.

I believe improving the test output is a net positive, both in situations where developer have easy access to the code (e.g. while performing TDD) and in situations where the test output is the main artifact (e.g. in continuous integration environments). But I realise that there might be other opinions, for example that adding "Expected widget to be added to card" is too much overhead for developers.

I think the ideal case is that assert helpers are more intelligent and can generate more useful messages automatically. The only way I can see to achieve that is for people to write custom assert_* helpers, but eventually those will end up in an assert call which again should have a failure message.

So I think the style guide should recommend always passing a failure message, especially to bare assert.

If this is a good idea, then this could also be included as a Rubocop cop. I only see one other cop that focuses on assert calls, Minitest/AssertWithExpectedArgument, which is focused on calls with more than one argument, and so doesn't lead to any ambiguous situations with this proposal.

Thoughts?

Coverage of mocha

I understand that minitest has its own framework for mocking and stubbing, but it seems very common to use Mocha insead. Should the styleguide cover this?

Assert and refute with a negation

Imagine the following statement

assert(!expr)

I think this should be rewritten to refute(expr). For symmetry, refute(!expr) should be rewritten to assert(expr), although I don't expect that to happen very often. And of course, the optional message should be handled as well.

We see a few more specific examples of this in the style guide. The first example of it is assert(!actual.nil?). This could (I'm not saying should) be deduced by an elimination process, where you first apply the elimination of the negated assert (resulting in refute(actual.nil?)), then apply the rule to use assert_nil. This would also provide a solution for multiple negations (e.g. assert(!!!!!expr) can be reduced to refute(expr) by applying the elimination rule 5 times).

The rubocop-minitest gem is pretty inconsistent regarding this subject, some rules include a single negation, some don't. Providing a higher level construct to eliminate these negations would result in a global fix (thus providing consistency), while keeping the cops themselves as simple as possible.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.