1 # Adapted from Autotest::Rails
4 class Autotest::Merb < Autotest
6 # +model_tests_dir+:: the directory to find model-centric tests
7 # +controller_tests_dir+:: the directory to find controller-centric tests
8 # +view_tests_dir+:: the directory to find view-centric tests
9 # +fixtures_dir+:: the directory to find fixtures in
10 attr_accessor :model_tests_dir, :controller_tests_dir, :view_tests_dir, :fixtures_dir
15 initialize_test_layout
17 # Ignore any happenings in these directories
18 add_exception %r%^\./(?:doc|log|public|tmp)%
20 # Ignore any mappings that Autotest may have already set up
23 # Any changes to a file in the root of the 'lib' directory will run any
24 # model test with a corresponding name.
25 add_mapping %r%^lib\/.*\.rb% do |filename, _|
26 files_matching Regexp.new(["^#{model_test_for(filename)}$"])
29 # Any changes to a fixture will run corresponding view, controller and
31 add_mapping %r%^#{fixtures_dir}/(.*)s.yml% do |_, m|
34 controller_test_for(m[1]),
39 # Any change to a test or test will cause it to be run
40 add_mapping %r%^test/(unit|models|integration|controllers|views|functional)/.*rb$% do |filename, _|
44 # Any change to a model will cause it's corresponding test to be run
45 add_mapping %r%^app/models/(.*)\.rb$% do |_, m|
49 # Any change to the global helper will result in all view and controller
51 add_mapping %r%^app/helpers/global_helpers.rb% do
52 files_matching %r%^test/(views|functional|controllers)/.*_test\.rb$%
55 # Any change to a helper will run it's corresponding view and controller
56 # tests, unless the helper is the global helper. Changes to the global
57 # helper run all view and controller tests.
58 add_mapping %r%^app/helpers/(.*)_helper(s)?.rb% do |_, m|
59 if m[1] == "global" then
60 files_matching %r%^test/(views|functional|controllers)/.*_test\.rb$%
64 controller_test_for(m[1])
69 # Changes to views result in their corresponding view and controller test
71 add_mapping %r%^app/views/(.*)/% do |_, m|
74 controller_test_for(m[1])
78 # Changes to a controller result in its corresponding test being run. If
79 # the controller is the exception or application controller, all
80 # controller tests are run.
81 add_mapping %r%^app/controllers/(.*)\.rb$% do |_, m|
82 if ["application", "exception"].include?(m[1])
83 files_matching %r%^test/(controllers|views|functional)/.*_test\.rb$%
85 controller_test_for(m[1])
89 # If a change is made to the router, run all controller and view tests
90 add_mapping %r%^config/router.rb$% do # FIX
91 files_matching %r%^test/(controllers|views|functional)/.*_test\.rb$%
94 # If any of the major files governing the environment are altered, run
96 add_mapping %r%^test/test_helper.rb|config/(init|rack|environments/test.rb|database.yml)% do # FIX
97 files_matching %r%^test/(unit|models|controllers|views|functional)/.*_test\.rb$%
103 # Determines the paths we can expect tests or specs to reside, as well as
104 # corresponding fixtures.
105 def initialize_test_layout
106 self.model_tests_dir = "test/unit"
107 self.controller_tests_dir = "test/functional"
108 self.view_tests_dir = "test/views"
109 self.fixtures_dir = "test/fixtures"
112 # Given a filename and the test type, this method will return the
113 # corresponding test's or spec's name.
116 # +filename+<String>:: the file name of the model, view, or controller
117 # +kind_of_test+<Symbol>:: the type of test we that we should run
120 # String:: the name of the corresponding test or spec
124 # > test_for("user", :model)
126 # > test_for("login", :controller)
127 # => "login_controller_test.rb"
128 # > test_for("form", :view)
129 # => "form_view_spec.rb" # If you're running a RSpec-like suite
130 def test_for(filename, kind_of_test)
132 name << kind_of_test.to_s if kind_of_test == :view
134 return name.join("_") + ".rb"
137 def model_test_for(filename)
138 [model_tests_dir, test_for(filename, :model)].join("/")
141 def controller_test_for(filename)
142 [controller_tests_dir, test_for(filename, :controller)].join("/")
145 def view_test_for(filename)
146 [view_tests_dir, test_for(filename, :view)].join("/")