Skip to content
Browse files

The RSpec 2 upgrade. First pass, tests running under RSpec 1 and 2

  • Loading branch information...
1 parent fc3cef0 commit 423e7b557c1f0a711384516b37d20e4349c2dfe9 @nicksieger nicksieger committed
View
48 Rakefile
@@ -1,6 +1,3 @@
-require 'spec/rake/spectask'
-require 'spec/rake/verify_rcov'
-
MANIFEST = FileList["History.txt", "Manifest.txt", "README.txt", "LICENSE.txt", "Rakefile",
"*.rake", "lib/**/*.rb", "spec/**/*.rb", "tasks/**/*.rake"]
@@ -49,21 +46,36 @@ else
task :default => :rcov
end
-Spec::Rake::SpecTask.new do |t|
- t.spec_opts = ["--diff", "unified"]
+RSpecTask = begin
+ require 'rspec/core/rake_task'
+ @spec_bin = 'rspec'
+ RSpec::Core::RakeTask
+rescue LoadError
+ require 'spec/rake/spectask'
+ @spec_bin = 'spec'
+ Spec::Rake::SpecTask
end
-Spec::Rake::SpecTask.new("spec:rcov") do |t|
- t.rcov_opts << '--exclude gems/*'
+RSpecTask.new do |t|
+end
+
+RSpecTask.new("spec:rcov") do |t|
+ t.rcov_opts = ['--exclude gems/*']
t.rcov = true
end
-# so we don't confuse autotest
-RCov::VerifyTask.new(:rcov) do |t|
- # Can't get threshold up to 100 unless RSpec backwards compatibility
- # code is dropped
- t.threshold = 98
- t.require_exact_threshold = false
+
+begin
+ require 'spec/rake/verify_rcov'
+ # so we don't confuse autotest
+ RCov::VerifyTask.new(:rcov) do |t|
+ # Can't get threshold up to 100 unless RSpec backwards compatibility
+ # code is dropped
+ t.threshold = 98
+ t.require_exact_threshold = false
+ end
+rescue LoadError
end
+
task "spec:rcov" do
rm_f "Manifest.txt"
end
@@ -73,17 +85,17 @@ task :generate_output do
rm_rf "acceptance/reports"
ENV['CI_REPORTS'] = "acceptance/reports"
begin
- `ruby -Ilib acceptance/test_unit_example_test.rb` rescue nil
- `ruby -Ilib -S spec --require ci/reporter/rake/rspec_loader --format CI::Reporter::RSpec acceptance/rspec_example_spec.rb` rescue nil
- `ruby -Ilib -rci/reporter/rake/cucumber_loader -S cucumber --format CI::Reporter::Cucumber acceptance/cucumber` rescue nil
+ `ruby -Ilib -rci/reporter/rake/test_unit_loader acceptance/test_unit_example_test.rb` rescue puts "Warning: #{$!}"
+ `ruby -Ilib -S #{@spec_bin} --require ci/reporter/rake/rspec_loader --format CI::Reporter::RSpec acceptance/rspec_example_spec.rb` rescue puts "Warning: #{$!}"
+ `ruby -Ilib -rci/reporter/rake/cucumber_loader -S cucumber --format CI::Reporter::Cucumber acceptance/cucumber` rescue puts "Warning: #{$!}"
ensure
ENV.delete 'CI_REPORTS'
end
end
task :acceptance => :generate_output
-Spec::Rake::SpecTask.new(:acceptance_spec) do |t|
- t.spec_files = FileList['acceptance/verification_spec.rb']
+RSpecTask.new(:acceptance_spec) do |t|
+ t.pattern = FileList['acceptance/verification_spec.rb']
end
task :acceptance => :acceptance_spec
View
6 acceptance/cucumber/step_definitions/development_steps.rb
@@ -1,4 +1,8 @@
-require 'spec/expectations'
+begin
+ require 'rspec/expectations'
+rescue LoadError
+ require 'spec/expectations'
+end
Given /^that I am a conscientious developer$/ do
end
View
2 acceptance/rspec_example_spec.rb
@@ -5,7 +5,7 @@
end
it "should fail" do
- violated
+ true.should be_false
end
it "should be pending"
View
1 acceptance/test_unit_example_test.rb
@@ -1,5 +1,4 @@
require 'test/unit'
-require 'ci/reporter/rake/test_unit_loader'
class TestUnitExampleTestOne < Test::Unit::TestCase
def test_one
View
4 acceptance/verification_spec.rb
@@ -52,7 +52,7 @@
doc.root.elements.to_a("/testsuite/testcase").size.should == 3
failures = doc.root.elements.to_a("/testsuite/testcase/failure")
failures.size.should == 1
- failures.first.attributes["type"].should == "Spec::Expectations::ExpectationNotMetError"
+ failures.first.attributes["type"].should =~ /ExpectationNotMetError/
end
it "should have one test in the nested example report" do
@@ -90,7 +90,7 @@
it "should have one failure for the lazy hacker" do
failures = @doc.root.elements.to_a("/testsuite/testcase[@name='Lazy hacker']/failure")
failures.size.should == 1
- failures.first.attributes["type"].should == "Spec::Expectations::ExpectationNotMetError"
+ failures.first.attributes["type"].should =~ /ExpectationNotMetError/
end
it "should have one failure for the bad coder" do
View
107 lib/ci/reporter/rspec.rb
@@ -3,26 +3,36 @@
# software license details.
require 'ci/reporter/core'
-tried_gem = false
-begin
- require 'spec'
- require 'spec/runner/formatter/progress_bar_formatter'
- require 'spec/runner/formatter/specdoc_formatter'
-rescue LoadError
- unless tried_gem
- tried_gem = true
- require 'rubygems'
- gem 'rspec'
- retry
- end
-end
module CI
module Reporter
+ module RSpecFormatters
+ begin
+ require 'rspec/core/formatters/base_formatter'
+ require 'rspec/core/formatters/progress_formatter'
+ require 'rspec/core/formatters/documentation_formatter'
+ BaseFormatter = ::RSpec::Core::Formatters::BaseFormatter
+ ProgressFormatter = ::RSpec::Core::Formatters::ProgressFormatter
+ DocFormatter = ::RSpec::Core::Formatters::DocumentationFormatter
+ rescue LoadError => first_error
+ begin
+ require 'spec/runner/formatter/progress_bar_formatter'
+ require 'spec/runner/formatter/specdoc_formatter'
+ BaseFormatter = ::Spec::Runner::Formatter::BaseFormatter
+ ProgressFormatter = ::Spec::Runner::Formatter::ProgressBarFormatter
+ DocFormatter = ::Spec::Runner::Formatter::SpecdocFormatter
+ rescue LoadError
+ raise first_error
+ end
+ end
+ end
+
# Wrapper around a <code>RSpec</code> error or failure to be used by the test suite to interpret results.
class RSpecFailure
+ attr_reader :exception
def initialize(failure)
@failure = failure
+ @exception = failure.exception
end
def failure?
@@ -30,21 +40,32 @@ def failure?
end
def error?
- !@failure.expectation_not_met?
+ !failure?
+ end
+
+ def name() exception.class.name end
+ def message() exception.message end
+ def location() (exception.backtrace || ["No backtrace available"]).join("\n") end
+ end
+
+ class RSpec2Failure < RSpecFailure
+ def initialize(example)
+ @example = example
+ @exception = @example.execution_result[:exception_encountered]
end
- def name() @failure.exception.class.name end
- def message() @failure.exception.message end
- def location() @failure.exception.backtrace.join("\n") end
+ def failure?
+ exception.is_a?(::RSpec::Expectations::ExpectationNotMetError)
+ end
end
# Custom +RSpec+ formatter used to hook into the spec runs and capture results.
- class RSpec < Spec::Runner::Formatter::BaseFormatter
+ class RSpec < RSpecFormatters::BaseFormatter
attr_accessor :report_manager
attr_accessor :formatter
def initialize(*args)
super
- @formatter ||= Spec::Runner::Formatter::ProgressBarFormatter.new(*args)
+ @formatter ||= RSpecFormatters::ProgressFormatter.new(*args)
@report_manager = ReportManager.new("spec")
@suite = nil
end
@@ -62,44 +83,50 @@ def add_behaviour(name)
# Compatibility with rspec < 1.2.4
def add_example_group(example_group)
@formatter.add_example_group(example_group)
- new_suite(example_group.description)
+ new_suite(description_for(example_group))
end
# rspec >= 1.2.4
def example_group_started(example_group)
@formatter.example_group_started(example_group)
- new_suite(example_group.description)
+ new_suite(description_for(example_group))
end
- def example_started(name)
- @formatter.example_started(name)
+ def example_started(name_or_example)
+ @formatter.example_started(name_or_example)
spec = TestCase.new
@suite.testcases << spec
spec.start
end
- def example_failed(name, counter, failure)
- @formatter.example_failed(name, counter, failure)
+ def example_failed(name_or_example, *rest)
+ @formatter.example_failed(name_or_example, *rest)
+
# In case we fail in before(:all)
- if @suite.testcases.empty?
- example_started(name)
+ example_started(name_or_example) if @suite.testcases.empty?
+
+ if name_or_example.respond_to?(:execution_result) # RSpec 2
+ failure = RSpec2Failure.new(name_or_example)
+ else
+ failure = RSpecFailure.new(rest[1]) # example_failed(name, counter, failure) in RSpec 1
end
+
spec = @suite.testcases.last
spec.finish
- spec.name = name.respond_to?(:description) ? name.description : "UNKNOWN"
- spec.failures << RSpecFailure.new(failure)
+ spec.name = description_for(name_or_example)
+ spec.failures << failure
end
- def example_passed(name)
- @formatter.example_passed(name)
+ def example_passed(name_or_example)
+ @formatter.example_passed(name_or_example)
spec = @suite.testcases.last
spec.finish
- spec.name = name.respond_to?(:description) ? name.description : "UNKNOWN"
+ spec.name = description_for(name_or_example)
end
def example_pending(*args)
@formatter.example_pending(*args)
- name = args[0].respond_to?(:description) ? args[0].description : "UNKNOWN"
+ name = description_for(args[0])
spec = @suite.testcases.last
spec.finish
spec.name = "#{name} (PENDING)"
@@ -128,6 +155,18 @@ def close
end
private
+ def description_for(name_or_example)
+ if name_or_example.respond_to?(:full_description)
+ name_or_example.full_description
+ elsif name_or_example.respond_to?(:metadata)
+ name_or_example.metadata[:example_group][:full_description]
+ elsif name_or_example.respond_to?(:description)
+ name_or_example.description
+ else
+ "UNKNOWN"
+ end
+ end
+
def write_report
@suite.finish
@report_manager.write_report(@suite)
@@ -142,7 +181,7 @@ def new_suite(name)
class RSpecDoc < RSpec
def initialize(*args)
- @formatter = Spec::Runner::Formatter::SpecdocFormatter.new(*args)
+ @formatter = RSpecFormatters::DocFormatter.new(*args)
super
end
end
View
7 spec/ci/reporter/rspec_spec.rb
@@ -10,10 +10,11 @@
@error = mock("error")
@error.stub!(:expectation_not_met?).and_return(false)
@error.stub!(:pending_fixed?).and_return(false)
+ @error.stub!(:exception).and_return(StandardError.new)
@report_mgr = mock("report manager")
@options = mock("options")
@args = [@options, StringIO.new("")]
- @args.shift if Spec::VERSION::MAJOR == 1 && Spec::VERSION::MINOR < 1
+ @args.shift unless defined?(::Spec) && ::Spec::VERSION::MAJOR == 1 && ::Spec::VERSION::MINOR >= 1
@fmt = CI::Reporter::RSpec.new *@args
@fmt.report_manager = @report_mgr
@formatter = mock("formatter")
@@ -22,12 +23,12 @@
it "should use a progress bar formatter by default" do
fmt = CI::Reporter::RSpec.new *@args
- fmt.formatter.should be_instance_of(Spec::Runner::Formatter::ProgressBarFormatter)
+ fmt.formatter.should be_instance_of(CI::Reporter::RSpecFormatters::ProgressFormatter)
end
it "should use a specdoc formatter for RSpecDoc" do
fmt = CI::Reporter::RSpecDoc.new *@args
- fmt.formatter.should be_instance_of(Spec::Runner::Formatter::SpecdocFormatter)
+ fmt.formatter.should be_instance_of(CI::Reporter::RSpecFormatters::DocFormatter)
end
it "should create a test suite with one success, one failure, and one pending" do
View
7 spec/spec_helper.rb
@@ -3,8 +3,11 @@
# software license details.
require 'rubygems'
-gem 'rspec'
-require 'spec'
+begin
+ require 'rspec'
+rescue
+ require 'spec'
+end
unless defined?(CI_REPORTER_LIB)
CI_REPORTER_LIB = File.expand_path(File.dirname(__FILE__) + "/../lib")

0 comments on commit 423e7b5

Please sign in to comment.
Something went wrong with that request. Please try again.