【问题标题】:Rubocop: Is it possible to tell Rubocop to return zero as exit code when only warnings are found?Rubocop:当只发现警告时,是否可以告诉 Rubocop 返回零作为退出代码?
【发布时间】:2020-07-29 22:31:34
【问题描述】:

问题

当只发现警告时,是否可以告诉 Rubocop 返回 0 作为退出代码?


先决条件

  1. 我正在使用以下命令在 Travis CI 上运行 Rubocop:

    bundle exec rubocop
    
  2. 我有一个在rubocop.yml 中配置为警告的警察:

    Style/Documentation:
      Severity: warning
    

问题

只有当退出代码等于0时,Travis CI 才会将命令视为成功。

Rubocop 在没有发现任何违规行为时返回 0 作为退出代码,

但当它发现至少一项违规行为时,

它返回1 作为退出代码,无论此攻击是错误还是警告。

因此,当 Rubocop 仅发现警告时,Travis CI 构建会失败。

因此,是否可以告诉 Rubocop 在仅发现警告时返回 0 作为退出代码?

提前致谢。


备注

  • 请不要建议禁用警察。

  • 我正在使用以下方法检查退出代码:

    $ bundle exec rubocop
    // ...
    14 files inspected, 8 offenses detected
    
    $ echo "$?"
    1
    

【问题讨论】:

    标签: ruby continuous-integration travis-ci rubocop


    【解决方案1】:

    --fail-level 标志与适当的severity 级别一起使用:

    rubocop --fail-level error
    

    您可以在Rubocop Docs for command line flagsseverityRubocop Docs for Generic configuration parameters 中阅读有关此标志的更多信息。

    给定foo.rb

    # foo.rb
    class Foo; end
    

    还有.rubocop.yml:

    Style/FrozenStringLiteralComment:
      Severity: warning
    

    使用适当的标志运行 Rubocop:

    rubocop --fail-level error
    

    并得到以下输出:

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

    然后获取退出码:

    echo $?
    0
    

    通过将.rubocop.yml 修改为使用error 而不是warning 来验证它是否按预期工作:

    Style/FrozenStringLiteralComment:
      Severity: error
    

    再次运行,得到输出:

    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
    

    并获取退出码:

    echo $?
    1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-17
      • 1970-01-01
      • 2014-08-09
      • 2016-08-22
      • 1970-01-01
      • 2011-08-03
      相关资源
      最近更新 更多