kneath / greed

A simple application written in merb to help you manage ad serving across multiple sites

This URL has Read+Write access

greed / autotest / merb_rspec.rb
100644 202 lines (170 sloc) 6.598 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
# Adapted from Autotest::Rails
require 'autotest'
 
class Autotest::MerbRspec < Autotest
  
  # +model_tests_dir+:: the directory to find model-centric tests
  # +controller_tests_dir+:: the directory to find controller-centric tests
  # +view_tests_dir+:: the directory to find view-centric tests
  # +fixtures_dir+:: the directory to find fixtures in
  attr_accessor :model_tests_dir, :controller_tests_dir, :view_tests_dir, :fixtures_dir
  
  def initialize # :nodoc:
    super
    
    initialize_test_layout
    
    # Ignore any happenings in these directories
    add_exception %r%^\./(?:doc|log|public|tmp)%
# Ignore any mappings that Autotest may have already set up
clear_mappings
# Any changes to a file in the root of the 'lib' directory will run any
# model test with a corresponding name.
add_mapping %r%^lib\/.*\.rb% do |filename, _|
files_matching Regexp.new("^#{model_test_for(filename)}$")
end
add_mapping %r%^spec/(spec_helper|shared/.*)\.rb$% do
files_matching %r%^spec/.*_spec\.rb$%
end
# Any changes to a fixture will run corresponding view, controller and
# model tests
add_mapping %r%^#{fixtures_dir}/(.*)s.yml% do |_, m|
[
model_test_for(m[1]),
controller_test_for(m[1]),
view_test_for(m[1])
]
end
# Any change to a test or spec will cause it to be run
add_mapping %r%^spec/(unit|models|integration|controllers|views|functional)/.*rb$% do |filename, _|
filename
end
# Any change to a model will cause it's corresponding test to be run
add_mapping %r%^app/models/(.*)\.rb$% do |_, m|
model_test_for(m[1])
end
# Any change to the global helper will result in all view and controller
# tests being run
add_mapping %r%^app/helpers/global_helpers.rb% do
files_matching %r%^spec/(views|functional|controllers)/.*_spec\.rb$%
end
# Any change to a helper will run it's corresponding view and controller
# tests, unless the helper is the global helper. Changes to the global
# helper run all view and controller tests.
add_mapping %r%^app/helpers/(.*)_helper(s)?.rb% do |_, m|
if m[1] == "global" then
files_matching %r%^spec/(views|functional|controllers)/.*_spec\.rb$%
else
[
view_test_for(m[1]),
controller_test_for(m[1])
]
end
end
# Changes to views result in their corresponding view and controller test
# being run
add_mapping %r%^app/views/(.*)/% do |_, m|
[
view_test_for(m[1]),
controller_test_for(m[1])
]
end
# Changes to a controller result in its corresponding test being run. If
# the controller is the exception or application controller, all
# controller tests are run.
add_mapping %r%^app/controllers/(.*)\.rb$% do |_, m|
if ["application", "exception"].include?(m[1])
files_matching %r%^spec/(controllers|views|functional)/.*_spec\.rb$%
else
controller_test_for(m[1])
end
end
 
# If a change is made to the router, run all controller and view tests
add_mapping %r%^config/router.rb$% do # FIX
files_matching %r%^spec/(controllers|views|functional)/.*_spec\.rb$%
end
 
# If any of the major files governing the environment are altered, run
# everything
add_mapping %r%^spec/spec_helper.rb|config/(init|rack|environments/test.rb|database.yml)% do # FIX
files_matching %r%^spec/(unit|models|controllers|views|functional)/.*_spec\.rb$%
    end
  end
  
  ##
  # Methods below this point are adapted from Auotest::Rspec
  
  def consolidate_failures(failed)
    filters = Hash.new { |h,k| h[k] = [] }
    failed.each do |spec, failed_trace|
      if f = test_files_for(failed).find { |f| failed_trace =~ Regexp.new(f) } then
        filters[f] << spec
        break
      end
    end
    return filters
  end
 
  def make_test_cmd(files_to_test)
    return "#{ruby} -S #{spec_command} #{add_options_if_present} #{files_to_test.keys.flatten.join(' ')}"
  end
  
  def add_options_if_present
    File.exist?("spec/spec.opts") ? "-O spec/spec.opts " : ""
  end
 
  # Finds the proper spec command to use. Precendence is set in the
  # lazily-evaluated method spec_commands. Alias + Override that in
  # ~/.autotest to provide a different spec command then the default
  # paths provided.
  def spec_command(separator=File::ALT_SEPARATOR)
    unless defined? @spec_command then
      @spec_command = spec_commands.find { |cmd| File.exists? cmd }
 
      raise RspecCommandError, "No spec command could be found!" unless @spec_command
 
      @spec_command.gsub! File::SEPARATOR, separator if separator
    end
    @spec_command
  end
 
  # Autotest will look for spec commands in the following
  # locations, in this order:
  #
  # * bin/spec
  # * default spec bin/loader installed in Rubygems
  def spec_commands
    [
      File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'bin', 'spec')),
      File.join(Config::CONFIG['bindir'], 'spec')
    ]
  end
  
private
 
  # Determines the paths we can expect tests or specs to reside, as well as
  # corresponding fixtures.
  def initialize_test_layout
    self.model_tests_dir = "spec/models"
    self.controller_tests_dir = "spec/controllers"
    self.view_tests_dir = "spec/views"
    self.fixtures_dir = "spec/fixtures"
  end
  
  # Given a filename and the test type, this method will return the
  # corresponding test's or spec's name.
  #
  # ==== Arguments
  # +filename+<String>:: the file name of the model, view, or controller
  # +kind_of_test+<Symbol>:: the type of test we that we should run
  #
  # ==== Returns
  # String:: the name of the corresponding test or spec
  #
  # ==== Example
  #
  # > test_for("user", :model)
  # => "user_test.rb"
  # > test_for("login", :controller)
  # => "login_controller_test.rb"
  # > test_for("form", :view)
  # => "form_view_spec.rb" # If you're running a RSpec-like suite
  def test_for(filename, kind_of_test) # :nodoc:
    name = [filename]
    name << kind_of_test.to_s unless if == :view
    name << "spec"
    return name.join("_") + ".rb"
  end
  
  def model_test_for(filename)
    [model_tests_dir, test_for(filename, :model)].join("/")
  end
  
  def controller_test_for(filename)
    [controller_tests_dir, test_for(filename, :controller)].join("/")
  end
  
  def view_test_for(filename)
    [view_tests_dir, test_for(filename, :view)].join("/")
  end
  
end