Class: MutationTester::BatchRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/mutation_tester/batch_runner.rb

Defined Under Namespace

Classes: ProcessedEntry, Result, SkippedEntry

Constant Summary collapse

NAME_PLACEHOLDER =
'{name}'.freeze
DEFAULT_SPEC_TEMPLATE =
'spec/{name}_spec.rb'.freeze
TEST_FILE_SUFFIXES =
['_spec.rb', '.spec.rb', '_test.rb'].freeze
SKIP_REASONS =
{
  missing: 'file not found',
  not_ruby: 'not a Ruby source file',
  test_file: 'a test file, not a mutable source',
  no_spec: 'no matching spec file'
}.freeze
SKIP_ORDER =
%i[missing not_ruby test_file no_spec].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(glob: nil, files: nil, spec_template: nil, config: MutationTester.configuration, since: nil, changed_files: nil) ⇒ BatchRunner

Returns a new instance of BatchRunner.

Raises:

  • (ArgumentError)


121
122
123
124
125
126
127
128
129
130
# File 'lib/mutation_tester/batch_runner.rb', line 121

def initialize(glob: nil, files: nil, spec_template: nil, config: MutationTester.configuration, since: nil, changed_files: nil)
  raise ArgumentError, 'provide exactly one of glob: or files:' unless glob.nil? ^ files.nil?

  @glob = glob
  @files = files
  @spec_template = spec_template.nil? || spec_template.empty? ? DEFAULT_SPEC_TEMPLATE : spec_template
  @config = config
  @since = since
  @changed_files = changed_files
end

Class Method Details

.changed_files_since(revision, dir: Dir.pwd) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/mutation_tester/batch_runner.rb', line 65

def self.changed_files_since(revision, dir: Dir.pwd)
  toplevel = git_capture(dir, 'rev-parse', '--show-toplevel') do |stderr|
    "not a git repository (--since needs one): #{stderr}"
  end.strip

  git_capture(dir, 'rev-parse', '--verify', '--quiet', "#{revision}^{commit}") do |stderr|
    detail = stderr.empty? ? 'not a commit' : stderr
    "unknown revision #{revision.inspect}: #{detail}"
  end

  diff = git_capture(dir, 'diff', '--name-only', revision) do |stderr|
    "git diff --name-only #{revision} failed: #{stderr}"
  end
  untracked = git_capture(dir, 'ls-files', '--others', '--exclude-standard') do |stderr|
    "git ls-files failed: #{stderr}"
  end

  (diff.lines + untracked.lines)
    .map(&:strip).reject(&:empty?)
    .map { |path| File.expand_path(path, toplevel) }
    .to_set
end

.staged_files(dir: Dir.pwd) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/mutation_tester/batch_runner.rb', line 88

def self.staged_files(dir: Dir.pwd)
  toplevel = git_capture(dir, 'rev-parse', '--show-toplevel') do |stderr|
    "not a git repository (--staged needs one): #{stderr}"
  end.strip

  staged = git_capture(dir, 'diff', '--cached', '--name-only', '--diff-filter=d') do |stderr|
    "git diff --cached --name-only failed: #{stderr}"
  end

  base = Pathname.new(File.realpath(dir))
  staged.lines
        .map(&:strip).reject(&:empty?)
        .map { |path| Pathname.new(File.expand_path(path, toplevel)).relative_path_from(base).to_s }
end

.test_file?(path) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
106
107
108
109
# File 'lib/mutation_tester/batch_runner.rb', line 103

def self.test_file?(path)
  basename = File.basename(path)
  return true if TEST_FILE_SUFFIXES.any? { |suffix| basename.end_with?(suffix) }
  return true if basename.start_with?('test_') && basename.end_with?('.rb')

  FrameworkDetector.minitest_content?(path)
end

Instance Method Details

#classify(source_file) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/mutation_tester/batch_runner.rb', line 166

def classify(source_file)
  if @files
    return SkippedEntry.new(source_file: source_file, reason: :missing) unless File.file?(source_file)
    return SkippedEntry.new(source_file: source_file, reason: :not_ruby) unless source_file.end_with?('.rb')
    return SkippedEntry.new(source_file: source_file, reason: :test_file) if self.class.test_file?(source_file)
  end

  spec_file = spec_path_for(source_file)
  return SkippedEntry.new(source_file: source_file, expected_spec: spec_file, reason: :no_spec) unless File.exist?(spec_file)

  spec_file
end

#runObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/mutation_tester/batch_runner.rb', line 132

def run
  sources = @files ? @files.uniq : Dir.glob(@glob).select { |path| File.file?(path) }.sort
  if sources.empty?
    puts Rainbow("❌ No source files matched glob: #{@glob}").red if @glob
    return Result.new(processed: [], skipped: [], unchanged: [])
  end

  sources, unchanged = partition_by_change(sources)
  if sources.empty?
    puts Rainbow("✓ Nothing to mutate: none of the #{unchanged.size} matched files changed since #{@since}.").green
    return Result.new(processed: [], skipped: [], unchanged: unchanged)
  end

  processed = []
  skipped = []
  interrupted = false

  sources.each do |source_file|
    outcome = classify(source_file)
    if outcome.is_a?(SkippedEntry)
      skipped << outcome
      next
    end

    entry, interrupted = run_one(source_file, outcome)
    processed << entry
    break if interrupted
  end

  result = Result.new(processed: processed, skipped: skipped, unchanged: unchanged, interrupted: interrupted)
  print_summary(result)
  result
end