6

Question

Is it possible to tell Rubocop to return 0 as exit code when only warnings are found?


Prerequisites

  1. I am using the following command to run Rubocop on Travis CI:

    bundle exec rubocop
    
  2. I have a cop which is configured as a warning in rubocop.yml:

    Style/Documentation:
      Severity: warning
    

Problem

Travis CI treats a command as a successful one only if its exit code is equal to 0.

Rubocop returns 0 as exit code when it finds no offences,

but when it finds at least one offence,

it returns 1 as exit code, no matter whether this offence is an error or a warning.

As a result, Travis CI builds are failed when only warnings are found by Rubocop.

Therefore, is it possible to tell Rubocop to return 0 as exit code when only warnings are found?

Thanks in advance.


Notes

  • Please, do NOT suggest to disable cops.

  • I am using the following approach to check exit codes:

    $ bundle exec rubocop
    // ...
    14 files inspected, 8 offenses detected
    
    $ echo "$?"
    1
    
Marian13
  • 7,740
  • 2
  • 47
  • 51

1 Answers1

11

Use the --fail-level flag with the appropriate severity level:

rubocop --fail-level error

You can read more about this flag in the Rubocop Docs for command line flags and the severity level in the Rubocop Docs for Generic configuration parameters.

Given foo.rb:

# foo.rb
class Foo; end

And .rubocop.yml:

Style/FrozenStringLiteralComment:
  Severity: warning

Run Rubocop with the appropriate flags:

rubocop --fail-level error

And get the following output:

Inspecting 1 file
W

Offenses:

foo.rb:1:1: W: Style/FrozenStringLiteralComment: Missing frozen string literal comment.
class Foo
^

And then get the exit code:

echo $?
0

Verify it's working as expected by modifying your .rubocop.yml to use error rather than warning:

Style/FrozenStringLiteralComment:
  Severity: error

Run it again and get the output:

rubocop --fail-level error
Inspecting 1 file
E

Offenses:

foo.rb:1:1: E: Style/FrozenStringLiteralComment: Missing frozen string literal comment.
class Foo
^

1 file inspected, 1 offense detected

And get the exit code:

echo $?
1
anothermh
  • 9,815
  • 3
  • 33
  • 52