diff options
Diffstat (limited to 'railties/test/generators')
12 files changed, 299 insertions, 135 deletions
diff --git a/railties/test/generators/actions_test.rb b/railties/test/generators/actions_test.rb index a8c8fcd5b7..bc086c5986 100644 --- a/railties/test/generators/actions_test.rb +++ b/railties/test/generators/actions_test.rb @@ -67,6 +67,14 @@ class ActionsTest < Rails::Generators::TestCase assert_file 'Gemfile', /^gem "rspec-rails"$/ end + def test_gem_should_include_options + run_generator + + action :gem, 'rspec', github: 'dchelimsky/rspec', tag: '1.2.9.rc1' + + assert_file 'Gemfile', /gem "rspec", github: "dchelimsky\/rspec", tag: "1\.2\.9\.rc1"/ + end + def test_gem_group_should_wrap_gems_in_a_group run_generator diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb index 55c7ce1047..e7b6a20b32 100644 --- a/railties/test/generators/app_generator_test.rb +++ b/railties/test/generators/app_generator_test.rb @@ -83,6 +83,16 @@ class AppGeneratorTest < Rails::Generators::TestCase assert_equal false, $?.success? end + def test_application_new_show_help_message_inside_existing_rails_directory + app_root = File.join(destination_root, 'myfirstapp') + run_generator [app_root] + output = Dir.chdir(app_root) do + `rails new --help` + end + assert_match(/rails new APP_PATH \[options\]/, output) + assert_equal true, $?.success? + end + def test_application_name_is_detected_if_it_exists_and_app_folder_renamed app_root = File.join(destination_root, "myapp") app_moved_root = File.join(destination_root, "myapp_moved") @@ -136,9 +146,9 @@ class AppGeneratorTest < Rails::Generators::TestCase run_generator assert_file "config/database.yml", /sqlite3/ unless defined?(JRUBY_VERSION) - assert_file "Gemfile", /^gem\s+["']sqlite3["']$/ + assert_gem "sqlite3" else - assert_file "Gemfile", /^gem\s+["']activerecord-jdbcsqlite3-adapter["']$/ + assert_gem "activerecord-jdbcsqlite3-adapter" end end @@ -146,9 +156,9 @@ class AppGeneratorTest < Rails::Generators::TestCase run_generator([destination_root, "-d", "mysql"]) assert_file "config/database.yml", /mysql/ unless defined?(JRUBY_VERSION) - assert_file "Gemfile", /^gem\s+["']mysql2["']$/ + assert_gem "mysql2" else - assert_file "Gemfile", /^gem\s+["']activerecord-jdbcmysql-adapter["']$/ + assert_gem "activerecord-jdbcmysql-adapter" end end @@ -156,45 +166,45 @@ class AppGeneratorTest < Rails::Generators::TestCase run_generator([destination_root, "-d", "postgresql"]) assert_file "config/database.yml", /postgresql/ unless defined?(JRUBY_VERSION) - assert_file "Gemfile", /^gem\s+["']pg["']$/ + assert_gem "pg" else - assert_file "Gemfile", /^gem\s+["']activerecord-jdbcpostgresql-adapter["']$/ + assert_gem "activerecord-jdbcpostgresql-adapter" end end def test_config_jdbcmysql_database run_generator([destination_root, "-d", "jdbcmysql"]) assert_file "config/database.yml", /mysql/ - assert_file "Gemfile", /^gem\s+["']activerecord-jdbcmysql-adapter["']$/ + assert_gem "activerecord-jdbcmysql-adapter" # TODO: When the JRuby guys merge jruby-openssl in # jruby this will be removed - assert_file "Gemfile", /^gem\s+["']jruby-openssl["']$/ if defined?(JRUBY_VERSION) + assert_gem "jruby-openssl" if defined?(JRUBY_VERSION) end def test_config_jdbcsqlite3_database run_generator([destination_root, "-d", "jdbcsqlite3"]) assert_file "config/database.yml", /sqlite3/ - assert_file "Gemfile", /^gem\s+["']activerecord-jdbcsqlite3-adapter["']$/ + assert_gem "activerecord-jdbcsqlite3-adapter" end def test_config_jdbcpostgresql_database run_generator([destination_root, "-d", "jdbcpostgresql"]) assert_file "config/database.yml", /postgresql/ - assert_file "Gemfile", /^gem\s+["']activerecord-jdbcpostgresql-adapter["']$/ + assert_gem "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["']$/ + assert_gem "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["']$/ + assert_gem "activerecord-jdbcsqlite3-adapter" end end @@ -202,7 +212,6 @@ class AppGeneratorTest < Rails::Generators::TestCase 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 "config/application.rb", /#\s+config\.active_record\.dependent_restrict_raises = false/ assert_file "test/test_helper.rb" do |helper_content| assert_no_match(/fixtures :all/, helper_content) end @@ -212,7 +221,7 @@ class AppGeneratorTest < Rails::Generators::TestCase def test_generator_if_skip_sprockets_is_given run_generator [destination_root, "--skip-sprockets"] assert_file "config/application.rb" do |content| - assert_match(/#\s+require\s+["']sprockets\/railtie["']/, content) + assert_match(/#\s+require\s+["']sprockets\/rails\/railtie["']/, content) assert_no_match(/config\.assets\.enabled = true/, content) end assert_file "Gemfile" do |content| @@ -233,12 +242,19 @@ class AppGeneratorTest < Rails::Generators::TestCase def test_inclusion_of_javascript_runtime run_generator([destination_root]) if defined?(JRUBY_VERSION) - assert_file "Gemfile", /gem\s+["']therubyrhino["']$/ + assert_gem "therubyrhino" else - assert_file "Gemfile", /# gem\s+["']therubyracer["']$/ + assert_file "Gemfile", /# gem\s+["']therubyracer["']+, platforms: :ruby$/ end end + def test_generator_if_skip_index_html_is_given + run_generator [destination_root, '--skip-index-html'] + assert_no_file 'public/index.html' + assert_no_file 'app/assets/images/rails.png' + assert_file 'app/assets/images/.keep' + end + def test_creation_of_a_test_directory run_generator assert_file 'test' @@ -260,9 +276,7 @@ class AppGeneratorTest < Rails::Generators::TestCase assert_match %r{^//= require jquery}, contents assert_match %r{^//= require jquery_ujs}, contents end - assert_file 'Gemfile' do |contents| - assert_match(/^gem 'jquery-rails'/, contents) - end + assert_gem "jquery-rails" end def test_other_javascript_libraries @@ -271,9 +285,7 @@ class AppGeneratorTest < Rails::Generators::TestCase assert_match %r{^//= require prototype}, contents assert_match %r{^//= require prototype_ujs}, contents end - assert_file 'Gemfile' do |contents| - assert_match(/^gem 'prototype-rails'/, contents) - end + assert_gem "prototype-rails" end def test_javascript_is_skipped_if_required @@ -283,11 +295,9 @@ class AppGeneratorTest < Rails::Generators::TestCase end end - def test_inclusion_of_ruby_debug19 + def test_inclusion_of_debugger run_generator - assert_file "Gemfile" do |contents| - assert_match(/gem 'ruby-debug19', :require => 'ruby-debug'/, contents) - end + assert_file "Gemfile", /# gem 'debugger'/ end def test_template_from_dir_pwd @@ -301,7 +311,7 @@ class AppGeneratorTest < Rails::Generators::TestCase end def test_default_usage - File.expects(:exist?).returns(false) + Rails::Generators::AppGenerator.expects(:usage_path).returns(nil) assert_match(/Create rails files for app generator/, Rails::Generators::AppGenerator.desc) end @@ -325,11 +335,6 @@ class AppGeneratorTest < Rails::Generators::TestCase assert_file "config/application.rb", /#\s+require\s+["']active_record\/railtie["']/ end - def test_default_method_for_update_is_not_patch - run_generator [destination_root, "--skip-test-unit", "--skip-active-record"] - assert_file "config/application.rb", /#\s+config\.default_method_for_update\s+=\s+:patch/ - end - def test_new_hash_style run_generator [destination_root] assert_file "config/initializers/session_store.rb" do |file| @@ -337,15 +342,6 @@ class AppGeneratorTest < Rails::Generators::TestCase end end - def test_generated_environments_file_for_sanitizer - run_generator [destination_root, "--skip-active-record"] - %w(development test).each do |env| - assert_file "config/environments/#{env}.rb" do |file| - assert_no_match(/config.active_record.mass_assignment_sanitizer = :strict/, file) - end - end - end - def test_generated_environments_file_for_auto_explain run_generator [destination_root, "--skip-active-record"] %w(development production).each do |env| @@ -355,11 +351,6 @@ class AppGeneratorTest < Rails::Generators::TestCase end end - def test_active_record_dependent_restrict_raises_is_present_application_config - run_generator - assert_file "config/application.rb", /config\.active_record\.dependent_restrict_raises = false/ - end - def test_pretend_option output = run_generator [File.join(destination_root, "myapp"), "--pretend"] assert_no_match(/run bundle install/, output) @@ -371,6 +362,9 @@ protected silence(:stdout) { generator.send(*args, &block) } end + def assert_gem(gem) + assert_file "Gemfile", /^gem\s+["']#{gem}["']$/ + end end class CustomAppGeneratorTest < Rails::Generators::TestCase diff --git a/railties/test/generators/assets_generator_test.rb b/railties/test/generators/assets_generator_test.rb index d6338bd3da..a2b94f2e50 100644 --- a/railties/test/generators/assets_generator_test.rb +++ b/railties/test/generators/assets_generator_test.rb @@ -1,7 +1,6 @@ require 'generators/generators_test_helper' require 'rails/generators/rails/assets/assets_generator' -# FIXME: Silence the 'Could not find task "using_coffee?"' message in tests due to the public stub class AssetsGeneratorTest < Rails::Generators::TestCase include GeneratorsTestHelper arguments %w(posts) diff --git a/railties/test/generators/generated_attribute_test.rb b/railties/test/generators/generated_attribute_test.rb index 6e3fc84781..6ab1cd58c7 100644 --- a/railties/test/generators/generated_attribute_test.rb +++ b/railties/test/generators/generated_attribute_test.rb @@ -102,22 +102,28 @@ class GeneratedAttributeTest < Rails::Generators::TestCase def test_reference_is_true %w(references belongs_to).each do |attribute_type| - assert_equal( - true, - create_generated_attribute(attribute_type).reference? - ) + assert create_generated_attribute(attribute_type).reference? end end def test_reference_is_false %w(foo bar baz).each do |attribute_type| - assert_equal( - false, - create_generated_attribute(attribute_type).reference? - ) + assert !create_generated_attribute(attribute_type).reference? end end + def test_polymorphic_reference_is_true + %w(references belongs_to).each do |attribute_type| + assert create_generated_attribute("#{attribute_type}{polymorphic}").polymorphic? + end + end + + def test_polymorphic_reference_is_false + %w(foo bar baz).each do |attribute_type| + assert !create_generated_attribute("#{attribute_type}{polymorphic}").polymorphic? + end + 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 @@ -126,5 +132,6 @@ class GeneratedAttributeTest < Rails::Generators::TestCase def test_handles_index_names_for_references assert_equal "post", create_generated_attribute('string', 'post').index_name assert_equal "post_id", create_generated_attribute('references', 'post').index_name + assert_equal ["post_id", "post_type"], create_generated_attribute('references{polymorphic}', 'post').index_name end end diff --git a/railties/test/generators/migration_generator_test.rb b/railties/test/generators/migration_generator_test.rb index 4e08e5dae1..15e5a0b92b 100644 --- a/railties/test/generators/migration_generator_test.rb +++ b/railties/test/generators/migration_generator_test.rb @@ -28,6 +28,13 @@ class MigrationGeneratorTest < Rails::Generators::TestCase run_generator [migration] assert_migration "db/migrate/change_title_body_from_posts.rb", /class #{migration} < ActiveRecord::Migration/ end + + def test_migration_with_invalid_file_name + migration = "add_something:datetime" + assert_raise ActiveRecord::IllegalMigrationNameError do + run_generator [migration] + end + end def test_add_migration_with_attributes migration = "add_title_body_to_posts" @@ -41,6 +48,24 @@ class MigrationGeneratorTest < Rails::Generators::TestCase end end + def test_remove_migration_with_indexed_attribute + migration = "remove_title_body_from_posts" + run_generator [migration, "title:string:index", "body:text"] + + assert_migration "db/migrate/#{migration}.rb" do |content| + assert_method :up, content do |up| + assert_match(/remove_column :posts, :title/, up) + assert_match(/remove_column :posts, :body/, up) + end + + assert_method :down, content do |down| + assert_match(/add_column :posts, :title, :string/, down) + assert_match(/add_column :posts, :body, :text/, down) + assert_match(/add_index :posts, :title/, down) + end + end + end + def test_remove_migration_with_attributes migration = "remove_title_body_from_posts" run_generator [migration, "title:string", "body:text"] @@ -58,6 +83,23 @@ class MigrationGeneratorTest < Rails::Generators::TestCase end end + def test_remove_migration_with_references_options + migration = "remove_references_from_books" + run_generator [migration, "author:belongs_to", "distributor:references{polymorphic}"] + + assert_migration "db/migrate/#{migration}.rb" do |content| + assert_method :up, content do |up| + assert_match(/remove_reference :books, :author/, up) + assert_match(/remove_reference :books, :distributor, polymorphic: true/, up) + end + + assert_method :down, content do |down| + assert_match(/add_reference :books, :author, index: true/, down) + assert_match(/add_reference :books, :distributor, polymorphic: true, index: true/, down) + end + end + end + def test_add_migration_with_attributes_and_indices migration = "add_title_with_index_and_body_to_posts" run_generator [migration, "title:string:index", "body:text", "user_id:integer:uniq"] @@ -120,6 +162,31 @@ class MigrationGeneratorTest < Rails::Generators::TestCase end end + def test_add_migration_with_references_options + migration = "add_references_to_books" + run_generator [migration, "author:belongs_to", "distributor:references{polymorphic}"] + + assert_migration "db/migrate/#{migration}.rb" do |content| + assert_method :change, content do |up| + assert_match(/add_reference :books, :author, index: true/, up) + assert_match(/add_reference :books, :distributor, polymorphic: true, index: true/, up) + end + end + end + + def test_create_join_table_migration + migration = "add_media_join_table" + run_generator [migration, "artist_id", "musics:uniq"] + + assert_migration "db/migrate/#{migration}.rb" do |content| + assert_method :change, content do |up| + assert_match(/create_join_table :artists, :musics/, up) + assert_match(/# t.index \[:artist_id, :music_id\]/, up) + assert_match(/ t.index \[:music_id, :artist_id\], unique: true/, up) + end + end + end + def test_should_create_empty_migrations_if_name_not_start_with_add_or_remove migration = "create_books" run_generator [migration, "title:string", "content:text"] @@ -134,4 +201,8 @@ class MigrationGeneratorTest < Rails::Generators::TestCase end end end + + def test_properly_identifies_usage_file + assert generator_class.send(:usage_path) + end end diff --git a/railties/test/generators/model_generator_test.rb b/railties/test/generators/model_generator_test.rb index 156fa86eee..436de26826 100644 --- a/railties/test/generators/model_generator_test.rb +++ b/railties/test/generators/model_generator_test.rb @@ -166,7 +166,7 @@ class ModelGeneratorTest < Rails::Generators::TestCase end def test_add_migration_with_attributes_index_declaration_and_attribute_options - run_generator ["product", "title:string{40}:index", "content:string{255}", "price:decimal{5,2}:index", "discount:decimal{5,2}:uniq"] + run_generator ["product", "title:string{40}:index", "content:string{255}", "price:decimal{5,2}:index", "discount:decimal{5,2}:uniq", "supplier:references{polymorphic}"] assert_migration "db/migrate/create_products.rb" do |content| assert_method :change, content do |up| @@ -174,6 +174,7 @@ class ModelGeneratorTest < Rails::Generators::TestCase assert_match(/t.string :title, limit: 40/, up) assert_match(/t.string :content, limit: 255/, up) assert_match(/t.decimal :price, precision: 5, scale: 2/, up) + assert_match(/t.references :supplier, polymorphic: true/, up) end assert_match(/add_index :products, :title/, content) assert_match(/add_index :products, :price/, content) @@ -193,15 +194,25 @@ class ModelGeneratorTest < Rails::Generators::TestCase end def test_model_with_references_attribute_generates_belongs_to_associations - run_generator ["product", "name:string", "supplier_id:references"] + run_generator ["product", "name:string", "supplier:references"] assert_file "app/models/product.rb", /belongs_to :supplier/ end def test_model_with_belongs_to_attribute_generates_belongs_to_associations - run_generator ["product", "name:string", "supplier_id:belongs_to"] + run_generator ["product", "name:string", "supplier:belongs_to"] assert_file "app/models/product.rb", /belongs_to :supplier/ end + def test_model_with_polymorphic_references_attribute_generates_belongs_to_associations + run_generator ["product", "name:string", "supplier:references{polymorphic}"] + assert_file "app/models/product.rb", /belongs_to :supplier, polymorphic: true/ + end + + def test_model_with_polymorphic_belongs_to_attribute_generates_belongs_to_associations + run_generator ["product", "name:string", "supplier:belongs_to{polymorphic}"] + assert_file "app/models/product.rb", /belongs_to :supplier, polymorphic: true/ + end + def test_migration_with_timestamps run_generator assert_migration "db/migrate/create_accounts.rb", /t.timestamps/ @@ -283,7 +294,7 @@ class ModelGeneratorTest < Rails::Generators::TestCase assert_migration "db/migrate/create_accounts.rb" do |m| assert_method :change, m do |up| - assert_match(/add_index/, up) + assert_match(/index: true/, up) end end end @@ -293,7 +304,7 @@ class ModelGeneratorTest < Rails::Generators::TestCase assert_migration "db/migrate/create_accounts.rb" do |m| assert_method :change, m do |up| - assert_match(/add_index/, up) + assert_match(/index: true/, up) end end end @@ -303,7 +314,7 @@ class ModelGeneratorTest < Rails::Generators::TestCase assert_migration "db/migrate/create_accounts.rb" do |m| assert_method :change, m do |up| - assert_no_match(/add_index/, up) + assert_no_match(/index: true/, up) end end end @@ -313,7 +324,7 @@ class ModelGeneratorTest < Rails::Generators::TestCase assert_migration "db/migrate/create_accounts.rb" do |m| assert_method :change, m do |up| - assert_no_match(/add_index/, up) + assert_no_match(/index: true/, up) end end end diff --git a/railties/test/generators/namespaced_generators_test.rb b/railties/test/generators/namespaced_generators_test.rb index 5c63b13dce..db2b8af217 100644 --- a/railties/test/generators/namespaced_generators_test.rb +++ b/railties/test/generators/namespaced_generators_test.rb @@ -20,8 +20,14 @@ class NamespacedControllerGeneratorTest < NamespacedGeneratorTestCase def test_namespaced_controller_skeleton_is_created run_generator - assert_file "app/controllers/test_app/account_controller.rb", /module TestApp/, / class AccountController < ApplicationController/ - assert_file "test/functional/test_app/account_controller_test.rb", /module TestApp/, / class AccountControllerTest/ + assert_file "app/controllers/test_app/account_controller.rb", + /require_dependency "test_app\/application_controller"/, + /module TestApp/, + / class AccountController < ApplicationController/ + + assert_file "test/functional/test_app/account_controller_test.rb", + /module TestApp/, + / class AccountControllerTest/ end def test_skipping_namespace @@ -32,7 +38,9 @@ class NamespacedControllerGeneratorTest < NamespacedGeneratorTestCase def test_namespaced_controller_with_additional_namespace run_generator ["admin/account"] - assert_file "app/controllers/test_app/admin/account_controller.rb", /module TestApp/, / class Admin::AccountController < ApplicationController/ + assert_file "app/controllers/test_app/admin/account_controller.rb", /module TestApp/, / class Admin::AccountController < ApplicationController/ do |contents| + assert_match %r(require_dependency "test_app/application_controller"), contents + end end def test_helpr_is_also_namespaced @@ -56,11 +64,20 @@ class NamespacedControllerGeneratorTest < NamespacedGeneratorTestCase run_generator assert_file "config/routes.rb", /get "account\/foo"/, /get "account\/bar"/ end -# + def test_invokes_default_template_engine_even_with_no_action run_generator ["account"] assert_file "app/views/test_app/account" end + + def test_namespaced_controller_dont_indent_blank_lines + run_generator + assert_file "app/controllers/test_app/account_controller.rb" do |content| + content.split("\n").each do |line| + assert_no_match(/^\s+$/, line, "Don't indent blank lines") + end + end + end end class NamespacedModelGeneratorTest < NamespacedGeneratorTestCase @@ -82,7 +99,7 @@ class NamespacedModelGeneratorTest < NamespacedGeneratorTestCase run_generator ["admin/account"] assert_file "app/models/test_app/admin.rb", /module TestApp/, /module Admin/ assert_file "app/models/test_app/admin.rb", /def self\.table_name_prefix/ - assert_file "app/models/test_app/admin.rb", /'admin_'/ + assert_file "app/models/test_app/admin.rb", /'test_app_admin_'/ assert_file "app/models/test_app/admin/account.rb", /module TestApp/, /class Admin::Account < ActiveRecord::Base/ end @@ -218,9 +235,10 @@ class NamespacedScaffoldGeneratorTest < NamespacedGeneratorTestCase end # Controller - assert_file "app/controllers/test_app/product_lines_controller.rb" do |content| - assert_match(/module TestApp\n class ProductLinesController < ApplicationController/, content) - end + assert_file "app/controllers/test_app/product_lines_controller.rb", + /require_dependency "test_app\/application_controller"/, + /module TestApp/, + /class ProductLinesController < ApplicationController/ assert_file "test/functional/test_app/product_lines_controller_test.rb", /module TestApp\n class ProductLinesControllerTest < ActionController::TestCase/ diff --git a/railties/test/generators/plugin_new_generator_test.rb b/railties/test/generators/plugin_new_generator_test.rb index 4bb5f04a79..bfb20dad2d 100644 --- a/railties/test/generators/plugin_new_generator_test.rb +++ b/railties/test/generators/plugin_new_generator_test.rb @@ -32,7 +32,7 @@ class PluginNewGeneratorTest < Rails::Generators::TestCase content = capture(:stderr){ run_generator [File.join(destination_root, "things4.3")] } assert_equal "Invalid plugin name things4.3. Please give a name which use only alphabetic or numeric or \"_\" characters.\n", content - + content = capture(:stderr){ run_generator [File.join(destination_root, "43things")] } assert_equal "Invalid plugin name 43things. Please give a name which does not start with numbers.\n", content end @@ -58,6 +58,14 @@ class PluginNewGeneratorTest < Rails::Generators::TestCase assert_file "test/integration/navigation_test.rb", /ActionDispatch::IntegrationTest/ end + def test_generating_test_files_in_full_mode_without_unit_test_files + run_generator [destination_root, "-T", "--full"] + + assert_no_directory "test/integration/" + assert_no_file "test" + assert_no_match(/APP_RAKEFILE/, File.read(File.join(destination_root, "Rakefile"))) + end + def test_ensure_that_plugin_options_are_not_passed_to_app_generator FileUtils.cd(Rails.root) assert_no_match(/It works from file!.*It works_from_file/, run_generator([destination_root, "-m", "lib/template.rb"])) @@ -82,6 +90,14 @@ class PluginNewGeneratorTest < Rails::Generators::TestCase assert_file "bukkits.gemspec", /mysql/ end + def test_dont_generate_development_dependency + run_generator [destination_root, "--skip-active-record"] + + assert_file "bukkits.gemspec" do |contents| + assert_no_match(/s.add_development_dependency "sqlite3"/, contents) + end + end + def test_active_record_is_removed_from_frameworks_if_skip_active_record_is_given run_generator [destination_root, "--skip-active-record"] assert_file "test/dummy/config/application.rb", /#\s+require\s+["']active_record\/railtie["']/ @@ -99,7 +115,13 @@ class PluginNewGeneratorTest < Rails::Generators::TestCase end def test_generation_runs_bundle_install_with_full_and_mountable - result = run_generator [destination_root, "--mountable", "--full"] + result = run_generator [destination_root, "--mountable", "--full", "--dev"] + assert_file "#{destination_root}/Gemfile.lock" do |contents| + assert_match(/bukkits/, contents) + end + assert_match(/run bundle install/, result) + assert_match(/Using bukkits \(0\.0\.1\)/, result) + assert_match(/Your bundle is complete/, result) assert_equal 1, result.scan("Your bundle is complete").size end @@ -211,7 +233,7 @@ class PluginNewGeneratorTest < Rails::Generators::TestCase def test_creating_gemspec run_generator assert_file "bukkits.gemspec", /s.name\s+= "bukkits"/ - assert_file "bukkits.gemspec", /s.files = Dir\["\{app,config,db,lib\}\/\*\*\/\*"\]/ + assert_file "bukkits.gemspec", /s.files = Dir\["\{app,config,db,lib\}\/\*\*\/\*", "MIT-LICENSE", "Rakefile", "README\.rdoc"\]/ assert_file "bukkits.gemspec", /s.test_files = Dir\["test\/\*\*\/\*"\]/ assert_file "bukkits.gemspec", /s.version\s+ = Bukkits::VERSION/ end @@ -236,16 +258,26 @@ class PluginNewGeneratorTest < Rails::Generators::TestCase assert_no_file "test/dummy" end + def test_creating_dummy_application_with_different_name + run_generator [destination_root, "--dummy_path", "spec/fake"] + assert_file "spec/fake" + assert_file "spec/fake/config/application.rb" + assert_no_file "test/dummy" + end + def test_creating_dummy_without_tests_but_with_dummy_path run_generator [destination_root, "--dummy_path", "spec/dummy", "--skip-test-unit"] assert_file "spec/dummy" assert_file "spec/dummy/config/application.rb" assert_no_file "test" + assert_file '.gitignore' do |contents| + assert_match(/spec\/dummy/, contents) + end end def test_ensure_that_gitignore_can_be_generated_from_a_template_for_dummy_path FileUtils.cd(Rails.root) - run_generator([destination_root, "--dummy_path", "spec/dummy" "--skip-test-unit"]) + run_generator([destination_root, "--dummy_path", "spec/dummy", "--skip-test-unit"]) assert_file ".gitignore" do |contents| assert_match(/spec\/dummy/, contents) end @@ -257,11 +289,40 @@ class PluginNewGeneratorTest < Rails::Generators::TestCase assert_file "bukkits.gemspec" do |contents| assert_no_match(/s.test_files = Dir\["test\/\*\*\/\*"\]/, contents) end + assert_file '.gitignore' do |contents| + assert_no_match(/test\dummy/, contents) + end end def test_skipping_gemspec run_generator [destination_root, "--skip-gemspec"] assert_no_file "bukkits.gemspec" + assert_file "Gemfile" do |contents| + assert_no_match('gemspec', contents) + assert_match(/gem "rails", "~> #{Rails::VERSION::STRING}"/, contents) + assert_match(/group :development do\n gem "sqlite3"\nend/, contents) + assert_no_match(/# gem "jquery-rails"/, contents) + end + end + + def test_skipping_gemspec_in_full_mode + run_generator [destination_root, "--skip-gemspec", "--full"] + assert_no_file "bukkits.gemspec" + assert_file "Gemfile" do |contents| + assert_no_match('gemspec', contents) + assert_match(/gem "rails", "~> #{Rails::VERSION::STRING}"/, contents) + assert_match(/group :development do\n gem "sqlite3"\nend/, contents) + assert_match(/# gem "jquery-rails"/, contents) + assert_no_match(/# jquery-rails is used by the dummy application\ngem "jquery-rails"/, contents) + end + end + + def test_skipping_gemspec_in_full_mode_with_javascript_option + run_generator [destination_root, "--skip-gemspec", "--full", "--javascript=prototype"] + assert_file "Gemfile" do |contents| + assert_match(/# gem "prototype-rails"/, contents) + assert_match(/# jquery-rails is used by the dummy application\ngem "jquery-rails"/, contents) + end end def test_creating_plugin_in_app_directory_adds_gemfile_entry @@ -272,7 +333,7 @@ class PluginNewGeneratorTest < Rails::Generators::TestCase run_generator [destination_root] - assert_file gemfile_path, /gem 'bukkits', :path => 'tmp\/bukkits'/ + assert_file gemfile_path, /gem 'bukkits', path: 'tmp\/bukkits'/ ensure Object.send(:remove_const, 'APP_PATH') FileUtils.rm gemfile_path @@ -287,7 +348,7 @@ class PluginNewGeneratorTest < Rails::Generators::TestCase run_generator [destination_root, "--skip-gemfile-entry"] assert_file gemfile_path do |contents| - assert_no_match(/gem 'bukkits', :path => 'tmp\/bukkits'/, contents) + assert_no_match(/gem 'bukkits', path: 'tmp\/bukkits'/, contents) end ensure Object.send(:remove_const, 'APP_PATH') @@ -296,12 +357,10 @@ class PluginNewGeneratorTest < Rails::Generators::TestCase protected - def action(*args, &block) silence(:stdout){ generator.send(*args, &block) } end -protected def default_files ::DEFAULT_PLUGIN_FILES end @@ -341,4 +400,3 @@ protected silence(:stdout){ generator.send(*args, &block) } end end - diff --git a/railties/test/generators/scaffold_controller_generator_test.rb b/railties/test/generators/scaffold_controller_generator_test.rb index 1382133d7b..1e16f04d85 100644 --- a/railties/test/generators/scaffold_controller_generator_test.rb +++ b/railties/test/generators/scaffold_controller_generator_test.rb @@ -33,14 +33,14 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase end assert_instance_method :create, content do |m| - assert_match(/@user = User\.new\(params\[:user\]\)/, m) + assert_match(/@user = User\.new\(user_params\)/, m) assert_match(/@user\.save/, m) assert_match(/@user\.errors/, m) end assert_instance_method :update, content do |m| assert_match(/@user = User\.find\(params\[:id\]\)/, m) - assert_match(/@user\.update_attributes\(params\[:user\]\)/, m) + assert_match(/@user\.update_attributes\(user_params\)/, m) assert_match(/@user\.errors/, m) end @@ -48,6 +48,9 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase assert_match(/@user = User\.find\(params\[:id\]\)/, m) assert_match(/@user\.destroy/, m) end + + assert_match(/def user_params/, content) + assert_match(/params\.require\(:user\)\.permit\(:age, :name\)/, content) end end @@ -75,6 +78,19 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase assert_file "test/functional/users_controller_test.rb" do |content| assert_match(/class UsersControllerTest < ActionController::TestCase/, content) assert_match(/test "should get index"/, content) + assert_match(/post :create, user: \{ age: @user.age, name: @user.name \}/, content) + assert_match(/put :update, id: @user, user: \{ age: @user.age, name: @user.name \}/, content) + end + end + + def test_functional_tests_without_attributes + run_generator ["User"] + + assert_file "test/functional/users_controller_test.rb" do |content| + assert_match(/class UsersControllerTest < ActionController::TestCase/, content) + assert_match(/test "should get index"/, content) + assert_match(/post :create, user: \{ \}/, content) + assert_match(/put :update, id: @user, user: \{ \}/, content) end end diff --git a/railties/test/generators/scaffold_generator_test.rb b/railties/test/generators/scaffold_generator_test.rb index 2db8090621..40c5188042 100644 --- a/railties/test/generators/scaffold_generator_test.rb +++ b/railties/test/generators/scaffold_generator_test.rb @@ -14,10 +14,8 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase assert_file "app/models/product_line.rb", /class ProductLine < ActiveRecord::Base/ assert_file "test/unit/product_line_test.rb", /class ProductLineTest < ActiveSupport::TestCase/ assert_file "test/fixtures/product_lines.yml" - assert_migration "db/migrate/create_product_lines.rb", /belongs_to :product/ - assert_migration "db/migrate/create_product_lines.rb", /add_index :product_lines, :product_id/ - assert_migration "db/migrate/create_product_lines.rb", /references :user/ - assert_migration "db/migrate/create_product_lines.rb", /add_index :product_lines, :user_id/ + assert_migration "db/migrate/create_product_lines.rb", /belongs_to :product, index: true/ + assert_migration "db/migrate/create_product_lines.rb", /references :user, index: true/ # Route assert_file "config/routes.rb" do |route| @@ -45,14 +43,14 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase end assert_instance_method :create, content do |m| - assert_match(/@product_line = ProductLine\.new\(params\[:product_line\]\)/, m) + assert_match(/@product_line = ProductLine\.new\(product_line_params\)/, m) assert_match(/@product_line\.save/, m) assert_match(/@product_line\.errors/, m) end assert_instance_method :update, content do |m| assert_match(/@product_line = ProductLine\.find\(params\[:id\]\)/, m) - assert_match(/@product_line\.update_attributes\(params\[:product_line\]\)/, m) + assert_match(/@product_line\.update_attributes\(product_line_params\)/, m) assert_match(/@product_line\.errors/, m) end @@ -62,8 +60,11 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase end end - assert_file "test/functional/product_lines_controller_test.rb", - /class ProductLinesControllerTest < ActionController::TestCase/ + assert_file "test/functional/product_lines_controller_test.rb" do |test| + assert_match(/class ProductLinesControllerTest < ActionController::TestCase/, test) + assert_match(/post :create, product_line: \{ title: @product_line.title \}/, test) + assert_match(/put :update, id: @product_line, product_line: \{ title: @product_line.title \}/, test) + end # Views %w( @@ -85,6 +86,17 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase assert_file "app/assets/stylesheets/product_lines.css" end + def test_functional_tests_without_attributes + run_generator ["product_line"] + + assert_file "test/functional/product_lines_controller_test.rb" do |content| + assert_match(/class ProductLinesControllerTest < ActionController::TestCase/, content) + assert_match(/test "should get index"/, content) + assert_match(/post :create, product_line: \{ \}/, content) + assert_match(/put :update, id: @product_line, product_line: \{ \}/, content) + end + end + def test_scaffold_on_revoke run_generator run_generator ["product_line"], :behavior => :revoke @@ -154,14 +166,14 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase end assert_instance_method :create, content do |m| - assert_match(/@admin_role = Admin::Role\.new\(params\[:admin_role\]\)/, m) + assert_match(/@admin_role = Admin::Role\.new\(admin_role_params\)/, m) assert_match(/@admin_role\.save/, m) assert_match(/@admin_role\.errors/, m) end assert_instance_method :update, content do |m| assert_match(/@admin_role = Admin::Role\.find\(params\[:id\]\)/, m) - assert_match(/@admin_role\.update_attributes\(params\[:admin_role\]\)/, m) + assert_match(/@admin_role\.update_attributes\(admin_role_params\)/, m) assert_match(/@admin_role\.errors/, m) end diff --git a/railties/test/generators/session_migration_generator_test.rb b/railties/test/generators/session_migration_generator_test.rb deleted file mode 100644 index b590047ff0..0000000000 --- a/railties/test/generators/session_migration_generator_test.rb +++ /dev/null @@ -1,27 +0,0 @@ -require 'generators/generators_test_helper' -require 'rails/generators/rails/session_migration/session_migration_generator' - -class SessionMigrationGeneratorTest < Rails::Generators::TestCase - include GeneratorsTestHelper - - def test_session_migration_with_default_name - run_generator - assert_migration "db/migrate/add_sessions_table.rb", /class AddSessionsTable < ActiveRecord::Migration/ - end - - def test_session_migration_with_given_name - run_generator ["create_session_table"] - assert_migration "db/migrate/create_session_table.rb", /class CreateSessionTable < ActiveRecord::Migration/ - end - - def test_session_migration_with_custom_table_name - ActiveRecord::SessionStore::Session.table_name = "custom_table_name" - run_generator - assert_migration "db/migrate/add_sessions_table.rb" do |migration| - assert_match(/class AddSessionsTable < ActiveRecord::Migration/, migration) - assert_match(/create_table :custom_table_name/, migration) - end - ensure - ActiveRecord::SessionStore::Session.table_name = "sessions" - end -end diff --git a/railties/test/generators/shared_generator_tests.rb b/railties/test/generators/shared_generator_tests.rb index 14a20eddb8..a4bdfcf438 100644 --- a/railties/test/generators/shared_generator_tests.rb +++ b/railties/test/generators/shared_generator_tests.rb @@ -91,21 +91,6 @@ module SharedGeneratorTests assert_match(/It works!/, capture(:stdout) { generator.invoke_all }) end - def test_template_raises_an_error_with_invalid_path - content = capture(:stderr){ run_generator([destination_root, "-m", "non/existant/path"]) } - assert_match(/The template \[.*\] could not be loaded/, content) - assert_match(/non\/existant\/path/, content) - end - - def test_template_is_executed_when_supplied - path = "http://gist.github.com/103208.txt" - template = %{ say "It works!" } - template.instance_eval "def read; self; end" # Make the string respond to read - - generator([destination_root], :template => path).expects(:open).with(path, 'Accept' => 'application/x-thor-template').returns(template) - assert_match(/It works!/, capture(:stdout) { generator.invoke_all }) - end - def test_template_is_executed_when_supplied_an_https_path path = "https://gist.github.com/103208.txt" template = %{ say "It works!" } @@ -119,13 +104,13 @@ module SharedGeneratorTests generator([destination_root], :dev => true).expects(:bundle_command).with('install').once quietly { generator.invoke_all } rails_path = File.expand_path('../../..', Rails.root) - assert_file 'Gemfile', /^gem\s+["']rails["'],\s+:path\s+=>\s+["']#{Regexp.escape(rails_path)}["']$/ + assert_file 'Gemfile', /^gem\s+["']rails["'],\s+path:\s+["']#{Regexp.escape(rails_path)}["']$/ end def test_edge_option generator([destination_root], :edge => true).expects(:bundle_command).with('install').once quietly { generator.invoke_all } - assert_file 'Gemfile', %r{^gem\s+["']rails["'],\s+:git\s+=>\s+["']#{Regexp.escape("https://github.com/rails/rails.git")}["']$} + assert_file 'Gemfile', %r{^gem\s+["']rails["'],\s+github:\s+["']#{Regexp.escape("rails/rails")}["']$} end def test_skip_gemfile @@ -142,6 +127,18 @@ module SharedGeneratorTests # generated. assert_file 'Gemfile' end + + def test_skip_git + run_generator [destination_root, '--skip-git', '--full'] + assert_no_file('.gitignore') + assert_file('app/mailers/.keep') + end + + def test_skip_keeps + run_generator [destination_root, '--skip-keeps', '--full'] + assert_file('.gitignore') + assert_no_file('app/mailers/.keep') + end end module SharedCustomGeneratorTests |