2009-06-25 04:18:00 -04:00
require 'generators/generators_test_helper'
2010-03-23 08:40:19 -04:00
require 'rails/generators/rails/app/app_generator'
2012-09-21 17:57:15 -04:00
require 'generators/shared_generator_tests'
2009-06-20 09:43:25 -04:00
2010-04-30 16:08:28 -04:00
DEFAULT_APP_FILES = %w(
. gitignore
Gemfile
Rakefile
config . ru
2011-04-15 16:01:06 -04:00
app / assets / javascripts
app / assets / stylesheets
2011-06-04 04:31:11 -04:00
app / assets / images
2010-04-30 16:08:28 -04:00
app / controllers
2012-12-18 17:25:16 -05:00
app / controllers / concerns
2010-04-30 16:08:28 -04:00
app / helpers
2010-06-11 06:06:00 -04:00
app / mailers
2010-04-30 16:08:28 -04:00
app / models
2012-12-18 17:25:16 -05:00
app / models / concerns
2010-04-30 16:08:28 -04:00
app / views / layouts
2013-01-06 18:13:47 -05:00
bin / bundle
bin / rails
bin / rake
2010-04-30 16:08:28 -04:00
config / environments
config / initializers
config / locales
db
lib
lib / tasks
2011-06-04 04:31:11 -04:00
lib / assets
2010-04-30 16:08:28 -04:00
log
test / fixtures
2012-10-08 00:59:42 -04:00
test / controllers
test / models
test / helpers
test / mailers
2010-04-30 16:08:28 -04:00
test / integration
vendor
2011-04-15 16:01:06 -04:00
vendor / assets
2010-04-30 16:08:28 -04:00
tmp / cache
2011-07-12 21:55:34 -04:00
tmp / cache / assets
2010-04-30 16:08:28 -04:00
)
2010-01-18 18:07:11 -05:00
class AppGeneratorTest < Rails :: Generators :: TestCase
include GeneratorsTestHelper
2010-01-03 10:34:32 -05:00
arguments [ destination_root ]
2011-05-06 18:11:13 -04:00
# brings setup, teardown, and some tests
2010-10-20 18:55:08 -04:00
include SharedGeneratorTests
2009-06-20 09:43:25 -04:00
2010-10-20 18:55:08 -04:00
def default_files
:: DEFAULT_APP_FILES
2010-07-29 22:38:45 -04:00
end
2011-06-29 08:39:41 -04:00
def test_assets
2010-04-05 18:12:28 -04:00
run_generator
2011-04-15 16:01:06 -04:00
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 "
2010-04-05 18:12:28 -04:00
end
2010-01-03 07:01:18 -05:00
def test_invalid_application_name_raises_an_error
2010-01-07 15:09:32 -05:00
content = capture ( :stderr ) { run_generator [ File . join ( destination_root , " 43-things " ) ] }
2010-01-03 07:01:18 -05:00
assert_equal " Invalid application name 43-things. Please give a name which does not start with numbers. \n " , content
end
def test_invalid_application_name_is_fixed
2010-01-07 15:09:32 -05:00
run_generator [ File . join ( destination_root , " things-43 " ) ]
2013-04-30 12:26:55 -04:00
assert_file " things-43/config/environment.rb " , / Rails \ .application \ .initialize! /
2010-01-07 14:59:26 -05:00
assert_file " things-43/config/application.rb " , / ^module Things43$ /
2010-01-03 07:01:18 -05:00
end
2011-01-28 22:22:32 -05:00
def test_application_new_exits_with_non_zero_code_on_invalid_application_name
2013-02-01 06:14:26 -05:00
quietly { system 'rails new test --no-rc' }
2011-01-28 22:05:24 -05:00
assert_equal false , $? . success?
2011-01-28 22:23:12 -05:00
end
def test_application_new_exits_with_message_and_non_zero_code_when_generating_inside_existing_rails_directory
app_root = File . join ( destination_root , 'myfirstapp' )
run_generator [ app_root ]
output = nil
Dir . chdir ( app_root ) do
output = ` rails new mysecondapp `
end
assert_equal " Can't initialize a new Rails application within the directory of another, please change to a non-Rails directory first. \n Type 'rails' for help. \n " , output
assert_equal false , $? . success?
2011-01-28 22:05:24 -05:00
end
2012-04-09 12:26:52 -04:00
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
2012-05-14 11:31:51 -04:00
assert_match ( / rails new APP_PATH \ [options \ ] / , output )
2012-04-09 12:26:52 -04:00
assert_equal true , $? . success?
end
2010-07-28 08:55:57 -04:00
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 " )
run_generator [ app_root ]
Rails . application . config . root = app_moved_root
Rails . application . class . stubs ( :name ) . returns ( " Myapp " )
2010-08-03 01:05:00 -04:00
Rails . application . stubs ( :is_a? ) . returns ( Rails :: Application )
2010-07-28 08:55:57 -04:00
FileUtils . mv ( app_root , app_moved_root )
2012-10-14 06:03:39 -04:00
generator = Rails :: Generators :: AppGenerator . new [ " rails " ] , { with_dispatchers : true } ,
destination_root : app_moved_root , shell : @shell
2010-07-28 08:55:57 -04:00
generator . send ( :app_const )
2011-05-12 20:11:47 -04:00
quietly { generator . send ( :create_config_files ) }
2013-04-30 12:26:55 -04:00
assert_file " myapp_moved/config/environment.rb " , / Rails \ .application \ .initialize! /
2010-08-30 07:53:34 -04:00
assert_file " myapp_moved/config/initializers/session_store.rb " , / _myapp_session /
2010-07-28 08:55:57 -04:00
end
2010-11-11 14:07:47 -05:00
2010-08-24 21:36:07 -04:00
def test_rails_update_generates_correct_session_key
app_root = File . join ( destination_root , 'myapp' )
run_generator [ app_root ]
2010-11-11 14:07:47 -05:00
2010-08-24 21:36:07 -04:00
Rails . application . config . root = app_root
Rails . application . class . stubs ( :name ) . returns ( " Myapp " )
Rails . application . stubs ( :is_a? ) . returns ( Rails :: Application )
2012-10-14 06:03:39 -04:00
generator = Rails :: Generators :: AppGenerator . new [ " rails " ] , { with_dispatchers : true } , destination_root : app_root , shell : @shell
2010-08-24 21:36:07 -04:00
generator . send ( :app_const )
2011-05-12 20:11:47 -04:00
quietly { generator . send ( :create_config_files ) }
2010-08-24 21:36:07 -04:00
assert_file " myapp/config/initializers/session_store.rb " , / _myapp_session /
end
2010-07-28 08:55:57 -04:00
2010-01-07 15:09:32 -05:00
def test_application_names_are_not_singularized
run_generator [ File . join ( destination_root , " hats " ) ]
2013-04-30 12:26:55 -04:00
assert_file " hats/config/environment.rb " , / Rails \ .application \ .initialize! /
2010-01-07 15:09:32 -05:00
end
2011-12-12 16:46:05 -05:00
def test_gemfile_has_no_whitespace_errors
run_generator
absolute = File . expand_path ( " Gemfile " , destination_root )
File . open ( absolute , 'r' ) do | f |
f . each_line do | line |
2011-12-18 03:07:07 -05:00
assert_no_match %r{ /^[ \ t]+$/ } , line
2011-12-12 16:46:05 -05:00
end
end
end
2009-06-20 10:58:15 -04:00
def test_config_database_is_added_by_default
run_generator
assert_file " config/database.yml " , / sqlite3 /
2011-06-28 17:11:52 -04:00
unless defined? ( JRUBY_VERSION )
2012-05-13 12:44:43 -04:00
assert_gem " sqlite3 "
2011-06-28 17:11:52 -04:00
else
2012-05-13 12:44:43 -04:00
assert_gem " activerecord-jdbcsqlite3-adapter "
2011-06-28 17:11:52 -04:00
end
2010-02-04 07:53:37 -05:00
end
def test_config_another_database
run_generator ( [ destination_root , " -d " , " mysql " ] )
assert_file " config/database.yml " , / mysql /
2011-06-28 17:11:52 -04:00
unless defined? ( JRUBY_VERSION )
2012-05-13 12:44:43 -04:00
assert_gem " mysql2 "
2011-06-28 17:11:52 -04:00
else
2012-05-13 12:44:43 -04:00
assert_gem " activerecord-jdbcmysql-adapter "
2011-06-28 17:11:52 -04:00
end
2009-06-20 10:58:15 -04:00
end
2012-11-23 15:30:15 -05:00
def test_config_database_app_name_with_period
run_generator [ File . join ( destination_root , " common.usage.com " ) , " -d " , " postgresql " ]
assert_file " common.usage.com/config/database.yml " , / common_usage_com /
end
2011-10-16 15:00:07 -04:00
def test_config_postgresql_database
run_generator ( [ destination_root , " -d " , " postgresql " ] )
assert_file " config/database.yml " , / postgresql /
unless defined? ( JRUBY_VERSION )
2012-05-13 12:44:43 -04:00
assert_gem " pg "
2011-10-16 15:00:07 -04:00
else
2012-05-13 12:44:43 -04:00
assert_gem " activerecord-jdbcpostgresql-adapter "
2011-10-16 15:00:07 -04:00
end
end
2011-04-22 13:37:46 -04:00
def test_config_jdbcmysql_database
run_generator ( [ destination_root , " -d " , " jdbcmysql " ] )
2011-06-21 22:45:12 -04:00
assert_file " config/database.yml " , / mysql /
2012-05-13 12:44:43 -04:00
assert_gem " activerecord-jdbcmysql-adapter "
2011-04-22 13:37:46 -04:00
end
2011-04-23 09:25:17 -04:00
def test_config_jdbcsqlite3_database
run_generator ( [ destination_root , " -d " , " jdbcsqlite3 " ] )
2011-06-21 22:45:12 -04:00
assert_file " config/database.yml " , / sqlite3 /
2012-05-13 12:44:43 -04:00
assert_gem " activerecord-jdbcsqlite3-adapter "
2011-04-23 09:25:17 -04:00
end
2011-04-25 15:24:02 -04:00
def test_config_jdbcpostgresql_database
run_generator ( [ destination_root , " -d " , " jdbcpostgresql " ] )
2011-06-21 22:45:12 -04:00
assert_file " config/database.yml " , / postgresql /
2012-05-13 12:44:43 -04:00
assert_gem " activerecord-jdbcpostgresql-adapter "
2011-04-25 15:24:02 -04:00
end
2011-06-21 22:34:18 -04:00
def test_config_jdbc_database
run_generator ( [ destination_root , " -d " , " jdbc " ] )
assert_file " config/database.yml " , / jdbc /
2011-06-21 22:45:12 -04:00
assert_file " config/database.yml " , / mssql /
2012-05-13 12:44:43 -04:00
assert_gem " activerecord-jdbc-adapter "
2011-06-21 22:34:18 -04:00
end
2011-06-21 23:13:34 -04:00
def test_config_jdbc_database_when_no_option_given
if defined? ( JRUBY_VERSION )
run_generator ( [ destination_root ] )
assert_file " config/database.yml " , / sqlite3 /
2012-05-13 12:44:43 -04:00
assert_gem " activerecord-jdbcsqlite3-adapter "
2011-06-21 23:13:34 -04:00
end
end
2010-10-09 11:36:54 -04:00
def test_generator_if_skip_active_record_is_given
2010-07-25 16:08:34 -04:00
run_generator [ destination_root , " --skip-active-record " ]
2009-06-20 10:58:15 -04:00
assert_no_file " config/database.yml "
2011-06-29 08:39:41 -04:00
assert_file " config/application.rb " , / # \ s+require \ s+["']active_record \/ railtie["'] /
2010-10-09 11:31:32 -04:00
assert_file " test/test_helper.rb " do | helper_content |
2011-05-18 07:37:57 -04:00
assert_no_match ( / fixtures :all / , helper_content )
2010-10-09 11:31:32 -04:00
end
2009-06-20 10:58:15 -04:00
end
2011-08-21 18:55:15 -04:00
def test_generator_if_skip_sprockets_is_given
2011-06-29 08:39:41 -04:00
run_generator [ destination_root , " --skip-sprockets " ]
assert_file " config/application.rb " do | content |
2012-10-18 13:31:12 -04:00
assert_match ( / # \ s+require \ s+["']sprockets \/ railtie["'] / , content )
2013-01-05 12:00:43 -05:00
assert_match ( / config \ .assets \ .enabled = false / , content )
2011-06-29 08:39:41 -04:00
end
2011-12-09 21:52:10 -05:00
assert_file " Gemfile " do | content |
assert_no_match ( / sass-rails / , content )
assert_no_match ( / uglifier / , content )
2013-03-25 01:31:48 -04:00
assert_match ( / coffee-rails / , content )
2011-12-09 21:52:10 -05:00
end
2011-12-11 08:48:58 -05:00
assert_file " config/environments/development.rb " do | content |
assert_no_match ( / config \ .assets \ .debug = true / , content )
end
assert_file " config/environments/production.rb " do | content |
assert_no_match ( / config \ .assets \ .digest = true / , content )
2012-10-15 19:22:12 -04:00
assert_no_match ( / config \ .assets \ .js_compressor = :uglifier / , content )
assert_no_match ( / config \ .assets \ .css_compressor = :sass / , content )
2013-01-05 12:00:43 -05:00
assert_no_match ( / config \ .assets \ .version = '1 \ .0' / , content )
2011-12-11 08:48:58 -05:00
end
2010-01-16 17:34:38 -05:00
end
2009-06-20 10:58:15 -04:00
2012-01-12 21:58:25 -05:00
def test_inclusion_of_javascript_runtime
2011-12-09 21:38:10 -05:00
run_generator ( [ destination_root ] )
2011-11-14 07:07:38 -05:00
if defined? ( JRUBY_VERSION )
2012-05-13 12:44:43 -04:00
assert_gem " therubyrhino "
2011-12-09 21:38:10 -05:00
else
2012-05-30 21:46:53 -04:00
assert_file " Gemfile " , / # gem \ s+["']therubyracer["']+, platforms: :ruby$ /
2011-11-14 07:07:38 -05:00
end
end
2011-05-01 18:59:57 -04:00
def test_creation_of_a_test_directory
2009-06-20 10:58:15 -04:00
run_generator
2011-05-01 18:59:57 -04:00
assert_file 'test'
end
2013-05-02 23:28:37 -04:00
def test_creation_of_app_assets_images_directory
run_generator
assert_file " app/assets/images "
end
2011-10-11 03:48:51 -04:00
def test_creation_of_vendor_assets_javascripts_directory
run_generator
assert_file " vendor/assets/javascripts "
end
2011-10-13 02:47:19 -04:00
def test_creation_of_vendor_assets_stylesheets_directory
run_generator
assert_file " vendor/assets/stylesheets "
end
2011-05-01 18:59:57 -04:00
def test_jquery_is_the_default_javascript_library
run_generator
assert_file " app/assets/javascripts/application.js " do | contents |
assert_match %r{ ^//= require jquery } , contents
assert_match %r{ ^//= require jquery_ujs } , contents
end
2013-03-25 03:01:38 -04:00
assert_file " Gemfile " , / ^gem 'jquery-rails' /
2011-05-01 18:59:57 -04:00
end
def test_other_javascript_libraries
run_generator [ destination_root , '-j' , 'prototype' ]
assert_file " app/assets/javascripts/application.js " do | contents |
assert_match %r{ ^//= require prototype } , contents
assert_match %r{ ^//= require prototype_ujs } , contents
end
2012-05-13 12:44:43 -04:00
assert_gem " prototype-rails "
2009-06-20 10:58:15 -04:00
end
2010-11-11 14:07:47 -05:00
2010-09-11 15:09:16 -04:00
def test_javascript_is_skipped_if_required
run_generator [ destination_root , " --skip-javascript " ]
2011-05-01 18:59:57 -04:00
assert_file " app/assets/javascripts/application.js " do | contents |
assert_no_match %r{ ^//= \ s+require \ s } , contents
end
2013-03-25 01:31:48 -04:00
assert_file " Gemfile " do | content |
assert_match ( / coffee-rails / , content )
end
2010-09-11 15:09:16 -04:00
end
2010-11-11 14:07:47 -05:00
2012-04-16 22:36:09 -04:00
def test_inclusion_of_debugger
2011-05-17 22:08:34 -04:00
run_generator
2012-05-13 12:44:43 -04:00
assert_file " Gemfile " , / # gem 'debugger' /
2011-05-17 22:08:34 -04:00
end
2013-03-30 06:22:40 -04:00
def test_inclusion_of_lazy_loaded_sdoc
run_generator
assert_file 'Gemfile' , / gem 'sdoc', require: false /
end
2009-08-07 09:34:10 -04:00
def test_template_from_dir_pwd
2009-10-17 14:54:58 -04:00
FileUtils . cd ( Rails . root )
2011-05-18 07:37:57 -04:00
assert_match ( / It works from file! / , run_generator ( [ destination_root , " -m " , " lib/template.rb " ] ) )
2009-08-07 09:34:10 -04:00
end
2009-06-25 09:45:15 -04:00
def test_usage_read_from_file
File . expects ( :read ) . returns ( " USAGE FROM FILE " )
assert_equal " USAGE FROM FILE " , Rails :: Generators :: AppGenerator . desc
end
def test_default_usage
2012-03-31 19:12:49 -04:00
Rails :: Generators :: AppGenerator . expects ( :usage_path ) . returns ( nil )
2011-05-18 07:37:57 -04:00
assert_match ( / Create rails files for app generator / , Rails :: Generators :: AppGenerator . desc )
2009-06-25 09:45:15 -04:00
end
def test_default_namespace
2010-01-18 06:28:52 -05:00
assert_match " rails:app " , Rails :: Generators :: AppGenerator . namespace
2009-06-25 09:45:15 -04:00
end
2009-06-28 06:00:13 -04:00
def test_file_is_added_for_backwards_compatibility
action :file , 'lib/test_file.rb' , 'heres test data'
assert_file 'lib/test_file.rb' , 'heres test data'
end
2010-11-11 14:07:47 -05:00
def test_test_unit_is_removed_from_frameworks_if_skip_test_unit_is_given
run_generator [ destination_root , " --skip-test-unit " ]
2011-05-06 10:59:11 -04:00
assert_file " config/application.rb " , / # \ s+require \ s+["']rails \/ test_unit \/ railtie["'] /
end
def test_no_active_record_or_test_unit_if_skips_given
run_generator [ destination_root , " --skip-test-unit " , " --skip-active-record " ]
assert_file " config/application.rb " , / # \ s+require \ s+["']rails \/ test_unit \/ railtie["'] /
assert_file " config/application.rb " , / # \ s+require \ s+["']active_record \/ railtie["'] /
2010-11-11 14:07:47 -05:00
end
2011-04-10 00:30:58 -04:00
def test_new_hash_style
run_generator [ destination_root ]
assert_file " config/initializers/session_store.rb " do | file |
2013-03-28 15:35:48 -04:00
assert_match ( / config.session_store :cookie_store, key: '_.+_session' / , file )
2011-04-10 00:38:59 -04:00
end
end
2012-01-31 06:10:22 -05:00
def test_pretend_option
output = run_generator [ File . join ( destination_root , " myapp " ) , " --pretend " ]
assert_no_match ( / run bundle install / , output )
end
2010-04-30 16:08:28 -04:00
protected
2009-06-20 09:43:25 -04:00
2010-04-30 16:08:28 -04:00
def action ( * args , & block )
2011-05-12 20:11:47 -04:00
silence ( :stdout ) { generator . send ( * args , & block ) }
2010-04-30 16:08:28 -04:00
end
2012-05-13 12:44:43 -04:00
def assert_gem ( gem )
assert_file " Gemfile " , / ^gem \ s+["'] #{ gem } ["']$ /
end
2010-10-06 05:53:00 -04:00
end