Skip to content

Commit

Permalink
- Major refactoring -- split out reporting core from RSpec
Browse files Browse the repository at this point in the history
  so we can write a JUnit reporter also



git-svn-id: http://svn.caldersphere.net/svn/main/ci_reporter@53 b03c2d0b-2f10-0410-a2f9-fc8001506dfa
  • Loading branch information
nicksieger committed Jan 4, 2007
1 parent 63fb705 commit b8a4915
Show file tree
Hide file tree
Showing 14 changed files with 314 additions and 125 deletions.
4 changes: 1 addition & 3 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@ require 'spec/rake/spectask'

task :default => :spec

Spec::Rake::SpecTask.new do |t|
t.spec_files = FileList["spec/#{ENV['SPECS']}"]
end
Spec::Rake::SpecTask.new
2 changes: 2 additions & 0 deletions lib/ci/reporter/core.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require 'ci/reporter/test_suite'
require 'ci/reporter/report_manager'
19 changes: 19 additions & 0 deletions lib/ci/reporter/report_manager.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'fileutils'

module CI
module Reporter
class ReportManager
def initialize(prefix="test")
@basedir = ENV['CI_REPORTS'] || File.expand_path("#{Dir.getwd}/#{prefix.downcase}/reports")
@basename = "#{@basedir}/#{prefix.upcase}"
FileUtils.mkdir_p(@basedir)
end

def write_report(suite)
File.open("#{@basename}-#{suite.name.gsub(/[^a-zA-Z0-9]+/, '-')}.xml", "w") do |f|
f << suite.to_xml
end
end
end
end
end
81 changes: 81 additions & 0 deletions lib/ci/reporter/rspec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
require 'ci/reporter/core'

module CI
module Reporter
class RSpecFailure
def initialize(failure)
@failure = failure
end

def failure?
@failure.expectation_not_met?
end

def error?
!@failure.expectation_not_met?
end

def exception
@failure.exception
end
end

class RSpec < Spec::Runner::Formatter::ProgressBarFormatter
def initialize(output, dry_run=false, colour=false, report_mgr=nil)
super(output, dry_run, colour)
@report_manager = report_mgr || ReportManager.new("spec")
@suite = nil
end

def start(spec_count)
super
end

def add_context(name, first)
super
write_report if @suite
@suite = TestSuite.new name
@suite.start
end

def spec_started(name)
super
spec = TestCase.new name
@suite.testcases << spec
spec.start
end

def spec_failed(name, counter, failure)
super
spec = @suite.testcases.last
spec.finish
spec.failure = RSpecFailure.new(failure)
end

def spec_passed(name)
super
spec = @suite.testcases.last
spec.finish
end

def start_dump
super
end

def dump_failure(counter, failure)
super
end

def dump_summary(duration, spec_count, failure_count)
super
write_report
end

private
def write_report
@suite.finish
@report_manager.write_report(@suite)
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
require 'fileutils'

module RSpec
class JUnitReportFormatter < Spec::Runner::Formatter::ProgressBarFormatter
class Context < Struct.new(:name, :tests, :time, :failures, :errors)
module CI
module Reporter
class TestSuite < Struct.new(:name, :tests, :time, :failures, :errors)
attr_accessor :testcases
def initialize(name)
super
Expand All @@ -28,7 +26,7 @@ def create_builder
require_gem 'activesupport'
require 'active_support'
rescue
raise LoadError, "XML Builder is required for the JUnitReportFormatter"
raise LoadError, "XML Builder is required by CI::Reporter"
end
end unless defined?(Builder::XmlMarkup)
Builder::XmlMarkup.new(:indent => 2)
Expand All @@ -47,7 +45,7 @@ def to_xml
end
end

class Spec < Struct.new(:name, :time)
class TestCase < Struct.new(:name, :time)
attr_accessor :failure

def start
Expand All @@ -59,11 +57,11 @@ def finish
end

def failure?
failure && failure.expectation_not_met?
failure && failure.failure?
end

