2011-08-23 11:01:48 +00:00
|
|
|
require 'test_helper'
|
|
|
|
require 'fixtures/runner_listeners'
|
2010-01-27 16:38:11 +00:00
|
|
|
|
2010-01-28 15:30:18 +00:00
|
|
|
class RunnerTest < Test::Unit::TestCase
|
2010-01-28 16:01:54 +00:00
|
|
|
context "with a file to test and a destination to verify" do
|
2010-01-27 16:38:11 +00:00
|
|
|
setup do
|
2010-02-17 21:28:32 +00:00
|
|
|
sleep(0.2)
|
2010-01-29 19:56:02 +00:00
|
|
|
FileUtils.rm_f(target_file)
|
2010-03-31 15:05:42 +00:00
|
|
|
FileUtils.rm_f(alternate_target_file)
|
2010-01-28 16:01:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
teardown do
|
2010-01-29 19:56:02 +00:00
|
|
|
FileUtils.rm_f(target_file)
|
2010-03-31 15:05:42 +00:00
|
|
|
FileUtils.rm_f(alternate_target_file)
|
2010-01-28 16:01:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2010-02-03 21:03:16 +00:00
|
|
|
should "run a test in the foreground" do
|
2010-01-28 16:01:54 +00:00
|
|
|
# flip it around to the parent is in the fork, this gives
|
|
|
|
# us more direct control over the runner and proper test
|
|
|
|
# coverage output
|
2010-01-28 21:52:11 +00:00
|
|
|
pipe = Hydra::Pipe.new
|
|
|
|
parent = Process.fork do
|
2010-03-30 17:45:10 +00:00
|
|
|
request_a_file_and_verify_completion(pipe, test_file)
|
2010-01-27 16:38:11 +00:00
|
|
|
end
|
2010-01-28 21:52:11 +00:00
|
|
|
run_the_runner(pipe)
|
|
|
|
Process.wait(parent)
|
2010-01-27 16:38:11 +00:00
|
|
|
end
|
2010-01-28 16:01:54 +00:00
|
|
|
|
|
|
|
# this flips the above test, so that the main process runs a bit of the parent
|
|
|
|
# code, but only with minimal assertion
|
2010-02-03 21:03:16 +00:00
|
|
|
should "run a test in the background" do
|
2010-01-28 21:52:11 +00:00
|
|
|
pipe = Hydra::Pipe.new
|
|
|
|
child = Process.fork do
|
|
|
|
run_the_runner(pipe)
|
2010-01-28 16:01:54 +00:00
|
|
|
end
|
2010-03-30 17:45:10 +00:00
|
|
|
request_a_file_and_verify_completion(pipe, test_file)
|
2010-01-28 21:52:11 +00:00
|
|
|
Process.wait(child)
|
2010-01-27 20:57:26 +00:00
|
|
|
end
|
2010-02-03 21:03:16 +00:00
|
|
|
|
2010-05-28 14:38:58 +00:00
|
|
|
should "run a js lint file and find errors" do
|
|
|
|
runner = Hydra::Runner.new(:io => File.new('/dev/null', 'w'))
|
|
|
|
results = runner.run_file(javascript_file)
|
2010-06-18 17:55:48 +00:00
|
|
|
assert results =~ /Missing semicolon/, results
|
2010-05-28 14:38:58 +00:00
|
|
|
end
|
|
|
|
|
2010-05-28 14:42:17 +00:00
|
|
|
should "run a json data file and find errors" do
|
|
|
|
runner = Hydra::Runner.new(:io => File.new('/dev/null', 'w'))
|
|
|
|
results = runner.run_file(json_file)
|
2010-06-18 17:55:48 +00:00
|
|
|
assert results =~ /trailing comma/, results
|
2010-05-28 14:42:17 +00:00
|
|
|
end
|
|
|
|
|
2010-04-04 00:32:00 +00:00
|
|
|
should "run two rspec tests" do
|
2010-03-31 15:05:42 +00:00
|
|
|
runner = Hydra::Runner.new(:io => File.new('/dev/null', 'w'))
|
2010-04-04 00:32:00 +00:00
|
|
|
runner.run_file(rspec_file)
|
2010-03-31 15:05:42 +00:00
|
|
|
assert File.exists?(target_file)
|
|
|
|
assert_equal "HYDRA", File.read(target_file)
|
2010-04-04 00:32:00 +00:00
|
|
|
|
2010-03-31 15:05:42 +00:00
|
|
|
FileUtils.rm_f(target_file)
|
|
|
|
|
2010-04-04 00:32:00 +00:00
|
|
|
runner.run_file(alternate_rspec_file)
|
2010-03-31 15:05:42 +00:00
|
|
|
assert File.exists?(alternate_target_file)
|
|
|
|
assert_equal "HYDRA", File.read(alternate_target_file)
|
2010-08-23 18:42:51 +00:00
|
|
|
assert !File.exists?(target_file), "Tests are double running!"
|
2010-03-30 17:45:10 +00:00
|
|
|
end
|
|
|
|
|
2010-04-20 10:33:28 +00:00
|
|
|
should "run rspec tests with pending examples" do
|
|
|
|
runner = Hydra::Runner.new(:io => File.new('/dev/null', 'w'))
|
|
|
|
assert File.exists?(rspec_file_with_pending)
|
|
|
|
|
|
|
|
runner.run_file(rspec_file_with_pending)
|
|
|
|
|
|
|
|
assert File.exists?(target_file)
|
|
|
|
assert_equal "HYDRA", File.read(target_file)
|
|
|
|
|
|
|
|
FileUtils.rm_f(target_file)
|
|
|
|
end
|
|
|
|
|
2010-04-04 00:32:00 +00:00
|
|
|
should "run two cucumber tests" do
|
|
|
|
# because of all the crap cucumber pulls in
|
|
|
|
# we run this in a fork to not contaminate
|
|
|
|
# the main test environment
|
2011-06-01 21:29:45 +00:00
|
|
|
capture_stderr do # redirect stderr
|
|
|
|
pid = Process.fork do
|
|
|
|
runner = Hydra::Runner.new(:io => File.new('/dev/null', 'w'))
|
|
|
|
runner.run_file(cucumber_feature_file)
|
|
|
|
assert File.exists?(target_file)
|
|
|
|
assert_equal "HYDRA", File.read(target_file)
|
|
|
|
|
|
|
|
FileUtils.rm_f(target_file)
|
|
|
|
|
|
|
|
runner.run_file(alternate_cucumber_feature_file)
|
|
|
|
assert File.exists?(alternate_target_file)
|
|
|
|
assert_equal "HYDRA", File.read(alternate_target_file)
|
|
|
|
assert !File.exists?(target_file)
|
|
|
|
end
|
|
|
|
Process.wait pid
|
2010-04-04 00:32:00 +00:00
|
|
|
end
|
2010-04-03 21:27:27 +00:00
|
|
|
end
|
|
|
|
|
2010-02-03 21:03:16 +00:00
|
|
|
should "be able to run a runner over ssh" do
|
2011-06-02 18:18:54 +00:00
|
|
|
ssh = Hydra::SSH.new(
|
|
|
|
'localhost',
|
|
|
|
File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')),
|
|
|
|
"ruby -e \"require 'rubygems'; require 'hydra'; Hydra::Runner.new(:io => Hydra::Stdio.new, :verbose => true);\""
|
|
|
|
)
|
|
|
|
assert ssh.gets.is_a?(Hydra::Messages::Runner::RequestFile)
|
|
|
|
ssh.write(Hydra::Messages::Worker::RunFile.new(:file => test_file))
|
|
|
|
|
|
|
|
# grab its response. This makes us wait for it to finish
|
|
|
|
echo = ssh.gets # get the ssh echo
|
|
|
|
response = ssh.gets
|
|
|
|
|
|
|
|
assert_equal Hydra::Messages::Runner::Results, response.class
|
|
|
|
|
|
|
|
# tell it to shut down
|
|
|
|
ssh.write(Hydra::Messages::Worker::Shutdown.new)
|
|
|
|
|
|
|
|
ssh.close
|
|
|
|
|
|
|
|
# ensure it ran
|
|
|
|
assert File.exists?(target_file)
|
|
|
|
assert_equal "HYDRA", File.read(target_file)
|
2011-05-30 22:08:50 +00:00
|
|
|
end
|
2010-02-05 20:54:48 +00:00
|
|
|
|
2011-05-30 22:08:50 +00:00
|
|
|
context "using runner events" do
|
2011-06-02 18:18:54 +00:00
|
|
|
context "on successful termination" do
|
|
|
|
setup do
|
|
|
|
@pipe = Hydra::Pipe.new
|
|
|
|
@parent = Process.fork do
|
|
|
|
request_a_file_and_verify_completion(@pipe, test_file)
|
|
|
|
end
|
2011-05-30 22:08:50 +00:00
|
|
|
end
|
2010-02-05 20:54:48 +00:00
|
|
|
|
2011-06-02 18:18:54 +00:00
|
|
|
should "fire runner_begin event" do
|
|
|
|
run_the_runner(@pipe, [HydraExtension::RunnerListener::RunnerBeginTest.new] )
|
|
|
|
Process.wait(@parent)
|
2011-05-30 22:08:50 +00:00
|
|
|
|
2011-06-02 18:18:54 +00:00
|
|
|
# ensure runner_begin was fired
|
2011-06-02 19:02:58 +00:00
|
|
|
assert_file_exists alternate_target_file
|
2011-06-02 18:18:54 +00:00
|
|
|
end
|
2011-05-30 22:08:50 +00:00
|
|
|
|
2011-06-02 19:02:58 +00:00
|
|
|
should "fire runner_end event" do
|
2011-06-02 18:18:54 +00:00
|
|
|
run_the_runner(@pipe, [HydraExtension::RunnerListener::RunnerEndTest.new] )
|
|
|
|
Process.wait(@parent)
|
2011-05-30 22:08:50 +00:00
|
|
|
|
2011-06-02 19:02:58 +00:00
|
|
|
assert_file_exists alternate_target_file
|
2011-06-02 18:18:54 +00:00
|
|
|
end
|
2011-05-30 22:08:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
should "fire runner_end event after losing communication with worker" do
|
|
|
|
pipe = Hydra::Pipe.new
|
|
|
|
parent = Process.fork do
|
|
|
|
pipe.identify_as_parent
|
|
|
|
|
|
|
|
# grab its response.
|
|
|
|
response = pipe.gets
|
|
|
|
pipe.close #this will be detected by the runner and it should call runner_end
|
|
|
|
end
|
|
|
|
|
2011-06-01 16:45:43 +00:00
|
|
|
run_the_runner(pipe, [HydraExtension::RunnerListener::RunnerEndTest.new] )
|
2011-05-30 22:08:50 +00:00
|
|
|
Process.wait(parent)
|
|
|
|
|
2011-06-03 22:51:36 +00:00
|
|
|
# ensure runner_end was fired
|
2011-05-30 22:08:50 +00:00
|
|
|
assert File.exists?( alternate_target_file )
|
|
|
|
end
|
2010-02-03 21:03:16 +00:00
|
|
|
end
|
2010-01-28 16:01:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
module RunnerTestHelper
|
2010-03-30 17:45:10 +00:00
|
|
|
def request_a_file_and_verify_completion(pipe, file)
|
2010-01-28 16:01:54 +00:00
|
|
|
pipe.identify_as_parent
|
|
|
|
|
|
|
|
# make sure it asks for a file, then give it one
|
|
|
|
assert pipe.gets.is_a?(Hydra::Messages::Runner::RequestFile)
|
2010-03-30 17:45:10 +00:00
|
|
|
pipe.write(Hydra::Messages::Worker::RunFile.new(:file => file))
|
2010-01-28 16:01:54 +00:00
|
|
|
|
|
|
|
# grab its response. This makes us wait for it to finish
|
|
|
|
response = pipe.gets
|
|
|
|
|
|
|
|
# tell it to shut down
|
2010-01-29 18:30:25 +00:00
|
|
|
pipe.write(Hydra::Messages::Worker::Shutdown.new)
|
2010-01-28 16:01:54 +00:00
|
|
|
|
|
|
|
# ensure it ran
|
2010-01-29 19:56:02 +00:00
|
|
|
assert File.exists?(target_file)
|
|
|
|
assert_equal "HYDRA", File.read(target_file)
|
2010-01-27 16:38:11 +00:00
|
|
|
end
|
2010-01-27 22:19:32 +00:00
|
|
|
|
2011-05-30 22:08:50 +00:00
|
|
|
def run_the_runner(pipe, listeners = [])
|
2010-01-28 16:01:54 +00:00
|
|
|
pipe.identify_as_child
|
2011-05-30 22:08:50 +00:00
|
|
|
Hydra::Runner.new( :io => pipe, :runner_listeners => listeners )
|
|
|
|
end
|
2010-01-27 16:38:11 +00:00
|
|
|
end
|
2010-01-28 16:01:54 +00:00
|
|
|
include RunnerTestHelper
|
2010-01-27 16:38:11 +00:00
|
|
|
end
|
2010-01-28 16:01:54 +00:00
|
|
|
|