首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在运行Rails测试时,如何设置Brakeman使其始终运行?

在运行Rails测试时,如何设置Brakeman使其始终运行?
EN

Stack Overflow用户
提问于 2018-05-03 00:39:38
回答 1查看 265关注 0票数 1

我在Rails 5中使用MiniTest。当我运行以下命令时,我希望制动器在测试运行之前扫描我的应用程序:

代码语言:javascript
复制
bundle exec rake test
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-03 00:39:38

按照Rubocop 这里的示例,我向lib/tasks/test.rake添加了以下任务

代码语言:javascript
复制
# Add additional test suite definitions to the default test task here
namespace :test do
  desc 'Runs Brakeman'
  # based on https://brakemanscanner.org/docs/rake/
  task :brakeman, :output_files do |_task, args|
    # To abort on failures, set to true.
    EXIT_ON_FAIL = false

    require 'brakeman'

    files = args[:output_files].split(' ') if args[:output_files]

    # For more options, see source here:
    # https://github.com/presidentbeef/brakeman/blob/master/lib/brakeman.rb#L30
    options = {
      app_path: ".",
      exit_on_error: EXIT_ON_FAIL,
      exit_on_warn: EXIT_ON_FAIL,
      output_files: files,
      print_report: true,
      pager: false,
      summary_only: true
    }

    tracker = Brakeman.run options
    failures = tracker.filtered_warnings + tracker.errors

    # Based on code here:
    # https://github.com/presidentbeef/brakeman/blob/f2376c/lib/brakeman/commandline.rb#L120
    if EXIT_ON_FAIL && failures.any?
      puts 'Brakeman violations found. Aborting now...'
      exit Brakeman::Warnings_Found_Exit_Code unless tracker.filtered_warnings.empty?
      exit Brakeman::Errors_Found_Exit_Code if tracker.errors.any?
    end
  end
end

Rake::Task[:test].enhance ['test:brakeman']

它还可以作为rake任务运行:

代码语言:javascript
复制
bundle exec rake test:brakeman
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50145370

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档