RubyCritic 4.2 发布,生成 Ruby 代码质量报告

RubyCritic 集成 Reek,Flay 和 Flog 这 3 个代码静态分析的工具,对你的Ruby代码进行静态分析并生成代码质量报告。4.2 版本已发布,此版本支持 SimpleCov,主要内容如下:

在 RubyCritic 的 HTML 报告中 pull 请求添加了一个新的部分(称为 Coverage),如果 RubyCritic 在 Ruby 项目中找到 .resultset.json,则可生成 Coverage。它假定所有代码覆盖结果都合并在该文件中。

.resultset.json 是 SimpleCov 每次在运行测试套件之前加载它时生成的文件。RubyCritic::Analyser::Coverage 使用 analysed_modules 列表,它将尝试查找该特定文件的 coverage 数据:

# RubyCritic::Analyser::Coverage
def find_coverage_percentage(analysed_module)
  source_file = find_source_file(analysed_module)

  return 0 unless source_file

  source_file.covered_percent
end

def find_source_file(analysed_module)
  return unless @result

  needle = File.join(SimpleCov.root, analysed_module.path)

  @result.source_files.detect { |file| file.filename == needle }
end

基于模块的 coverage 范围,计算它的“等级”。此版本它只在文件级别有效,覆盖部分将对模块进行排序,从覆盖最少的模块到覆盖最多的模块。

 RubyCritic::AnalysedModule
def coverage_rating
  @coverage_rating ||= Rating.from_cost(100 - coverage)
end

限制:目前还不支持并行化。如果你在 Circle CI 或其他支持并行化的 CI 工具中运行测试,则需要手动将所有结果合并到一个大的 .resultset.json 中,然后可以运行 rubyReview 来生成 HTML 报告。

发布公告:

https://www.fastruby.io/blog/code-quality/code-coverage/rubycritic-4-2-0-simplecov-support.html

猜你喜欢

转载自www.oschina.net/news/110966/rubycritic-4-2-released
4.2