Parent

Autotest::MerbRspec

This class maps your application’s structure so Autotest can understand what specs to run when files change.

Fixtures are not covered by this class. If you change a fixture file, you will have to run your spec suite manually, or, better yet, provide your own Autotest map explaining how your fixtures are set up.


This class maps your application’s structure so Autotest can understand what specs to run when files change.

Fixtures are not covered by this class. If you change a fixture file, you will have to run your spec suite manually, or, better yet, provide your own Autotest map explaining how your fixtures are set up.

Public Class Methods

new() click to toggle source
    # File lib/generators/templates/application/merb_stack/autotest/merb_rspec.rb, line 13
13:   def initialize
14:     super
15: 
16:     # Ignore any happenings in these directories
17:     add_exception %^\./(?:doc|log|public|tmp|\.git|\.hg|\.svn|framework|gems|schema|\.DS_Store|autotest|bin|.*\.sqlite3|.*\.thor)% 
18:     # Ignore SCM directories and custom Autotest mappings
19:     ].svn .hg .git .autotest].each { |exception| add_exception(exception) }
20: 
21:     # Ignore any mappings that Autotest may have already set up
22:     clear_mappings
23: 
24:     # Anything in /lib could have a spec anywhere, if at all. So, look for
25:     # files with roughly the same name as the file in /lib
26:     add_mapping %^lib\/(.*)\.rb% do |_, m|
27:       files_matching %^spec\/#{m[1]}%
28:     end
29: 
30:     add_mapping %^spec/(spec_helper|shared/.*)\.rb$% do
31:       all_specs
32:     end
33: 
34:     # Changing a spec will cause it to run itself
35:     add_mapping %^spec/.*\.rb$% do |filename, _|
36:       filename
37:     end
38: 
39:     # Any change to a model will cause it's corresponding test to be run
40:     add_mapping %^app/models/(.*)\.rb$% do |_, m|
41:       spec_for(m[1], 'model')
42:     end
43: 
44:     # Any change to global_helpers will result in all view and controller
45:     # tests being run
46:     add_mapping %^app/helpers/global_helpers\.rb% do
47:       files_matching %^spec/(views|controllers|helpers|requests)/.*_spec\.rb$%
48:     end
49: 
50:     # Any change to a helper will cause its spec to be run
51:     add_mapping %^app/helpers/((.*)_helper(s)?)\.rb% do |_, m|
52:       spec_for(m[1], 'helper')
53:     end
54: 
55:     # Changes to a view cause its spec to be run
56:     add_mapping %^app/views/(.*)/% do |_, m|
57:       spec_for(m[1], 'request')
58:     end
59: 
60:     # Changes to a controller result in its corresponding spec being run. If
61:     # the controller is the exception or application controller, all
62:     # controller specs are run.
63:     add_mapping %^app/controllers/(.*)\.rb$% do |_, m|
64:       if ["application", "exception"].include?(m[1])
65:         files_matching %^spec/requests/.*_spec\.rb$%
66:       else
67:         spec_for(m[1], 'request')
68:       end
69:     end
70: 
71:     # If a change is made to the router, run controller, view and helper specs
72:     add_mapping %^config/router.rb$% do
73:       files_matching %^spec/(views|controllers|helpers|requests)/.*_spec\.rb$%
74:     end
75: 
76:     # If any of the major files governing the environment are altered, run
77:     # everything
78:     add_mapping %^config/(init|rack|environments/test).*\.rb|database\.yml% do 
79:       all_specs
80:     end
81:   end
new() click to toggle source
    # File lib/generators/templates/application/merb_core/autotest/merb_rspec.rb, line 13
13:   def initialize
14:     super
15: 
16:     # Ignore any happenings in these directories
17:     add_exception %^\./(?:doc|log|public|tmp|\.git|\.hg|\.svn|framework|gems|schema|\.DS_Store|autotest|bin|.*\.sqlite3|.*\.thor)%
18:     # Ignore SCM directories and custom Autotest mappings
19:     ].svn .hg .git .autotest].each { |exception| add_exception(exception) }
20: 
21:     # Ignore any mappings that Autotest may have already set up
22:     clear_mappings
23: 
24:     # Anything in /lib could have a spec anywhere, if at all. So, look for
25:     # files with roughly the same name as the file in /lib
26:     add_mapping %^lib\/(.*)\.rb% do |_, m|
27:       files_matching %^spec\/#{m[1]}%
28:     end
29: 
30:     add_mapping %^spec/(spec_helper|shared/.*)\.rb$% do
31:       all_specs
32:     end
33: 
34:     # Changing a spec will cause it to run itself
35:     add_mapping %^spec/.*\.rb$% do |filename, _|
36:       filename
37:     end
38: 
39:     # Any change to a model will cause it's corresponding test to be run
40:     add_mapping %^app/models/(.*)\.rb$% do |_, m|
41:       spec_for(m[1], 'model')
42:     end
43: 
44:     # Any change to global_helpers will result in all view and controller
45:     # tests being run
46:     add_mapping %^app/helpers/global_helpers\.rb% do
47:       files_matching %^spec/(views|controllers|helpers|requests)/.*_spec\.rb$%
48:     end
49: 
50:     # Any change to a helper will cause its spec to be run
51:     add_mapping %^app/helpers/((.*)_helper(s)?)\.rb% do |_, m|
52:       spec_for(m[1], 'helper')
53:     end
54: 
55:     # Changes to a view cause its spec to be run
56:     add_mapping %^app/views/(.*)/% do |_, m|
57:       spec_for(m[1], 'request')
58:     end
59: 
60:     # Changes to a controller result in its corresponding spec being run. If
61:     # the controller is the exception or application controller, all
62:     # controller specs are run.
63:     add_mapping %^app/controllers/(.*)\.rb$% do |_, m|
64:       if ["application", "exception"].include?(m[1])
65:         files_matching %^spec/requests/.*_spec\.rb$%
66:       else
67:         spec_for(m[1], 'request')
68:       end
69:     end
70: 
71:     # If a change is made to the router, run controller, view and helper specs
72:     add_mapping %^config/router.rb$% do
73:       files_matching %^spec/(views|controllers|helpers|requests)/.*_spec\.rb$%
74:     end
75: 
76:     # If any of the major files governing the environment are altered, run
77:     # everything
78:     add_mapping %^config/(init|rack|environments/test).*\.rb|database\.yml% do 
79:       all_specs
80:     end
81:   end

Public Instance Methods

add_options_if_present() click to toggle source
     # File lib/generators/templates/application/merb_core/autotest/merb_rspec.rb, line 115
115:   def add_options_if_present
116:     File.exist?("spec/spec.opts") ? "-O spec/spec.opts " : ""
117:   end
add_options_if_present() click to toggle source
     # File lib/generators/templates/application/merb_stack/autotest/merb_rspec.rb, line 115
115:   def add_options_if_present
116:     File.exist?("spec/spec.opts") ? "-O spec/spec.opts " : ""
117:   end
consolidate_failures(failed) click to toggle source
     # File lib/generators/templates/application/merb_stack/autotest/merb_rspec.rb, line 94
 94:   def consolidate_failures(failed)
 95:     filters = Hash.new { |h,k| h[k] = [] }
 96:     failed.each do |spec, failed_trace|
 97:       if f = test_files_for(failed).find { |f| f =~ /spec\// }
 98:         filters[f] << spec
 99:         break
100:       end
101:     end
102:     filters
103:   end
consolidate_failures(failed) click to toggle source
     # File lib/generators/templates/application/merb_core/autotest/merb_rspec.rb, line 94
 94:   def consolidate_failures(failed)
 95:     filters = Hash.new { |h,k| h[k] = [] }
 96:     failed.each do |spec, failed_trace|
 97:       if f = test_files_for(failed).find { |f| f =~ /spec\// }
 98:         filters[f] << spec
 99:         break
100:       end
101:     end
102:     filters
103:   end
failed_results(results) click to toggle source
    # File lib/generators/templates/application/merb_core/autotest/merb_rspec.rb, line 83
83:   def failed_results(results)
84:     results.scan(/^\d+\)\n(?:\e\[\d*m)?(?:.*?Error in )?'([^\n]*)'(?: FAILED)?(?:\e\[\d*m)?\n(.*?)\n\n/)
85:   end
failed_results(results) click to toggle source
    # File lib/generators/templates/application/merb_stack/autotest/merb_rspec.rb, line 83
83:   def failed_results(results)
84:     results.scan(/^\d+\)\n(?:\e\[\d*m)?(?:.*?Error in )?'([^\n]*)'(?: FAILED)?(?:\e\[\d*m)?\n(.*?)\n\n/)
85:   end
handle_results(results) click to toggle source
    # File lib/generators/templates/application/merb_stack/autotest/merb_rspec.rb, line 87
87:   def handle_results(results)
88:     @failures      = failed_results(results)
89:     @files_to_test = consolidate_failures(@failures)
90:     @files_to_test.empty? && !$TESTING ? hook(:green) : hook(:red)
91:     @tainted = !@files_to_test.empty?
92:   end
handle_results(results) click to toggle source
    # File lib/generators/templates/application/merb_core/autotest/merb_rspec.rb, line 87
87:   def handle_results(results)
88:     @failures      = failed_results(results)
89:     @files_to_test = consolidate_failures(@failures)
90:     @files_to_test.empty? && !$TESTING ? hook(:green) : hook(:red)
91:     @tainted = !@files_to_test.empty?
92:   end
make_test_cmd(specs_to_runs) click to toggle source
     # File lib/generators/templates/application/merb_core/autotest/merb_rspec.rb, line 105
105:   def make_test_cmd(specs_to_runs)
106:     [
107:       ruby,
108:       "-S",
109:       spec_command,
110:       add_options_if_present,
111:       files_to_test.keys.flatten.join(' ')
112:     ].join(' ')
113:   end
make_test_cmd(specs_to_runs) click to toggle source
     # File lib/generators/templates/application/merb_stack/autotest/merb_rspec.rb, line 105
105:   def make_test_cmd(specs_to_runs)
106:     [
107:       ruby,
108:       "-S",
109:       spec_command,
110:       add_options_if_present,
111:       files_to_test.keys.flatten.join(' ')
112:     ].join(' ')
113:   end
spec_command(separator=File::ALT_SEPARATOR) click to toggle source

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.

     # File lib/generators/templates/application/merb_stack/autotest/merb_rspec.rb, line 123
123:   def spec_command(separator=File::ALT_SEPARATOR)
124:     unless defined?(@spec_command)
125:       @spec_command = spec_commands.find { |cmd| File.exists?(cmd) }
126: 
127:       raise RspecCommandError, "No spec command could be found" unless @spec_command
128: 
129:       @spec_command.gsub!(File::SEPARATOR, separator) if separator
130:     end
131:     @spec_command
132:   end
spec_command(separator=File::ALT_SEPARATOR) click to toggle source

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.

     # File lib/generators/templates/application/merb_core/autotest/merb_rspec.rb, line 123
123:   def spec_command(separator=File::ALT_SEPARATOR)
124:     unless defined?(@spec_command)
125:       @spec_command = spec_commands.find { |cmd| File.exists?(cmd) }
126: 
127:       raise RspecCommandError, "No spec command could be found" unless @spec_command
128: 
129:       @spec_command.gsub!(File::SEPARATOR, separator) if separator
130:     end
131:     @spec_command
132:   end
spec_commands() click to toggle source

Autotest will look for spec commands in the following locations, in this order:

  * default spec bin/loader installed in Rubygems
  * any spec command found in PATH
     # File lib/generators/templates/application/merb_stack/autotest/merb_rspec.rb, line 139
139:   def spec_commands
140:     [File.join(Config::CONFIG['bindir'], 'spec'), 'spec']
141:   end
spec_commands() click to toggle source

Autotest will look for spec commands in the following locations, in this order:

  * default spec bin/loader installed in Rubygems
  * any spec command found in PATH
     # File lib/generators/templates/application/merb_core/autotest/merb_rspec.rb, line 139
139:   def spec_commands
140:     [File.join(Config::CONFIG['bindir'], 'spec'), 'spec']
141:   end

Private Instance Methods

all_specs() click to toggle source

Runs files_matching for all specs

     # File lib/generators/templates/application/merb_core/autotest/merb_rspec.rb, line 146
146:   def all_specs
147:     files_matching %^spec/.*_spec\.rb$%
148:   end
all_specs() click to toggle source

Runs files_matching for all specs

     # File lib/generators/templates/application/merb_stack/autotest/merb_rspec.rb, line 146
146:   def all_specs
147:     files_matching %^spec/.*_spec\.rb$%
148:   end
spec_for(match, kind) click to toggle source

Generates a path to some spec given its kind and the match from a mapping

Arguments

match

the match from a mapping

kind

the kind of spec that the match represents

Returns

String

Example

  > spec_for('post', :model')
  => "spec/models/post_spec.rb"
     # File lib/generators/templates/application/merb_stack/autotest/merb_rspec.rb, line 162
162:   def spec_for(match, kind)
163:     files_matching %^spec/#{kind}s/#{match}(/.*)?_spec.rb$%
164:   end
spec_for(match, kind) click to toggle source

Generates a path to some spec given its kind and the match from a mapping

Arguments

match

the match from a mapping

kind

the kind of spec that the match represents

Returns

String

Example

  > spec_for('post', :model')
  => "spec/models/post_spec.rb"
     # File lib/generators/templates/application/merb_core/autotest/merb_rspec.rb, line 162
162:   def spec_for(match, kind)
163:     files_matching %^spec/#{kind}s/#{match}(/.*)?_spec.rb$%
164:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.