diff --git a/lib/hydra/message/worker_messages.rb b/lib/hydra/message/worker_messages.rb index 889176a..a687ba2 100644 --- a/lib/hydra/message/worker_messages.rb +++ b/lib/hydra/message/worker_messages.rb @@ -30,6 +30,7 @@ module Hydra #:nodoc: # Message relaying the results of a worker up to the master class Results < Hydra::Messages::Runner::Results def handle(master, worker) #:nodoc: + $stdout.write output master.send_file(worker) end end diff --git a/lib/hydra/runner.rb b/lib/hydra/runner.rb index 105e17f..c39ba13 100644 --- a/lib/hydra/runner.rb +++ b/lib/hydra/runner.rb @@ -14,10 +14,40 @@ module Hydra #:nodoc: @io = opts.fetch(:io) { raise "No IO Object" } @verbose = opts.fetch(:verbose) { false } + Test::Unit.run = true + @io.write RequestFile.new process_messages end + # Run a test file and report the results + def run_file(file) + require file + output = [] + @result = Test::Unit::TestResult.new + @result.add_listener(Test::Unit::TestResult::FAULT) do |value| + output << value + end + + klasses = Runner.find_classes_in_file(file) + begin + klasses.each{|klass| klass.suite.run(@result){|status, name| ;}} + rescue => ex + output << ex.to_s + end + + output << '.' if output.empty? + + @io.write Results.new(:output => output.join("\n"), :file => file) + end + + # Stop running + def stop + @running = false + end + + private + # The runner will continually read messages and handle them. def process_messages $stdout.write "RUNNER| Processing Messages\n" if @verbose @@ -39,15 +69,26 @@ module Hydra #:nodoc: end end - # Run a test file and report the results - def run_file(file) - `ruby #{file}` - @io.write Results.new(:output => "Finished", :file => file) - end - - # Stop running - def stop - @running = false + def self.find_classes_in_file(f) + code = "" + File.open(f) {|buffer| code = buffer.read} + matches = code.scan(/class\s+([\S]+)/) + klasses = matches.collect do |c| + begin + if c.first.respond_to? :constantize + c.first.constantize + else + eval(c.first) + end + rescue NameError + # $stderr.write "Could not load [#{c.first}] from [#{f}]\n" + nil + rescue SyntaxError + # $stderr.write "Could not load [#{c.first}] from [#{f}]\n" + nil + end + end + return klasses.select{|k| k.respond_to? 'suite'} end end end diff --git a/test/fixtures/write_file.rb b/test/fixtures/write_file.rb index 8c1ad54..8eae699 100644 --- a/test/fixtures/write_file.rb +++ b/test/fixtures/write_file.rb @@ -1,7 +1,7 @@ require File.join(File.dirname(__FILE__), '..', 'test_helper') class WriteFileTest < Test::Unit::TestCase - should "write file" do + def test_write_a_file File.open(File.join(Dir.tmpdir, 'hydra_test.txt'), 'w') do |f| f.write "HYDRA" end diff --git a/test/master_test.rb b/test/master_test.rb index ea381f3..4561a2f 100644 --- a/test/master_test.rb +++ b/test/master_test.rb @@ -3,19 +3,19 @@ require File.join(File.dirname(__FILE__), 'test_helper') class MasterTest < Test::Unit::TestCase context "with a file to test and a destination to verify" do setup do - FileUtils.rm_f(TARGET) + FileUtils.rm_f(target_file) end teardown do - FileUtils.rm_f(TARGET) + FileUtils.rm_f(target_file) end should "run a test" do m = Hydra::Master.new({ - :files => Array(TESTFILE) + :files => Array(test_file) }) - assert File.exists?(TARGET) - assert_equal "HYDRA", File.read(TARGET) + assert File.exists?(target_file) + assert_equal "HYDRA", File.read(target_file) end end end diff --git a/test/runner_test.rb b/test/runner_test.rb index 3d75299..a31244d 100644 --- a/test/runner_test.rb +++ b/test/runner_test.rb @@ -3,11 +3,11 @@ require File.join(File.dirname(__FILE__), 'test_helper') class RunnerTest < Test::Unit::TestCase context "with a file to test and a destination to verify" do setup do - FileUtils.rm_f(TARGET) + FileUtils.rm_f(target_file) end teardown do - FileUtils.rm_f(TARGET) + FileUtils.rm_f(target_file) end @@ -41,7 +41,7 @@ class RunnerTest < Test::Unit::TestCase # make sure it asks for a file, then give it one assert pipe.gets.is_a?(Hydra::Messages::Runner::RequestFile) - pipe.write(Hydra::Messages::Worker::RunFile.new(:file => TESTFILE)) + pipe.write(Hydra::Messages::Worker::RunFile.new(:file => test_file)) # grab its response. This makes us wait for it to finish response = pipe.gets @@ -50,8 +50,8 @@ class RunnerTest < Test::Unit::TestCase pipe.write(Hydra::Messages::Worker::Shutdown.new) # ensure it ran - assert File.exists?(TARGET) - assert_equal "HYDRA", File.read(TARGET) + assert File.exists?(target_file) + assert_equal "HYDRA", File.read(target_file) end def run_the_runner(pipe) diff --git a/test/test_helper.rb b/test/test_helper.rb index 58faa93..82080a2 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -8,10 +8,14 @@ $LOAD_PATH.unshift(File.dirname(__FILE__)) require 'hydra' class Test::Unit::TestCase -end + def target_file + File.join(Dir.tmpdir, 'hydra_test.txt') + end -TARGET = File.join(Dir.tmpdir, 'hydra_test.txt') -TESTFILE = File.join(File.dirname(__FILE__), 'fixtures', 'write_file.rb') + def test_file + File.join(File.dirname(__FILE__), 'fixtures', 'write_file.rb') + end +end module Hydra #:nodoc: module Messages #:nodoc: diff --git a/test/worker_test.rb b/test/worker_test.rb index 4036b2a..aaab7c5 100644 --- a/test/worker_test.rb +++ b/test/worker_test.rb @@ -3,11 +3,11 @@ require File.join(File.dirname(__FILE__), 'test_helper') class WorkerTest < Test::Unit::TestCase context "with a file to test and a destination to verify" do setup do - FileUtils.rm_f(TARGET) + FileUtils.rm_f(target_file) end teardown do - FileUtils.rm_f(TARGET) + FileUtils.rm_f(target_file) end # run the worker in the foreground and the requests in the background @@ -44,14 +44,14 @@ class WorkerTest < Test::Unit::TestCase num_runners.times do assert pipe.gets.is_a?(Hydra::Messages::Worker::RequestFile) end - pipe.write(Hydra::Messages::Master::RunFile.new(:file => TESTFILE)) + pipe.write(Hydra::Messages::Master::RunFile.new(:file => test_file)) assert pipe.gets.is_a?(Hydra::Messages::Worker::Results) pipe.write(Hydra::Messages::Master::Shutdown.new) - assert File.exists?(TARGET) - assert_equal "HYDRA", File.read(TARGET) + assert File.exists?(target_file) + assert_equal "HYDRA", File.read(target_file) end end include WorkerTestHelper