Skip to content

Adds autotest code extracted from rspec-rails #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 10, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ directory or your home directory:
# in .autotest
require "autotest/bundler"

## Rails

To use RSpec and Rails with autotest, bring in the `autotest-rails` gem:

```ruby
# Gemfile
gem 'autotest-rails', :group => [:development, :test]
```

`autotest` will now autodetect RSpec and Rails after you run the `rails
generate rspec:install` command.

## Upgrading from previous versions of rspec

Previous versions of RSpec used a different mechanism for telling autotest to
Expand Down
1 change: 1 addition & 0 deletions lib/autotest/rails_rspec2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require 'rspec/rails/autotest'
84 changes: 84 additions & 0 deletions lib/rspec/rails/autotest.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# (c) Copyright 2006 Nick Sieger <[email protected]>
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

$:.push(*Dir["vendor/rails/*/lib"])

require 'active_support/core_ext'
require 'autotest/rspec2'

class Autotest::RailsRspec2 < Autotest::Rspec2

def initialize
super
setup_rails_rspec2_mappings
end

def setup_rails_rspec2_mappings
%w{coverage/ db/ doc/ log/ public/ script/ tmp/ vendor/rails vendor/plugins vendor/gems}.each do |exception|
add_exception(/^([\.\/]*)?#{exception}/)
end

clear_mappings

add_mapping(%r%^(test|spec)/fixtures/(.*).yml$%) { |_, m|
["spec/models/#{m[2].singularize}_spec.rb"] + files_matching(%r%^spec\/views\/#{m[2]}/.*_spec\.rb$%)
}
add_mapping(%r%^spec/.*_spec\.rb$%) { |filename, _|
filename
}
add_mapping(%r%^app/models/(.*)\.rb$%) { |_, m|
["spec/models/#{m[1]}_spec.rb"]
}
add_mapping(%r%^app/views/(.*)$%) { |_, m|
files_matching %r%^spec/views/#{m[1]}_spec.rb$%
}
add_mapping(%r%^app/controllers/(.*)\.rb$%) { |_, m|
if m[1] == "application"
files_matching %r%^spec/controllers/.*_spec\.rb$%
else
["spec/controllers/#{m[1]}_spec.rb"]
end
}
add_mapping(%r%^app/helpers/(.*)_helper\.rb$%) { |_, m|
if m[1] == "application" then
files_matching(%r%^spec/(views|helpers)/.*_spec\.rb$%)
else
["spec/helpers/#{m[1]}_helper_spec.rb"] + files_matching(%r%^spec\/views\/#{m[1]}/.*_spec\.rb$%)
end
}
add_mapping(%r%^config/routes\.rb$%) {
files_matching %r%^spec/(controllers|routing|views|helpers)/.*_spec\.rb$%
}
add_mapping(%r%^config/database\.yml$%) { |_, m|
files_matching %r%^spec/models/.*_spec\.rb$%
}
add_mapping(%r%^(spec/(spec_helper|support/.*)|config/(boot|environment(s/test)?))\.rb$%) {
files_matching %r%^spec/(models|controllers|routing|views|helpers)/.*_spec\.rb$%
}
add_mapping(%r%^lib/(.*)\.rb$%) { |_, m|
["spec/lib/#{m[1]}_spec.rb"]
}
add_mapping(%r%^app/mailers/(.*)\.rb$%) { |_, m|
["spec/mailers/#{m[1]}_spec.rb"]
}
end
end
11 changes: 6 additions & 5 deletions rspec-autotest.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ Gem::Specification.new do |spec|

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

spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "rake", "~> 10.0.0"
spec.add_development_dependency "aruba", "~> 0.5"
spec.add_development_dependency "ZenTest", "~> 4.6"

spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "rake", "~> 10.0.0"
spec.add_development_dependency "aruba", "~> 0.5"
spec.add_development_dependency "ZenTest", "~> 4.6"
spec.add_development_dependency "i18n", "~> 0.6.4"
spec.add_development_dependency "activesupport", "~> 3.0"
end
36 changes: 36 additions & 0 deletions spec/autotest/rspec_rails_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require "spec_helper"
require "autotest/rails_rspec2"

describe Autotest::RailsRspec2 do

let(:rails_rspec2_autotest) { Autotest::RailsRspec2.new }

describe 'exceptions' do
let(:exceptions_regexp) { rails_rspec2_autotest.exceptions }

it "matches './log/test.log'" do
expect(exceptions_regexp).to match('./log/test.log')
end

it "matches 'log/test.log'" do
expect(exceptions_regexp).to match('log/test.log')
end

it "does not match './spec/models/user_spec.rb'" do
expect(exceptions_regexp).not_to match('./spec/models/user_spec.rb')
end

it "does not match 'spec/models/user_spec.rb'" do
expect(exceptions_regexp).not_to match('spec/models/user_spec.rb')
end
end

describe 'mappings' do
it 'runs model specs when support files change' do
rails_rspec2_autotest.find_order = %w(spec/models/user_spec.rb spec/support/blueprints.rb)
expect(rails_rspec2_autotest.test_files_for('spec/support/blueprints.rb')).to(
include('spec/models/user_spec.rb'))
end
end

end