2017-08-14 17:08:09 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 17:16:09 +00:00
|
|
|
require "generators/generators_test_helper"
|
|
|
|
require "rails/generators/rails/model/model_generator"
|
|
|
|
require "rails/generators/test_unit/model/model_generator"
|
2009-07-03 12:55:37 +00:00
|
|
|
|
2010-01-18 23:07:11 +00:00
|
|
|
class GeneratorsTest < Rails::Generators::TestCase
|
|
|
|
include GeneratorsTestHelper
|
2010-01-18 14:04:39 +00:00
|
|
|
|
2009-08-10 16:29:20 +00:00
|
|
|
def setup
|
2010-01-18 14:04:39 +00:00
|
|
|
@path = File.expand_path("lib", Rails.root)
|
|
|
|
$LOAD_PATH.unshift(@path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
$LOAD_PATH.delete(@path)
|
2009-08-10 16:29:20 +00:00
|
|
|
end
|
|
|
|
|
2010-01-18 14:04:39 +00:00
|
|
|
def test_simple_invoke
|
2016-08-06 17:16:09 +00:00
|
|
|
assert File.exist?(File.join(@path, "generators", "model_generator.rb"))
|
2015-08-15 12:08:18 +00:00
|
|
|
assert_called_with(TestUnit::Generators::ModelGenerator, :start, [["Account"], {}]) do
|
2015-05-21 18:42:58 +00:00
|
|
|
Rails::Generators.invoke("test_unit:model", ["Account"])
|
|
|
|
end
|
2010-01-03 11:01:29 +00:00
|
|
|
end
|
|
|
|
|
2009-07-03 12:55:37 +00:00
|
|
|
def test_invoke_when_generator_is_not_found
|
2014-05-24 12:28:50 +00:00
|
|
|
name = :unknown
|
2016-08-16 07:30:11 +00:00
|
|
|
output = capture(:stdout) { Rails::Generators.invoke name }
|
2021-06-03 17:14:03 +00:00
|
|
|
assert_match "Could not find generator '#{name}'.", output
|
2019-01-22 08:53:47 +00:00
|
|
|
assert_match "`bin/rails generate --help`", output
|
2021-06-03 17:14:03 +00:00
|
|
|
assert_no_match "Did you mean", output
|
2014-06-03 23:22:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_generator_suggestions
|
|
|
|
name = :migrationz
|
2016-08-16 07:30:11 +00:00
|
|
|
output = capture(:stdout) { Rails::Generators.invoke name }
|
2021-06-03 17:14:03 +00:00
|
|
|
assert_match "Did you mean? migration", output
|
2009-07-03 12:55:37 +00:00
|
|
|
end
|
|
|
|
|
2017-07-21 15:01:40 +00:00
|
|
|
def test_generator_suggestions_except_en_locale
|
|
|
|
orig_available_locales = I18n.available_locales
|
|
|
|
orig_default_locale = I18n.default_locale
|
|
|
|
I18n.available_locales = :ja
|
|
|
|
I18n.default_locale = :ja
|
|
|
|
name = :tas
|
|
|
|
output = capture(:stdout) { Rails::Generators.invoke name }
|
2021-06-03 17:14:03 +00:00
|
|
|
assert_match "Did you mean? task", output
|
2017-07-21 15:01:40 +00:00
|
|
|
ensure
|
|
|
|
I18n.available_locales = orig_available_locales
|
|
|
|
I18n.default_locale = orig_default_locale
|
|
|
|
end
|
|
|
|
|
2009-07-03 12:55:37 +00:00
|
|
|
def test_help_when_a_generator_with_required_arguments_is_invoked_without_arguments
|
2016-08-16 07:30:11 +00:00
|
|
|
output = capture(:stdout) { Rails::Generators.invoke :model, [] }
|
2011-05-18 11:37:57 +00:00
|
|
|
assert_match(/Description:/, output)
|
2009-07-03 12:55:37 +00:00
|
|
|
end
|
|
|
|
|
2010-01-21 13:11:24 +00:00
|
|
|
def test_should_give_higher_preference_to_rails_generators
|
2016-08-06 17:16:09 +00:00
|
|
|
assert File.exist?(File.join(@path, "generators", "model_generator.rb"))
|
2015-08-15 12:08:18 +00:00
|
|
|
assert_called_with(Rails::Generators::ModelGenerator, :start, [["Account"], {}]) do
|
2016-08-16 07:30:11 +00:00
|
|
|
warnings = capture(:stderr) { Rails::Generators.invoke :model, ["Account"] }
|
2018-01-25 23:16:57 +00:00
|
|
|
assert_empty warnings
|
2015-05-21 18:42:58 +00:00
|
|
|
end
|
2010-01-21 13:11:24 +00:00
|
|
|
end
|
|
|
|
|
2009-07-03 13:12:18 +00:00
|
|
|
def test_invoke_with_default_values
|
2015-08-15 12:08:18 +00:00
|
|
|
assert_called_with(Rails::Generators::ModelGenerator, :start, [["Account"], {}]) do
|
2015-05-21 18:42:58 +00:00
|
|
|
Rails::Generators.invoke :model, ["Account"]
|
|
|
|
end
|
2009-07-03 12:55:37 +00:00
|
|
|
end
|
|
|
|
|
2009-07-03 13:12:18 +00:00
|
|
|
def test_invoke_with_config_values
|
2015-08-15 12:08:18 +00:00
|
|
|
assert_called_with(Rails::Generators::ModelGenerator, :start, [["Account"], behavior: :skip]) do
|
2015-05-21 18:42:58 +00:00
|
|
|
Rails::Generators.invoke :model, ["Account"], behavior: :skip
|
|
|
|
end
|
2009-07-03 12:55:37 +00:00
|
|
|
end
|
|
|
|
|
2010-01-18 14:04:39 +00: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 12:55:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_find_by_namespace_with_base
|
|
|
|
klass = Rails::Generators.find_by_namespace(:model, :rails)
|
|
|
|
assert klass
|
2010-01-18 11:28:52 +00:00
|
|
|
assert_equal "rails:model", klass.namespace
|
2009-07-03 12:55:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_find_by_namespace_with_context
|
|
|
|
klass = Rails::Generators.find_by_namespace(:test_unit, nil, :model)
|
|
|
|
assert klass
|
2010-01-18 11:28:52 +00:00
|
|
|
assert_equal "test_unit:model", klass.namespace
|
2009-07-03 12:55:37 +00:00
|
|
|
end
|
|
|
|
|
2010-01-18 14:04:39 +00:00
|
|
|
def test_find_by_namespace_with_generator_on_root
|
2009-07-03 12:55:37 +00:00
|
|
|
klass = Rails::Generators.find_by_namespace(:fixjour)
|
|
|
|
assert klass
|
|
|
|
assert_equal "fixjour", klass.namespace
|
|
|
|
end
|
|
|
|
|
2010-01-18 14:04:39 +00:00
|
|
|
def test_find_by_namespace_in_subfolder
|
2009-07-03 12:55:37 +00:00
|
|
|
klass = Rails::Generators.find_by_namespace(:fixjour, :active_record)
|
|
|
|
assert klass
|
2010-01-18 11:28:52 +00:00
|
|
|
assert_equal "active_record:fixjour", klass.namespace
|
2009-07-03 12:55:37 +00:00
|
|
|
end
|
|
|
|
|
2010-01-18 14:04:39 +00:00
|
|
|
def test_find_by_namespace_with_duplicated_name
|
|
|
|
klass = Rails::Generators.find_by_namespace(:foobar)
|
2009-07-03 12:55:37 +00:00
|
|
|
assert klass
|
2010-01-18 14:04:39 +00:00
|
|
|
assert_equal "foobar:foobar", klass.namespace
|
2009-07-03 12:55:37 +00:00
|
|
|
end
|
|
|
|
|
2010-01-18 14:04:39 +00:00
|
|
|
def test_find_by_namespace_without_base_or_context_looks_into_rails_namespace
|
|
|
|
assert Rails::Generators.find_by_namespace(:model)
|
2009-07-04 08:32:10 +00:00
|
|
|
end
|
|
|
|
|
2011-01-12 23:27:38 +00:00
|
|
|
def test_invoke_with_nested_namespaces
|
2015-05-21 18:42:58 +00:00
|
|
|
model_generator = Minitest::Mock.new
|
|
|
|
model_generator.expect(:start, nil, [["Account"], {}])
|
2016-08-06 17:16:09 +00:00
|
|
|
assert_called_with(Rails::Generators, :find_by_namespace, ["namespace", "my:awesome"], returns: model_generator) do
|
|
|
|
Rails::Generators.invoke "my:awesome:namespace", ["Account"]
|
2011-01-12 23:27:38 +00:00
|
|
|
end
|
2015-05-21 18:42:58 +00:00
|
|
|
model_generator.verify
|
2011-01-12 23:27:38 +00:00
|
|
|
end
|
|
|
|
|
2009-07-03 12:55:37 +00:00
|
|
|
def test_rails_generators_help_with_builtin_information
|
2016-08-16 07:30:11 +00:00
|
|
|
output = capture(:stdout) { Rails::Generators.help }
|
2011-05-18 11:37:57 +00:00
|
|
|
assert_match(/Rails:/, output)
|
|
|
|
assert_match(/^ model$/, output)
|
|
|
|
assert_match(/^ scaffold_controller$/, output)
|
|
|
|
assert_no_match(/^ app$/, output)
|
2009-07-03 12:55:37 +00:00
|
|
|
end
|
|
|
|
|
2010-11-17 22:31:29 +00:00
|
|
|
def test_rails_generators_help_does_not_include_app_nor_plugin_new
|
2016-08-16 07:30:11 +00:00
|
|
|
output = capture(:stdout) { Rails::Generators.help }
|
2017-07-24 18:17:56 +00:00
|
|
|
assert_no_match(/app\W/, output)
|
2013-06-30 21:02:19 +00:00
|
|
|
assert_no_match(/[^:]plugin/, output)
|
2010-11-17 22:31:29 +00:00
|
|
|
end
|
|
|
|
|
2009-07-03 12:55:37 +00:00
|
|
|
def test_rails_generators_with_others_information
|
2016-08-16 07:30:11 +00:00
|
|
|
output = capture(:stdout) { Rails::Generators.help }
|
2011-05-18 11:37:57 +00:00
|
|
|
assert_match(/Fixjour:/, output)
|
|
|
|
assert_match(/^ fixjour$/, output)
|
2010-03-12 19:50:36 +00:00
|
|
|
end
|
|
|
|
|
2010-07-25 20:08:34 +00:00
|
|
|
def test_rails_generators_does_not_show_active_record_hooks
|
2016-08-16 07:30:11 +00:00
|
|
|
output = capture(:stdout) { Rails::Generators.help }
|
2011-05-18 11:37:57 +00:00
|
|
|
assert_match(/ActiveRecord:/, output)
|
|
|
|
assert_match(/^ active_record:fixjour$/, output)
|
2009-07-04 17:03:52 +00:00
|
|
|
end
|
|
|
|
|
2010-12-31 16:54:49 +00:00
|
|
|
def test_default_banner_should_show_generator_namespace
|
|
|
|
klass = Rails::Generators.find_by_namespace(:foobar)
|
2023-03-29 05:17:04 +00:00
|
|
|
assert_match(/^bin\/rails generate foobar:foobar/, klass.banner)
|
2010-12-31 16:54:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_default_banner_should_not_show_rails_generator_namespace
|
|
|
|
klass = Rails::Generators.find_by_namespace(:model)
|
2023-03-29 05:17:04 +00:00
|
|
|
assert_match(/^bin\/rails generate model/, klass.banner)
|
2010-12-31 16:54:49 +00:00
|
|
|
end
|
|
|
|
|
2009-07-03 13:12:18 +00: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 20:37:22 +00:00
|
|
|
|
2009-07-21 10:16:25 +00: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 11:28:52 +00:00
|
|
|
assert_equal "test_unit:plugin", klass.namespace
|
2014-08-13 11:59:31 +00:00
|
|
|
ensure
|
|
|
|
Rails::Generators.fallbacks.delete(:remarkable)
|
2009-07-21 10:16:25 +00:00
|
|
|
end
|
|
|
|
|
2009-11-20 14:16:01 +00: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 11:28:52 +00:00
|
|
|
assert_equal "test_unit:plugin", klass.namespace
|
2014-08-13 11:59:31 +00:00
|
|
|
ensure
|
|
|
|
Rails::Generators.fallbacks.delete(:remarkable)
|
2009-11-20 14:16:01 +00:00
|
|
|
end
|
|
|
|
|
2009-07-15 22:17:28 +00:00
|
|
|
def test_fallbacks_for_generators_on_invoke
|
|
|
|
Rails::Generators.fallbacks[:shoulda] = :test_unit
|
2015-08-15 12:08:18 +00:00
|
|
|
assert_called_with(TestUnit::Generators::ModelGenerator, :start, [["Account"], {}]) do
|
2015-05-21 18:42:58 +00:00
|
|
|
Rails::Generators.invoke "shoulda:model", ["Account"]
|
|
|
|
end
|
2014-08-13 11:59:31 +00:00
|
|
|
ensure
|
|
|
|
Rails::Generators.fallbacks.delete(:shoulda)
|
2009-07-15 22:17:28 +00:00
|
|
|
end
|
|
|
|
|
2009-07-21 10:16:25 +00:00
|
|
|
def test_nested_fallbacks_for_generators
|
2014-08-13 11:59:31 +00:00
|
|
|
Rails::Generators.fallbacks[:shoulda] = :test_unit
|
2009-07-21 10:16:25 +00:00
|
|
|
Rails::Generators.fallbacks[:super_shoulda] = :shoulda
|
2015-08-15 12:08:18 +00:00
|
|
|
assert_called_with(TestUnit::Generators::ModelGenerator, :start, [["Account"], {}]) do
|
2015-05-21 18:42:58 +00:00
|
|
|
Rails::Generators.invoke "super_shoulda:model", ["Account"]
|
|
|
|
end
|
2014-08-13 11:59:31 +00:00
|
|
|
ensure
|
|
|
|
Rails::Generators.fallbacks.delete(:shoulda)
|
|
|
|
Rails::Generators.fallbacks.delete(:super_shoulda)
|
2009-07-15 22:17:28 +00:00
|
|
|
end
|
2009-07-28 06:48:21 +00:00
|
|
|
|
2013-03-31 15:20:21 +00:00
|
|
|
def test_developer_options_are_overwritten_by_user_options
|
2012-10-14 10:03:39 +00:00
|
|
|
Rails::Generators.options[:with_options] = { generate: false }
|
2009-07-28 06:48:21 +00:00
|
|
|
|
2012-07-17 16:16:55 +00:00
|
|
|
self.class.class_eval(<<-end_eval, __FILE__, __LINE__ + 1)
|
2010-02-26 09:44:53 +00:00
|
|
|
class WithOptionsGenerator < Rails::Generators::Base
|
2017-03-08 03:28:00 +00:00
|
|
|
class_option :generate, default: true, type: :boolean
|
2010-02-25 18:46:29 +00:00
|
|
|
end
|
|
|
|
end_eval
|
2009-07-28 06:48:21 +00:00
|
|
|
|
2010-02-26 09:44:53 +00:00
|
|
|
assert_equal false, WithOptionsGenerator.class_options[:generate].default
|
2010-01-03 11:01:29 +00:00
|
|
|
ensure
|
2010-02-26 09:44:53 +00:00
|
|
|
Rails::Generators.subclasses.delete(WithOptionsGenerator)
|
2009-07-28 06:48:21 +00:00
|
|
|
end
|
2009-07-28 06:51:57 +00:00
|
|
|
|
2010-01-18 14:04:39 +00: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))
|
2016-08-16 07:30:11 +00:00
|
|
|
File.open(template, "w") { |f| f.write "empty" }
|
2010-01-18 14:04:39 +00:00
|
|
|
|
2012-05-30 09:15:30 +00:00
|
|
|
capture(:stdout) do
|
2012-10-14 10:03:39 +00:00
|
|
|
Rails::Generators.invoke :model, ["user"], destination_root: destination_root
|
2010-01-18 14:04:39 +00: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 06:51:57 +00:00
|
|
|
def test_source_paths_for_not_namespaced_generators
|
2010-01-18 14:04:39 +00:00
|
|
|
mspec = Rails::Generators.find_by_namespace :fixjour
|
2016-09-16 16:44:05 +00:00
|
|
|
assert_includes mspec.source_paths, File.join(Rails.root, "lib", "templates", "fixjour")
|
2009-07-28 06:51:57 +00:00
|
|
|
end
|
2012-02-03 11:17:59 +00:00
|
|
|
|
2011-11-02 13:00:18 +00:00
|
|
|
def test_usage_with_embedded_ruby
|
2017-06-11 12:59:23 +00:00
|
|
|
require_relative "fixtures/lib/generators/usage_template/usage_template_generator"
|
2016-08-06 17:16:09 +00:00
|
|
|
output = capture(:stdout) { Rails::Generators.invoke :usage_template, ["--help"] }
|
2012-05-30 09:08:56 +00:00
|
|
|
assert_match(/:: 2 ::/, output)
|
2011-11-02 13:00:18 +00:00
|
|
|
end
|
2012-02-03 11:17:59 +00:00
|
|
|
|
|
|
|
def test_hide_namespace
|
2016-09-16 16:44:05 +00:00
|
|
|
assert_not_includes Rails::Generators.hidden_namespaces, "special:namespace"
|
2012-02-03 11:17:59 +00:00
|
|
|
Rails::Generators.hide_namespace("special:namespace")
|
2016-09-16 16:44:05 +00:00
|
|
|
assert_includes Rails::Generators.hidden_namespaces, "special:namespace"
|
2012-02-03 11:17:59 +00:00
|
|
|
end
|
2009-07-03 12:55:37 +00:00
|
|
|
end
|