Class: MutationTester::BatchRunner
- Inherits:
-
Object
- Object
- MutationTester::BatchRunner
- 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
- SPEC_MAP_SEPARATOR =
'=>'.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
- .changed_files_since(revision, dir: Dir.pwd) ⇒ Object
- .parse_spec_map(rule) ⇒ Object
- .staged_files(dir: Dir.pwd) ⇒ Object
- .test_file?(path) ⇒ Boolean
Instance Method Summary collapse
- #classify(source_file) ⇒ Object
-
#initialize(glob: nil, files: nil, spec_template: nil, spec_map: nil, config: MutationTester.configuration, since: nil, changed_files: nil) ⇒ BatchRunner
constructor
A new instance of BatchRunner.
- #run ⇒ Object
Constructor Details
#initialize(glob: nil, files: nil, spec_template: nil, spec_map: nil, config: MutationTester.configuration, since: nil, changed_files: nil) ⇒ BatchRunner
Returns a new instance of BatchRunner.
153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/mutation_tester/batch_runner.rb', line 153 def initialize(glob: nil, files: nil, spec_template: nil, spec_map: 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 @spec_map = spec_map || [] @config = config @since = since @changed_files = changed_files end |
Class Method Details
.changed_files_since(revision, dir: Dir.pwd) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/mutation_tester/batch_runner.rb', line 79 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.(path, toplevel) } .to_set end |
.parse_spec_map(rule) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/mutation_tester/batch_runner.rb', line 117 def self.parse_spec_map(rule) pattern, replacement = rule.to_s.split(SPEC_MAP_SEPARATOR, 2) if replacement.nil? raise MutationTester::Error, "--spec-map #{rule.to_s.inspect} has no #{SPEC_MAP_SEPARATOR} separator; " \ "write it as 'PATTERN#{SPEC_MAP_SEPARATOR}REPLACEMENT'" end if pattern.empty? raise MutationTester::Error, "--spec-map #{rule.to_s.inspect} has an empty pattern" end begin [Regexp.new(pattern), replacement] rescue RegexpError => e raise MutationTester::Error, "--spec-map pattern #{pattern.inspect} is not a valid regular expression: #{e.}" end end |
.staged_files(dir: Dir.pwd) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/mutation_tester/batch_runner.rb', line 102 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.(path, toplevel)).relative_path_from(base).to_s } end |
.test_file?(path) ⇒ Boolean
135 136 137 138 139 140 141 |
# File 'lib/mutation_tester/batch_runner.rb', line 135 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
199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/mutation_tester/batch_runner.rb', line 199 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 |
#run ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/mutation_tester/batch_runner.rb', line 165 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 |