Possible to enforce rubocop to return 0 as exit only code when warnings are found

I am using the following command to run Rubocop on Travis CI, is there anyway I can enforce to exit with 0 only when finding errors

bundle exec rubocop
  1. I have a cop which is configured as a warning in rubocop.yml :
Style/Documentation:
  Severity: warning

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 warnings

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.

Thanks in advance…

-SU

Try using 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. If you’re not familar with them already.

Given enforce.rb :

# enforce.rb
class Enforce; 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 Enforce
^

And then get the exit code:

echo $?
0

Verify this is working as expected by modifying your .rubocop.yml to use error rather than warning just as a cursory test:

Style/FrozenStringLiteralComment:
  Severity: error

Run this again, and get this:

rubocop --fail-level error
Inspecting 1 file
E

Offenses:

enforce.rb:1:1: E: Style/FrozenStringLiteralComment: Missing frozen string literal comment.
class Enforce
^

1 file inspected, 1 offense detected

And get the exit code:

echo $?
1

Hope this helps. Please let me know, and I’ll get back to you.

1 Like
Imprint