First step for upgrading to rails 3.

Check rake rails:upgrade:check for next steps.
This commit is contained in:
benni 2011-05-11 01:32:21 +02:00
parent a689bdd5f5
commit 4eba5fbc9c
45 changed files with 6139 additions and 2212 deletions

View file

@ -0,0 +1,330 @@
require 'test_helper'
require 'application_checker'
require 'fileutils'
tmp_dir = File.expand_path("#{File.dirname(__FILE__)}/fixtures/tmp")
if defined? BASE_ROOT
BASE_ROOT.replace tmp_dir
else
BASE_ROOT = tmp_dir
end
FileUtils.mkdir_p BASE_ROOT
# Stub out methods on upgrader class
module Rails
module Upgrading
class ApplicationChecker
attr_reader :alerts, :culprits
def base_path
BASE_ROOT + "/"
end
def in_rails_app?
true
end
def initialize
@alerts = {}
@culprits = {}
end
def alert(title, text, more_info_url, culprits)
@alerts[title] = [text, more_info_url]
@culprits[title] = culprits
end
end
end
end
class ApplicationCheckerTest < ActiveSupport::TestCase
def setup
@checker = Rails::Upgrading::ApplicationChecker.new
@old_dir = Dir.pwd
Dir.chdir(BASE_ROOT)
end
def test_check_ar_methods_in_controller
make_file("app/controllers", "post_controller.rb", "Post.find(:all)")
@checker.check_ar_methods
assert @checker.alerts.has_key?("Soon-to-be-deprecated ActiveRecord calls")
end
def test_check_ar_methods_in_models
make_file("app/models", "post.rb", "Post.find(:all)")
@checker.check_ar_methods
key = "Soon-to-be-deprecated ActiveRecord calls"
assert @checker.alerts.has_key?(key)
assert_equal "app/models/post.rb", @checker.culprits[key].first
end
def test_check_svn_subdirs_are_not_included
make_file("app/models/.svn/text-base", "foo.rb.tmp", "Post.find(:all)")
@checker.check_ar_methods
assert @checker.alerts.empty?
end
def test_check_validation_on_methods
make_file("app/models", "post.rb", "validate_on_create :comments_valid?")
@checker.check_validation_on_methods
assert @checker.alerts.has_key?("Updated syntax for validate_on_* methods")
end
def test_check_before_validation_on_methods
make_file("app/models", "post.rb", "before_validation_on_create :comments_valid?")
@checker.check_before_validation_on_methods
assert @checker.alerts.has_key?("Updated syntax for before_validation_on_* methods")
end
def test_named_scope_left_over
make_file("app/models", "post.rb", "named_scope :failure")
@checker.check_ar_methods
assert @checker.alerts.has_key?("named_scope is now just scope")
end
def test_check_routes
make_file("config/", "routes.rb", " map.connect 'fail'")
@checker.check_routes
assert @checker.alerts.has_key?("Old router API")
end
def test_check_for_old_test_help
make_file("test/", "test_helper.rb", " require 'test_help'")
@checker.check_test_help
assert @checker.alerts.has_key?("Deprecated test_help path")
end
def test_check_for_old_test_help_with_double_quotes
make_file("test/", "test_helper.rb", " require \"test_help\"")
@checker.check_test_help
assert @checker.alerts.has_key?("Deprecated test_help path")
end
def test_check_for_old_test_help_doesnt_see_test_helper
make_file("test/", "test_helper.rb", " require 'test_helper'")
@checker.check_test_help
assert !@checker.alerts.has_key?("Deprecated test_help path")
end
def test_check_lack_of_app_dot_rb
@checker.check_environment
assert @checker.alerts.has_key?("New file needed: config/application.rb")
end
def test_check_environment_syntax
make_file("config/", "environment.rb", "config.frameworks = []")
@checker.check_environment
assert @checker.alerts.has_key?("Old environment.rb")
end
def test_check_gems
make_file("config/", "environment.rb", "config.gem 'rails'")
@checker.check_gems
assert @checker.alerts.has_key?("Old gem bundling (config.gems)")
end
def test_check_gems_finds_nothing
@checker.check_gems
assert_equal false, @checker.alerts.has_key?("Old gem bundling (config.gems)")
end
def test_check_mailer_finds_nothing
@checker.check_mailers
assert_equal false, @checker.alerts.has_key?("Old ActionMailer class API")
end
def test_check_mailer_syntax
make_file("app/models/", "notifications.rb", "def signup\nrecipients @users\n end")
@checker.check_mailers
assert @checker.alerts.has_key?("Old ActionMailer class API")
end
def test_check_mailer_syntax_from
make_file("app/models/", "notifications.rb", "def signup\nfrom @user\n end")
@checker.check_mailers
assert @checker.alerts.has_key?("Old ActionMailer class API")
end
def test_check_mailer_syntax_subject
make_file("app/models/", "notifications.rb", "def signup\nsubject @subject\n end")
@checker.check_mailers
assert @checker.alerts.has_key?("Old ActionMailer class API")
end
def test_check_mailer_syntax_attachment
make_file("app/models/", "notifications.rb", "def signup\nattachment 'application/pdf' do |a|\n end")
@checker.check_mailers
assert @checker.alerts.has_key?("Old ActionMailer class API")
end
def test_new_check_mailer_syntax_from
make_file("app/models/", "notifications.rb", "def signup\n:from => @users\n end")
@checker.check_mailers
assert ! @checker.alerts.has_key?("Old ActionMailer class API")
end
def test_new_check_mailer_syntax_subject
make_file("app/models/", "notifications.rb", "def signup\n:subject => @users\n end")
@checker.check_mailers
assert ! @checker.alerts.has_key?("Old ActionMailer class API")
end
def test_new_check_mailer_syntax_attachments
make_file("app/models/", "notifications.rb", "def signup\nattachments['an-image.jp'] = File.read('an-image.jpg')\n end")
@checker.check_mailers
assert ! @checker.alerts.has_key?("Old ActionMailer class API")
end
def test_check_mailer_api
make_file("app/controllers/", "thing_controller.rb", "def signup\n Notifications.deliver_signup\n end")
@checker.check_mailers
assert @checker.alerts.has_key?("Deprecated ActionMailer API")
end
def test_check_generators
make_file("vendor/plugins/thing/generators/thing/", "thing_generator.rb", "def manifest\n m.whatever\n end")
@checker.check_generators
assert @checker.alerts.has_key?("Old Rails generator API")
end
def test_check_plugins
make_file("vendor/plugins/rspec-rails/", "whatever.rb", "def rspec; end")
@checker.check_plugins
assert @checker.alerts.has_key?("Known broken plugins")
end
def test_ignoring_comments
make_file("config/", "routes.rb", "# map.connect 'fail'")
@checker.check_routes
assert !@checker.alerts.has_key?("Old router API")
end
def test_check_deprecated_constants_in_app_code
make_file("app/controllers/", "thing_controller.rb", "class ThingController; THING = RAILS_ENV; end;")
@checker.check_deprecated_constants
assert @checker.alerts.has_key?("Deprecated constant(s)")
end
def test_check_deprecated_constants_in_lib
make_file("lib/", "extra_thing.rb", "class ExtraThing; THING = RAILS_ENV; end;")
@checker.check_deprecated_constants
assert @checker.alerts.has_key?("Deprecated constant(s)")
end
def test_check_deprecated_cookie_finds_nothing
@checker.check_old_cookie_secret
assert_equal false, @checker.alerts.has_key?("Deprecated cookie secret setting")
end
def test_check_deprecated_cookie_settings
make_file("config/initializers/", "more_settings.rb", "ActionController::Base.cookie_verifier_secret = 'OMG'")
@checker.check_old_cookie_secret
assert @checker.alerts.has_key?("Deprecated cookie secret setting")
end
def test_check_deprecated_session_finds_nothing
@checker.check_old_session_secret
assert_equal false, @checker.alerts.has_key?("Deprecated session secret setting")
end
def test_check_deprecated_session_secret
make_file("config/initializers/", "more_settings.rb", "ActionController::Base.session = {\n:whatever => 'woot'\n}")
@checker.check_old_session_secret
assert @checker.alerts.has_key?("Deprecated session secret setting")
end
def test_check_old_session_setting_finds_nothing
@checker.check_old_session_setting
assert_equal false, @checker.alerts.has_key?("Old session store setting")
end
def test_check_deprecated_session_settings
make_file("config/initializers/", "more_settings.rb", "ActionController::Base.session_store = :cookie\nthings.awesome(:whatever)")
@checker.check_old_session_setting
assert @checker.alerts.has_key?("Old session store setting")
end
def test_check_helpers
make_file("app/views/users/", "test.html.erb", "<b>blah blah blah</b><% form_for(:thing) do |f| %> <label>doo dah</label> <%= f.whatever %> <% end %>")
@checker.check_old_helpers
assert @checker.alerts.has_key?("Deprecated ERb helper calls")
end
def test_check_old_helpers_lets_regular_blocks_pass
make_file("app/views/users/", "another_test.html.erb", "<b>blah blah blah</b><% @some_items.each do |item| %> <label>doo dah</label> <%= item %> <% end %>")
@checker.check_old_helpers
assert_equal @checker.alerts.has_key?("Deprecated ERb helper calls"), false
end
def test_check_old_helpers_lets_regular_blocks_pass
make_file("app/views/users/", "another_test.html.erb", "<b>blah blah blah</b><% @some_items.each do |item| %> <label>doo dah</label> <%= item %> <% end %>")
@checker.check_old_helpers
assert_equal false, @checker.alerts.has_key?("Deprecated ERb helper calls")
end
def test_check_old_ajax_helpers
make_file("app/views/sections", "section.js", "<%= link_to_remote 'section-', :update => 'sections', :url => {:action => :destroy, :controller => 'sections', :id => @section.id } %>")
@checker.check_old_ajax_helpers
assert @checker.alerts.has_key?("Deprecated AJAX helper calls")
end
def test_check_old_ajax_helpers_empty
@checker.check_old_ajax_helpers
assert ! @checker.alerts.has_key?("Deprecated AJAX helper calls")
end
def teardown
clear_files
Dir.chdir(@old_dir)
end
def make_file(where, name=nil, contents=nil)
FileUtils.mkdir_p "#{BASE_ROOT}/#{where}"
File.open("#{BASE_ROOT}/#{where}/#{name}", "w+") do |f|
f.write(contents)
end if name
end
def clear_files
FileUtils.rm_rf(Dir.glob("#{BASE_ROOT}/*"))
end
end

