Fix style

This commit is contained in:
Markus Schirp 2014-12-22 01:28:30 +00:00
parent ae65509258
commit 2ce02af8aa
24 changed files with 154 additions and 104 deletions

View file

@ -5,4 +5,6 @@ AllCops:
- 'Gemfile.devtools' - 'Gemfile.devtools'
- 'vendor/**/*' - 'vendor/**/*'
- 'tmp/**/*' - 'tmp/**/*'
- 'test_app/**/*'
- 'benchmarks/**/*' - 'benchmarks/**/*'
- 'bin/mutant'

View file

@ -1,3 +1,3 @@
--- ---
threshold: 18 threshold: 18
total_score: 1191 total_score: 1198

View file

@ -1,18 +1,5 @@
inherit_from: ../.rubocop.yml inherit_from: ../.rubocop.yml
# General note about rubocop.
# It does NOT allow to silence a specific rule violation.
# For that reason I sometimes have to disable a whole cop where
# I just tried to whitelist a specific occurrence.
AllCops:
Include:
- '../**/*.rake'
- 'Gemfile'
- 'Gemfile.triage'
- 'mutant.gemspec'
# Avoid parameter lists longer than five parameters. # Avoid parameter lists longer than five parameters.
ParameterLists: ParameterLists:
Max: 3 Max: 3
@ -30,53 +17,31 @@ CollectionMethods:
find: 'detect' find: 'detect'
find_all: 'select' find_all: 'select'
# Use square brackets for literal Array objects # Do not force public/protected/private keyword to be indented at the same
PercentLiteralDelimiters: # level as the def keyword. My personal preference is to outdent these keywords
PreferredDelimiters: # because I think when scanning code it makes it easier to identify the
'%': () # sections of code and visually separate them. When the keyword is at the same
'%i': '[]' # level I think it sort of blends in with the def keywords and makes it harder
'%q': () # to scan the code and see where the sections are.
'%Q': () AccessModifierIndentation:
'%r': '{}'
'%s': ()
'%w': '[]'
'%W': '[]'
'%x': ()
MethodLength:
CountComments: false
Max: 17 # TODO: Bring down to 10
RegexpLiteral: # I do not agree %r(\A) is more readable than /\A/
Enabled: false Enabled: false
Eval:
Enabled: false # Mutant must use Kernel#eval to inject mutated source
# Limit line length # Limit line length
LineLength: LineLength:
Max: 124 # TODO: lower to 79 Max: 120
# Disable documentation checking until a class needs to be documented once # Disable documentation checking until a class needs to be documented once
Documentation: Documentation:
Enabled: false Enabled: false
# Do not always use &&/|| instead of and/or.
AndOr:
Enabled: false
# Do not favor modifier if/unless usage when you have a single-line body # Do not favor modifier if/unless usage when you have a single-line body
IfUnlessModifier: IfUnlessModifier:
Enabled: false Enabled: false
# Mutant needs to define methods like def bar; end in specs
Semicolon:
Enabled: false
# Mutant needs to define multiple methods on same line in specs
EmptyLineBetweenDefs:
Enabled: false
# Mutant needs to define singleton methods like Foo.bar in specs
ClassMethods:
Enabled: false
# Allow case equality operator (in limited use within the specs) # Allow case equality operator (in limited use within the specs)
CaseEquality: CaseEquality:
Enabled: false Enabled: false
@ -89,33 +54,63 @@ ConstantName:
TrivialAccessors: TrivialAccessors:
Enabled: false Enabled: false
# And also have a different opinion here # Allow empty lines around class body
AndOr: EmptyLinesAroundClassBody:
Enabled: false Enabled: false
# I like my raise # Allow empty lines around module body
SignalException: EmptyLinesAroundModuleBody:
Enabled: false Enabled: false
# I need to chain optparse builder, else it is more ugly # Allow empty lines around block body
MultilineBlockChain: EmptyLinesAroundBlockBody:
Enabled: false Enabled: false
ClassLength: # Allow multiple line operations to not require indentation
Max: 119 MultilineOperationIndentation:
# I align private keywords with class body
IndentationWidth:
Enabled: false Enabled: false
# I like to have an empty line before closing the currently opened body # Prefer String#% over Kernel#sprintf
EmptyLinesAroundBody: FormatString:
Enabled: false Enabled: false
# I test this style for a while # Use square brackets for literal Array objects
PercentLiteralDelimiters:
PreferredDelimiters:
'%': '{}'
'%i': '[]'
'%q': ()
'%Q': ()
'%r': '{}'
'%s': ()
'%w': '[]'
'%W': '[]'
'%x': ()
# Align if/else blocks with the variable assignment
EndAlignment:
AlignWith: variable
# Do not always align parameters when it is easier to read
AlignParameters:
Exclude:
- spec/**/*_spec.rb
# Prefer #kind_of? over #is_a?
ClassCheck:
EnforcedStyle: kind_of?
# Do not prefer double quotes to be used when %q or %Q is more appropriate
UnneededPercentQ:
Enabled: false
# Allow a maximum ABC score
Metrics/AbcSize:
Max: 18.00
# Do not prefer lambda.call(...) over lambda.(...)
LambdaCall: LambdaCall:
Enabled: false Enabled: false
# I like my style more Style/RegexpLiteral:
AccessModifierIndentation: MaxSlashes: 0
Enabled: false

