Sha256: ac553cd94f564aa0e9e1be9d52f40d300007218bf8fab88a0570af44f14a76a5

Contents?: true

Size: 1.4 KB

Versions: 5

Compression:

Stored size: 1.4 KB

Contents

# https://raw.githubusercontent.com/metric_fu/metric_fu/master/spec/capture_warnings.rb
require 'tempfile'
require 'fileutils'

class CaptureWarnings
  def initialize(fail_on_warnings = true)
    @fail_on_warnings = fail_on_warnings
    @stderr_file = Tempfile.new('app.stderr')
    @app_root ||= Dir.pwd
    @output_dir = File.join(app_root, 'tmp')
    FileUtils.mkdir_p(output_dir)
    @bundle_dir = File.join(app_root, 'bundle')
  end

  def before_tests
    $stderr.reopen(stderr_file.path)
    $VERBOSE = true
    at_exit { $stderr.reopen(STDERR) }
  end

  def after_tests
    stderr_file.rewind
    lines = stderr_file.read.split("\n")
    stderr_file.close!

    $stderr.reopen(STDERR)

    app_warnings, other_warnings = lines.partition { |line|
      line.include?(app_root) && !line.include?(bundle_dir)
    }

    if app_warnings.any?
      puts <<-WARNINGS
#{'-' * 30} app warnings: #{'-' * 30}

#{app_warnings.join("\n")}

#{'-' * 75}
      WARNINGS
    end

    if other_warnings.any?
      File.write(File.join(output_dir, 'warnings.txt'), other_warnings.join("\n") << "\n")
      puts
      puts 'Non-app warnings written to tmp/warnings.txt'
      puts
    end

    # fail the build...
    if fail_on_warnings && app_warnings.any?
      abort "Failing build due to app warnings: #{app_warnings.inspect}"
    end
  end

  private

  attr_reader :stderr_file, :app_root, :output_dir, :bundle_dir, :fail_on_warnings
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
cheap_ams-0.10.11 test/capture_warnings.rb
cheap_ams-0.10.10 test/capture_warnings.rb
cheap_ams-0.10.8 test/capture_warnings.rb
cheap_ams-0.10.7 test/capture_warnings.rb
cheap_ams-0.10.6 test/capture_warnings.rb