module CrudeMutant

Constants

VERSION

Public Class Methods

start(file_path, test_command, section: 0, total_sections: 1, result_printer: :standard, &block) click to toggle source
# File lib/crude_mutant.rb, line 21
def start(file_path, test_command, section: 0, total_sections: 1, result_printer: :standard, &block)
  printer_klass = result_printer == :json ? JsonResultPrinter : ResultPrinter

  start_time = Time.now.to_f
  original_file_contents = File.read(file_path)
  permuter = LinePermuter.new(original_file_contents)

  permutations_to_run = PermutationSelector.select(
    number_of_permutations: permuter.number_of_permutations,
    number_of_sections: total_sections,
    section_number: section,
  )

  initial_success = Executor.call(test_command)

  if !initial_success
    raise NeutralCaseError, 'Initial test run did not succeed'
  end

  begin
    test_runs = permutations_to_run.reduce([]) do |acc, permutation|
      success, bench = perform_run(
        file_path: file_path,
        file_contents: permuter.take(permutation),
        command: test_command,
      )

      result = [RunResult.new(
        file_path,
        permutation,
        success,
        permuter.line(permutation),
        bench,
      )]

      if block_given?
        block.call(
          Progress.new(
            permutations_to_run.size,
            acc + result
          )
        )
      end

      acc + result
    end
  ensure
    File.write(file_path, original_file_contents)
  end

  stop_time = Time.now.to_f
  total_time = stop_time - start_time
  printer_klass.call(
    Result.new(file_path, test_runs, total_time)
  )
end

Private Class Methods

perform_run(file_path:, file_contents:, command:) click to toggle source
# File lib/crude_mutant.rb, line 80
def perform_run(file_path:, file_contents:, command:)
  success = false
  bench = Benchmark.measure do
    File.write(file_path, file_contents)

    success = Executor.call(command)
  end

  [success, bench]
end