2009-11-05 19:11:52 -05:00
|
|
|
require 'generators/generators_test_helper'
|
2009-09-24 17:01:31 -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
|
|
|
require 'mocha'
|
|
|
|
|
|
|
|
class GeneratorsTest < GeneratorsTestCase
|
2009-08-10 12:29:20 -04:00
|
|
|
def setup
|
|
|
|
Rails::Generators.instance_variable_set(:@load_paths, nil)
|
|
|
|
Gem.stubs(:respond_to?).with(:loaded_specs).returns(false)
|
|
|
|
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, [] }
|
|
|
|
assert_match /Description:/, output
|
|
|
|
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
|
2009-07-03 08:55:37 -04:00
|
|
|
Rails::Generators::ModelGenerator.expects(:start).with(["Account"], :behavior => :skip)
|
2009-07-03 09:12:18 -04:00
|
|
|
Rails::Generators.invoke :model, ["Account"], :behavior => :skip
|
2009-07-03 08:55:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_find_by_namespace_without_base_or_context
|
|
|
|
assert_nil Rails::Generators.find_by_namespace(:model)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_find_by_namespace_with_base
|
|
|
|
klass = Rails::Generators.find_by_namespace(:model, :rails)
|
|
|
|
assert klass
|
|
|
|
assert_equal "rails:generators:model", klass.namespace
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_find_by_namespace_with_context
|
|
|
|
klass = Rails::Generators.find_by_namespace(:test_unit, nil, :model)
|
|
|
|
assert klass
|
|
|
|
assert_equal "test_unit:generators:model", klass.namespace
|
|
|
|
end
|
|
|
|
|
2009-11-03 17:50:39 -05:00
|
|
|
def test_find_by_namespace_with_duplicated_name
|
|
|
|
klass = Rails::Generators.find_by_namespace(:foobar)
|
|
|
|
assert klass
|
|
|
|
assert_equal "foobar:foobar", klass.namespace
|
|
|
|
end
|
|
|
|
|
2009-07-03 08:55:37 -04:00
|
|
|
def test_find_by_namespace_add_generators_to_raw_lookups
|
|
|
|
klass = Rails::Generators.find_by_namespace("test_unit:model")
|
|
|
|
assert klass
|
|
|
|
assert_equal "test_unit:generators:model", klass.namespace
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_find_by_namespace_lookup_to_the_rails_root_folder
|
|
|
|
klass = Rails::Generators.find_by_namespace(:fixjour)
|
|
|
|
assert klass
|
|
|
|
assert_equal "fixjour", klass.namespace
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_find_by_namespace_lookup_to_deep_rails_root_folders
|
|
|
|
klass = Rails::Generators.find_by_namespace(:fixjour, :active_record)
|
|
|
|
assert klass
|
|
|
|
assert_equal "active_record:generators:fixjour", klass.namespace
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_find_by_namespace_lookup_traverse_folders
|
|
|
|
klass = Rails::Generators.find_by_namespace(:javascripts, :rails)
|
|
|
|
assert klass
|
|
|
|
assert_equal "rails:generators:javascripts", klass.namespace
|
|
|
|
end
|
|
|
|
|
2009-07-04 04:32:10 -04:00
|
|
|
def test_find_by_namespace_lookup_to_vendor_folders
|
|
|
|
klass = Rails::Generators.find_by_namespace(:mspec)
|
|
|
|
assert klass
|
|
|
|
assert_equal "mspec", klass.namespace
|
|
|
|
end
|
|
|
|
|
2009-08-10 12:29:20 -04:00
|
|
|
def test_find_by_namespace_lookup_with_gem_specification
|
|
|
|
assert_nil Rails::Generators.find_by_namespace(:xspec)
|
|
|
|
Rails::Generators.instance_variable_set(:@load_paths, nil)
|
|
|
|
|
|
|
|
spec = Gem::Specification.new
|
2009-10-18 11:53:43 -04:00
|
|
|
spec.expects(:full_gem_path).returns(File.join(Rails.root, 'vendor', 'another_gem_path', 'xspec'))
|
2009-08-10 12:29:20 -04:00
|
|
|
Gem.expects(:respond_to?).with(:loaded_specs).returns(true)
|
|
|
|
Gem.expects(:loaded_specs).returns(:spec => spec)
|
|
|
|
|
|
|
|
klass = Rails::Generators.find_by_namespace(:xspec)
|
|
|
|
assert klass
|
|
|
|
assert_equal "xspec", klass.namespace
|
|
|
|
end
|
|
|
|
|
2009-07-03 08:55:37 -04:00
|
|
|
def test_builtin_generators
|
|
|
|
assert Rails::Generators.builtin.include? %w(rails model)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_rails_generators_help_with_builtin_information
|
|
|
|
output = capture(:stdout){ Rails::Generators.help }
|
|
|
|
assert_match /model/, output
|
|
|
|
assert_match /scaffold_controller/, output
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_rails_generators_with_others_information
|
|
|
|
output = capture(:stdout){ Rails::Generators.help }.split("\n").last
|
2009-11-03 17:50:39 -05:00
|
|
|
assert_equal "Others: active_record:fixjour, fixjour, foobar, mspec, rails:javascripts.", output
|
2009-07-04 13:03:52 -04:00
|
|
|
end
|
|
|
|
|
2009-07-15 16:37:22 -04:00
|
|
|
def test_warning_is_shown_if_generator_cant_be_loaded
|
2009-11-02 21:08:33 -05:00
|
|
|
Rails::Generators.load_paths << File.join(Rails.root, "vendor", "gems", "gems", "wrong")
|
2009-07-04 13:03:52 -04:00
|
|
|
output = capture(:stderr){ Rails::Generators.find_by_namespace(:wrong) }
|
2009-11-03 17:50:39 -05:00
|
|
|
|
2009-07-04 13:03:52 -04:00
|
|
|
assert_match /\[WARNING\] Could not load generator at/, output
|
2009-11-03 17:50:39 -05:00
|
|
|
assert_match /Rails 2\.x generator/, output
|
2009-07-03 08:55:37 -04: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
|
|
|
|
|
|
|
def test_rails_root_templates
|
2009-10-18 11:53:43 -04:00
|
|
|
template = File.join(Rails.root, "lib", "templates", "active_record", "model", "model.rb")
|
2009-07-15 16:37:22 -04:00
|
|
|
|
|
|
|
# Create template
|
|
|
|
mkdir_p(File.dirname(template))
|
|
|
|
File.open(template, 'w'){ |f| f.write "empty" }
|
|
|
|
|
|
|
|
output = capture(:stdout) do
|
|
|
|
Rails::Generators.invoke :model, ["user"], :destination_root => destination_root
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_file "app/models/user.rb" do |content|
|
|
|
|
assert_equal "empty", content
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
rm_rf File.dirname(template)
|
|
|
|
end
|
2009-07-15 18:17:28 -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
|
|
|
|
assert_equal "test_unit:generators:plugin", klass.namespace
|
|
|
|
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
|
|
|
|
|
|
|
def test_developer_options_are_overwriten_by_user_options
|
|
|
|
Rails::Generators.options[:new_generator] = { :generate => false }
|
|
|
|
|
|
|
|
klass = Class.new(Rails::Generators::Base) do
|
|
|
|
def self.name
|
|
|
|
"NewGenerator"
|
|
|
|
end
|
|
|
|
|
|
|
|
class_option :generate, :default => true
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal false, klass.class_options[:generate].default
|
|
|
|
end
|
2009-07-28 02:51:57 -04:00
|
|
|
|
|
|
|
def test_source_paths_for_not_namespaced_generators
|
|
|
|
mspec = Rails::Generators.find_by_namespace :mspec
|
2009-10-18 11:53:43 -04:00
|
|
|
assert mspec.source_paths.include?(File.join(Rails.root, "lib", "templates", "mspec"))
|
2009-07-28 02:51:57 -04:00
|
|
|
end
|
2009-07-03 08:55:37 -04:00
|
|
|
end
|