Class: AIRefactor::Refactors::Minitest::WriteTestForClass

Inherits:
BaseRefactor
  • Object
show all
Defined in:
lib/ai_refactor/refactors/minitest/write_test_for_class.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



41
42
43
# File 'lib/ai_refactor/refactors/minitest/write_test_for_class.rb', line 41

def self.description
  "Write a minitest test for a class"
end

Instance Method Details

#default_output_pathObject



45
46
47
# File 'lib/ai_refactor/refactors/minitest/write_test_for_class.rb', line 45

def default_output_path
  File.join("test", input_file.gsub(/\.rb$/, "_test.rb"))
end

#runObject



7
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
# File 'lib/ai_refactor/refactors/minitest/write_test_for_class.rb', line 7

def run
  logger.verbose "'Write minitest test' refactor for #{input_file}..."
  logger.verbose "Write output to #{output_file_path}..." if output_file_path

  begin
    output_content = process!
  rescue => e
    logger.error "Failed to process #{input_file}: #{e.message}"
    return false
  end

  return false unless output_file_path

  logger.verbose "Generated #{output_file_path} from #{input_file} ..." if output_content

  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 "#{input_file} was translated to #{output_file_path} but the resulting test is failing..."
    logger.error "Failed to run test, exited with status #{test_run.exitstatus}. Stdout: #{test_run.stdout}\n\nStderr: #{test_run.stderr}\n\n"
    logger.error "New test failed!", bold: true
    self.failed_message = "Generated test file failed to run correctly"
    return false
  end

  logger.verbose "\nNew test file ran and returned the following results:"
  logger.verbose ">> Runs: #{test_run.example_count}, Failures: #{test_run.failure_count}, Skips: #{test_run.pending_count}\n"

  output_file_path ? true : output_content
end