View file

@ -74,6 +74,8 @@ module Mutant
# #
# @api private # @api private
# #
# rubocop:disable MethodLength
#
def self.singleton_subclass_instance(name, superclass, &block) def self.singleton_subclass_instance(name, superclass, &block)
klass = Class.new(superclass) do klass = Class.new(superclass) do
def inspect def inspect

View file

@ -15,7 +15,7 @@ module Mutant
# @api private # @api private
# #
def self.walk(node, stack, &block) def self.walk(node, stack, &block)
raise ArgumentError, 'block expected' unless block_given? fail ArgumentError, 'block expected' unless block_given?
block.call(node, stack) block.call(node, stack)
node.children.grep(Parser::AST::Node).each do |child| node.children.grep(Parser::AST::Node).each do |child|

View file

@ -74,7 +74,7 @@ module Mutant
parse_match_expressions(opts.parse!(arguments)) parse_match_expressions(opts.parse!(arguments))
rescue OptionParser::ParseError => error rescue OptionParser::ParseError => error
fail(Error, error.message, error.backtrace) raise(Error, error.message, error.backtrace)
end end
# Parse matchers # Parse matchers
@ -101,6 +101,8 @@ module Mutant
# #
# @api private # @api private
# #
# rubocop:disable MethodLength
#
def add_environment_options(opts) def add_environment_options(opts)
opts.separator('Environment:') opts.separator('Environment:')
opts.on('--zombie', 'Run mutant zombified') do opts.on('--zombie', 'Run mutant zombified') do
@ -129,7 +131,7 @@ module Mutant
require "mutant/integration/#{name}" require "mutant/integration/#{name}"
update(integration: Integration.lookup(name)) update(integration: Integration.lookup(name))
rescue LoadError rescue LoadError
fail Error, "Could not load integration #{name.inspect} (you may want to try installing the gem mutant-#{name})" raise Error, "Could not load integration #{name.inspect} (you may want to try installing the gem mutant-#{name})"
end end
# Add options # Add options
@ -146,7 +148,8 @@ module Mutant
opts.on('--score COVERAGE', 'Fail unless COVERAGE is not reached exactly') do |coverage| opts.on('--score COVERAGE', 'Fail unless COVERAGE is not reached exactly') do |coverage|
update(expected_coverage: Float(coverage)) update(expected_coverage: Float(coverage))
end.on('--use STRATEGY', 'Use STRATEGY for killing mutations', &method(:setup_integration)) end
opts.on('--use STRATEGY', 'Use STRATEGY for killing mutations', &method(:setup_integration))
end end
# Add filter options # Add filter options
@ -177,12 +180,15 @@ module Mutant
def add_debug_options(opts) def add_debug_options(opts)
opts.on('--fail-fast', 'Fail fast') do opts.on('--fail-fast', 'Fail fast') do
update(fail_fast: true) update(fail_fast: true)
end.on('--version', 'Print mutants version') do end
opts.on('--version', 'Print mutants version') do
puts("mutant-#{Mutant::VERSION}") puts("mutant-#{Mutant::VERSION}")
Kernel.exit(EXIT_SUCCESS) Kernel.exit(EXIT_SUCCESS)
end.on('-d', '--debug', 'Enable debugging output') do end
opts.on('-d', '--debug', 'Enable debugging output') do
update(debug: true) update(debug: true)
end.on_tail('-h', '--help', 'Show this message') do end
opts.on_tail('-h', '--help', 'Show this message') do
puts(opts.to_s) puts(opts.to_s)
Kernel.exit(EXIT_SUCCESS) Kernel.exit(EXIT_SUCCESS)
end end

