Skip to content

Instantly share code, notes, and snippets.

@andrzejsliwa
Last active March 7, 2018 09:13
Show Gist options
  • Save andrzejsliwa/e982cfa810a4b9ef0d6b1a958c5966a3 to your computer and use it in GitHub Desktop.
Save andrzejsliwa/e982cfa810a4b9ef0d6b1a958c5966a3 to your computer and use it in GitHub Desktop.
ugly proof of concept for Mutant rake task for Rails application with configuration files
app/models/*.rb
ApplicationController
class MutantRunner
def run
out = false
classes_list.each do |c|
puts "Running mutant for '#{c}'"
out = system("RAILS_EAGER_LOAD=true RAILS_ENV=test bundle exec mutant -r \
./config/environment #{ignored_subjects} --use rspec #{c}")
break unless out
end
out
end
private
def ignored_subjects
ignored_subjects_path = Rails.root.join('.mutant_ignored_subjects')
return "" unless File.exist?(ignored_subjects_path)
File.read(ignored_subjects_path)
.split("\n")
.reject { |s| s.blank? }
.map { |s| "--ignore-subject #{s}" }
.join(" ")
end
def classes_list
mutants_paths_path = Rails.root.join('.mutant_subjects')
return [] unless File.exist?(mutants_paths_path)
lines = File.read(mutants_paths_path).split("\n").reject { |s| s.blank? }
paths, classes = lines.partition { |s| s =~ /\.rb/ }
classes = lines.select { |s| s !~ /\.rb/ }
Dir[*paths].map do |path|
path.match(/(\w+).rb/)
end.compact.each do |c|
classes << camelize(c[1])
end
classes
end
def camelize(string)
mod_string = ''
string.split('_').each do |part|
mod_string += "#{part[0].upcase}#{part[1..-1]}"
end
mod_string
end
end
desc "Run mutant for paths defined in `.mutant_subjects` and ignored subjects from `.mutant_ignored_subjects`"
task :mutant do
MutantRunner.new.run
end
# in config/environments/test.rb
config.eager_load = ENV['RAILS_EAGER_LOAD'] == 'true'
# and same in config/environments/development.rb
config.eager_load = ENV['RAILS_EAGER_LOAD'] == 'true'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment