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
class Results < Hydra::Messages::Runner::Results
def handle(master, worker) #:nodoc:
$stdout.write output
master.send_file(worker)
end
end

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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:

View File

@ -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