View file

@ -125,7 +125,7 @@ module Mutant
def expression(scope) def expression(scope)
name = scope_name(scope) or return name = scope_name(scope) or return
unless name.is_a?(String) unless name.instance_of?(String)
warn("#{scope.class}#name from: #{scope.inspect} returned #{name.inspect}. #{SEMANTICS_MESSAGE}") warn("#{scope.class}#name from: #{scope.inspect} returned #{name.inspect}. #{SEMANTICS_MESSAGE}")
return return
end end

View file

@ -17,7 +17,7 @@ module Mutant
def self.call(&block) def self.call(&block)
block.call block.call
rescue => exception rescue => exception
fail Error, exception raise Error, exception
end end
end end
@ -37,6 +37,8 @@ module Mutant
# #
# @api private # @api private
# #
# rubocop:disable MethodLength
#
def self.call(&block) def self.call(&block)
reader, writer = IO.pipe.map(&:binmode) reader, writer = IO.pipe.map(&:binmode)
@ -52,7 +54,7 @@ module Mutant
writer.close writer.close
Marshal.load(reader.read) Marshal.load(reader.read)
rescue => exception rescue => exception
fail Error, exception raise Error, exception
ensure ensure
Process.waitpid(pid) if pid Process.waitpid(pid) if pid
end end

View file

