Class: AIRefactor::Refactors::Rails::Minitest::RspecToMinitest

Inherits:
BaseRefactor
  • Object
show all
Defined in:
lib/ai_refactor/refactors/rails/minitest/rspec_to_minitest.rb

Instance Attribute Summary

Attributes inherited from BaseRefactor

#ai_client, #failed_message, #input_content, #input_file, #logger, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseRefactor

command_line_options, inherited, #initialize, refactor_name, takes_input_files?

Constructor Details

This class inherits a constructor from AIRefactor::Refactors::BaseRefactor

Class Method Details

.descriptionObject



66
67
68
# File 'lib/ai_refactor/refactors/rails/minitest/rspec_to_minitest.rb', line 66

def self.description
  "Convert RSpec file to Minitest (for Rails apps)"
end

Instance Method Details

#default_output_pathObject



70
71
72
# File 'lib/ai_refactor/refactors/rails/minitest/rspec_to_minitest.rb', line 70

def default_output_path
  input_file.gsub("_spec.rb", "_test.rb").gsub("spec/", "test/")
end

#runObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ai_refactor/refactors/rails/minitest/rspec_to_minitest.rb', line 8

def run
  spec_runner = AIRefactor::TestRunners::RSpecRunner.new(input_file, command_template: options.rspec_run_command)
  logger.verbose "Run spec #{input_file}... (#{spec_runner.command})"

  spec_run = spec_runner.run

  if spec_run.failed?
    logger.warn "Skipping #{input_file}..."
    logger.error "Failed to run #{input_file}, exited with status #{spec_run.exitstatus}. Stdout: #{spec_run.stdout}\n\nStderr: #{spec_run.stderr}\n\n"
    self.failed_message = "Failed to run RSpec file, has errors"
    return false
  end

  logger.debug "\nOriginal test run results:"
  logger.debug ">> Examples: #{spec_run.example_count}, Failures: #{spec_run.failure_count}, Pendings: #{spec_run.pending_count}\n"

  begin
    result = process!
  rescue AIRefactor::NoOutputError => _e
    return false
  rescue => e
    logger.error "Failed to convert #{input_file} to Minitest, error: #{e.message}"
    return false
  end

  logger.verbose "Converted #{input_file} to #{output_file_path}..." if result

  minitest_runner = AIRefactor::TestRunners::MinitestRunner.new(output_file_path, command_template: options.minitest_run_command)

  logger.verbose "Run generated test file #{output_file_path} (#{minitest_runner.command})..."
  test_run = minitest_runner.run

  if test_run.failed?
    logger.warn "Skipping #{input_file}..."
    logger.error "Failed to run translated #{output_file_path}, exited with status #{test_run.exitstatus}. Stdout: #{test_run.stdout}\n\nStderr: #{test_run.stderr}\n\n"
    logger.error "Conversion failed!", bold: true
    self.failed_message = "Generated test file failed to run correctly"
    return false
  end

  logger.debug "\nTranslated test file results:"
  logger.debug ">> Runs: #{test_run.example_count}, Failures: #{test_run.failure_count}, Skips: #{test_run.pending_count}\n"

  report = AIRefactor::TestRunners::TestRunDiffReport.new(spec_run, test_run)

  if report.no_differences?
    logger.verbose "Done converting #{input_file} to #{output_file_path}..."
    logger.success "\nNo differences found! Conversion worked!"
    true
  else
    logger.warn report.diff.colorize(:yellow)
    logger.verbose "Done converting #{input_file} to #{output_file_path}..."
    logger.error "\nDifferences found! Conversion failed!", bold: true
    self.failed_message = "Generated test file run output did not match original RSpec spec run output"
    false
  end
end