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-12-06 07:05:45 -05:00
|
|
|
require 'env_helpers'
|
2015-05-29 02:02:44 -04:00
|
|
|
require 'minitest/mock'
|
2009-06-19 07:13:46 -04:00
|
|
|
|
2010-01-18 18:07:11 -05:00
|
|
|
class ActionsTest < Rails::Generators::TestCase
|
|
|
|
include GeneratorsTestHelper
|
2012-12-06 07:05:45 -05:00
|
|
|
include EnvHelpers
|
|
|
|
|
2010-01-03 10:34:32 -05:00
|
|
|
tests Rails::Generators::AppGenerator
|
|
|
|
arguments [destination_root]
|
|
|
|
|
2009-06-19 07:13:46 -04:00
|
|
|
def setup
|
2010-09-25 07:11:07 -04:00
|
|
|
Rails.application = TestApp::Application
|
2015-05-29 02:02:44 -04:00
|
|
|
@mock_generator = Minitest::Mock.new
|
2009-06-19 07:13:46 -04:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2010-09-25 07:11:07 -04:00
|
|
|
def teardown
|
|
|
|
Rails.application = TestApp::Application.instance
|
2015-05-29 02:02:44 -04:00
|
|
|
@mock_generator.verify
|
2010-09-25 07:11:07 -04:00
|
|
|
end
|
|
|
|
|
2010-01-18 04:43:10 -05:00
|
|
|
def test_invoke_other_generator_with_shortcut
|
|
|
|
action :invoke, 'model', ['my_model']
|
|
|
|
assert_file 'app/models/my_model.rb', /MyModel/
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_invoke_other_generator_with_full_namespace
|
2010-01-18 06:28:52 -05:00
|
|
|
action :invoke, 'rails:model', ['my_model']
|
2010-01-18 04:43:10 -05:00
|
|
|
assert_file 'app/models/my_model.rb', /MyModel/
|
|
|
|
end
|
|
|
|
|
2009-06-28 06:00:13 -04:00
|
|
|
def test_create_file_should_write_data_to_file_path
|
|
|
|
action :create_file, 'lib/test_file.rb', 'heres test data'
|
2009-06-19 10:08:03 -04:00
|
|
|
assert_file 'lib/test_file.rb', 'heres test data'
|
|
|
|
end
|
|
|
|
|
2009-06-28 06:00:13 -04:00
|
|
|
def test_create_file_should_write_block_contents_to_file_path
|
|
|
|
action(:create_file, 'lib/test_file.rb'){ 'heres block data' }
|
2009-06-19 10:08:03 -04:00
|
|
|
assert_file 'lib/test_file.rb', 'heres block data'
|
|
|
|
end
|
|
|
|
|
2009-11-03 19:02:24 -05:00
|
|
|
def test_add_source_adds_source_to_gemfile
|
2009-06-19 10:08:03 -04:00
|
|
|
run_generator
|
2009-11-03 19:02:24 -05:00
|
|
|
action :add_source, 'http://gems.github.com'
|
2014-05-26 00:50:11 -04:00
|
|
|
assert_file 'Gemfile', /source 'http:\/\/gems\.github\.com'/
|
2009-06-19 10:08:03 -04:00
|
|
|
end
|
|
|
|
|
2015-06-25 02:25:49 -04:00
|
|
|
def test_add_source_with_block_adds_source_to_gemfile_with_gem
|
|
|
|
run_generator
|
|
|
|
action :add_source, 'http://gems.github.com' do
|
|
|
|
gem 'rspec-rails'
|
|
|
|
end
|
|
|
|
assert_file 'Gemfile', /source 'http:\/\/gems\.github\.com' do\n gem 'rspec-rails'\nend/
|
|
|
|
end
|
|
|
|
|
2009-11-03 19:02:24 -05:00
|
|
|
def test_gem_should_put_gem_dependency_in_gemfile
|
2009-06-19 10:08:03 -04:00
|
|
|
run_generator
|
2009-11-03 19:02:24 -05:00
|
|
|
action :gem, 'will-paginate'
|
2014-05-26 00:50:11 -04:00
|
|
|
assert_file 'Gemfile', /gem 'will\-paginate'/
|
2009-06-19 10:08:03 -04:00
|
|
|
end
|
|
|
|
|
2010-02-08 14:58:32 -05:00
|
|
|
def test_gem_with_version_should_include_version_in_gemfile
|
|
|
|
run_generator
|
|
|
|
|
|
|
|
action :gem, 'rspec', '>=2.0.0.a5'
|
|
|
|
|
2014-05-26 00:50:11 -04:00
|
|
|
assert_file 'Gemfile', /gem 'rspec', '>=2.0.0.a5'/
|
2010-02-08 14:58:32 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_gem_should_insert_on_separate_lines
|
|
|
|
run_generator
|
|
|
|
|
2011-11-25 12:06:14 -05:00
|
|
|
File.open('Gemfile', 'a') {|f| f.write('# Some content...') }
|
|
|
|
|
2010-02-08 14:58:32 -05:00
|
|
|
action :gem, 'rspec'
|
|
|
|
action :gem, 'rspec-rails'
|
|
|
|
|
2014-05-26 00:50:11 -04:00
|
|
|
assert_file 'Gemfile', /^gem 'rspec'$/
|
|
|
|
assert_file 'Gemfile', /^gem 'rspec-rails'$/
|
2010-02-08 14:58:32 -05:00
|
|
|
end
|
|
|
|
|
2012-05-22 05:26:06 -04:00
|
|
|
def test_gem_should_include_options
|
|
|
|
run_generator
|
|
|
|
|
|
|
|
action :gem, 'rspec', github: 'dchelimsky/rspec', tag: '1.2.9.rc1'
|
|
|
|
|
2014-05-26 00:50:11 -04:00
|
|
|
assert_file 'Gemfile', /gem 'rspec', github: 'dchelimsky\/rspec', tag: '1\.2\.9\.rc1'/
|
2012-05-22 05:26:06 -04:00
|
|
|
end
|
|
|
|
|
2014-08-27 06:30:35 -04:00
|
|
|
def test_gem_with_non_string_options
|
|
|
|
run_generator
|
|
|
|
|
|
|
|
action :gem, 'rspec', require: false
|
|
|
|
action :gem, 'rspec-rails', group: [:development, :test]
|
|
|
|
|
|
|
|
assert_file 'Gemfile', /^gem 'rspec', require: false$/
|
|
|
|
assert_file 'Gemfile', /^gem 'rspec-rails', group: \[:development, :test\]$/
|
|
|
|
end
|
|
|
|
|
2014-05-27 03:21:16 -04:00
|
|
|
def test_gem_falls_back_to_inspect_if_string_contains_single_quote
|
|
|
|
run_generator
|
|
|
|
|
|
|
|
action :gem, 'rspec', ">=2.0'0"
|
|
|
|
|
|
|
|
assert_file 'Gemfile', /^gem 'rspec', ">=2\.0'0"$/
|
2012-05-22 05:26:06 -04:00
|
|
|
end
|
|
|
|
|
2011-09-04 04:14:53 -04:00
|
|
|
def test_gem_group_should_wrap_gems_in_a_group
|
|
|
|
run_generator
|
|
|
|
|
|
|
|
action :gem_group, :development, :test do
|
|
|
|
gem 'rspec-rails'
|
|
|
|
end
|
|
|
|
|
|
|
|
action :gem_group, :test do
|
|
|
|
gem 'fakeweb'
|
|
|
|
end
|
2011-12-09 16:49:29 -05:00
|
|
|
|
2014-05-26 00:50:11 -04:00
|
|
|
assert_file 'Gemfile', /\ngroup :development, :test do\n gem 'rspec-rails'\nend\n\ngroup :test do\n gem 'fakeweb'\nend/
|
2011-09-04 04:14:53 -04:00
|
|
|
end
|
|
|
|
|
2009-06-19 10:08:03 -04:00
|
|
|
def test_environment_should_include_data_in_environment_initializer_block
|
|
|
|
run_generator
|
2010-06-22 17:17:20 -04:00
|
|
|
autoload_paths = 'config.autoload_paths += %w["#{Rails.root}/app/extras"]'
|
|
|
|
action :environment, autoload_paths
|
2011-09-02 05:31:00 -04:00
|
|
|
assert_file 'config/application.rb', / class Application < Rails::Application\n #{Regexp.escape(autoload_paths)}/
|
2009-06-19 10:08:03 -04:00
|
|
|
end
|
|
|
|
|
2011-07-17 02:47:21 -04:00
|
|
|
def test_environment_should_include_data_in_environment_initializer_block_with_env_option
|
|
|
|
run_generator
|
|
|
|
autoload_paths = 'config.autoload_paths += %w["#{Rails.root}/app/extras"]'
|
2012-10-14 06:03:39 -04:00
|
|
|
action :environment, autoload_paths, env: 'development'
|
2013-04-30 12:26:55 -04:00
|
|
|
assert_file "config/environments/development.rb", /Rails\.application\.configure do\n #{Regexp.escape(autoload_paths)}/
|
2011-07-17 02:47:21 -04:00
|
|
|
end
|
|
|
|
|
2009-06-19 10:08:03 -04:00
|
|
|
def test_environment_with_block_should_include_block_contents_in_environment_initializer_block
|
|
|
|
run_generator
|
|
|
|
|
|
|
|
action :environment do
|
2014-12-29 14:08:38 -05:00
|
|
|
_ = '# This wont be added'# assignment to silence parse-time warning "unused literal ignored"
|
2009-06-19 10:08:03 -04:00
|
|
|
'# This will be added'
|
|
|
|
end
|
|
|
|
|
2009-10-15 17:50:02 -04:00
|
|
|
assert_file 'config/application.rb' do |content|
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(/# This will be added/, content)
|
|
|
|
assert_no_match(/# This wont be added/, content)
|
2009-07-06 12:31:28 -04:00
|
|
|
end
|
2009-06-19 10:08:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_git_with_symbol_should_run_command_using_git_scm
|
2015-05-29 02:02:44 -04:00
|
|
|
@mock_generator.expect(:call, nil, ['git init'])
|
|
|
|
generator.stub(:run, @mock_generator) do
|
|
|
|
action :git, :init
|
|
|
|
end
|
2009-06-19 10:08:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_git_with_hash_should_run_each_command_using_git_scm
|
2015-05-29 02:02:44 -04:00
|
|
|
@mock_generator.expect(:call, nil, ["git rm README"])
|
|
|
|
@mock_generator.expect(:call, nil, ["git add ."])
|
|
|
|
generator.stub(:run, @mock_generator) do
|
|
|
|
action :git, rm: 'README', add: '.'
|
|
|
|
end
|
2009-06-19 10:08:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_vendor_should_write_data_to_file_in_vendor
|
|
|
|
action :vendor, 'vendor_file.rb', '# vendor data'
|
|
|
|
assert_file 'vendor/vendor_file.rb', '# vendor data'
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_lib_should_write_data_to_file_in_lib
|
|
|
|
action :lib, 'my_library.rb', 'class MyLibrary'
|
|
|
|
assert_file 'lib/my_library.rb', 'class MyLibrary'
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_rakefile_should_write_date_to_file_in_lib_tasks
|
2012-10-14 06:03:39 -04:00
|
|
|
action :rakefile, 'myapp.rake', 'task run: [:environment]'
|
|
|
|
assert_file 'lib/tasks/myapp.rake', 'task run: [:environment]'
|
2009-06-19 10:08:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_initializer_should_write_date_to_file_in_config_initializers
|
|
|
|
action :initializer, 'constants.rb', 'MY_CONSTANT = 42'
|
|
|
|
assert_file 'config/initializers/constants.rb', 'MY_CONSTANT = 42'
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_generate_should_run_script_generate_with_argument_and_options
|
2015-05-29 02:02:44 -04:00
|
|
|
@mock_generator.expect(:call, nil, ['bin/rails generate model MyModel', verbose: false])
|
|
|
|
generator.stub(:run_ruby_script, @mock_generator) do
|
|
|
|
action :generate, 'model', 'MyModel'
|
|
|
|
end
|
2009-06-19 07:13:46 -04:00
|
|
|
end
|
|
|
|
|
2011-10-08 16:09:28 -04:00
|
|
|
def test_rake_should_run_rake_command_with_default_env
|
2015-05-29 02:02:44 -04:00
|
|
|
@mock_generator.expect(:call, nil, ["rake log:clear RAILS_ENV=development", verbose: false])
|
|
|
|
generator.stub(:run, @mock_generator) do
|
|
|
|
with_rails_env nil do
|
|
|
|
action :rake, 'log:clear'
|
|
|
|
end
|
2012-12-06 07:05:45 -05:00
|
|
|
end
|
2009-06-19 10:08:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_rake_with_env_option_should_run_rake_command_in_env
|
2015-05-29 02:02:44 -04:00
|
|
|
@mock_generator.expect(:call, nil, ['rake log:clear RAILS_ENV=production', verbose: false])
|
|
|
|
generator.stub(:run, @mock_generator) do
|
|
|
|
action :rake, 'log:clear', env: 'production'
|
|
|
|
end
|
2009-06-19 10:08:03 -04:00
|
|
|
end
|
|
|
|
|
2011-09-29 08:35:25 -04:00
|
|
|
def test_rake_with_rails_env_variable_should_run_rake_command_in_env
|
2015-05-29 02:02:44 -04:00
|
|
|
@mock_generator.expect(:call, nil, ['rake log:clear RAILS_ENV=production', verbose: false])
|
|
|
|
generator.stub(:run, @mock_generator) do
|
|
|
|
with_rails_env "production" do
|
|
|
|
action :rake, 'log:clear'
|
|
|
|
end
|
2012-12-06 07:05:45 -05:00
|
|
|
end
|
2011-09-29 08:35:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_env_option_should_win_over_rails_env_variable_when_running_rake
|
2015-05-29 02:02:44 -04:00
|
|
|
@mock_generator.expect(:call, nil, ['rake log:clear RAILS_ENV=production', verbose: false])
|
|
|
|
generator.stub(:run, @mock_generator) do
|
|
|
|
with_rails_env "staging" do
|
|
|
|
action :rake, 'log:clear', env: 'production'
|
|
|
|
end
|
2012-12-06 07:05:45 -05:00
|
|
|
end
|
2011-09-29 08:35:25 -04:00
|
|
|
end
|
|
|
|
|
2009-06-19 10:08:03 -04:00
|
|
|
def test_rake_with_sudo_option_should_run_rake_command_with_sudo
|
2015-05-29 02:02:44 -04:00
|
|
|
@mock_generator.expect(:call, nil, ["sudo rake log:clear RAILS_ENV=development", verbose: false])
|
|
|
|
generator.stub(:run, @mock_generator) do
|
|
|
|
with_rails_env nil do
|
|
|
|
action :rake, 'log:clear', sudo: true
|
|
|
|
end
|
2012-12-06 07:05:45 -05:00
|
|
|
end
|
2009-06-19 10:08:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_capify_should_run_the_capify_command
|
2015-05-29 02:02:44 -04:00
|
|
|
@mock_generator.expect(:call, nil, ['capify .', verbose: false])
|
|
|
|
generator.stub(:run, @mock_generator) do
|
|
|
|
action :capify!
|
|
|
|
end
|
2009-06-19 10:08:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_route_should_add_data_to_the_routes_block_in_config_routes
|
|
|
|
run_generator
|
2012-10-14 06:03:39 -04:00
|
|
|
route_command = "route '/login', controller: 'sessions', action: 'new'"
|
2009-06-19 10:08:03 -04:00
|
|
|
action :route, route_command
|
|
|
|
assert_file 'config/routes.rb', /#{Regexp.escape(route_command)}/
|
|
|
|
end
|
|
|
|
|
2015-03-18 10:27:55 -04:00
|
|
|
def test_route_should_add_data_with_an_new_line
|
|
|
|
run_generator
|
|
|
|
action :route, "root 'welcome#index'"
|
|
|
|
route_path = File.expand_path("config/routes.rb", destination_root)
|
|
|
|
content = File.read(route_path)
|
|
|
|
|
|
|
|
# Remove all of the comments and blank lines from the routes file
|
|
|
|
content.gsub!(/^ \#.*\n/, '')
|
|
|
|
content.gsub!(/^\n/, '')
|
|
|
|
|
|
|
|
File.open(route_path, "wb") { |file| file.write(content) }
|
|
|
|
assert_file "config/routes.rb", /\.routes\.draw do\n root 'welcome#index'\nend\n\z/
|
|
|
|
|
|
|
|
action :route, "resources :product_lines"
|
|
|
|
|
|
|
|
routes = <<-F
|
|
|
|
Rails.application.routes.draw do
|
|
|
|
resources :product_lines
|
|
|
|
root 'welcome#index'
|
|
|
|
end
|
|
|
|
F
|
|
|
|
assert_file "config/routes.rb", routes
|
|
|
|
end
|
|
|
|
|
2010-02-17 12:09:13 -05:00
|
|
|
def test_readme
|
|
|
|
run_generator
|
2015-05-29 02:02:44 -04:00
|
|
|
2.times { @mock_generator.expect(:call, destination_root,[]) }
|
|
|
|
Rails::Generators::AppGenerator.stub(:source_root, @mock_generator) do
|
|
|
|
assert_match "application up and running", action(:readme, "README.md")
|
|
|
|
end
|
2010-02-17 12:09:13 -05:00
|
|
|
end
|
|
|
|
|
2011-03-04 07:05:27 -05:00
|
|
|
def test_readme_with_quiet
|
2012-10-14 06:03:39 -04:00
|
|
|
generator(default_arguments, quiet: true)
|
2011-03-04 07:05:27 -05:00
|
|
|
run_generator
|
2015-05-29 02:02:44 -04:00
|
|
|
2.times { @mock_generator.expect(:call, destination_root,[]) }
|
|
|
|
Rails::Generators::AppGenerator.stub(:source_root, @mock_generator) do
|
|
|
|
assert_no_match "application up and running", action(:readme, "README.md")
|
|
|
|
end
|
2011-03-04 07:05:27 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_log
|
|
|
|
assert_equal("YES\n", action(:log, "YES"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_log_with_status
|
|
|
|
assert_equal(" yes YES\n", action(:log, :yes, "YES"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_log_with_quiet
|
2012-10-14 06:03:39 -04:00
|
|
|
generator(default_arguments, quiet: true)
|
2011-03-04 07:05:27 -05:00
|
|
|
assert_equal("", action(:log, "YES"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_log_with_status_with_quiet
|
2012-10-14 06:03:39 -04:00
|
|
|
generator(default_arguments, quiet: true)
|
2011-03-04 07:05:27 -05:00
|
|
|
assert_equal("", action(:log, :yes, "YES"))
|
|
|
|
end
|
|
|
|
|
2009-06-19 10:08:03 -04:00
|
|
|
protected
|
|
|
|
|
|
|
|
def action(*args, &block)
|
2014-07-15 16:56:27 -04:00
|
|
|
capture(:stdout){ generator.send(*args, &block) }
|
2009-06-19 10:08:03 -04:00
|
|
|
end
|
|
|
|
|
2009-06-19 07:13:46 -04:00
|
|
|
end
|