Class: MutatorRails::SingleMutate

Inherits:
Object
  • Object
show all
Defined in:
lib/mutator_rails/single_mutate.rb

Instance Method Summary collapse

Instance Method Details

#baseObject



52
53
54
# File 'lib/mutator_rails/single_mutate.rb', line 52

def base
  path.basename.to_s.sub(path.extname, '').camelize
end

#callObject



9
10
11
12
13
14
15
16
17
18
# File 'lib/mutator_rails/single_mutate.rb', line 9

def call
  parms = BASIC_PARMS.dup
  parms << preface(path.basename) + base

  parms << '1> ' + log.to_s
  log_dir

  cmd = first_run(parms)
  rerun(cmd)
end

#code_md5Object



56
57
58
# File 'lib/mutator_rails/single_mutate.rb', line 56

def code_md5
  Digest::MD5.file(path).hexdigest
end

#complete?(log) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
100
# File 'lib/mutator_rails/single_mutate.rb', line 97

def complete?(log)
  content = File.read(log)
  /^Subjects: / === content
end

#failed?(log) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
105
# File 'lib/mutator_rails/single_mutate.rb', line 102

def failed?(log)
  content = File.read(log)
  /Failures:/ === content
end

#first_run(parms) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/mutator_rails/single_mutate.rb', line 77

def first_run(parms)
  cmd = spec_opt + COMMAND + parms.join(' ')

  if changed? || !complete?(log) || failed?(log)
    puts "[#{Time.current.iso8601}] #{cmd}"
    `#{cmd}` unless ENV['RACK_ENV'].eql?('test')
    guide.update(log, code_md5, spec_md5)
  end

  cmd
end

#full_logObject



30
31
32
# File 'lib/mutator_rails/single_mutate.rb', line 30

def full_log
  log_location.to_s + '.log'
end

#logObject



20
21
22
23
24
25
26
27
28
# File 'lib/mutator_rails/single_mutate.rb', line 20

def log
  if File.exist?(old_log)
    # repair - this is one time only
    guide.update(full_log, code_md5, spec_md5)
    File.rename(old_log, full_log)
  end
  
  full_log
end

#log_correct?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/mutator_rails/single_mutate.rb', line 107

def log_correct?
  guide.current?(log, code_md5, spec_md5)
end

#log_dirObject



42
43
44
45
46
# File 'lib/mutator_rails/single_mutate.rb', line 42

def log_dir
  log_location.dirname.tap do |dir|
    FileUtils.mkdir_p(dir)
  end
end

#log_locationObject



38
39
40
# File 'lib/mutator_rails/single_mutate.rb', line 38

def log_location
  path.sub(APP_BASE, logroot).sub('.rb', '')
end

#old_logObject



34
35
36
# File 'lib/mutator_rails/single_mutate.rb', line 34

def old_log
  "#{log_location}_#{code_md5}_#{spec_md5}_#{MUTANT_VERSION}.log"
end

#pathObject



60
61
62
# File 'lib/mutator_rails/single_mutate.rb', line 60

def path
  Pathname.new(file)
end

#preface(base) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/mutator_rails/single_mutate.rb', line 111

def preface(base)
  rest = file.sub(APP_BASE, '').sub(/(lib)\//, '').sub(base.to_s, '')
  return '' if rest == ''

  content = File.read(spec_file)
  d       = content.match(/RSpec.describe\s+([^ ,]+)/)
  cs      = d[1].split('::')
  cs.pop
  f = cs.join('::')
  f += '::' if f.present?
  f
end

#rerun(cmd) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/mutator_rails/single_mutate.rb', line 64

def rerun(cmd)
  return unless File.exist?(log)

  content = File.read(log)
  return unless /Failures:/ === content

  FileUtils.cp(log, '/tmp')
  cmd2 = cmd.sub('--use', '-j1 --use')
  puts log
  puts "[#{Time.current.iso8601}] #{cmd2}"
  `#{cmd2}` unless ENV['RACK_ENV'].eql?('test')
end

#spec_fileObject



93
94
95
# File 'lib/mutator_rails/single_mutate.rb', line 93

def spec_file
  file.sub(APP_BASE, 'spec/').sub('.rb', '_spec.rb')
end

#spec_md5Object



48
49
50
# File 'lib/mutator_rails/single_mutate.rb', line 48

def spec_md5
  Digest::MD5.file(spec_file).hexdigest
end

#spec_optObject



89
90
91
# File 'lib/mutator_rails/single_mutate.rb', line 89

def spec_opt
  "SPEC_OPTS=\"--pattern #{spec_file}\" "
end