2011-05-06 18:11:13 -04:00
#
# Tests, setup, and teardown common to the application and plugin generator suites.
#
2010-10-20 18:55:08 -04:00
module SharedGeneratorTests
def setup
Rails . application = TestApp :: Application
super
Rails :: Generators :: AppGenerator . instance_variable_set ( '@desc' , nil )
Kernel :: silence_warnings do
Thor :: Base . shell . send ( :attr_accessor , :always_force )
@shell = Thor :: Base . shell . new
@shell . send ( :always_force = , true )
end
end
def teardown
super
Rails :: Generators :: AppGenerator . instance_variable_set ( '@desc' , nil )
Rails . application = TestApp :: Application . instance
end
def test_skeleton_is_created
run_generator
2011-05-12 18:43:43 -04:00
default_files . each { | path | assert_file path }
end
2013-11-02 14:25:02 -04:00
def assert_generates_with_bundler ( options = { } )
generator ( [ destination_root ] , options )
2015-08-20 23:27:37 -04:00
command_check = - > command do
@install_called || = 0
case command
when 'install'
@install_called += 1
assert_equal 1 , @install_called , " install expected to be called once, but was called #{ @install_called } times "
when 'exec spring binstub --all'
# Called when running tests with spring, let through unscathed.
end
end
generator . stub :bundle_command , command_check do
quietly { generator . invoke_all }
end
2010-10-20 18:55:08 -04:00
end
2013-11-02 14:25:02 -04:00
def test_generation_runs_bundle_install
assert_generates_with_bundler
end
2010-10-20 18:55:08 -04:00
def test_plugin_new_generate_pretend
run_generator [ " testapp " , " --pretend " ]
2011-03-23 13:06:22 -04:00
default_files . each { | path | assert_no_file File . join ( " testapp " , path ) }
2010-10-20 18:55:08 -04:00
end
2010-11-02 09:23:45 -04:00
def test_invalid_database_option_raises_an_error
content = capture ( :stderr ) { run_generator ( [ destination_root , " -d " , " unknown " ] ) }
2011-03-12 19:01:12 -05:00
assert_match ( / Invalid value for \ - \ -database option / , content )
2010-11-02 09:23:45 -04:00
end
2015-01-29 15:44:26 -05:00
def test_test_files_are_skipped_if_required
2015-01-27 17:59:48 -05:00
run_generator [ destination_root , " --skip-test " ]
2010-11-02 09:52:08 -04:00
assert_no_file " test "
end
2010-10-20 18:55:08 -04:00
def test_name_collision_raises_an_error
reserved_words = %w[ application destroy plugin runner test ]
reserved_words . each do | reserved |
content = capture ( :stderr ) { run_generator [ File . join ( destination_root , reserved ) ] }
2015-07-01 10:58:42 -04:00
assert_match ( / Invalid \ w+ name #{ reserved } . Please give a name which does not match one of the reserved rails words: application, destroy, plugin, runner, test \ n / , content )
2010-10-20 18:55:08 -04:00
end
end
def test_name_raises_an_error_if_name_already_used_constant
%w{ String Hash Class Module Set Symbol } . each do | ruby_class |
content = capture ( :stderr ) { run_generator [ File . join ( destination_root , ruby_class ) ] }
2011-03-12 19:01:12 -05:00
assert_match ( / Invalid \ w+ name #{ ruby_class } , constant #{ ruby_class } is already in use. Please choose another \ w+ name. \ n / , content )
2010-10-20 18:55:08 -04:00
end
end
def test_shebang_is_added_to_rails_file
2011-05-27 07:44:10 -04:00
run_generator [ destination_root , " --ruby " , " foo/bar/baz " , " --full " ]
2013-01-06 18:13:47 -05:00
assert_file " bin/rails " , / # !foo \/ bar \/ baz /
2010-10-20 18:55:08 -04:00
end
def test_shebang_when_is_the_same_as_default_use_env
2011-05-27 07:44:10 -04:00
run_generator [ destination_root , " --ruby " , Thor :: Util . ruby_command , " --full " ]
2013-01-06 18:13:47 -05:00
assert_file " bin/rails " , / # ! \/ usr \/ bin \/ env /
2010-10-20 18:55:08 -04:00
end
def test_template_raises_an_error_with_invalid_path
2014-04-27 12:19:29 -04:00
quietly do
content = capture ( :stderr ) { run_generator ( [ destination_root , " -m " , " non/existent/path " ] ) }
assert_match ( / The template \ [.* \ ] could not be loaded / , content )
assert_match ( / non \/ existent \/ path / , content )
end
2010-10-20 18:55:08 -04:00
end
2010-11-27 05:40:42 -05:00
def test_template_is_executed_when_supplied_an_https_path
2013-02-18 09:57:37 -05:00
path = " https://gist.github.com/josevalim/103208/raw/ "
2010-11-27 05:40:42 -05:00
template = %{ say "It works!" }
template . instance_eval " def read; self; end " # Make the string respond to read
2015-08-20 23:27:37 -04:00
check_open = - > * args do
assert_equal [ path , 'Accept' = > 'application/x-thor-template' ] , args
template
end
generator ( [ destination_root ] , template : path ) . stub ( :open , check_open , template ) do
quietly { assert_match ( / It works! / , capture ( :stdout ) { generator . invoke_all } ) }
end
2010-10-20 18:55:08 -04:00
end
def test_dev_option
2013-11-02 14:25:02 -04:00
assert_generates_with_bundler dev : true
2010-10-20 18:55:08 -04:00
rails_path = File . expand_path ( '../../..' , Rails . root )
2012-05-02 08:01:29 -04:00
assert_file 'Gemfile' , / ^gem \ s+["']rails["'], \ s+path: \ s+["'] #{ Regexp . escape ( rails_path ) } ["']$ /
2010-10-20 18:55:08 -04:00
end
def test_edge_option
2013-11-02 14:25:02 -04:00
assert_generates_with_bundler edge : true
2012-05-02 08:01:29 -04:00
assert_file 'Gemfile' , %r{ ^gem \ s+["']rails["'], \ s+github: \ s+["'] #{ Regexp . escape ( " rails/rails " ) } ["']$ }
2010-10-20 18:55:08 -04:00
end
2011-05-12 18:43:43 -04:00
def test_skip_gemfile
2015-08-20 23:27:37 -04:00
assert_not_called ( generator ( [ destination_root ] , skip_gemfile : true ) , :bundle_command ) do
quietly { generator . invoke_all }
assert_no_file 'Gemfile'
end
2011-05-12 18:43:43 -04:00
end
2011-05-13 20:36:29 -04:00
def test_skip_bundle
2015-08-20 23:27:37 -04:00
assert_not_called ( generator ( [ destination_root ] , skip_bundle : true ) , :bundle_command ) do
quietly { generator . invoke_all }
# skip_bundle is only about running bundle install, ensure the Gemfile is still
# generated.
assert_file 'Gemfile'
end
2011-05-13 20:36:29 -04:00
end
2012-09-12 21:15:38 -04:00
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' ]
2015-05-22 08:24:48 -04:00
assert_file '.gitignore' do | content |
assert_no_match ( / \ .keep / , content )
end
2012-09-12 21:15:38 -04:00
assert_no_file ( 'app/mailers/.keep' )
end
2010-10-20 18:55:08 -04:00
end