public
Rubygem
Description: Apache Buildr
Homepage: http://incubator.apache.org/buildr
Clone URL: git://github.com/vic/buildr.git
buildr / spec / spec_helpers.rb
100644 305 lines (250 sloc) 9.044 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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with this
# work for additional information regarding copyright ownership. The ASF
# licenses this file to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
 
 
# This file gets loaded twice when running 'spec spec/*' and not with pleasent results,
# so ignore the second attempt to load it.
unless defined?(SpecHelpers)
 
  require 'rubygems'
  $LOAD_PATH.unshift File.expand_path('../lib', File.dirname(__FILE__)), File.expand_path('../addon', File.dirname(__FILE__))
  require 'buildr'
 
  require File.expand_path('sandbox', File.dirname(__FILE__))
 
  module SpecHelpers
 
    include Checks::Matchers
 
    [:info, :warn, :error].each do |severity|
      ::Object.class_eval do
        define_method severity do |message|
          $messages ||= {}
          ($messages[severity] ||= []) << message
        end
      end
    end
    
    class << Buildr.application
      alias :deprecated_without_capture :deprecated
      def deprecated(message)
        verbose(true) { deprecated_without_capture message }
      end
    end
 
    class MessageWithSeverityMatcher
      def initialize(severity, message)
        @severity = severity
        @expect = message
      end
 
      def matches?(target)
        $messages = {@severity => []}
        target.call
        return Regexp === @expect ? $messages[@severity].join('\n') =~ @expect : $messages[@severity].include?(@expect.to_s)
      end
 
      def failure_message
        "Expected #{@severity} #{@expect.inspect}, " +
          ($messages[@severity].empty? ? "no #{@severity} issued" : "found #{$messages[@severity].inspect}")
      end
 
      def negative_failure_message
        "Found unexpected #{$messages[@severity].inspect}"
      end
    end
 
    # Test if an info message was shown. You can use a string or regular expression.
    #
    # For example:
    # lambda { info 'ze test' }.should show_info(/ze test/)
    def show_info(message)
      MessageWithSeverityMatcher.new :info, message
    end
 
    # Tests if a warning was issued. You can use a string or regular expression.
    #
    # For example:
    # lambda { warn 'ze test' }.should show_warning(/ze test/)
    def show_warning(message)
      MessageWithSeverityMatcher.new :warn, message
    end
 
    # Test if an error message was shown. You can use a string or regular expression.
    #
    # For example:
    # lambda { error 'ze test' }.should show_error(/ze test/)
    def show_error(message)
      MessageWithSeverityMatcher.new :error, message
    end
 
 
    class ::Rake::Task
      alias :execute_without_a_record :execute
      def execute(args)
        $executed ||= []
        $executed << name
        execute_without_a_record args
      end
    end
 
    class InvokeMatcher
      def initialize(*tasks)
        @expecting = tasks.map { |task| [task].flatten.map(&:to_s) }
      end
 
      def matches?(target)
        $executed = []
        target.call
        return false unless all_ran?
        return !@but_not.any_ran? if @but_not
        return true
      end
 
      def failure_message
        return @but_not.negative_failure_message if all_ran? && @but_not
        "Expected the tasks #{expected} to run, but #{remaining} did not run, or not in the order we expected them to." +
          " Tasks that ran: #{$executed.inspect}"
      end
 
      def negative_failure_message
        if all_ran?
          "Expected the tasks #{expected} to not run, but they all ran."
        else
          "Expected the tasks #{expected} to not run, and all but #{remaining} ran."
        end
      end
 
      def but_not(*tasks)
        @but_not = InvokeMatcher.new(*tasks)
        self
      end
 
    protected
 
      def expected
        @expecting.map { |tests| tests.join('=>') }.join(', ')
      end
 
      def remaining
        @remaining.map { |tests| tests.join('=>') }.join(', ')
      end
 
      def all_ran?
        @remaining ||= $executed.inject(@expecting) do |expecting, executed|
          expecting.map { |tasks| tasks.first == executed ? tasks[1..-1] : tasks }.reject(&:empty?)
        end
        @remaining.empty?
      end
 
      def any_ran?
        all_ran?
        @remaining.size < @expecting.size
      end
 
    end
 
    # Tests that all the tasks ran, in the order specified. Can also be used to test that some
    # tasks and not others ran.
    #
    # Takes a list of arguments. Each argument can be a task name, matching only if that task ran.
    # Each argument can be an array of task names, matching only if all these tasks ran in that order.
    # So run_tasks('foo', 'bar') expects foo and bar to run in any order, but run_task(['foo', 'bar'])
    # expects foo to run before bar.
    #
    # You can call but_not on the matchers to specify that certain tasks must not execute.
    #
    # For example:
    # # Either task
    # lambda { task('compile').invoke }.should run_tasks('compile', 'resources')
    # # In that order
    # lambda { task('build').invoke }.should run_tasks(['compile', 'test'])
    # # With exclusion
    # lambda { task('build').invoke }.should run_tasks('compile').but_not('install')
    def run_tasks(*tasks)
      InvokeMatcher.new *tasks
    end
 
    # Tests that a task ran. Similar to run_tasks, but accepts a single task name.
    #
    # For example:
    # lambda { task('build').invoke }.should run_task('test')
    def run_task(task)
      InvokeMatcher.new [task]
    end
 
    class UriPathMatcher
      def initialize(re)
        @expression = re
      end
 
      def matches?(uri)
        @uri = uri
        uri.path =~ @expression
      end
 
      def description
        "URI with path matching #{@expression}"
      end
    end
    
    # Matches a parsed URI's path against the given regular expression
    def uri(re)
      UriPathMatcher.new(re)
    end
 
 
    class AbsolutePathMatcher
      def initialize(path)
        @expected = File.expand_path(path.to_s)
      end
 
      def matches?(path)
        @provided = File.expand_path(path.to_s)
        @provided == @expected
      end
 
      def failure_message
        "Expected path #{@expected}, but found path #{@provided}"
      end
 
      def negative_failure_message
        "Expected a path other than #{@expected}"
      end
    end
 
    def point_to_path(path)
      AbsolutePathMatcher.new(path)
    end
 
 
    def suppress_stdout
      stdout = $stdout
      $stdout = StringIO.new
      begin
        yield
      ensure
        $stdout = stdout
      end
    end
 
    def dryrun
      Buildr.application.options.dryrun = true
      begin
        suppress_stdout { yield }
      ensure
        Buildr.application.options.dryrun = false
      end
    end
 
    # We run tests with tracing off. Then things break. And we need to figure out what went wrong.
    # So just use trace() as you would use verbose() to find and squash the bug.
    def trace(value = nil)
      old_value = Buildr.application.options.trace
      Buildr.application.options.trace = value unless value.nil?
      if block_given?
        begin
          yield
        ensure
          Buildr.application.options.trace = old_value
        end
      end
      Buildr.application.options.trace
    end
 
    # Change the Buildr original directory, faking invocation from a different directory.
    def in_original_dir(dir)
      begin
        original_dir = Buildr.application.original_dir
        Buildr.application.instance_eval { @original_dir = File.expand_path(dir) }
        yield
      ensure
        Buildr.application.instance_eval { @original_dir = original_dir }
      end
    end
 
 
    # Buildr's define method creates a project definition but does not evaluate it
    # (that happens once the buildfile is loaded), and we include Buildr's define in
    # the test context so we can use it without prefixing with Buildr. This just patches
    # define to evaluate the project definition before returning it.
    def define(name, properties = nil, &block) #:yields:project
      Project.define(name, properties, &block).tap { |project| project.invoke }
    end
 
  end
 
 
  # Allow using matchers within the project definition.
  class Buildr::Project
    include ::Spec::Matchers, SpecHelpers
  end
 
 
  Spec::Runner.configure do |config|
    # Make all Buildr methods accessible from test cases, and add various helper methods.
    config.include Buildr, SpecHelpers
 
    # Sanbdox Buildr for each test.
    config.include Sandbox
  end
 
end