Skip to content

Commit

Permalink
Use only one runner
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexB52 committed Nov 24, 2024
1 parent 0235e6f commit 43598b5
Show file tree
Hide file tree
Showing 19 changed files with 286 additions and 507 deletions.
4 changes: 2 additions & 2 deletions exe/retest
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ end
prompt = Retest::Prompt.new
repository = Retest::Repository.new(files: Retest::VersionControl.files, prompt: prompt)
command = Retest::Command.for_options(options)
runner = Retest::Runners.runner_for(command)
runner = Retest::Runner.new(command)
sounds = Retest::Sounds.for(options)

# All test runner
all_test_command = Retest::Command.for_options(options.merge(%w[--all]), stdout: nil)
all_test_runner = Retest::Runners.runner_for(all_test_command.to_s)
all_test_runner = Retest::Runner.new(all_test_command)
all_test_runner.add_observer(sounds)

sounds.play(:start)
Expand Down
2 changes: 1 addition & 1 deletion lib/retest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
require 'observer'

require "retest/version"
require "retest/runners"
require "retest/runner"
require "retest/repository"
require "retest/matching_options"
require "retest/options"
Expand Down
86 changes: 86 additions & 0 deletions lib/retest/runner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
require_relative "runner/cached_test_file"

module Retest
class Runner
include Observable
include CachedTestFile

attr_accessor :command, :stdout
def initialize(command, stdout: $stdout)
@stdout = stdout
@command = command
end

def ==(obj)
return false unless obj.command

command.to_s == obj.command.to_s && self.class == obj.class
end

def run(changed_files: [], test_files: [])
changed_file = changed_files.is_a?(Array) ? changed_files.first : changed_files
test_file = test_files.is_a?(Array) ? test_files.first : test_files

self.cached_test_file = test_file

if command.to_s.include?('<changed>')
if changed_file.nil?
print_changed_file_not_found
return
end
log("Changed File Selected: #{changed_file}")
end

if command.to_s.include?('<test>')
if cached_test_file.nil?
print_test_file_not_found
return
end
log("Test File Selected: #{cached_test_file}")
end

system_run command.to_s
.gsub('<test>', cached_test_file.to_s)
.gsub('<changed>', changed_file.to_s)
end

def run_all_tests(tests_string)
log("Test Files Selected: #{tests_string}")
system_run command.to_s.gsub('<test>', tests_string)
end

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

def running?
@running
end

private

def system_run(command)
@running = true
result = system(command) ? :tests_pass : :tests_fail
changed
notify_observers(result)
@running = false
end

def log(message)
stdout.puts(message)
end

def print_changed_file_not_found
log(<<~ERROR)
FileNotFound - Retest could not find a changed file to run.
ERROR
end

def print_test_file_not_found
log(<<~ERROR)
FileNotFound - Retest could not find a matching test file to run.
ERROR
end
end
end
21 changes: 21 additions & 0 deletions lib/retest/runner/cached_test_file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Retest
module CachedTestFile
def cached_test_file
@cached_test_file
end

def cached_test_file=(value)
@cached_test_file = value || @cached_test_file
end

def purge_test_file(purged)
return if purged.empty?

if purged.is_a?(Array) && purged.include?(cached_test_file)
@cached_test_file = nil
elsif purged.is_a?(String) && purged == cached_test_file
@cached_test_file = nil
end
end
end
end
23 changes: 0 additions & 23 deletions lib/retest/runners.rb

This file was deleted.

23 changes: 0 additions & 23 deletions lib/retest/runners/cached_test_file.rb

This file was deleted.

24 changes: 0 additions & 24 deletions lib/retest/runners/change_runner.rb

This file was deleted.

48 changes: 0 additions & 48 deletions lib/retest/runners/runner.rb

This file was deleted.

42 changes: 0 additions & 42 deletions lib/retest/runners/test_runner.rb

This file was deleted.

42 changes: 0 additions & 42 deletions lib/retest/runners/variable_runner.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Retest
module Runners
class Runner
module OversableRunnerTests
def test_publishes_event_after_running_command
observer = MiniTest::Mock.new
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Retest
module Runners
class Runner
module RunnerInterfaceTest
def test_behaviour
assert_respond_to @subject, :==
Expand Down
Loading

0 comments on commit 43598b5

Please sign in to comment.