View file

@ -0,0 +1,72 @@
require 'test_helper'
require 'gemfile_generator'
# Stub out methods on upgrader class
module Rails
module Upgrading
class GemfileGenerator
attr_writer :environment_code
def has_environment?
true
end
def environment_code
@environment_code
end
end
end
end
class GemfileGeneratorTest < ActiveSupport::TestCase
PREAMBLE = <<STR
# Edit this Gemfile to bundle your application's dependencies.
# This preamble is the current preamble for Rails 3 apps; edit as needed.
source 'http://rubygems.org'
gem 'rails', '3.0.6'
STR
def test_generates_with_no_gems
generator = Rails::Upgrading::GemfileGenerator.new
generator.environment_code = ""
assert_equal PREAMBLE, generator.generate_gemfile
end
def test_generates_with_gem
generator = Rails::Upgrading::GemfileGenerator.new
generator.environment_code = "config.gem 'camping'"
assert_equal PREAMBLE + "gem 'camping'", generator.generate_gemfile
end
def test_generates_with_version
generator = Rails::Upgrading::GemfileGenerator.new
generator.environment_code = "config.gem 'camping', :version => '2.1.1'"
assert_equal PREAMBLE + "gem 'camping', '2.1.1'", generator.generate_gemfile
end
def test_can_add_sources
generator = Rails::Upgrading::GemfileGenerator.new
generator.environment_code = "config.gem 'camping', :source => 'http://code.whytheluckystiff.net'"
assert_equal PREAMBLE + "source 'http://code.whytheluckystiff.net'\ngem 'camping'", generator.generate_gemfile
end
def test_changes_lib_to_new_key
generator = Rails::Upgrading::GemfileGenerator.new
generator.environment_code = "config.gem 'camping', :lib => 'kamping'"
assert_equal PREAMBLE + "gem 'camping', :require => 'kamping'", generator.generate_gemfile
end
def test_generates_with_all_options
generator = Rails::Upgrading::GemfileGenerator.new
generator.environment_code = "config.gem 'camping', :lib => 'kamping', :source => 'http://code.whytheluckystiff.net', :version => '2.1.1'"
assert_equal PREAMBLE + "source 'http://code.whytheluckystiff.net'\ngem 'camping', '2.1.1', :require => 'kamping'", generator.generate_gemfile
end
end

