Class: Retest::Runner

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Observable, CachedTestFile
Defined in:
lib/retest/runner.rb

Instance Attribute Summary collapse

Attributes included from CachedTestFile

#cached_test_file

Instance Method Summary collapse

Methods included from CachedTestFile

#purge_test_file

Constructor Details

#initialize(command, stdout: $stdout) ⇒ Runner

Returns a new instance of Runner.



13
14
15
16
17
18
19
# File 'lib/retest/runner.rb', line 13

def initialize(command, stdout: $stdout)
  @stdout  = stdout
  @command = command
  if command.hardcoded?
    self.last_command = command.to_s
  end
end

Instance Attribute Details

#commandObject

Returns the value of attribute command.



12
13
14
# File 'lib/retest/runner.rb', line 12

def command
  @command
end

#last_commandObject

Returns the value of attribute last_command.



12
13
14
# File 'lib/retest/runner.rb', line 12

def last_command
  @last_command
end

#stdoutObject

Returns the value of attribute stdout.



12
13
14
# File 'lib/retest/runner.rb', line 12

def stdout
  @stdout
end

Instance Method Details

#format_changed_files(instruction:, files:) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/retest/runner.rb', line 81

def format_changed_files(instruction:, files:)
  return instruction unless has_changed?
  changed_file = files.first

  if changed_file.nil?
    raise FileNotFound, "Retest could not find a changed file to run."
  end

  log("Changed file: #{changed_file}")
  instruction.gsub('<changed>', changed_file)
end

#format_instruction(changed_files: [], test_files: []) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/retest/runner.rb', line 53

def format_instruction(changed_files: [], test_files: [])
  if changed_files.empty? && test_files.size >= 1
    new_command = command.switch_to(:batched)

    log("\nTests selected:")
    test_files.each { |test_file| log("  - #{test_file}") }

    return new_command.to_s.gsub('<test>', new_command.format_batch(*test_files))
  end

  instruction = command.to_s
  instruction = format_changed_files(instruction: instruction, files: changed_files)
  instruction = format_test_files(instruction: instruction, files: test_files)
end

#format_test_files(instruction:, files:) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/retest/runner.rb', line 68

def format_test_files(instruction:, files:)
  return instruction unless has_test?

  self.cached_test_file = files.first

  if cached_test_file.nil?
    raise FileNotFound, "Retest could not find a matching test file to run."
  end

  log("Test file: #{cached_test_file}")
  instruction.gsub('<test>', cached_test_file)
end

#interrupt_runObject



21
22
23
24
25
26
27
# File 'lib/retest/runner.rb', line 21

def interrupt_run
  return false unless @pid

  Process.kill('TERM', @pid)
rescue Errno::ESRCH
  false
end

#run(changed_files: [], test_files: []) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/retest/runner.rb', line 37

def run(changed_files: [], test_files: [])
  self.last_command = format_instruction(changed_files: changed_files, test_files: test_files)
  run_last_command
rescue FileNotFound => e
  log("FileNotFound - #{e.message}")
rescue Command::MultipleTestsNotSupported => e
  log("Command::MultipleTestsNotSupported - #{e.message}")
end

#run_allObject



46
47
48
49
50
51
# File 'lib/retest/runner.rb', line 46

def run_all
  self.last_command = command.switch_to(:all).to_s
  run_last_command
rescue Command::AllTestsNotSupported => e
  log("Command::AllTestsNotSupported - #{e.message}")
end

#run_last_commandObject



29
30
31
32
33
34
35
# File 'lib/retest/runner.rb', line 29

def run_last_command
  unless last_command
    return log('Error - Not enough information to run a command. Please trigger a run first.')
  end

  system_run last_command
end

#sync(added:, removed:) ⇒ Object



93
94
95
# File 'lib/retest/runner.rb', line 93

def sync(added:, removed:)
  purge_test_file(removed)
end