lib/tork/driver.rb in tork-18.2.4 vs lib/tork/driver.rb in tork-19.0.0

- old
+ new

@@ -1,60 +1,92 @@ require 'set' -require 'tork/client' require 'tork/engine' +require 'tork/server' require 'tork/config' module Tork -class Driver < Engine +class Driver < Server + REABSORB_FILE_GREPS = [] + ALL_TEST_FILE_GLOBS = [] + TEST_FILE_GLOBBERS = {} + def initialize super - - @herald = Client::Transceiver.new('tork-herald') do |changed_files| - changed_files.each do |changed_file| - # find and run the tests that correspond to the changed file - visited = Set.new - visitor = lambda do |source_file| - Config.test_file_globbers.each do |regexp, globber| - if regexp =~ source_file and globs = globber.call($~) - Dir[*globs].each do |test_file| - if visited.add? test_file - run_test_file test_file - visitor.call test_file - end - end - end - end - end - visitor.call changed_file - - # reabsorb text execution overhead if overhead files changed - if Config.reabsorb_file_greps.any? {|r| r =~ changed_file } - @client.send [:over, changed_file] - reabsorb_overhead_files - end - end - end - - reabsorb_overhead_files + Tork.config :driver end - def quit - @herald.quit + def loop + @herald = popen('tork-herald') + @engine = popen('tork-engine') super + ensure + pclose @herald + pclose @engine end def run_all_test_files - all_test_files = Dir[*Config.all_test_file_globs] + all_test_files = Dir[*ALL_TEST_FILE_GLOBS] if all_test_files.empty? - warn "#{$0}: There are no test files to run." + tell @client, 'There are no test files to run.' else run_test_files all_test_files end end - def reabsorb_overhead_files - reabsorb_overhead Config.overhead_load_paths, Dir[*Config.overhead_file_globs] + # accept and delegate tork-engine(1) commands + Engine.public_instance_methods(false).each do |name| + unless method_defined? name + define_method name do |*args| + send @engine, [name, *args] + end + end + end + +protected + + def recv client, message + case client + when @engine + send @clients, message # propagate downstream + + when @herald + message.each do |changed_file| + # reabsorb text execution overhead if overhead files changed + overhead_changed = REABSORB_FILE_GREPS.any? do |pattern| + if pattern.kind_of? Regexp + pattern =~ changed_file + else + pattern == changed_file + end + end + + if overhead_changed + send @clients, [:reabsorb, changed_file] + reabsorb_overhead + else + run_test_files find_dependent_test_files(changed_file).to_a + end + end + + else + super + end + end + +private + + def find_dependent_test_files source_file, results=Set.new + TEST_FILE_GLOBBERS.each do |regexp, globber| + if regexp =~ source_file and globs = globber.call($~) + Dir[*globs].each do |dependent_file| + if results.add? dependent_file + find_dependent_test_files dependent_file, results + end + end + end + end + results end end end