View file

@ -0,0 +1,63 @@
require 'test_helper'
require 'new_configuration_generator'
# Stub out methods on upgrader class
module Rails
module Upgrading
class NewConfigurationGenerator
attr_writer :environment_code
def has_environment?
true
end
def environment_code
@environment_code
end
def app_name
"my_application"
end
end
end
end
class NewConfigurationGeneratorTest < ActiveSupport::TestCase
FRAME = "# Put this in config/application.rb
require File.expand_path('../boot', __FILE__)
module MyApplication
class Application < Rails::Application
%s
end
end"
CONFIG = " config.what_have_you = 'thing'
config.action_controller = 'what'"
CODE = "require 'w/e'
this_happens_before_the(code)
more_before_the_code!
Rails::Initializer.run do |config|
%s
end
this_is_after_the_code
"
def test_raises_error_with_no_code
generator = Rails::Upgrading::NewConfigurationGenerator.new
generator.environment_code = ""
assert_raises(RuntimeError) { generator.generate_new_application_rb }
end
def test_generates_with_code
generator = Rails::Upgrading::NewConfigurationGenerator.new
generator.environment_code = CODE % [CONFIG]
assert_equal FRAME % [generator.indent(CONFIG)], generator.generate_new_application_rb
end
end

View file

@ -0,0 +1,184 @@
require 'test_helper'
require 'routes_upgrader'
# Stub out methods on upgrader class
module Rails
module Upgrading
class RoutesUpgrader
attr_writer :routes_code
def has_routes_file?
true
end
def routes_code
@routes_code
end
end
class RouteGenerator
def app_name
"MyApplication"
end
end
end
end
class RoutesUpgraderTest < ActiveSupport::TestCase
def setup
Rails::Upgrading::RouteRedrawer.stack = []
end
def test_generates_routes_file
routes_code = "
ActionController::Routing::Routes.draw do |map|
map.connect '/home', :controller => 'home', :action => 'index'
map.login '/login', :controller => 'sessions', :action => 'new'
map.resources :hats
map.resource :store
end
"
new_routes_code = "MyApplication::Application.routes.draw do
match '/home' => 'home#index'
match '/login' => 'sessions#new', :as => :login
resources :hats
resource :store
end
"
upgrader = Rails::Upgrading::RoutesUpgrader.new
upgrader.routes_code = routes_code
result = upgrader.generate_new_routes
assert_equal new_routes_code, result
end
def test_generates_code_for_regular_route
route = Rails::Upgrading::FakeRoute.new("/about", {:controller => 'static', :action => 'about'})
assert_equal "match '/about' => 'static#about'", route.to_route_code
end
def test_generates_code_for_named_route
route = Rails::Upgrading::FakeRoute.new("/about", {:controller => 'static', :action => 'about'}, "about")
assert_equal "match '/about' => 'static#about', :as => :about", route.to_route_code
end
def test_generates_code_for_namespace
ns = Rails::Upgrading::FakeNamespace.new("static")
# Add a route to the namespace
ns << Rails::Upgrading::FakeRoute.new("/about", {:controller => 'static', :action => 'about'})
assert_equal "namespace :static do\nmatch '/about' => 'static#about'\nend\n", ns.to_route_code
end
def test_generates_code_for_namespace_with_options
ns = Rails::Upgrading::FakeNamespace.new("static", { :path_prefix => 'prefix' })
# Add a route to the namespace
ns << Rails::Upgrading::FakeRoute.new("/about", {:controller => 'static', :action => 'about'})
assert_equal "namespace :static, :path_prefix => 'prefix' do\nmatch '/about' => 'static#about'\nend\n", ns.to_route_code
end
def test_generates_code_for_resources
route = Rails::Upgrading::FakeResourceRoute.new("hats")
assert_equal "resources :hats", route.to_route_code
end
def test_generates_code_for_resources
route = Rails::Upgrading::FakeSingletonResourceRoute.new("hat")
assert_equal "resource :hat", route.to_route_code
end
def test_generates_code_for_resources_with_special_methods
route = Rails::Upgrading::FakeResourceRoute.new("hats", {:member => {:wear => :get}, :collection => {:toss => :post}})
assert_equal "resources :hats do\ncollection do\npost :toss\nend\nmember do\nget :wear\nend\n\nend\n", route.to_route_code
end
def test_generates_code_for_resources_with_multiple_special_methods_per_name
route = Rails::Upgrading::FakeResourceRoute.new("hats", {:member => {:wear => [:get, :put]}, :collection => {:toss => [:get, :post]}})
assert_equal "resources :hats do\ncollection do\nget :toss\npost :toss\nend\nmember do\nget :wear\nput :wear\nend\n\nend\n", route.to_route_code
end
def test_generates_code_for_route_with_extra_params
route = Rails::Upgrading::FakeRoute.new("/about", {:controller => 'static', :action => 'about', :something => 'extra'})
assert_equal "match '/about' => 'static#about', :something => 'extra'", route.to_route_code
end
def test_generates_code_for_route_with_requirements
route = Rails::Upgrading::FakeRoute.new("/foo", {:controller => 'foo', :action => 'bar', :requirements => {:digit => /%d/}})
assert_equal "match '/foo' => 'foo#bar', :constraints => { :digit => /%d/ }", route.to_route_code
end
def test_generates_code_for_root
routes_code = "
ActionController::Routing::Routes.draw do |map|
map.root :controller => 'home', :action => 'index'
end
"
new_routes_code = "MyApplication::Application.routes.draw do
match '/' => 'home#index'
end
"
upgrader = Rails::Upgrading::RoutesUpgrader.new
upgrader.routes_code = routes_code
result = upgrader.generate_new_routes
assert_equal new_routes_code, result
end
def test_generates_code_for_default_route
routes_code = "
ActionController::Routing::Routes.draw do |map|
map.connect ':controller/:action/:id.:format'
map.connect ':controller/:action/:id'
end
"
new_routes_code = "MyApplication::Application.routes.draw do
match '/:controller(/:action(/:id))'
end
"
upgrader = Rails::Upgrading::RoutesUpgrader.new
upgrader.routes_code = routes_code
result = upgrader.generate_new_routes
assert_equal new_routes_code, result
end
def test_preserves_resources_except_option
route = Rails::Upgrading::FakeResourceRoute.new("hats", :except => [:index])
assert_equal "resources :hats, :except => [:index]", route.to_route_code
end
def test_preserves_resources_only_option
route = Rails::Upgrading::FakeResourceRoute.new("hats", :only => :show)
assert_equal "resources :hats, :only => :show", route.to_route_code
end
def test_generates_code_for_delete_route
routes_code = %Q{
ActionController::Routing::Routes.draw do |map|
map.sign_out '/sign_out', :controller => 'sessions', :action => 'destroy', :method => :delete
end
}
new_routes_code = %Q{
MyApplication::Application.routes.draw do
match '/sign_out' => 'sessions#destroy', :as => :sign_out, :via => 'delete'
end
}
upgrader = Rails::Upgrading::RoutesUpgrader.new
upgrader.routes_code = routes_code
assert_equal new_routes_code.strip, upgrader.generate_new_routes.strip
end
end

View file

@ -0,0 +1,5 @@
require 'test/unit'
require 'rubygems'
require 'active_support'
require 'active_support/test_case'