free_mutant/lib/mutant/rspec/strategy.rb
Markus Schirp a19f3b1691 Nuke UTF-8 encoding headers
* I do not use 1.9.3
* Also keeping them in each file increases mental overhead (true it *can* be autoamted)
* None of the files encodes NON ASCII chars.
* I do not expect it makes any difference, since nobody programmatically
  will consume strings generated by mutant under the assumption they are UTF-8 encoded.
* 1.9.3 Users have to deal with the encoding fuckup under ruby anyways.
2014-06-09 15:37:48 +00:00

135 lines
3 KiB
Ruby

module Mutant
module Rspec
# Rspec killer strategy
class Strategy < Mutant::Strategy
RSPEC_2_VERSION_PREFIX = '2.'.freeze
register 'rspec'
# Setup rspec strategy
#
# @return [self]
#
# @api private
#
def setup
options.configure(configuration)
configuration.load_spec_files
self
end
memoize :setup
# Test for rspec2
#
# @return [Boolean]
#
# @api private
#
def rspec2?
RSpec::Core::Version::STRING.start_with?(RSPEC_2_VERSION_PREFIX)
end
# Return report for test
#
# @param [Rspec::Test] test
#
# @api private
#
def run(test)
output = StringIO.new
success = false
reporter = new_reporter(output)
reporter.report(1) do
success = test.example_group.run(reporter)
end
output.rewind
Test::Report.new(
test: self,
output: output.read,
success: success
)
end
# Return all available tests
#
# @return [Enumerable<Test>]
#
# @api private
#
def all_tests
example_groups
.flat_map(&:descendants)
.map do |example_group|
Test.new(self, example_group)
end.select do |test|
test.identification.split(' ', 2).first.eql?(test.identification)
end
end
memoize :all_tests
private
# Return example groups
#
# @return [Enumerable<RSpec::Core::ExampleGroup>]
#
# @api private
#
def example_groups
RSpec.world.example_groups
end
# Return new reporter
#
# @param [StringIO] output
#
# @return [RSpec::Core::Reporter]
#
# @api private
#
def new_reporter(output)
reporter_class = RSpec::Core::Reporter
# rspec3 does not require that one via a very indirect autoload setup
require 'rspec/core/formatters/base_text_formatter'
formatter = RSpec::Core::Formatters::BaseTextFormatter.new(output)
if rspec2?
reporter_class.new(formatter)
else
notifications = RSpec::Core::Formatters::Loader.allocate.send(:notifications_for, formatter.class)
reporter = reporter_class.new(configuration)
reporter.register_listener(formatter, *notifications)
reporter
end
end
# Return configuration
#
# @return [RSpec::Core::Configuration]
#
# @api private
#
def configuration
RSpec::Core::Configuration.new
end
memoize :configuration, freezer: :noop
# Return options
#
# @return [RSpec::Core::ConfigurationOptions]
#
# @api private
#
def options
options = RSpec::Core::ConfigurationOptions.new(%w[--fail-fast spec])
options.parse_options if rspec2?
options
end
memoize :options, freezer: :noop
end # Strategy
end # Rspec
end # Mutant