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
2016-08-06 13:16:09 -04:00
Rails :: Generators :: AppGenerator . instance_variable_set ( " @desc " , nil )
2010-10-20 18:55:08 -04:00
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
2016-08-06 13:16:09 -04:00
Rails :: Generators :: AppGenerator . instance_variable_set ( " @desc " , nil )
2010-10-20 18:55:08 -04:00
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
2010-10-20 18:55:08 -04:00
def test_plugin_new_generate_pretend
run_generator [ " testapp " , " --pretend " ]
2016-10-28 23:05:58 -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
2016-08-16 03:30:11 -04:00
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 |
2016-08-16 03:30:11 -04:00
content = capture ( :stderr ) { run_generator [ File . join ( destination_root , reserved ) ] }
2017-05-06 15:08:58 -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 |
2016-08-16 03:30:11 -04:00
content = capture ( :stderr ) { run_generator [ File . join ( destination_root , ruby_class ) ] }
2017-05-06 15:08:58 -04: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
2016-08-16 03:30:11 -04:00
content = capture ( :stderr ) { run_generator ( [ destination_root , " -m " , " non/existent/path " ] ) }
2014-04-27 12:19:29 -04:00
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/ "
2017-07-22 08:45:53 -04:00
template = %{ say "It works!" } . dup
2010-11-27 05:40:42 -05:00
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
2016-08-06 13:16:09 -04:00
assert_equal [ path , " Accept " = > " application/x-thor-template " ] , args
2015-08-20 23:27:37 -04:00
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
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 }
2016-08-06 13:16:09 -04:00
assert_no_file " Gemfile "
2015-08-20 23:27:37 -04:00
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.
2016-08-06 13:16:09 -04:00
assert_file " Gemfile "
2015-08-20 23:27:37 -04:00
end
2011-05-13 20:36:29 -04:00
end
2012-09-12 21:15:38 -04:00
def test_skip_git
2016-08-06 13:16:09 -04:00
run_generator [ destination_root , " --skip-git " , " --full " ]
assert_no_file ( " .gitignore " )
2017-01-10 08:58:28 -05:00
assert_no_directory ( " .git " )
2012-09-12 21:15:38 -04:00
end
def test_skip_keeps
2016-08-06 13:16:09 -04:00
run_generator [ destination_root , " --skip-keeps " , " --full " ]
2015-05-22 08:24:48 -04:00
2016-08-06 13:16:09 -04:00
assert_file " .gitignore " do | content |
2015-05-22 08:24:48 -04:00
assert_no_match ( / \ .keep / , content )
end
2016-08-06 13:16:09 -04:00
assert_no_file ( " app/models/concerns/.keep " )
2012-09-12 21:15:38 -04:00
end
2010-10-20 18:55:08 -04:00
end