Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 28d371d5b2
Fetching contributors…

Cannot retrieve contributors at this time

file 134 lines (113 sloc) 3.232 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
# (c) Copyright 2006-2007 Nick Sieger <nicksieger@gmail.com>
# See the file LICENSE.txt included with the distribution for
# 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
    # Wrapper around a <code>RSpec</code> error or failure to be used by the test suite to interpret results.
    class RSpecFailure
      def initialize(failure)
        @failure = failure
      end

      def failure?
        @failure.expectation_not_met?
      end

      def error?
        !@failure.expectation_not_met?
      end

      def name() @failure.exception.class.name end
      def message() @failure.exception.message end
      def location() @failure.exception.backtrace.join("\n") end
    end

    # Custom +RSpec+ formatter used to hook into the spec runs and capture results.
    class RSpec < Spec::Runner::Formatter::BaseFormatter
      attr_accessor :report_manager
      attr_accessor :formatter
      def initialize(*args)
        super
        @formatter ||= Spec::Runner::Formatter::ProgressBarFormatter.new(*args)
        @report_manager = ReportManager.new("spec")
        @suite = nil
      end

      def start(spec_count)
        @formatter.start(spec_count)
      end

      def add_behaviour(name)
        @formatter.add_behaviour(name)
        new_suite(name)
      end

      def add_example_group(example_group)
        @formatter.add_example_group(example_group)
        new_suite(example_group.description)
      end

      def example_started(name)
        @formatter.example_started(name)
        spec = TestCase.new name
        @suite.testcases << spec
        spec.start
      end

      def example_failed(name, counter, failure)
        @formatter.example_failed(name, counter, failure)
        spec = @suite.testcases.last
        spec.finish
        spec.failures << RSpecFailure.new(failure)
      end

      def example_passed(name)
        @formatter.example_passed(name)
        spec = @suite.testcases.last
        spec.finish
      end

      def example_pending(*args)
        @formatter.example_pending(*args)
        spec = @suite.testcases.last
        spec.finish
        spec.name = "#{spec.name} (PENDING)"
      end

      def start_dump
        @formatter.start_dump
      end

      def dump_failure(*args)
        @formatter.dump_failure(*args)
      end

      def dump_summary(*args)
        @formatter.dump_summary(*args)
        write_report
      end

      def dump_pending
        @formatter.dump_pending
      end

      def close
        @formatter.close
      end

      private
      def write_report
        @suite.finish
        @report_manager.write_report(@suite)
      end

      def new_suite(name)
        write_report if @suite
        @suite = TestSuite.new name
        @suite.start
      end
    end

    class RSpecDoc < RSpec
      def initialize(*args)
        @formatter = Spec::Runner::Formatter::SpecdocFormatter.new(*args)
        super
      end
    end
  end
end
Something went wrong with that request. Please try again.