diff options
Diffstat (limited to 'railties/test/generators_test.rb')
-rw-r--r-- | railties/test/generators_test.rb | 137 |
1 files changed, 61 insertions, 76 deletions
diff --git a/railties/test/generators_test.rb b/railties/test/generators_test.rb index 2df218debc..60c81a813f 100644 --- a/railties/test/generators_test.rb +++ b/railties/test/generators_test.rb @@ -1,15 +1,21 @@ require 'generators/generators_test_helper' -require 'rails/generators/rails/model/model_generator' -require 'rails/generators/test_unit/model/model_generator' +require 'generators/rails/model/model_generator' +require 'generators/test_unit/model/model_generator' require 'mocha' -class GeneratorsTest < GeneratorsTestCase +class GeneratorsTest < Rails::Generators::TestCase + include GeneratorsTestHelper + def setup - Rails::Generators.instance_variable_set(:@load_paths, nil) - Gem.stubs(:respond_to?).with(:loaded_specs).returns(false) + @path = File.expand_path("lib", Rails.root) + $LOAD_PATH.unshift(@path) + end + + def teardown + $LOAD_PATH.delete(@path) end - def test_invoke_add_generators_to_raw_lookups + def test_simple_invoke TestUnit::Generators::ModelGenerator.expects(:start).with(["Account"], {}) Rails::Generators.invoke("test_unit:model", ["Account"]) end @@ -34,87 +40,66 @@ class GeneratorsTest < GeneratorsTestCase Rails::Generators.invoke :model, ["Account"], :behavior => :skip end - def test_find_by_namespace_without_base_or_context - assert_nil Rails::Generators.find_by_namespace(:model) + def test_find_by_namespace + klass = Rails::Generators.find_by_namespace("rails:model") + assert klass + assert_equal "rails:model", klass.namespace 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 + assert_equal "rails: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 - - def test_find_by_namespace_with_duplicated_name - klass = Rails::Generators.find_by_namespace(:foobar) - assert klass - assert_equal "foobar:foobar", klass.namespace + assert_equal "test_unit:model", klass.namespace end - def test_find_by_namespace_lookup_to_the_rails_root_folder + def test_find_by_namespace_with_generator_on_root 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 + def test_find_by_namespace_in_subfolder klass = Rails::Generators.find_by_namespace(:fixjour, :active_record) assert klass - assert_equal "active_record:generators:fixjour", klass.namespace + assert_equal "active_record:fixjour", klass.namespace end - def test_find_by_namespace_lookup_traverse_folders - klass = Rails::Generators.find_by_namespace(:javascripts, :rails) + def test_find_by_namespace_with_duplicated_name + klass = Rails::Generators.find_by_namespace(:foobar) assert klass - assert_equal "rails:generators:javascripts", klass.namespace + assert_equal "foobar:foobar", klass.namespace end - def test_find_by_namespace_lookup_to_vendor_folders - klass = Rails::Generators.find_by_namespace(:mspec) - assert klass - assert_equal "mspec", klass.namespace + def test_find_by_namespace_without_base_or_context_looks_into_rails_namespace + assert Rails::Generators.find_by_namespace(:model) end - 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 - spec.expects(:full_gem_path).returns(File.join(Rails.root, 'vendor', 'another_gem_path', 'xspec')) - 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 - - def test_builtin_generators - assert Rails::Generators.builtin.include?("rails:model") + def test_find_by_namespace_show_warning_if_generator_cant_be_loaded + output = capture(:stderr) { Rails::Generators.find_by_namespace(:wrong) } + assert_match /\[WARNING\] Could not load generator/, output + assert_match /Rails 2\.x generator/, output end def test_rails_generators_help_with_builtin_information output = capture(:stdout){ Rails::Generators.help } - assert_match /model/, output - assert_match /scaffold_controller/, output + assert_match /Rails:/, output + 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 - assert_equal "Others: active_record:fixjour, fixjour, foobar:foobar, mspec, rails:javascripts, xspec.", output - end - - def test_warning_is_shown_if_generator_cant_be_loaded - Rails::Generators.load_paths << File.join(Rails.root, "vendor", "gems", "gems", "wrong") - output = capture(:stderr){ Rails::Generators.find_by_namespace(:wrong) } - - assert_match /\[WARNING\] Could not load generator at/, output - assert_match /Rails 2\.x generator/, output + output = capture(:stdout){ Rails::Generators.help } + assert_match /ActiveRecord:/, output + assert_match /Fixjour:/, output + assert_match /^ active_record:model$/, output + assert_match /^ active_record:fixjour$/, output + assert_match /^ fixjour$/, output end def test_no_color_sets_proper_shell @@ -124,36 +109,18 @@ class GeneratorsTest < GeneratorsTestCase Thor::Base.shell = Thor::Shell::Color end - 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" } - - 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 - 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 + assert_equal "test_unit:plugin", klass.namespace end 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 - assert_equal "test_unit:generators:plugin", klass.namespace + assert_equal "test_unit:plugin", klass.namespace end def test_fallbacks_for_generators_on_invoke @@ -181,8 +148,26 @@ class GeneratorsTest < GeneratorsTestCase Rails::Generators.subclasses.delete(klass) end + 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" } + + 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 + def test_source_paths_for_not_namespaced_generators - mspec = Rails::Generators.find_by_namespace :mspec - assert mspec.source_paths.include?(File.join(Rails.root, "lib", "templates", "mspec")) + mspec = Rails::Generators.find_by_namespace :fixjour + assert mspec.source_paths.include?(File.join(Rails.root, "lib", "templates", "fixjour")) end end |