run test files via require instead of shelling. output is now bubbled up to the master

This commit is contained in:
Nick Gauthier 2010-01-29 14:56:02 -05:00
parent 7fbb72d0a8
commit 85f279832b
7 changed files with 74 additions and 28 deletions

View File

@ -30,6 +30,7 @@ module Hydra #:nodoc:
# Message relaying the results of a worker up to the master # Message relaying the results of a worker up to the master
class Results < Hydra::Messages::Runner::Results class Results < Hydra::Messages::Runner::Results
def handle(master, worker) #:nodoc: def handle(master, worker) #:nodoc:
$stdout.write output
master.send_file(worker) master.send_file(worker)
end end
end end

View File

@ -14,10 +14,40 @@ module Hydra #:nodoc:
@io = opts.fetch(:io) { raise "No IO Object" } @io = opts.fetch(:io) { raise "No IO Object" }
@verbose = opts.fetch(:verbose) { false } @verbose = opts.fetch(:verbose) { false }
Test::Unit.run = true
@io.write RequestFile.new @io.write RequestFile.new
process_messages process_messages
end 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. # The runner will continually read messages and handle them.
def process_messages def process_messages
$stdout.write "RUNNER| Processing Messages\n" if @verbose $stdout.write "RUNNER| Processing Messages\n" if @verbose
@ -39,15 +69,26 @@ module Hydra #:nodoc:
end end
end end
# Run a test file and report the results def self.find_classes_in_file(f)
def run_file(file) code = ""
`ruby #{file}` File.open(f) {|buffer| code = buffer.read}
@io.write Results.new(:output => "Finished", :file => file) 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 end
rescue NameError
# Stop running # $stderr.write "Could not load [#{c.first}] from [#{f}]\n"
def stop nil
@running = false 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 end
end end

View File

@ -1,7 +1,7 @@
require File.join(File.dirname(__FILE__), '..', 'test_helper') require File.join(File.dirname(__FILE__), '..', 'test_helper')
class WriteFileTest < Test::Unit::TestCase 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| File.open(File.join(Dir.tmpdir, 'hydra_test.txt'), 'w') do |f|
f.write "HYDRA" f.write "HYDRA"
end end

View File

@ -3,19 +3,19 @@ require File.join(File.dirname(__FILE__), 'test_helper')
class MasterTest < Test::Unit::TestCase class MasterTest < Test::Unit::TestCase
context "with a file to test and a destination to verify" do context "with a file to test and a destination to verify" do
setup do setup do
FileUtils.rm_f(TARGET) FileUtils.rm_f(target_file)
end end
teardown do teardown do
FileUtils.rm_f(TARGET) FileUtils.rm_f(target_file)
end end
should "run a test" do should "run a test" do
m = Hydra::Master.new({ m = Hydra::Master.new({
:files => Array(TESTFILE) :files => Array(test_file)
}) })
assert File.exists?(TARGET) assert File.exists?(target_file)
assert_equal "HYDRA", File.read(TARGET) assert_equal "HYDRA", File.read(target_file)
end end
end end
end end

View File

@ -3,11 +3,11 @@ require File.join(File.dirname(__FILE__), 'test_helper')
class RunnerTest < Test::Unit::TestCase class RunnerTest < Test::Unit::TestCase
context "with a file to test and a destination to verify" do context "with a file to test and a destination to verify" do
setup do setup do
FileUtils.rm_f(TARGET) FileUtils.rm_f(target_file)
end end
teardown do teardown do
FileUtils.rm_f(TARGET) FileUtils.rm_f(target_file)
end end
@ -41,7 +41,7 @@ class RunnerTest < Test::Unit::TestCase
# make sure it asks for a file, then give it one # make sure it asks for a file, then give it one
assert pipe.gets.is_a?(Hydra::Messages::Runner::RequestFile) 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 # grab its response. This makes us wait for it to finish
response = pipe.gets response = pipe.gets
@ -50,8 +50,8 @@ class RunnerTest < Test::Unit::TestCase
pipe.write(Hydra::Messages::Worker::Shutdown.new) pipe.write(Hydra::Messages::Worker::Shutdown.new)
# ensure it ran # ensure it ran
assert File.exists?(TARGET) assert File.exists?(target_file)
assert_equal "HYDRA", File.read(TARGET) assert_equal "HYDRA", File.read(target_file)
end end
def run_the_runner(pipe) def run_the_runner(pipe)

View File

@ -8,10 +8,14 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
require 'hydra' require 'hydra'
class Test::Unit::TestCase class Test::Unit::TestCase
def target_file
File.join(Dir.tmpdir, 'hydra_test.txt')
end end
TARGET = File.join(Dir.tmpdir, 'hydra_test.txt') def test_file
TESTFILE = File.join(File.dirname(__FILE__), 'fixtures', 'write_file.rb') File.join(File.dirname(__FILE__), 'fixtures', 'write_file.rb')
end
end
module Hydra #:nodoc: module Hydra #:nodoc:
module Messages #:nodoc: module Messages #:nodoc:

View File

@ -3,11 +3,11 @@ require File.join(File.dirname(__FILE__), 'test_helper')
class WorkerTest < Test::Unit::TestCase class WorkerTest < Test::Unit::TestCase
context "with a file to test and a destination to verify" do context "with a file to test and a destination to verify" do
setup do setup do
FileUtils.rm_f(TARGET) FileUtils.rm_f(target_file)
end end
teardown do teardown do
FileUtils.rm_f(TARGET) FileUtils.rm_f(target_file)
end end
# run the worker in the foreground and the requests in the background # 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 num_runners.times do
assert pipe.gets.is_a?(Hydra::Messages::Worker::RequestFile) assert pipe.gets.is_a?(Hydra::Messages::Worker::RequestFile)
end 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) assert pipe.gets.is_a?(Hydra::Messages::Worker::Results)
pipe.write(Hydra::Messages::Master::Shutdown.new) pipe.write(Hydra::Messages::Master::Shutdown.new)
assert File.exists?(TARGET) assert File.exists?(target_file)
assert_equal "HYDRA", File.read(TARGET) assert_equal "HYDRA", File.read(target_file)
end end
end end
include WorkerTestHelper include WorkerTestHelper