multiple rspec files running!

This commit is contained in:
Nick Gauthier 2010-04-03 20:32:00 -04:00
parent c1c44f89d2
commit 55fb81f5de
6 changed files with 84 additions and 49 deletions

View File

@ -29,6 +29,15 @@ module Hydra #:nodoc:
# the connectivity of the IO # the connectivity of the IO
end end
end end
# The runner forks to run rspec messages
# so that specs don't get rerun. It uses
# this message to report the results. See
# Runner::run_rspec_file.
class RSpecResult < Hydra::Message
# the output of the spec
attr_accessor :output
end
end end
end end
end end

View File

@ -35,10 +35,12 @@ module Hydra #:nodoc:
trace "Running file: #{file}" trace "Running file: #{file}"
output = "" output = ""
if file =~ /.rb$/ if file =~ /_spec.rb$/
output = run_ruby_file(file) output = run_rspec_file(file)
elsif file =~ /.feature$/ elsif file =~ /.feature$/
output = run_cucumber_file(file) output = run_cucumber_file(file)
else
output = run_test_unit_file(file)
end end
output = "." if output == "" output = "." if output == ""
@ -75,15 +77,6 @@ module Hydra #:nodoc:
end end
end end
# Run a ruby file (ending in .rb)
def run_ruby_file(file)
if file =~ /_spec.rb$/
return run_rspec_file(file)
else
return run_test_unit_file(file)
end
end
# Run all the Test::Unit Suites in a ruby file # Run all the Test::Unit Suites in a ruby file
def run_test_unit_file(file) def run_test_unit_file(file)
begin begin
@ -110,20 +103,21 @@ module Hydra #:nodoc:
# run all the Specs in an RSpec file (NOT IMPLEMENTED) # run all the Specs in an RSpec file (NOT IMPLEMENTED)
def run_rspec_file(file) def run_rspec_file(file)
# TODO: # pull in rspec
# 1. do some of this only once, like the requires and stuff
# 2. fork the file loading so that it doesn't get re-run
# 3. test that running two files doesn't re-run the first
# to test 2. above. Like for cucumber
# 4. try this on a real rspec project
begin begin
require 'spec/autorun' require 'spec/autorun'
require 'hydra/spec/hydra_formatter'
rescue LoadError => ex rescue LoadError => ex
return ex.to_s return ex.to_s
end end
# we have to run the rspec test in a sub-process
# this is because rspec runs all the tests that
# have been required, so if we kept requiring the
# files they'd get run over and over
pipe = Hydra::Pipe.new
pid = SafeFork.fork do
pipe.identify_as_child
options = Spec::Runner.options options = Spec::Runner.options
require 'spec/runner/formatter/progress_bar_formatter'
require 'hydra/spec/hydra_formatter'
hydra_output = StringIO.new hydra_output = StringIO.new
options.formatters = [Spec::Runner::Formatter::HydraFormatter.new(options.formatter_options, hydra_output)] options.formatters = [Spec::Runner::Formatter::HydraFormatter.new(options.formatter_options, hydra_output)]
require file require file
@ -131,10 +125,13 @@ module Hydra #:nodoc:
hydra_output.rewind hydra_output.rewind
output = hydra_output.read.chomp output = hydra_output.read.chomp
output = "" if output == "." output = "" if output == "."
pipe.write RSpecResult.new(:output => output)
pipe.close
end
pipe.identify_as_parent
output = pipe.gets
Process.wait pid
return output return output
#return `spec #{File.expand_path(file)}`
end end
# run all the scenarios in a cucumber feature file # run all the scenarios in a cucumber feature file

View File

@ -1,3 +1,5 @@
require 'spec/autorun'
require 'spec/runner/formatter/progress_bar_formatter'
module Spec module Spec
module Runner module Runner
class Options class Options

View File

@ -0,0 +1,9 @@
require 'tmpdir'
context "file writing" do
it "writes to a file" do
File.open(File.join(Dir.tmpdir, 'alternate_hydra_test.txt'), 'a') do |f|
f.write "HYDRA"
end
end
end

View File

@ -37,7 +37,25 @@ class RunnerTest < Test::Unit::TestCase
Process.wait(child) Process.wait(child)
end end
should "run two rspec tests" do
runner = Hydra::Runner.new(:io => File.new('/dev/null', 'w'))
runner.run_file(rspec_file)
assert File.exists?(target_file)
assert_equal "HYDRA", File.read(target_file)
FileUtils.rm_f(target_file)
runner.run_file(alternate_rspec_file)
assert File.exists?(alternate_target_file)
assert_equal "HYDRA", File.read(alternate_target_file)
assert !File.exists?(target_file)
end
should "run two cucumber tests" do 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
pid = Process.fork do
puts "THE FOLLOWING WARNINGS CAN BE IGNORED" puts "THE FOLLOWING WARNINGS CAN BE IGNORED"
puts "It is caused by Cucumber loading all rb files near its features" puts "It is caused by Cucumber loading all rb files near its features"
@ -51,15 +69,11 @@ class RunnerTest < Test::Unit::TestCase
runner.run_file(alternate_cucumber_feature_file) runner.run_file(alternate_cucumber_feature_file)
assert File.exists?(alternate_target_file) assert File.exists?(alternate_target_file)
assert_equal "HYDRA", File.read(alternate_target_file) assert_equal "HYDRA", File.read(alternate_target_file)
assert !File.exists?(target_file)
puts "END IGNORABLE OUTPUT" puts "END IGNORABLE OUTPUT"
end end
Process.wait pid
should "run an rspec test" do
runner = Hydra::Runner.new(:io => File.new('/dev/null', 'w'))
runner.run_file(rspec_file)
assert File.exists?(target_file)
assert_equal "HYDRA", File.read(target_file)
end end
should "be able to run a runner over ssh" do should "be able to run a runner over ssh" do

View File

@ -27,6 +27,10 @@ class Test::Unit::TestCase
File.expand_path(File.join(File.dirname(__FILE__), 'fixtures', 'write_file_spec.rb')) File.expand_path(File.join(File.dirname(__FILE__), 'fixtures', 'write_file_spec.rb'))
end end
def alternate_rspec_file
File.expand_path(File.join(File.dirname(__FILE__), 'fixtures', 'write_file_alternate_spec.rb'))
end
def cucumber_feature_file def cucumber_feature_file
File.expand_path(File.join(File.dirname(__FILE__), 'fixtures', 'features', 'write_file.feature')) File.expand_path(File.join(File.dirname(__FILE__), 'fixtures', 'features', 'write_file.feature'))
end end