Whitespace cleanup
[merb_mart.git] / autotest.bak / merb.rb
blob9fc4735a4c0f6db6318ff55a98937d836d08f4c1
1 # Adapted from Autotest::Rails
2 require 'autotest'
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
12   def initialize # :nodoc:
13     super
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
21     clear_mappings
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)}$"])
27     end
29     # Any changes to a fixture will run corresponding view, controller and
30     # model tests
31     add_mapping %r%^#{fixtures_dir}/(.*)s.yml% do |_, m|
32       [
33         model_test_for(m[1]),
34         controller_test_for(m[1]),
35         view_test_for(m[1])
36       ]
37     end
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, _|
41       filename
42     end
44     # Any change to a model will cause it's corresponding test to be run
45     add_mapping %r%^app/models/(.*)\.rb$% do |_, m|
46       model_test_for(m[1])
47     end
49     # Any change to the global helper will result in all view and controller
50     # tests being run
51     add_mapping %r%^app/helpers/global_helpers.rb% do
52       files_matching %r%^test/(views|functional|controllers)/.*_test\.rb$%
53     end
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$%
61       else
62         [
63           view_test_for(m[1]),
64           controller_test_for(m[1])
65         ]
66       end
67     end
69     # Changes to views result in their corresponding view and controller test
70     # being run
71     add_mapping %r%^app/views/(.*)/% do |_, m|
72       [
73         view_test_for(m[1]),
74         controller_test_for(m[1])
75       ]
76     end
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$%
84       else
85         controller_test_for(m[1])
86       end
87     end
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$%
92     end
94     # If any of the major files governing the environment are altered, run
95     # everything
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$%
98     end
99   end
101 private
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"
110   end
112   # Given a filename and the test type, this method will return the
113   # corresponding test's or spec's name.
114   #
115   # ==== Arguments
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
118   #
119   # ==== Returns
120   # String:: the name of the corresponding test or spec
121   #
122   # ==== Example
123   #
124   #   > test_for("user", :model)
125   #   => "user_test.rb"
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) # :nodoc:
131     name  = [filename]
132     name << kind_of_test.to_s if kind_of_test == :view
133     name << "test"
134     return name.join("_") + ".rb"
135   end
137   def model_test_for(filename)
138     [model_tests_dir, test_for(filename, :model)].join("/")
139   end
141   def controller_test_for(filename)
142     [controller_tests_dir, test_for(filename, :controller)].join("/")
143   end
145   def view_test_for(filename)
146     [view_tests_dir, test_for(filename, :view)].join("/")
147   end