如何在运行Rails测试时将Brakeman设置为始终运行?

时间:2022-03-23 02:23:38

I'm using MiniTest with Rails 5. When I run the following command, I want Brakeman to scan my application before tests run:

我正在使用MiniTest和Rails 5.当我运行以下命令时,我希望Brakeman在测试运行之前扫描我的应用程序:

bundle exec rake test

1 个解决方案

#1


1  

Following the example with Rubocop here, I added the following task to lib/tasks/test.rake:

按照Rubocop的例子,我在lib / tasks / test.rake中添加了以下任务:

# 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']

It can also be run as a rake task:

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

bundle exec rake test:brakeman

#1


1  

Following the example with Rubocop here, I added the following task to lib/tasks/test.rake:

按照Rubocop的例子,我在lib / tasks / test.rake中添加了以下任务:

# 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']

It can also be run as a rake task:

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

bundle exec rake test:brakeman