2009-11-05 19:11:52 -05:00
|
|
|
require 'generators/generators_test_helper'
|
2010-03-23 08:40:19 -04:00
|
|
|
require 'rails/generators/rails/model/model_generator'
|
|
|
|
require 'rails/generators/test_unit/model/model_generator'
|
2009-07-03 08:55:37 -04:00
|
|
|
|
2010-01-18 18:07:11 -05:00
|
|
|
class GeneratorsTest < Rails::Generators::TestCase
|
|
|
|
include GeneratorsTestHelper
|
2010-01-18 09:04:39 -05:00
|
|
|
|
2009-08-10 12:29:20 -04:00
|
|
|
def setup
|
2010-01-18 09:04:39 -05:00
|
|
|
@path = File.expand_path("lib", Rails.root)
|
|
|
|
$LOAD_PATH.unshift(@path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
$LOAD_PATH.delete(@path)
|
2009-08-10 12:29:20 -04:00
|
|
|
end
|
|
|
|
|
2010-01-18 09:04:39 -05:00
|
|
|
def test_simple_invoke
|
2010-01-21 08:11:24 -05:00
|
|
|
assert File.exists?(File.join(@path, 'generators', 'model_generator.rb'))
|
2010-01-03 06:01:29 -05:00
|
|
|
TestUnit::Generators::ModelGenerator.expects(:start).with(["Account"], {})
|
|
|
|
Rails::Generators.invoke("test_unit:model", ["Account"])
|
|
|
|
end
|
|
|
|
|
2009-07-03 08:55:37 -04:00
|
|
|
def test_invoke_when_generator_is_not_found
|
|
|
|
output = capture(:stdout){ Rails::Generators.invoke :unknown }
|
|
|
|
assert_equal "Could not find generator unknown.\n", output
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_help_when_a_generator_with_required_arguments_is_invoked_without_arguments
|
|
|
|
output = capture(:stdout){ Rails::Generators.invoke :model, [] }
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(/Description:/, output)
|
2009-07-03 08:55:37 -04:00
|
|
|
end
|
|
|
|
|
2010-01-21 08:11:24 -05:00
|
|
|
def test_should_give_higher_preference_to_rails_generators
|
|
|
|
assert File.exists?(File.join(@path, 'generators', 'model_generator.rb'))
|
|
|
|
Rails::Generators::ModelGenerator.expects(:start).with(["Account"], {})
|
|
|
|
warnings = capture(:stderr){ Rails::Generators.invoke :model, ["Account"] }
|
|
|
|
assert warnings.empty?
|
|
|
|
end
|
|
|
|
|
2009-07-03 09:12:18 -04:00
|
|
|
def test_invoke_with_default_values
|
|
|
|
Rails::Generators::ModelGenerator.expects(:start).with(["Account"], {})
|
2009-07-03 08:55:37 -04:00
|
|
|
Rails::Generators.invoke :model, ["Account"]
|
|
|
|
end
|
|
|
|
|
2009-07-03 09:12:18 -04:00
|
|
|
def test_invoke_with_config_values
|
2012-10-14 06:03:39 -04:00
|
|
|
Rails::Generators::ModelGenerator.expects(:start).with(["Account"], behavior: :skip)
|
|
|
|
Rails::Generators.invoke :model, ["Account"], behavior: :skip
|
2009-07-03 08:55:37 -04:00
|
|
|
end
|
|
|
|
|
2010-01-18 09:04:39 -05:00
|
|
|
def test_find_by_namespace
|
|
|
|
klass = Rails::Generators.find_by_namespace("rails:model")
|
|
|
|
assert klass
|
|
|
|
assert_equal "rails:model", klass.namespace
|
2009-07-03 08:55:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_find_by_namespace_with_base
|
|
|
|
klass = Rails::Generators.find_by_namespace(:model, :rails)
|
|
|
|
assert klass
|
2010-01-18 06:28:52 -05:00
|
|
|
assert_equal "rails:model", klass.namespace
|
2009-07-03 08:55:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_find_by_namespace_with_context
|
|
|
|
klass = Rails::Generators.find_by_namespace(:test_unit, nil, :model)
|
|
|
|
assert klass
|
2010-01-18 06:28:52 -05:00
|
|
|
assert_equal "test_unit:model", klass.namespace
|
2009-07-03 08:55:37 -04:00
|
|
|
end
|
|
|
|
|
2010-01-18 09:04:39 -05:00
|
|
|
def test_find_by_namespace_with_generator_on_root
|
2009-07-03 08:55:37 -04:00
|
|
|
klass = Rails::Generators.find_by_namespace(:fixjour)
|
|
|
|
assert klass
|
|
|
|
assert_equal "fixjour", klass.namespace
|
|
|
|
end
|
|
|
|
|
2010-01-18 09:04:39 -05:00
|
|
|
def test_find_by_namespace_in_subfolder
|
2009-07-03 08:55:37 -04:00
|
|
|
klass = Rails::Generators.find_by_namespace(:fixjour, :active_record)
|
|
|
|
assert klass
|
2010-01-18 06:28:52 -05:00
|
|
|
assert_equal "active_record:fixjour", klass.namespace
|
2009-07-03 08:55:37 -04:00
|
|
|
end
|
|
|
|
|
2010-01-18 09:04:39 -05:00
|
|
|
def test_find_by_namespace_with_duplicated_name
|
|
|
|
klass = Rails::Generators.find_by_namespace(:foobar)
|
2009-07-03 08:55:37 -04:00
|
|
|
assert klass
|
2010-01-18 09:04:39 -05:00
|
|
|
assert_equal "foobar:foobar", klass.namespace
|
2009-07-03 08:55:37 -04:00
|
|
|
end
|
|
|
|
|
2010-01-18 09:04:39 -05:00
|
|
|
def test_find_by_namespace_without_base_or_context_looks_into_rails_namespace
|
|
|
|
assert Rails::Generators.find_by_namespace(:model)
|
2009-07-04 04:32:10 -04:00
|
|
|
end
|
|
|
|
|
2011-01-12 18:27:38 -05:00
|
|
|
def test_invoke_with_nested_namespaces
|
|
|
|
model_generator = mock('ModelGenerator') do
|
|
|
|
expects(:start).with(["Account"], {})
|
|
|
|
end
|
|
|
|
Rails::Generators.expects(:find_by_namespace).with('namespace', 'my:awesome').returns(model_generator)
|
|
|
|
Rails::Generators.invoke 'my:awesome:namespace', ["Account"]
|
|
|
|
end
|
|
|
|
|
2009-07-03 08:55:37 -04:00
|
|
|
def test_rails_generators_help_with_builtin_information
|
|
|
|
output = capture(:stdout){ Rails::Generators.help }
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(/Rails:/, output)
|
|
|
|
assert_match(/^ model$/, output)
|
|
|
|
assert_match(/^ scaffold_controller$/, output)
|
|
|
|
assert_no_match(/^ app$/, output)
|
2009-07-03 08:55:37 -04:00
|
|
|
end
|
|
|
|
|
2010-11-17 17:31:29 -05:00
|
|
|
def test_rails_generators_help_does_not_include_app_nor_plugin_new
|
|
|
|
output = capture(:stdout){ Rails::Generators.help }
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_no_match(/app/, output)
|
|
|
|
assert_no_match(/plugin_new/, output)
|
2010-11-17 17:31:29 -05:00
|
|
|
end
|
|
|
|
|
2009-07-03 08:55:37 -04:00
|
|
|
def test_rails_generators_with_others_information
|
2010-01-18 10:14:32 -05:00
|
|
|
output = capture(:stdout){ Rails::Generators.help }
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(/Fixjour:/, output)
|
|
|
|
assert_match(/^ fixjour$/, output)
|
2010-03-12 14:50:36 -05:00
|
|
|
end
|
|
|
|
|
2010-07-25 16:08:34 -04:00
|
|
|
def test_rails_generators_does_not_show_active_record_hooks
|
2010-03-12 14:50:36 -05:00
|
|
|
output = capture(:stdout){ Rails::Generators.help }
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(/ActiveRecord:/, output)
|
|
|
|
assert_match(/^ active_record:fixjour$/, output)
|
2009-07-04 13:03:52 -04:00
|
|
|
end
|
|
|
|
|
2010-12-31 11:54:49 -05:00
|
|
|
def test_default_banner_should_show_generator_namespace
|
|
|
|
klass = Rails::Generators.find_by_namespace(:foobar)
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(/^rails generate foobar:foobar/, klass.banner)
|
2010-12-31 11:54:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_default_banner_should_not_show_rails_generator_namespace
|
|
|
|
klass = Rails::Generators.find_by_namespace(:model)
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(/^rails generate model/, klass.banner)
|
2010-12-31 11:54:49 -05:00
|
|
|
end
|
|
|
|
|
2009-07-03 09:12:18 -04:00
|
|
|
def test_no_color_sets_proper_shell
|
|
|
|
Rails::Generators.no_color!
|
|
|
|
assert_equal Thor::Shell::Basic, Thor::Base.shell
|
|
|
|
ensure
|
|
|
|
Thor::Base.shell = Thor::Shell::Color
|
|
|
|
end
|
2009-07-15 16:37:22 -04:00
|
|
|
|
2009-07-21 06:16:25 -04:00
|
|
|
def test_fallbacks_for_generators_on_find_by_namespace
|
|
|
|
Rails::Generators.fallbacks[:remarkable] = :test_unit
|
|
|
|
klass = Rails::Generators.find_by_namespace(:plugin, :remarkable)
|
|
|
|
assert klass
|
2010-01-18 06:28:52 -05:00
|
|
|
assert_equal "test_unit:plugin", klass.namespace
|
2009-07-21 06:16:25 -04:00
|
|
|
end
|
|
|
|
|
2009-11-20 09:16:01 -05:00
|
|
|
def test_fallbacks_for_generators_on_find_by_namespace_with_context
|
|
|
|
Rails::Generators.fallbacks[:remarkable] = :test_unit
|
|
|
|
klass = Rails::Generators.find_by_namespace(:remarkable, :rails, :plugin)
|
|
|
|
assert klass
|
2010-01-18 06:28:52 -05:00
|
|
|
assert_equal "test_unit:plugin", klass.namespace
|
2009-11-20 09:16:01 -05:00
|
|
|
end
|
|
|
|
|
2009-07-15 18:17:28 -04:00
|
|
|
def test_fallbacks_for_generators_on_invoke
|
|
|
|
Rails::Generators.fallbacks[:shoulda] = :test_unit
|
|
|
|
TestUnit::Generators::ModelGenerator.expects(:start).with(["Account"], {})
|
|
|
|
Rails::Generators.invoke "shoulda:model", ["Account"]
|
|
|
|
end
|
|
|
|
|
2009-07-21 06:16:25 -04:00
|
|
|
def test_nested_fallbacks_for_generators
|
|
|
|
Rails::Generators.fallbacks[:super_shoulda] = :shoulda
|
|
|
|
TestUnit::Generators::ModelGenerator.expects(:start).with(["Account"], {})
|
|
|
|
Rails::Generators.invoke "super_shoulda:model", ["Account"]
|
2009-07-15 18:17:28 -04:00
|
|
|
end
|
2009-07-28 02:48:21 -04:00
|
|
|
|
2013-03-31 11:20:21 -04:00
|
|
|
def test_developer_options_are_overwritten_by_user_options
|
2012-10-14 06:03:39 -04:00
|
|
|
Rails::Generators.options[:with_options] = { generate: false }
|
2009-07-28 02:48:21 -04:00
|
|
|
|
2012-07-17 12:16:55 -04:00
|
|
|
self.class.class_eval(<<-end_eval, __FILE__, __LINE__ + 1)
|
2010-02-26 04:44:53 -05:00
|
|
|
class WithOptionsGenerator < Rails::Generators::Base
|
2010-02-25 13:46:29 -05:00
|
|
|
class_option :generate, :default => true
|
|
|
|
end
|
|
|
|
end_eval
|
2009-07-28 02:48:21 -04:00
|
|
|
|
2010-02-26 04:44:53 -05:00
|
|
|
assert_equal false, WithOptionsGenerator.class_options[:generate].default
|
2010-01-03 06:01:29 -05:00
|
|
|
ensure
|
2010-02-26 04:44:53 -05:00
|
|
|
Rails::Generators.subclasses.delete(WithOptionsGenerator)
|
2009-07-28 02:48:21 -04:00
|
|
|
end
|
2009-07-28 02:51:57 -04:00
|
|
|
|
2010-01-18 09:04:39 -05:00
|
|
|
def test_rails_root_templates
|
|
|
|
template = File.join(Rails.root, "lib", "templates", "active_record", "model", "model.rb")
|
|
|
|
|
|
|
|
# Create template
|
|
|
|
mkdir_p(File.dirname(template))
|
|
|
|
File.open(template, 'w'){ |f| f.write "empty" }
|
|
|
|
|
2012-05-30 05:15:30 -04:00
|
|
|
capture(:stdout) do
|
2012-10-14 06:03:39 -04:00
|
|
|
Rails::Generators.invoke :model, ["user"], destination_root: destination_root
|
2010-01-18 09:04:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_file "app/models/user.rb" do |content|
|
|
|
|
assert_equal "empty", content
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
rm_rf File.dirname(template)
|
|
|
|
end
|
|
|
|
|
2009-07-28 02:51:57 -04:00
|
|
|
def test_source_paths_for_not_namespaced_generators
|
2010-01-18 09:04:39 -05:00
|
|
|
mspec = Rails::Generators.find_by_namespace :fixjour
|
|
|
|
assert mspec.source_paths.include?(File.join(Rails.root, "lib", "templates", "fixjour"))
|
2009-07-28 02:51:57 -04:00
|
|
|
end
|
2012-02-03 06:17:59 -05:00
|
|
|
|
2011-11-02 09:00:18 -04:00
|
|
|
def test_usage_with_embedded_ruby
|
|
|
|
require File.expand_path("fixtures/lib/generators/usage_template/usage_template_generator", File.dirname(__FILE__))
|
|
|
|
output = capture(:stdout) { Rails::Generators.invoke :usage_template, ['--help'] }
|
2012-05-30 05:08:56 -04:00
|
|
|
assert_match(/:: 2 ::/, output)
|
2011-11-02 09:00:18 -04:00
|
|
|
end
|
2012-02-03 06:17:59 -05:00
|
|
|
|
|
|
|
def test_hide_namespace
|
|
|
|
assert !Rails::Generators.hidden_namespaces.include?("special:namespace")
|
|
|
|
Rails::Generators.hide_namespace("special:namespace")
|
|
|
|
assert Rails::Generators.hidden_namespaces.include?("special:namespace")
|
|
|
|
end
|
2009-07-03 08:55:37 -04:00
|
|
|
end
|