Sha256: a50d09521b046cb30829829c827a0ebc0e820dbe33ed19961349f02b05b5f8ed

Contents?: true

Size: 1.88 KB

Versions: 294

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true
# With thanks to @myronmarston
# https://github.com/vcr/vcr/blob/master/spec/capture_warnings.rb

module CaptureWarnings
  def report_warnings(&block)
    current_dir = Dir.pwd
    warnings, errors = capture_error(&block).partition { |line| line.include?('warning') }
    project_warnings, other_warnings = warnings.uniq.partition { |line| line.include?(current_dir) }

    if errors.any?
      puts errors.join("\n")
    end

    if other_warnings.any?
      puts "#{ other_warnings.count } warnings detected, set VIEW_OTHER_WARNINGS=true to see them."
      print_warnings('other', other_warnings) if ENV['VIEW_OTHER_WARNINGS']
    end

    # Until they fix https://bugs.ruby-lang.org/issues/10661
    if RUBY_VERSION == "2.2.0"
      project_warnings = project_warnings.reject { |w| w =~ /warning: possible reference to past scope/ }
    end

    if project_warnings.any?
      puts "#{ project_warnings.count } warnings detected"
      print_warnings('cucumber-expressions', project_warnings)
      fail "Please remove all cucumber-expressions warnings."
    end

    ensure_system_exit_if_required
  end

  def capture_error(&block)
    old_stderr = STDERR.clone
    pipe_r, pipe_w = IO.pipe
    pipe_r.sync    = true
    error          = String.new
    reader = Thread.new do
      begin
        loop do
          error << pipe_r.readpartial(1024)
        end
      rescue EOFError
      end
    end
    STDERR.reopen(pipe_w)
    block.call
  ensure
    capture_system_exit
    STDERR.reopen(old_stderr)
    pipe_w.close
    reader.join
    return error.split("\n")
  end

  def print_warnings(type, warnings)
    puts
    puts "-" * 30 + " #{type} warnings: " + "-" * 30
    puts
    puts warnings.join("\n")
    puts
    puts "-" * 75
    puts
  end

  def ensure_system_exit_if_required
    raise @system_exit if @system_exit
  end

  def capture_system_exit
    @system_exit = $!
  end
end

Version data entries

294 entries across 261 versions & 16 rubygems

Version Path
cucumber-gherkin-28.0.0 spec/capture_warnings.rb
honeybadger-5.4.0 vendor/bundle/ruby/3.2.0/gems/cucumber-html-formatter-20.4.0/spec/capture_warnings.rb
honeybadger-5.4.0 vendor/bundle/ruby/3.2.0/gems/cucumber-messages-22.0.0/spec/capture_warnings.rb
honeybadger-5.4.0 vendor/bundle/ruby/3.2.0/gems/cucumber-gherkin-26.2.0/spec/capture_warnings.rb
honeybadger-5.4.0 vendor/bundle/ruby/3.2.0/gems/cucumber-ci-environment-9.2.0/spec/capture_warnings.rb
cucumber-html-formatter-21.0.0 spec/capture_warnings.rb
honeybadger-5.3.0 vendor/bundle/ruby/3.2.0/gems/cucumber-cucumber-expressions-16.1.2/spec/capture_warnings.rb
honeybadger-5.3.0 vendor/bundle/ruby/3.2.0/gems/cucumber-html-formatter-20.4.0/spec/capture_warnings.rb
honeybadger-5.3.0 vendor/bundle/ruby/3.2.0/gems/cucumber-messages-21.0.1/spec/capture_warnings.rb
honeybadger-5.3.0 vendor/bundle/ruby/3.2.0/gems/cucumber-gherkin-26.2.0/spec/capture_warnings.rb
honeybadger-5.3.0 vendor/bundle/ruby/3.2.0/gems/cucumber-tag-expressions-4.1.0/spec/capture_warnings.rb
honeybadger-5.3.0 vendor/bundle/ruby/3.2.0/gems/cucumber-ci-environment-9.2.0/spec/capture_warnings.rb
cucumber-gherkin-27.0.0 spec/capture_warnings.rb
cucumber-html-formatter-20.4.0 spec/capture_warnings.rb
cucumber-compatibility-kit-12.0.0 spec/capture_warnings.rb
rubypitaya-3.12.5 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/cucumber-ci-environment-9.1.0/spec/capture_warnings.rb
rubypitaya-3.12.5 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/cucumber-html-formatter-19.2.0/spec/capture_warnings.rb
rubypitaya-3.12.5 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/cucumber-messages-18.0.0/spec/capture_warnings.rb
rubypitaya-3.12.5 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/cucumber-cucumber-expressions-15.2.0/spec/capture_warnings.rb
rubypitaya-3.12.5 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/cucumber-gherkin-23.0.1/spec/capture_warnings.rb