free_mutant/spec/support/warning.rb
Bartek Bułat 0be9fa75d6 Add Ruby 2.4 support for mutant
Several steps were made to achieve mutant working with Ruby 2.4 without
warnings and spec failures:

* Fix Fixnum deprecation warning
* Update RuboCop to 0.47 and fix offenses
* Relax version requirements on parser gem
* Update diff-lcs to 1.3
* Update rake 11.x -> 12.x
* Update reek 4.5 -> 4.6 (to work with parser 2.4)
* Update json 2.0 -> 2.1
* Update parallel 1.10 -> 1.11
* Update simplecov 0.12 -> 0.14
* Run regexp_parser integration against v0.4.3 (as we requiring this
    exact version)
* Update warnings.yml with new whitelist
* Run CircleCI tests on Ruby 2.3 and 2.4 (Bundler.with_clean_env
    was causing troubles on new CircleCI 2.0 setup, so it was removed)
2017-07-06 11:18:41 +02:00

64 lines
1.3 KiB
Ruby

require 'yaml'
require 'equalizer'
require 'memoizable'
require 'ice_nine'
module MutantSpec
class Warning
def self.assert_no_warnings
return if EXTRACTOR.warnings.empty?
fail UnexpectedWarnings, EXTRACTOR.warnings.to_a
end
class UnexpectedWarnings < StandardError
MSG = 'Unexpected warnings: %s'.freeze
def initialize(warnings)
super(MSG % warnings.join("\n"))
end
end
class Extractor < DelegateClass(IO)
PATTERN = /\A(?:.+):(?:\d+): warning: (?:.+)\n\z/.freeze
include Equalizer.new(:whitelist, :seen, :io), Memoizable
def initialize(io, whitelist)
@whitelist = whitelist
@seen = Set.new
@io = io
super(io)
end
def write(message)
return super if PATTERN !~ message
add(message.chomp)
self
end
def warnings
seen.dup
end
memoize :warnings
private
def add(warning)
return if whitelist.any?(&warning.public_method(:end_with?))
seen << warning
end
attr_reader :whitelist, :seen, :io
end
warnings = Pathname.new(__dir__).join('warnings.yml').freeze
whitelist = IceNine.deep_freeze(YAML.load(warnings.read)) # rubocop:disable Security/YAMLLoad
EXTRACTOR = Extractor.new(STDERR, whitelist)
end
end