Skip to content

Instantly share code, notes, and snippets.

@tjchambers
Created December 27, 2015 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjchambers/bf7962dd6da56f27c02d to your computer and use it in GitHub Desktop.
Save tjchambers/bf7962dd6da56f27c02d to your computer and use it in GitHub Desktop.
Runs mutant selectively
namespace :mutant do
MUTANT_VERSION = `mutant --version`.strip.split('-').last
def complete?(log)
content = File.read(log)
/Kills:/.match(content)
end
def preface(file, base)
rest = file.sub('app/', '').sub(/(lib)\//, AOT::EMPTY_STRING).sub(base.to_s, AOT::EMPTY_STRING)
return AOT::EMPTY_STRING if rest == AOT::EMPTY_STRING
sections = rest.split(AOT::SLASH)
if sections.size == 1
segment = sections.first
segment = 'SIT' if segment == 'Sit'
if segment.size <= 3
return (segment.upcase + '::').sub('::::', '::').sub('Agr', "AGR")
end
end
(rest.camelize + '::').sub('::::', '::').sub('Agr', "AGR")
end
task :lib do
require 'fileutils'
require_relative '../../config/environment.rb'
begin
FileList.new('app/lib/**/*.rb',
'app/export/**/*.rb',
'app/queuer/**/*.rb',
'app/worker/**/*.rb',
'app/mailers/**/*.rb',
'app/import/**/*.rb')
.sort_by { |x| File.size(x) }.each do |file|
next if file =~ /shared/ || file =~ /observer/ || file =~ /eradicator/
parms = ['-r./config/environment.rb']
parms << '-j6'
path = Pathname.new(file)
md5 = Digest::MD5.file(path).hexdigest
# parms << "-I#{path.dirname}"
# parms << "-r#{path.basename}"
parms << '--use rspec'
base = path.basename.to_s.sub(path.extname, '').camelize
if base.present?
parms << preface(file, path.basename) + base
spec = path.sub('app/', 'spec/').sub('.rb', '_spec.rb')
md5_spec = Digest::MD5.file(spec).hexdigest
log_dir = path.dirname.sub('app/', 'log/mutant/')
log_location = path.sub('app/', 'log/mutant/').sub('.rb', '_')
log = "#{log_location}#{md5}_#{md5_spec}_#{MUTANT_VERSION}.log"
FileUtils.mkdir_p(log_dir)
parms << '> ' + log
cmd = 'RAILS_ENV=test bundle exec mutant ' + parms.join(AOT::BLANK)
if Dir.glob(log).empty? || !complete?(log)
File.delete(Dir.glob("#{log_location}*.log").first) rescue nil
File.write('/Users/tj/.rspec', "--pattern '#{spec}'")
puts "[#{Time.now.iso8601}] #{cmd}"
`#{cmd}`
end
if File.exists?(log)
content = File.read(log)
if /Mysql2::Error:/.match(content)
FileUtils.cp(log, '/tmp')
File.write('/Users/tj/.rspec', "--pattern '#{spec}'")
cmd2 = cmd.sub('-j6', '-j1')
puts log
puts "[#{Time.now.iso8601}] #{cmd2}"
`#{cmd2}`
end
end
end
end
ensure
File.write('/Users/tj/.rspec', '')
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment