Skip to content

Commit 318f09e

Browse files
committed
Merge pull request #6 from rspec/rspec-rails
Adds autotest code extracted from rspec-rails
2 parents e6748c8 + ea7926d commit 318f09e

File tree

5 files changed

+139
-5
lines changed

5 files changed

+139
-5
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ directory or your home directory:
2020
# in .autotest
2121
require "autotest/bundler"
2222

23+
## Rails
24+
25+
To use RSpec and Rails with autotest, bring in the `autotest-rails` gem:
26+
27+
```ruby
28+
# Gemfile
29+
gem 'autotest-rails', :group => [:development, :test]
30+
```
31+
32+
`autotest` will now autodetect RSpec and Rails after you run the `rails
33+
generate rspec:install` command.
34+
2335
## Upgrading from previous versions of rspec
2436

2537
Previous versions of RSpec used a different mechanism for telling autotest to

lib/autotest/rails_rspec2.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require 'rspec/rails/autotest'

lib/rspec/rails/autotest.rb

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# (c) Copyright 2006 Nick Sieger <[email protected]>
2+
#
3+
# Permission is hereby granted, free of charge, to any person
4+
# obtaining a copy of this software and associated documentation files
5+
# (the "Software"), to deal in the Software without restriction,
6+
# including without limitation the rights to use, copy, modify, merge,
7+
# publish, distribute, sublicense, and/or sell copies of the Software,
8+
# and to permit persons to whom the Software is furnished to do so,
9+
# subject to the following conditions:
10+
#
11+
# The above copyright notice and this permission notice shall be
12+
# included in all copies or substantial portions of the Software.
13+
#
14+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18+
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19+
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
23+
$:.push(*Dir["vendor/rails/*/lib"])
24+
25+
require 'active_support/core_ext'
26+
require 'autotest/rspec2'
27+
28+
class Autotest::RailsRspec2 < Autotest::Rspec2
29+
30+
def initialize
31+
super
32+
setup_rails_rspec2_mappings
33+
end
34+
35+
def setup_rails_rspec2_mappings
36+
%w{coverage/ db/ doc/ log/ public/ script/ tmp/ vendor/rails vendor/plugins vendor/gems}.each do |exception|
37+
add_exception(/^([\.\/]*)?#{exception}/)
38+
end
39+
40+
clear_mappings
41+
42+
add_mapping(%r%^(test|spec)/fixtures/(.*).yml$%) { |_, m|
43+
["spec/models/#{m[2].singularize}_spec.rb"] + files_matching(%r%^spec\/views\/#{m[2]}/.*_spec\.rb$%)
44+
}
45+
add_mapping(%r%^spec/.*_spec\.rb$%) { |filename, _|
46+
filename
47+
}
48+
add_mapping(%r%^app/models/(.*)\.rb$%) { |_, m|
49+
["spec/models/#{m[1]}_spec.rb"]
50+
}
51+
add_mapping(%r%^app/views/(.*)$%) { |_, m|
52+
files_matching %r%^spec/views/#{m[1]}_spec.rb$%
53+
}
54+
add_mapping(%r%^app/controllers/(.*)\.rb$%) { |_, m|
55+
if m[1] == "application"
56+
files_matching %r%^spec/controllers/.*_spec\.rb$%
57+
else
58+
["spec/controllers/#{m[1]}_spec.rb"]
59+
end
60+
}
61+
add_mapping(%r%^app/helpers/(.*)_helper\.rb$%) { |_, m|
62+
if m[1] == "application" then
63+
files_matching(%r%^spec/(views|helpers)/.*_spec\.rb$%)
64+
else
65+
["spec/helpers/#{m[1]}_helper_spec.rb"] + files_matching(%r%^spec\/views\/#{m[1]}/.*_spec\.rb$%)
66+
end
67+
}
68+
add_mapping(%r%^config/routes\.rb$%) {
69+
files_matching %r%^spec/(controllers|routing|views|helpers)/.*_spec\.rb$%
70+
}
71+
add_mapping(%r%^config/database\.yml$%) { |_, m|
72+
files_matching %r%^spec/models/.*_spec\.rb$%
73+
}
74+
add_mapping(%r%^(spec/(spec_helper|support/.*)|config/(boot|environment(s/test)?))\.rb$%) {
75+
files_matching %r%^spec/(models|controllers|routing|views|helpers)/.*_spec\.rb$%
76+
}
77+
add_mapping(%r%^lib/(.*)\.rb$%) { |_, m|
78+
["spec/lib/#{m[1]}_spec.rb"]
79+
}
80+
add_mapping(%r%^app/mailers/(.*)\.rb$%) { |_, m|
81+
["spec/mailers/#{m[1]}_spec.rb"]
82+
}
83+
end
84+
end

rspec-autotest.gemspec

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ Gem::Specification.new do |spec|
2424

2525
spec.add_dependency "rspec-core", ">= 2.99.0.pre"
2626

27-
spec.add_development_dependency "bundler", "~> 1.3"
28-
spec.add_development_dependency "rake", "~> 10.0.0"
29-
spec.add_development_dependency "aruba", "~> 0.5"
30-
spec.add_development_dependency "ZenTest", "~> 4.6"
31-
27+
spec.add_development_dependency "bundler", "~> 1.3"
28+
spec.add_development_dependency "rake", "~> 10.0.0"
29+
spec.add_development_dependency "aruba", "~> 0.5"
30+
spec.add_development_dependency "ZenTest", "~> 4.6"
31+
spec.add_development_dependency "i18n", "~> 0.6.4"
32+
spec.add_development_dependency "activesupport", "~> 3.0"
3233
end

spec/autotest/rspec_rails_spec.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
require "spec_helper"
2+
require "autotest/rails_rspec2"
3+
4+
describe Autotest::RailsRspec2 do
5+
6+
let(:rails_rspec2_autotest) { Autotest::RailsRspec2.new }
7+
8+
describe 'exceptions' do
9+
let(:exceptions_regexp) { rails_rspec2_autotest.exceptions }
10+
11+
it "matches './log/test.log'" do
12+
expect(exceptions_regexp).to match('./log/test.log')
13+
end
14+
15+
it "matches 'log/test.log'" do
16+
expect(exceptions_regexp).to match('log/test.log')
17+
end
18+
19+
it "does not match './spec/models/user_spec.rb'" do
20+
expect(exceptions_regexp).not_to match('./spec/models/user_spec.rb')
21+
end
22+
23+
it "does not match 'spec/models/user_spec.rb'" do
24+
expect(exceptions_regexp).not_to match('spec/models/user_spec.rb')
25+
end
26+
end
27+
28+
describe 'mappings' do
29+
it 'runs model specs when support files change' do
30+
rails_rspec2_autotest.find_order = %w(spec/models/user_spec.rb spec/support/blueprints.rb)
31+
expect(rails_rspec2_autotest.test_files_for('spec/support/blueprints.rb')).to(
32+
include('spec/models/user_spec.rb'))
33+
end
34+
end
35+
36+
end

0 commit comments

Comments
 (0)