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,31 +103,35 @@ 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
options = Spec::Runner.options # we have to run the rspec test in a sub-process
require 'spec/runner/formatter/progress_bar_formatter' # this is because rspec runs all the tests that
require 'hydra/spec/hydra_formatter' # have been required, so if we kept requiring the
hydra_output = StringIO.new # files they'd get run over and over
options.formatters = [Spec::Runner::Formatter::HydraFormatter.new(options.formatter_options, hydra_output)] pipe = Hydra::Pipe.new
require file pid = SafeFork.fork do
options.run_examples pipe.identify_as_child
hydra_output.rewind options = Spec::Runner.options
output = hydra_output.read.chomp hydra_output = StringIO.new
output = "" if output == "." options.formatters = [Spec::Runner::Formatter::HydraFormatter.new(options.formatter_options, hydra_output)]
require file
options.run_examples
hydra_output.rewind
output = hydra_output.read.chomp
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,29 +37,43 @@ class RunnerTest < Test::Unit::TestCase
Process.wait(child) Process.wait(child)
end end
should "run two cucumber tests" do should "run two rspec tests" do
puts "THE FOLLOWING WARNINGS CAN BE IGNORED"
puts "It is caused by Cucumber loading all rb files near its features"
runner = Hydra::Runner.new(:io => File.new('/dev/null', 'w')) runner = Hydra::Runner.new(:io => File.new('/dev/null', 'w'))
runner.run_file(cucumber_feature_file) runner.run_file(rspec_file)
assert File.exists?(target_file) assert File.exists?(target_file)
assert_equal "HYDRA", File.read(target_file) assert_equal "HYDRA", File.read(target_file)
FileUtils.rm_f(target_file) FileUtils.rm_f(target_file)
runner.run_file(alternate_cucumber_feature_file) runner.run_file(alternate_rspec_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"
end end
should "run an rspec test" do should "run two cucumber tests" do
runner = Hydra::Runner.new(:io => File.new('/dev/null', 'w')) # because of all the crap cucumber pulls in
runner.run_file(rspec_file) # we run this in a fork to not contaminate
assert File.exists?(target_file) # the main test environment
assert_equal "HYDRA", File.read(target_file) pid = Process.fork do
puts "THE FOLLOWING WARNINGS CAN BE IGNORED"
puts "It is caused by Cucumber loading all rb files near its features"
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)
puts "END IGNORABLE OUTPUT"
end
Process.wait pid
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