diff options
Diffstat (limited to 'railties/test')
-rw-r--r-- | railties/test/application/assets_test.rb | 42 | ||||
-rw-r--r-- | railties/test/application/configuration_test.rb | 15 | ||||
-rw-r--r-- | railties/test/application/rake_test.rb | 26 | ||||
-rw-r--r-- | railties/test/fixtures/lib/generators/wrong_generator.rb | 3 | ||||
-rw-r--r-- | railties/test/generators/app_generator_test.rb | 50 | ||||
-rw-r--r-- | railties/test/generators/assets_generator_test.rb | 10 | ||||
-rw-r--r-- | railties/test/generators/controller_generator_test.rb | 2 | ||||
-rw-r--r-- | railties/test/generators/generated_attribute_test.rb | 13 | ||||
-rw-r--r-- | railties/test/generators/model_generator_test.rb | 12 | ||||
-rw-r--r-- | railties/test/generators/plugin_new_generator_test.rb | 8 | ||||
-rw-r--r-- | railties/test/generators/scaffold_generator_test.rb | 33 | ||||
-rw-r--r-- | railties/test/generators_test.rb | 6 | ||||
-rw-r--r-- | railties/test/isolation/abstract_unit.rb | 4 | ||||
-rw-r--r-- | railties/test/railties/generators_test.rb | 5 | ||||
-rw-r--r-- | railties/test/railties/shared_tests.rb | 9 |
15 files changed, 177 insertions, 61 deletions
diff --git a/railties/test/application/assets_test.rb b/railties/test/application/assets_test.rb index 2b593005a2..e1eee71a9e 100644 --- a/railties/test/application/assets_test.rb +++ b/railties/test/application/assets_test.rb @@ -1,8 +1,9 @@ require 'isolation/abstract_unit' +require 'active_support/core_ext/kernel/reporting' require 'rack/test' module ApplicationTests - class RoutingTest < Test::Unit::TestCase + class AssetsTest < Test::Unit::TestCase include ActiveSupport::Testing::Isolation include Rack::Test::Methods @@ -34,6 +35,45 @@ module ApplicationTests assert_match "alert()", last_response.body end + test "assets do not require compressors until it is used" do + app_file "app/assets/javascripts/demo.js.erb", "<%= :alert %>();" + ENV["RAILS_ENV"] = "production" + require "#{app_path}/config/environment" + + assert !defined?(Uglifier) + get "/assets/demo.js" + assert_match "alert()", last_response.body + assert defined?(Uglifier) + end + + test "assets are compiled properly" do + app_file "app/assets/javascripts/application.js", "alert();" + app_file "app/assets/javascripts/foo/application.js", "alert();" + + capture(:stdout) do + Dir.chdir(app_path){ `bundle exec rake assets:precompile` } + end + files = Dir["#{app_path}/public/assets/application-*.js"] + files << Dir["#{app_path}/public/assets/foo/application-*.js"].first + files.each do |file| + assert_not_nil file, "Expected application.js asset to be generated, but none found" + assert_equal "alert();\n", File.read(file) + end + end + + test "assets are cleaned up properly" do + app_file "public/assets/application.js", "alert();" + app_file "public/assets/application.css", "a { color: green; }" + app_file "public/assets/subdir/broken.png", "not really an image file" + + capture(:stdout) do + Dir.chdir(app_path){ `bundle exec rake assets:clean` } + end + + files = Dir["#{app_path}/public/assets/**/*"] + assert_equal 0, files.length, "Expected no assets, but found #{files.join(', ')}" + end + test "does not stream session cookies back" do app_file "app/assets/javascripts/demo.js.erb", "<%= :alert %>();" diff --git a/railties/test/application/configuration_test.rb b/railties/test/application/configuration_test.rb index 477dada820..2547863d12 100644 --- a/railties/test/application/configuration_test.rb +++ b/railties/test/application/configuration_test.rb @@ -39,6 +39,21 @@ module ApplicationTests FileUtils.rm_rf(new_app) if File.directory?(new_app) end + test "Rails.groups returns available groups" do + require "rails" + + Rails.env = "development" + assert_equal [:default, "development"], Rails.groups + assert_equal [:default, "development", :assets], Rails.groups(:assets => [:development]) + assert_equal [:default, "development", :assets], Rails.groups(:assets => %w(development)) + + Rails.env = "test" + assert_equal [:default, "test"], Rails.groups(:assets => [:development]) + + ENV["RAILS_GROUPS"] = "javascripts,stylesheets" + assert_equal [:default, "test", "javascripts", "stylesheets"], Rails.groups + end + test "Rails.application is nil until app is initialized" do require 'rails' assert_nil Rails.application diff --git a/railties/test/application/rake_test.rb b/railties/test/application/rake_test.rb index 44ac6615de..b61e2851bf 100644 --- a/railties/test/application/rake_test.rb +++ b/railties/test/application/rake_test.rb @@ -59,6 +59,32 @@ module ApplicationTests Dir.chdir(app_path){ `rake stats` } end + def test_rake_test_error_output + Dir.chdir(app_path){ `rake db:migrate` } + + app_file "config/database.yml", <<-RUBY + development: + RUBY + + app_file "test/unit/one_unit_test.rb", <<-RUBY + RUBY + + app_file "test/functional/one_functional_test.rb", <<-RUBY + raise RuntimeError + RUBY + + app_file "test/integration/one_integration_test.rb", <<-RUBY + raise RuntimeError + RUBY + + silence_stderr do + output = Dir.chdir(app_path){ `rake test` } + assert_match /Errors running test:units! #<ActiveRecord::AdapterNotSpecified/, output + assert_match /Errors running test:functionals! #<RuntimeError/, output + assert_match /Errors running test:integration! #<RuntimeError/, output + end + end + def test_rake_routes_output_strips_anchors_from_http_verbs app_file "config/routes.rb", <<-RUBY AppTemplate::Application.routes.draw do diff --git a/railties/test/fixtures/lib/generators/wrong_generator.rb b/railties/test/fixtures/lib/generators/wrong_generator.rb deleted file mode 100644 index 6aa7cb052e..0000000000 --- a/railties/test/fixtures/lib/generators/wrong_generator.rb +++ /dev/null @@ -1,3 +0,0 @@ -# Old generator version -class WrongGenerator < Rails::Generator::NamedBase -end diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb index c31c65a27d..81f0bf5e82 100644 --- a/railties/test/generators/app_generator_test.rb +++ b/railties/test/generators/app_generator_test.rb @@ -10,6 +10,7 @@ DEFAULT_APP_FILES = %w( config.ru app/assets/javascripts app/assets/stylesheets + app/assets/images app/controllers app/helpers app/mailers @@ -22,8 +23,8 @@ DEFAULT_APP_FILES = %w( doc lib lib/tasks + lib/assets log - app/assets/images script/rails test/fixtures test/functional @@ -47,11 +48,12 @@ class AppGeneratorTest < Rails::Generators::TestCase ::DEFAULT_APP_FILES end - def test_application_controller_and_layout_files + def test_assets run_generator assert_file "app/views/layouts/application.html.erb", /stylesheet_link_tag\s+"application"/ assert_file "app/views/layouts/application.html.erb", /javascript_include_tag\s+"application"/ assert_file "app/assets/stylesheets/application.css" + assert_file "config/application.rb", /config\.assets\.enabled = true/ end def test_invalid_application_name_raises_an_error @@ -123,18 +125,26 @@ class AppGeneratorTest < Rails::Generators::TestCase def test_config_database_is_added_by_default run_generator assert_file "config/database.yml", /sqlite3/ - assert_file "Gemfile", /^gem\s+["']sqlite3["']$/ + unless defined?(JRUBY_VERSION) + assert_file "Gemfile", /^gem\s+["']sqlite3["']$/ + else + assert_file "Gemfile", /^gem\s+["']activerecord-jdbcsqlite3-adapter["']$/ + end end def test_config_another_database run_generator([destination_root, "-d", "mysql"]) assert_file "config/database.yml", /mysql/ - assert_file "Gemfile", /^gem\s+["']mysql2["']$/ + unless defined?(JRUBY_VERSION) + assert_file "Gemfile", /^gem\s+["']mysql2["']$/ + else + assert_file "Gemfile", /^gem\s+["']activerecord-jdbcmysql-adapter["']$/ + end end def test_config_jdbcmysql_database run_generator([destination_root, "-d", "jdbcmysql"]) - assert_file "config/database.yml", /jdbcmysql/ + assert_file "config/database.yml", /mysql/ assert_file "Gemfile", /^gem\s+["']activerecord-jdbcmysql-adapter["']$/ # TODO: When the JRuby guys merge jruby-openssl in # jruby this will be removed @@ -143,28 +153,48 @@ class AppGeneratorTest < Rails::Generators::TestCase def test_config_jdbcsqlite3_database run_generator([destination_root, "-d", "jdbcsqlite3"]) - assert_file "config/database.yml", /jdbcsqlite3/ + assert_file "config/database.yml", /sqlite3/ assert_file "Gemfile", /^gem\s+["']activerecord-jdbcsqlite3-adapter["']$/ end def test_config_jdbcpostgresql_database run_generator([destination_root, "-d", "jdbcpostgresql"]) - assert_file "config/database.yml", /jdbcpostgresql/ + assert_file "config/database.yml", /postgresql/ assert_file "Gemfile", /^gem\s+["']activerecord-jdbcpostgresql-adapter["']$/ end + def test_config_jdbc_database + run_generator([destination_root, "-d", "jdbc"]) + assert_file "config/database.yml", /jdbc/ + assert_file "config/database.yml", /mssql/ + assert_file "Gemfile", /^gem\s+["']activerecord-jdbc-adapter["']$/ + end + + def test_config_jdbc_database_when_no_option_given + if defined?(JRUBY_VERSION) + run_generator([destination_root]) + assert_file "config/database.yml", /sqlite3/ + assert_file "Gemfile", /^gem\s+["']activerecord-jdbcsqlite3-adapter["']$/ + end + end + def test_generator_if_skip_active_record_is_given run_generator [destination_root, "--skip-active-record"] assert_no_file "config/database.yml" + assert_file "config/application.rb", /#\s+require\s+["']active_record\/railtie["']/ assert_file "test/test_helper.rb" do |helper_content| assert_no_match(/fixtures :all/, helper_content) end assert_file "test/performance/browsing_test.rb" end - def test_active_record_is_removed_from_frameworks_if_skip_active_record_is_given - run_generator [destination_root, "--skip-active-record"] - assert_file "config/application.rb", /#\s+require\s+["']active_record\/railtie["']/ + def test_generator_if_skip_active_record_is_given + run_generator [destination_root, "--skip-sprockets"] + assert_file "config/application.rb" do |content| + assert_match(/#\s+require\s+["']sprockets\/railtie["']/, content) + assert_no_match(/config\.assets\.enabled = true/, content) + end + assert_file "test/performance/browsing_test.rb" end def test_creation_of_a_test_directory diff --git a/railties/test/generators/assets_generator_test.rb b/railties/test/generators/assets_generator_test.rb index 2d20982d04..044e0b6bc6 100644 --- a/railties/test/generators/assets_generator_test.rb +++ b/railties/test/generators/assets_generator_test.rb @@ -8,19 +8,13 @@ class AssetsGeneratorTest < Rails::Generators::TestCase def test_assets run_generator - assert_file "app/assets/javascripts/posts.js.coffee" + assert_file "app/assets/javascripts/posts.js" assert_file "app/assets/stylesheets/posts.css" end def test_skipping_assets content = run_generator ["posts", "--no-stylesheets", "--no-javascripts"] - assert_no_file "app/assets/javascripts/posts.js.coffee" + assert_no_file "app/assets/javascripts/posts.js" assert_no_file "app/assets/stylesheets/posts.css" end - - def test_vanilla_assets - run_generator ["posts", "--no-javascript-engine"] - assert_file "app/assets/javascripts/posts.js" - assert_file "app/assets/stylesheets/posts.css" - end end diff --git a/railties/test/generators/controller_generator_test.rb b/railties/test/generators/controller_generator_test.rb index 3adf7be118..c3fa9ebb03 100644 --- a/railties/test/generators/controller_generator_test.rb +++ b/railties/test/generators/controller_generator_test.rb @@ -39,7 +39,7 @@ class ControllerGeneratorTest < Rails::Generators::TestCase def test_invokes_assets run_generator - assert_file "app/assets/javascripts/account.js.coffee" + assert_file "app/assets/javascripts/account.js" assert_file "app/assets/stylesheets/account.css" end diff --git a/railties/test/generators/generated_attribute_test.rb b/railties/test/generators/generated_attribute_test.rb index 0d2e624f44..c9f8ab0a7b 100644 --- a/railties/test/generators/generated_attribute_test.rb +++ b/railties/test/generators/generated_attribute_test.rb @@ -113,15 +113,8 @@ class GeneratedAttributeTest < Rails::Generators::TestCase end end - def test_nil_type_raises_exception - assert_raise Thor::Error do - create_generated_attribute(nil, 'title') - end - end - - def test_missing_type_raises_exception - assert_raise Thor::Error do - create_generated_attribute('', 'title') - end + def test_blank_type_defaults_to_string_raises_exception + assert_equal :string, create_generated_attribute(nil, 'title').type + assert_equal :string, create_generated_attribute("", 'title').type end end diff --git a/railties/test/generators/model_generator_test.rb b/railties/test/generators/model_generator_test.rb index 8c5ba9926b..1b0cb425c6 100644 --- a/railties/test/generators/model_generator_test.rb +++ b/railties/test/generators/model_generator_test.rb @@ -12,9 +12,15 @@ class ModelGeneratorTest < Rails::Generators::TestCase end def test_model_with_missing_attribute_type - content = capture(:stderr) { run_generator ["post", "title:string", "body"] } - assert_match(/Missing type for attribute 'body'/, content) - assert_match(/Example: 'body:string' where string is the type/, content) + run_generator ["post", "title", "body:text", "author"] + + assert_migration "db/migrate/create_posts.rb" do |m| + assert_method :change, m do |up| + assert_match(/t\.string :title/, up) + assert_match(/t\.text :body/, up) + assert_match(/t\.string :author/, up) + end + end end def test_invokes_default_orm diff --git a/railties/test/generators/plugin_new_generator_test.rb b/railties/test/generators/plugin_new_generator_test.rb index be72391e58..4bd77ff7e3 100644 --- a/railties/test/generators/plugin_new_generator_test.rb +++ b/railties/test/generators/plugin_new_generator_test.rb @@ -159,6 +159,14 @@ class PluginNewGeneratorTest < Rails::Generators::TestCase assert_match(/1 tests, 1 assertions, 0 failures, 0 errors/, `bundle exec rake test`) end + def test_ensure_that_migration_tasks_work_with_mountable_option + run_generator [destination_root, "--mountable"] + FileUtils.cd destination_root + quietly { system 'bundle install' } + `bundle exec rake db:migrate` + assert_equal 0, $?.exitstatus + end + def test_creating_engine_in_full_mode run_generator [destination_root, "--full"] assert_file "app/assets/javascripts/bukkits" diff --git a/railties/test/generators/scaffold_generator_test.rb b/railties/test/generators/scaffold_generator_test.rb index 2135ffac81..2db8090621 100644 --- a/railties/test/generators/scaffold_generator_test.rb +++ b/railties/test/generators/scaffold_generator_test.rb @@ -81,7 +81,7 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase # Assets assert_file "app/assets/stylesheets/scaffold.css" - assert_file "app/assets/javascripts/product_lines.js.coffee" + assert_file "app/assets/javascripts/product_lines.js" assert_file "app/assets/stylesheets/product_lines.css" end @@ -114,7 +114,7 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase # Assets assert_file "app/assets/stylesheets/scaffold.css", /:visited/ - assert_no_file "app/assets/javascripts/product_lines.js.coffee" + assert_no_file "app/assets/javascripts/product_lines.js" assert_no_file "app/assets/stylesheets/product_lines.css" end @@ -190,7 +190,7 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase # Assets assert_file "app/assets/stylesheets/scaffold.css", /:visited/ - assert_file "app/assets/javascripts/admin/roles.js.coffee" + assert_file "app/assets/javascripts/admin/roles.js" assert_file "app/assets/stylesheets/admin/roles.css" end @@ -224,7 +224,7 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase # Assets assert_file "app/assets/stylesheets/scaffold.css" - assert_no_file "app/assets/javascripts/admin/roles.js.coffee" + assert_no_file "app/assets/javascripts/admin/roles.js" assert_no_file "app/assets/stylesheets/admin/roles.css" end @@ -246,34 +246,33 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase def test_scaffold_generator_no_assets run_generator [ "posts", "--no-assets" ] assert_file "app/assets/stylesheets/scaffold.css" - assert_no_file "app/assets/javascripts/posts.js.coffee" + assert_no_file "app/assets/javascripts/posts.js" assert_no_file "app/assets/stylesheets/posts.css" end def test_scaffold_generator_no_stylesheets run_generator [ "posts", "--no-stylesheets" ] assert_no_file "app/assets/stylesheets/scaffold.css" - assert_file "app/assets/javascripts/posts.js.coffee" + assert_file "app/assets/javascripts/posts.js" assert_no_file "app/assets/stylesheets/posts.css" end def test_scaffold_generator_no_javascripts run_generator [ "posts", "--no-javascripts" ] assert_file "app/assets/stylesheets/scaffold.css" - assert_no_file "app/assets/javascripts/posts.js.coffee" - assert_file "app/assets/stylesheets/posts.css" - end - - def test_scaffold_generator_no_engines - run_generator [ "posts", "--no-javascript-engine" ] - assert_file "app/assets/stylesheets/scaffold.css" - assert_file "app/assets/javascripts/posts.js" + assert_no_file "app/assets/javascripts/posts.js" assert_file "app/assets/stylesheets/posts.css" end def test_scaffold_generator_outputs_error_message_on_missing_attribute_type - content = capture(:stderr) { run_generator ["post", "title:string", "body"]} - assert_match(/Missing type for attribute 'body'/, content) - assert_match(/Example: 'body:string' where string is the type/, content) + run_generator ["post", "title", "body:text", "author"] + + assert_migration "db/migrate/create_posts.rb" do |m| + assert_method :change, m do |up| + assert_match(/t\.string :title/, up) + assert_match(/t\.text :body/, up) + assert_match(/t\.string :author/, up) + end + end end end diff --git a/railties/test/generators_test.rb b/railties/test/generators_test.rb index 301ae80bcf..56329f3183 100644 --- a/railties/test/generators_test.rb +++ b/railties/test/generators_test.rb @@ -88,12 +88,6 @@ class GeneratorsTest < Rails::Generators::TestCase assert Rails::Generators.find_by_namespace(:model) end - def test_find_by_namespace_show_warning_if_generator_cant_be_loaded - output = capture(:stderr) { Rails::Generators.find_by_namespace(:wrong) } - assert_match(/\[WARNING\] Could not load generator/, output) - assert_match(/Rails 2\.x generator/, output) - end - def test_invoke_with_nested_namespaces model_generator = mock('ModelGenerator') do expects(:start).with(["Account"], {}) diff --git a/railties/test/isolation/abstract_unit.rb b/railties/test/isolation/abstract_unit.rb index 0a203fd4d0..685d1b154b 100644 --- a/railties/test/isolation/abstract_unit.rb +++ b/railties/test/isolation/abstract_unit.rb @@ -238,6 +238,10 @@ module TestHelpers end end + def remove_file(path) + FileUtils.rm_rf "#{app_path}/#{path}" + end + def controller(name, contents) app_file("app/controllers/#{name}_controller.rb", contents) end diff --git a/railties/test/railties/generators_test.rb b/railties/test/railties/generators_test.rb index fe59dcd52b..1d4ba0e5b6 100644 --- a/railties/test/railties/generators_test.rb +++ b/railties/test/railties/generators_test.rb @@ -1,7 +1,8 @@ RAILS_ISOLATED_ENGINE = true require "isolation/abstract_unit" -require "#{RAILS_FRAMEWORK_ROOT}/railties/lib/rails/generators/test_case" +require 'generators/generators_test_helper' +require "rails/generators/test_case" module RailtiesTests class GeneratorTest < Rails::Generators::TestCase @@ -31,8 +32,8 @@ module RailtiesTests end def build_engine(is_mountable=false) + FileUtils.rm_rf(engine_path) FileUtils.mkdir_p(engine_path) - FileUtils.rm_r(engine_path) mountable = is_mountable ? "--mountable" : "" diff --git a/railties/test/railties/shared_tests.rb b/railties/test/railties/shared_tests.rb index 8cbc76db71..9a64b7c64e 100644 --- a/railties/test/railties/shared_tests.rb +++ b/railties/test/railties/shared_tests.rb @@ -84,6 +84,15 @@ module RailtiesTest end end + def test_no_rake_task_without_migrations + boot_rails + require 'rake' + require 'rdoc/task' + require 'rake/testtask' + Rails.application.load_tasks + assert !Rake::Task.task_defined?('bukkits:install:migrations') + end + def test_puts_its_lib_directory_on_load_path boot_rails require "another" |