@ -12,6 +12,10 @@ module Mutant
# #
# @api private # @api private
# #
# One off the very few valid uses of eval
#
# rubocop:disable Lint/Eval
#
def call def call
eval( eval(
source, source,

View file

@ -42,6 +42,8 @@ module Mutant
# #
# @api private # @api private
# #
# rubocop:disable MethodLength
#
def predicate def predicate
if subject_selector && subject_rejector if subject_selector && subject_rejector
Morpher::Evaluator::Predicate::Boolean::And.new([ Morpher::Evaluator::Predicate::Boolean::And.new([

View file

@ -16,6 +16,8 @@ module Mutant
# #
# @api private # @api private
# #
# rubocop:disable MethodLength
#
def kill(isolation, integration) def kill(isolation, integration)
start = Time.now start = Time.now
tests = subject.tests tests = subject.tests

View file

@ -19,7 +19,7 @@ module Mutant
emit_singletons unless parent_node && n_const?(parent_node) emit_singletons unless parent_node && n_const?(parent_node)
emit_type(nil, *children.drop(1)) emit_type(nil, *children.drop(1))
children.each_with_index do |child, index| children.each_with_index do |child, index|
mutate_child(index) if child.is_a?(Parser::AST::Node) mutate_child(index) if child.instance_of?(Parser::AST::Node)
end end
end end

View file

@ -26,7 +26,7 @@ module Mutant
# #
def dispatch def dispatch
children.each_with_index do |child, index| children.each_with_index do |child, index|
mutate_child(index) if child.is_a?(Parser::AST::Node) mutate_child(index) if child.instance_of?(Parser::AST::Node)
end end
end end

View file

@ -43,6 +43,8 @@ module Mutant
# #
# @api private # @api private
# #
# rubocop:disable MethodLength
#
def run def run
config.jobs.times do config.jobs.times do
@workers += 1 @workers += 1

View file

@ -14,13 +14,11 @@ module Mutant
# #
def self.build(output) def self.build(output)
tty = output.respond_to?(:tty?) && output.tty? tty = output.respond_to?(:tty?) && output.tty?
format = format = if !Mutant.ci? && tty && Tput::INSTANCE.available
if !Mutant.ci? && tty && Tput::INSTANCE.available Format::Framed.new(tty: tty, tput: Tput::INSTANCE)
Format::Framed.new(tty: tty, tput: Tput::INSTANCE) else
else Format::Progressive.new(tty: tty)
Format::Progressive.new(tty: tty) end
end
new(output, format) new(output, format)
end end

View file

@ -176,7 +176,7 @@ module Mutant
# @api private # @api private
# #
def active_subject_results def active_subject_results
active_mutation_jobs = active_jobs.select { |job| job.payload.is_a?(Mutant::Mutation) } active_mutation_jobs = active_jobs.select { |job| job.payload.kind_of?(Mutant::Mutation) }
active_subjects = active_mutation_jobs.map(&:payload).flat_map(&:subject).to_set active_subjects = active_mutation_jobs.map(&:payload).flat_map(&:subject).to_set
payload.subject_results.select do |subject_result| payload.subject_results.select do |subject_result|
@ -197,6 +197,8 @@ module Mutant
# #
# @api private # @api private
# #
# rubocop:disable AbcSize
#
def run def run
info 'Mutant configuration:' info 'Mutant configuration:'
info 'Matcher: %s', object.matcher_config.inspect info 'Matcher: %s', object.matcher_config.inspect
@ -231,6 +233,8 @@ module Mutant
# #
# @api private # @api private
# #
# rubocop:disable MethodLength
#
def run def run
visit(Config, env.config) visit(Config, env.config)
info 'Subjects: %s', amount_subjects info 'Subjects: %s', amount_subjects
@ -480,7 +484,8 @@ module Mutant
delegate :mutation, :test_result delegate :mutation, :test_result
DIFF_ERROR_MESSAGE = 'BUG: Mutation NOT resulted in exactly one diff hunk. Please report a reproduction!'.freeze DIFF_ERROR_MESSAGE =
'BUG: Mutation NOT resulted in exactly one diff hunk. Please report a reproduction!'.freeze
MAP = { MAP = {
Mutant::Mutation::Evil => :evil_details, Mutant::Mutation::Evil => :evil_details,

View file

@ -10,6 +10,10 @@ module Mutant
# #
# @api private # @api private
# #
# Probably one of the only valid uses of eval.
#
# rubocop:disable Lint/Eval
#
def zombify(namespace) def zombify(namespace)
$stderr.puts("Zombifying #{path}") $stderr.puts("Zombifying #{path}")
eval( eval(
@ -33,15 +37,7 @@ module Mutant
# @api private # @api private
# #
def self.find(logical_name) def self.find(logical_name)
file_name = file_name = expand_file_name(logical_name)
case ::File.extname(logical_name)
when '.so'
return
when '.rb'
logical_name
else
"#{logical_name}.rb"
end
$LOAD_PATH.each do |path| $LOAD_PATH.each do |path|
path = Pathname.new(path).join(file_name) path = Pathname.new(path).join(file_name)
@ -52,6 +48,29 @@ module Mutant
nil nil
end end
# Return expanded file name
#
# @param [String] logical_name
#
# @return [nil]
# if no expansion is possible
#
# @return [String]
#
# @api private
#
def self.expand_file_name(logical_name)
case ::File.extname(logical_name)
when '.so'
return
when '.rb'
logical_name
else
"#{logical_name}.rb"
end
end
private_class_method :expand_file_name
private private
# Return node # Return node

View file

@ -47,6 +47,8 @@ module Corpus
# otherwise # otherwise
# #
# rubocop:disable MethodLength # rubocop:disable MethodLength
# rubocop:disable AbcSize
#
def verify_mutation_generation def verify_mutation_generation
checkout checkout
start = Time.now start = Time.now

View file

@ -12,7 +12,7 @@ module FakeActor
def verify(other) def verify(other)
unless eql?(other) unless eql?(other)
raise "Got:\n#{other.inspect}\nExpected:\n#{inspect}" fail "Got:\n#{other.inspect}\nExpected:\n#{inspect}"
end end
block.call(other.message) if block block.call(other.message) if block
end end
@ -31,16 +31,16 @@ module FakeActor
end end
def sending(expectation) def sending(expectation)
raise "Unexpected send: #{expectation.inspect}" if messages.empty? fail "Unexpected send: #{expectation.inspect}" if messages.empty?
expected = messages.shift expected = messages.shift
expected.verify(expectation) expected.verify(expectation)
self self
end end
def receiving(name) def receiving(name)
raise "No message to read for #{name.inspect}" if messages.empty? fail "No message to read for #{name.inspect}" if messages.empty?
expected = messages.shift expected = messages.shift
raise "Unexpected message #{expected.inspect} for #{name.inspect}" unless expected.name.eql?(name) fail "Unexpected message #{expected.inspect} for #{name.inspect}" unless expected.name.eql?(name)
expected.message expected.message
end end

View file

@ -43,6 +43,8 @@ private
# #
# @api private # @api private
# #
# rubocop:disable AbcSize
#
def mutation_report def mutation_report
message = ['Original-AST:', original_node.inspect, 'Original-Source:', Unparser.unparse(original_node)] message = ['Original-AST:', original_node.inspect, 'Original-Source:', Unparser.unparse(original_node)]
if missing.any? if missing.any?

View file

@ -8,12 +8,13 @@ module SharedContext
def messages(&block) def messages(&block)
let(:message_sequence) do let(:message_sequence) do
FakeActor::MessageSequence.new.tap do |sequence| FakeActor::MessageSequence.new.tap do |sequence|
sequence.instance_eval(&block) sequence.instance_eval(&block)
end end
end end
end end
# rubocop:disable MethodLength # rubocop:disable MethodLength
# rubocop:disable AbcSize
def setup_shared_context def setup_shared_context
let(:env) { double('env', config: config, subjects: [subject_a], mutations: mutations) } let(:env) { double('env', config: config, subjects: [subject_a], mutations: mutations) }
let(:job_a) { Mutant::Parallel::Job.new(index: 0, payload: mutation_a) } let(:job_a) { Mutant::Parallel::Job.new(index: 0, payload: mutation_a) }

View file

@ -8,11 +8,13 @@ RSpec.describe Mutant::Env do
it 'warns via reporter' do it 'warns via reporter' do
klass = Class.new do klass = Class.new do
def self.name def self.name
raise fail
end end
end end
expected_warnings = ["Class#name from: #{klass} raised an error: RuntimeError. #{Mutant::Env::SEMANTICS_MESSAGE}"] expected_warnings = [
"Class#name from: #{klass} raised an error: RuntimeError. #{Mutant::Env::SEMANTICS_MESSAGE}"
]
expect { subject }.to change { config.reporter.warn_calls }.from([]).to(expected_warnings) expect { subject }.to change { config.reporter.warn_calls }.from([]).to(expected_warnings)

View file

@ -51,7 +51,10 @@ RSpec.describe Mutant::Expression do
let(:input) { 'foo bar' } let(:input) { 'foo bar' }
it 'raises an exception' do it 'raises an exception' do
expect { subject }.to raise_error(Mutant::Expression::InvalidExpressionError, 'Expression: "foo bar" is not valid') expect { subject }.to raise_error(
Mutant::Expression::InvalidExpressionError,
'Expression: "foo bar" is not valid'
)
end end
end end

View file

@ -54,7 +54,7 @@ RSpec.describe Mutant::WarningFilter do
it 'executes block with warning filter enabled' do it 'executes block with warning filter enabled' do
found = false found = false
object.use do object.use do
found = $stderr.is_a?(described_class) found = $stderr.instance_of?(described_class)
end end
expect(found).to be(true) expect(found).to be(true)
end end
@ -71,6 +71,7 @@ RSpec.describe Mutant::WarningFilter do
it 'returns warnings generated within block' do it 'returns warnings generated within block' do
warnings = object.use do warnings = object.use do
# rubocop:disable Lint/Eval
eval(<<-RUBY) eval(<<-RUBY)
Class.new do Class.new do
def foo def foo