def error?
failure && !failure.expectation_not_met?
failure && failure.error?
end

def to_xml(builder)
Expand All @@ -79,65 +77,5 @@ def to_xml(builder)
end
end
end

def initialize(output, dry_run=false, colour=false)
super
@basedir = File.expand_path("#{Dir.getwd}/spec/reports")
@basename = "#{@basedir}/SPEC"
FileUtils.mkdir_p(@basedir)
@context = nil
end

def start(spec_count)
super
end

def add_context(name, first)
super
write_report if @context
@context = Context.new name
@context.start
end

def spec_started(name)
super
spec = Spec.new name
@context.testcases << spec
spec.start
end

def spec_failed(name, counter, failure)
super
spec = @context.testcases.last
spec.finish
spec.failure = failure
end

def spec_passed(name)
super
spec = @context.testcases.last
spec.finish
end

def start_dump
super
end

def dump_failure(counter, failure)
super
end

def dump_summary(duration, spec_count, failure_count)
super
write_report
end

private
def write_report
@context.finish
File.open("#{@basename}-#{@context.name.gsub(/[^a-zA-Z0-9]+/, '-')}.xml", "w") do |f|
f << @context.to_xml
end
end
end
end
21 changes: 21 additions & 0 deletions lib/ci/reporter/test_unit.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module CI
module Reporter
class TestUnitFailure
def failure?

end

def error?

end

def exception

end
end

class TestUnit

end
end
end
35 changes: 35 additions & 0 deletions spec/ci/reporter/report_manager_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require File.dirname(__FILE__) + "/../../spec_helper.rb"

context "The ReportManager" do
setup do
@reports_dir = REPORTS_DIR
end

teardown do
FileUtils.rm_rf @reports_dir
ENV["CI_REPORTS"] = nil
end

specify "should create the report directory according to the given prefix" do
CI::Reporter::ReportManager.new("spec")
File.directory?(@reports_dir).should_be true
end

specify "should create the report directory based on CI_REPORTS environment variable if set" do
@reports_dir = "#{Dir.getwd}/dummy"
ENV["CI_REPORTS"] = @reports_dir
CI::Reporter::ReportManager.new("spec")
File.directory?(@reports_dir).should_be true
end

specify "should write reports based on name and xml content of a test suite" do
reporter = CI::Reporter::ReportManager.new("spec")
suite = mock("test suite")
suite.should_receive(:name).and_return("some test suite name")
suite.should_receive(:to_xml).and_return("<xml></xml>")
reporter.write_report(suite)
filename = "#{REPORTS_DIR}/SPEC-some-test-suite-name.xml"
File.exist?(filename).should_be true
File.open(filename) {|f| f.read.should == "<xml></xml>"}
end
end
35 changes: 35 additions & 0 deletions spec/ci/reporter/rspec_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require File.dirname(__FILE__) + "/../../spec_helper.rb"
require 'stringio'

context "The RSpec reporter" do
setup do
@error = mock("error")
@error.stub!(:exception).and_return do
begin
raise StandardError, "error message"
rescue => e
e
end
end
@error.stub!(:expectation_not_met?).and_return(false)
@report_mgr = mock("report manager")
@fmt = CI::Reporter::RSpec.new(StringIO.new(""), false, false, @report_mgr)
end

specify "should create a test suite with one success and one failure" do
@report_mgr.should_receive(:write_report).and_return do |suite|
suite.testcases.length.should == 2
suite.testcases.first.should_not_be_failure
suite.testcases.first.should_not_be_error
suite.testcases.last.should_be_error
end

@fmt.start(2)
@fmt.add_context("A context", true)
@fmt.spec_started("should pass")
@fmt.spec_passed("should pass")
@fmt.spec_started("should fail")
@fmt.spec_failed("should fail", 1, @error)
@fmt.dump_summary(0.1, 2, 1)
end
end
Loading

0 comments on commit b8a4915

Please sign in to comment.