uchoaaa (owner)

Revisions

gist: 88891 Download_button fork
public
Public Clone URL: git://gist.github.com/88891.git
Embed All Files: show embed
nb_rspec_mediator.rb #
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
#
# Copyright 2008 Sun Microsystems, Inc. All rights reserved.
#
# The contents of this file are subject to the terms of either the GNU
# General Public License Version 2 only ("GPL") or the Common
# Development and Distribution License("CDDL") (collectively, the
# "License"). You may not use this file except in compliance with the
# License. You can obtain a copy of the License at
# http://www.netbeans.org/cddl-gplv2.html
# or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
# specific language governing permissions and limitations under the
# License. When distributing the software, include this License Header
# Notice in each file and include the License file at
# nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
# particular file as subject to the "Classpath" exception as provided
# by Sun in the GPL Version 2 section of the License file that
# accompanied this code. If applicable, add the following below the
# License Header, with the fields enclosed by brackets [] replaced by
# your own identifying information:
# "Portions Copyrighted [year] [name of copyright owner]"
#
# If you wish your version of this file to be governed by only the CDDL
# or only the GPL Version 2, indicate your decision by adding
# "[Contributor] elects to include this software in this distribution
# under the [CDDL or GPL Version 2] license." If you do not indicate a
# single choice of license, a recipient has the option to distribute
# your version of this file under either the CDDL, the GPL Version 2 or
# to extend the choice of license to its licensees as provided above.
# However, if you add GPL Version 2 code and therefore, elected the GPL
# Version 2 license, then the option applies only if the new code is
# made subject to such option by the copyright holder.
#
# Contributor(s):
#
# Portions Copyrighted 2008 Sun Microsystems, Inc.
 
 
class NbRspecMediator < Spec::Runner::ExampleGroupRunner
 
  # default example groups that should be excluded from test results
  EXCLUDE = ["Spec::Rails::Example::ViewExampleGroup",
    "Spec::Rails::Example::HelperExampleGroup",
    "Spec::Rails::Example::ControllerExampleGroup",
    "Spec::Rails::Example::FunctionalExampleGroup",
    "Spec::Rails::Example::ModelExampleGroup",
    "Spec::Rails::Example::RailsExampleGroup",
    "ActionController::IntegrationTest",
    "ActionController::TestCase",
    "ActiveSupport::TestCase"]
 
  def initialize(options, args)
    super(options)
  end
 
  def load_files(files)
    super(files)
  end
 
  def run
    prepare
    success = true
    if @options.line_number != nil
      @spec_parser = NbSpecParser.new
      @spec_parser.spec_name_for(@options.files[0], @options.line_number)
    end
    overall_start_time = Time.now
    example_groups.each do |example_group|
      if exclude?(example_group)
        next
      end
      example_group_start_time = Time.now
      puts "%RSPEC_SUITE_STARTING% #{example_group.description}"
      success = success & run_example_group(example_group)
      elapsed_time = Time.now - example_group_start_time
      puts "%RSPEC_SUITE_FINISHED% #{example_group.description} time=#{elapsed_time}"
    end
    @duration = Time.now - overall_start_time
    return success
  ensure
    reporter.duration = @duration
    finish
  end
 
  private
 
  def run_example_group(example_group)
    # in rspec 1.1.12 the run method requires options as an arg
    run_method = example_group.method(:run)
    run_method.arity > 0 ? run_method.call(@options) : run_method.call
  end
 
  def exclude?(example_group)
    if (@spec_parser != nil && example_group.description != @spec_parser.example_group_description)
      return true
    end
    EXCLUDE.include?(example_group.name)
  end
 
  protected
  def reporter
    if @reporter.nil?
      @reporter = Reporter.new(@options)
    end
    @reporter
  end
 
end
 
# TODO: probably would be better to use a formatter instead
class Reporter < Spec::Runner::Reporter
 
  attr_accessor :duration
  def duration
    @duration
  end
 
  def example_started(example)
    start_timer
    puts "%RSPEC_TEST_STARTED% #{example.description}"
    super
  end
 
  def failure(example, error)
    backtrace_tweaker.tweak_backtrace(error)
    error_msg = error.message != nil ? error.message : ""
    puts "%RSPEC_TEST_FAILED% file=#{location(example)} description=#{example.description} time=#{elapsed_time} message=#{error_msg.to_s.gsub($/, " ")} location=#{error.backtrace[0]}"
    super
  end
  alias_method :example_failed, :failure
 
  private
  def example_passed(example)
    puts "%RSPEC_TEST_FINISHED% file=#{location(example)} description=#{example.description} time=#{elapsed_time}"
    super
  end
 
  # need to use varargs since rspec 1.1.3 and 1.1.4 have different number of params
  def example_pending(*args)
    msg = "Not Yet Implemented"
    if args.size == 3 && args[2] == nil
        args[2] = msg
    elsif args.size == 2 && args[1] == nil
      args[1] = msg
    end
 
    case args[1]
    when String
      # 1.1.4
      puts "%RSPEC_TEST_PENDING% file=#{location(args[0])} description=#{args[0].description} time=#{elapsed_time} message=#{args[1]}"
    else
      # 1.1.3 or older
      puts "%RSPEC_TEST_PENDING% file=#{location(args[1])} description=#{args[1].description} time=#{elapsed_time} message=#{args[2]}"
    end
  end
 
  def start_timer
    @start_time = Time.now
  end
 
  def elapsed_time
    Time.now - @start_time
  end
 
  def location(example)
    location = ''
    # 'implementation_backtrace' is deprecated in > 1.1.11, replaced with 'backtrace'
    backtrace = example.respond_to?(:backtrace, false) ? example.backtrace : example.implementation_backtrace
    if (backtrace != nil)
      location = backtrace[0] unless backtrace.length == 0
    end
    location
  end
 
end
 
class NbSpecParser < Spec::Runner::SpecParser
 
  attr_reader :example_group_description, :example_description
 
  def spec_name_for(file, line_number)
    best_match.clear
    file = File.expand_path(file)
    safe_get_options.example_groups.each do |example_group|
      consider_example_groups_for_best_match example_group, file, line_number
      example_group.examples.each do |example|
        consider_example_for_best_match example, example_group, file, line_number
      end
    end
    if best_match[:example_group]
      @example_group_description = best_match[:example_group].description
      if best_match[:example]
        @example_description = best_match[:example].description
        "#{best_match[:example_group].description} #{best_match[:example].description}"
      else
        best_match[:example_group].description
      end
    else
      nil
    end
  end
 
  def safe_get_options
    # there's no 'rspec_options' method in rspec 1.1.11
    respond_to?(:rspec_options, true) ? rspec_options : Spec::Runner.options
  end
 
end