diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2008-04-01 00:29:24 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2008-04-01 00:29:24 +0000 |
commit | 2681d55e407233c126ff8f0a5956c302b3acfb91 (patch) | |
tree | b8806f323379fbaa82698f8b8e59e33eea98e423 | |
parent | 0a2e980ddd3b69dcce51896b454f04e3a1d05ee9 (diff) | |
download | rails-2681d55e407233c126ff8f0a5956c302b3acfb91.tar.gz rails-2681d55e407233c126ff8f0a5956c302b3acfb91.tar.bz2 rails-2681d55e407233c126ff8f0a5956c302b3acfb91.zip |
Update generator tests. Closes #11487 [thechrisoshow]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9176 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r-- | railties/lib/rails_generator/commands.rb | 44 | ||||
-rw-r--r-- | railties/test/generators/generator_test_helper.rb | 43 | ||||
-rw-r--r-- | railties/test/generators/rails_model_generator_test.rb | 7 | ||||
-rw-r--r-- | railties/test/generators/rails_resource_generator_test.rb | 5 | ||||
-rw-r--r-- | railties/test/generators/rails_scaffold_generator_test.rb | 212 | ||||
-rw-r--r-- | railties/test/rails_generator_test.rb | 11 |
6 files changed, 163 insertions, 159 deletions
diff --git a/railties/lib/rails_generator/commands.rb b/railties/lib/rails_generator/commands.rb index f74ca7722b..440bab276c 100644 --- a/railties/lib/rails_generator/commands.rb +++ b/railties/lib/rails_generator/commands.rb @@ -186,7 +186,7 @@ HELP # file 'config/empty.log', 'log/test.log', :chmod => 0664 # :shebang sets the #!/usr/bin/ruby line for scripts # file 'bin/generate.rb', 'script/generate', :chmod => 0755, :shebang => '/usr/bin/env ruby' - # :collision sets the collision option only for the destination file: + # :collision sets the collision option only for the destination file: # file 'settings/server.yml', 'config/server.yml', :collision => :skip # # Collisions are handled by checking whether the destination file @@ -200,7 +200,7 @@ HELP # If source and destination are identical then we're done. if destination_exists and identical?(source, destination, &block) - return logger.identical(relative_destination) + return logger.identical(relative_destination) end # Check for and resolve file collisions. @@ -300,25 +300,25 @@ HELP logger.exists relative_path else logger.create relative_path - unless options[:pretend] - FileUtils.mkdir_p(path) - # git doesn't require adding the paths, adding the files later will - # automatically do a path add. - - # Subversion doesn't do path adds, so we need to add - # each directory individually. - # So stack up the directory tree and add the paths to - # subversion in order without recursion. - if options[:svn] - stack=[relative_path] - until File.dirname(stack.last) == stack.last # dirname('.') == '.' - stack.push File.dirname(stack.last) - end - stack.reverse_each do |rel_path| - svn_path = destination_path(rel_path) - system("svn add -N #{svn_path}") unless File.directory?(File.join(svn_path, '.svn')) - end - end + unless options[:pretend] + FileUtils.mkdir_p(path) + # git doesn't require adding the paths, adding the files later will + # automatically do a path add. + + # Subversion doesn't do path adds, so we need to add + # each directory individually. + # So stack up the directory tree and add the paths to + # subversion in order without recursion. + if options[:svn] + stack = [relative_path] + until File.dirname(stack.last) == stack.last # dirname('.') == '.' + stack.push File.dirname(stack.last) + end + stack.reverse_each do |rel_path| + svn_path = destination_path(rel_path) + system("svn add -N #{svn_path}") unless File.directory?(File.join(svn_path, '.svn')) + end + end end end end @@ -544,7 +544,7 @@ end_message def readme(*args) logger.readme args.join(', ') end - + def migration_template(relative_source, relative_destination, options = {}) migration_directory relative_destination logger.migration_template file_name diff --git a/railties/test/generators/generator_test_helper.rb b/railties/test/generators/generator_test_helper.rb index 3af5886a01..4007cf16ca 100644 --- a/railties/test/generators/generator_test_helper.rb +++ b/railties/test/generators/generator_test_helper.rb @@ -1,3 +1,15 @@ +require 'initializer' + +# Mocks out the configuration +module Rails + def self.configuration + Rails::Configuration.new + end +end + +require 'rails_generator' + + module GeneratorTestHelper # Instantiates the Generator def build_generator(name,params) @@ -51,7 +63,7 @@ module GeneratorTestHelper # asserts that the given functional test was generated. # It takes a name or symbol without the <tt>_controller_test</tt> part and an optional super class. # the contents of the class source file is passed to a block. - def assert_generated_functional_test_for(name,parent="Test::Unit::TestCase") + def assert_generated_functional_test_for(name,parent="ActionController::TestCase") assert_generated_class "test/functional/#{name.to_s.underscore}_controller_test",parent do |body| yield body if block_given? end @@ -60,7 +72,7 @@ module GeneratorTestHelper # asserts that the given unit test was generated. # It takes a name or symbol without the <tt>_test</tt> part and an optional super class. # the contents of the class source file is passed to a block. - def assert_generated_unit_test_for(name,parent="Test::Unit::TestCase") + def assert_generated_unit_test_for(name,parent="ActiveSupport::TestCase") assert_generated_class "test/unit/#{name.to_s.underscore}_test",parent do |body| yield body if block_given? end @@ -77,7 +89,7 @@ module GeneratorTestHelper # asserts that the given file exists def assert_file_exists(path) - assert File.exist?("#{RAILS_ROOT}/#{path}"),"The file '#{path}' should exist" + assert File.exist?("#{RAILS_ROOT}/#{path}"),"The file '#{RAILS_ROOT}/#{path}' should exist" end # asserts that the given class source file was generated. @@ -128,7 +140,6 @@ module GeneratorTestHelper # the parsed yaml tree is passed to a block. def assert_generated_fixtures_for(name) assert_generated_yaml "test/fixtures/#{name.to_s.underscore}" do |yaml| - assert_generated_timestamps(yaml) yield yaml if block_given? end end @@ -144,15 +155,15 @@ module GeneratorTestHelper end end - # asserts that the given migration file was generated. - # It takes the name of the migration as a parameter. - # The migration body is passed to a block. def assert_generated_migration(name,parent="ActiveRecord::Migration") - assert_generated_class "db/migrate/001_#{name.to_s.underscore}",parent do |body| - assert body=~/timestamps/, "should have timestamps defined" - yield body if block_given? + file = + Dir.glob("#{RAILS_ROOT}/db/migrate/*_#{name.to_s.underscore}.rb").first + file = file.match(/db\/migrate\/[0-9]+_#{name.to_s.underscore}/).to_s + assert_generated_class file,parent do |body| + assert body=~/timestamps/, "should have timestamps defined" + yield body if block_given? + end end - end # Asserts that the given migration file was not generated. # It takes the name of the migration as a parameter. @@ -182,14 +193,4 @@ module GeneratorTestHelper def assert_generated_column(body,name,type) assert body=~/t\.#{type.to_s} :#{name.to_s}/, "should have column #{name.to_s} defined" end - - private - # asserts that the default timestamps are created in the fixture - def assert_generated_timestamps(yaml) - yaml.values.each do |v| - ["created_at", "updated_at"].each do |field| - assert v.keys.include?(field), "should have #{field} field by default" - end - end - end end diff --git a/railties/test/generators/rails_model_generator_test.rb b/railties/test/generators/rails_model_generator_test.rb index 58d1f53ebb..86ad9c4dc8 100644 --- a/railties/test/generators/rails_model_generator_test.rb +++ b/railties/test/generators/rails_model_generator_test.rb @@ -48,8 +48,9 @@ else end Dir.mkdir(RAILS_ROOT) unless File.exist?(RAILS_ROOT) + $LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../lib" -require 'rails_generator' + require 'generators/generator_test_helper' class RailsModelGeneratorTest < Test::Unit::TestCase @@ -80,7 +81,7 @@ class RailsModelGeneratorTest < Test::Unit::TestCase end def test_model_generates_resources - run_generator('model', %w(Product)) + run_generator('model', %w(Product name:string)) assert_generated_model_for :product assert_generated_fixtures_for :products @@ -88,7 +89,7 @@ class RailsModelGeneratorTest < Test::Unit::TestCase end def test_model_skip_migration_skips_migration - run_generator('model', %w(Product --skip-migration)) + run_generator('model', %w(Product name:string --skip-migration)) assert_generated_model_for :product assert_generated_fixtures_for :products diff --git a/railties/test/generators/rails_resource_generator_test.rb b/railties/test/generators/rails_resource_generator_test.rb index bec003dc4c..511f1059c2 100644 --- a/railties/test/generators/rails_resource_generator_test.rb +++ b/railties/test/generators/rails_resource_generator_test.rb @@ -49,7 +49,6 @@ end Dir.mkdir(RAILS_ROOT) unless File.exist?(RAILS_ROOT) $LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../lib" -require 'rails_generator' require 'generators/generator_test_helper' class RailsResourceGeneratorTest < Test::Unit::TestCase @@ -80,7 +79,7 @@ class RailsResourceGeneratorTest < Test::Unit::TestCase end def test_resource_generates_resources - run_generator('scaffold', %w(Product)) + run_generator('resource', %w(Product name:string)) assert_generated_controller_for :products assert_generated_model_for :product @@ -92,7 +91,7 @@ class RailsResourceGeneratorTest < Test::Unit::TestCase end def test_resource_skip_migration_skips_migration - run_generator('resource', %w(Product --skip-migration)) + run_generator('resource', %w(Product name:string --skip-migration)) assert_generated_controller_for :products assert_generated_model_for :product diff --git a/railties/test/generators/rails_scaffold_generator_test.rb b/railties/test/generators/rails_scaffold_generator_test.rb index f25ca74ed4..34a1ad2fe5 100644 --- a/railties/test/generators/rails_scaffold_generator_test.rb +++ b/railties/test/generators/rails_scaffold_generator_test.rb @@ -1,4 +1,4 @@ -require 'test/unit' +require 'abstract_unit' # Optionally load RubyGems. begin @@ -51,135 +51,139 @@ end Dir.mkdir(RAILS_ROOT) unless File.exist?(RAILS_ROOT) $LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../lib" -require 'rails_generator' + require 'generators/generator_test_helper' -class RailsScaffoldGeneratorTest < Test::Unit::TestCase - - include GeneratorTestHelper - - def setup - ActiveRecord::Base.pluralize_table_names = true - Dir.mkdir("#{RAILS_ROOT}/app") unless File.exist?("#{RAILS_ROOT}/app") - Dir.mkdir("#{RAILS_ROOT}/app/views") unless File.exist?("#{RAILS_ROOT}/app/views") - Dir.mkdir("#{RAILS_ROOT}/app/views/layouts") unless File.exist?("#{RAILS_ROOT}/app/views/layouts") - Dir.mkdir("#{RAILS_ROOT}/config") unless File.exist?("#{RAILS_ROOT}/config") - Dir.mkdir("#{RAILS_ROOT}/db") unless File.exist?("#{RAILS_ROOT}/db") - Dir.mkdir("#{RAILS_ROOT}/test") unless File.exist?("#{RAILS_ROOT}/test") - Dir.mkdir("#{RAILS_ROOT}/test/fixtures") unless File.exist?("#{RAILS_ROOT}/test/fixtures") - Dir.mkdir("#{RAILS_ROOT}/public") unless File.exist?("#{RAILS_ROOT}/public") - Dir.mkdir("#{RAILS_ROOT}/public/stylesheets") unless File.exist?("#{RAILS_ROOT}/public/stylesheets") - File.open("#{RAILS_ROOT}/config/routes.rb", 'w') do |f| - f<<"ActionController::Routing::Routes.draw do |map|\n\nend\n" +uses_mocha "Scaffold Generator Tests" do + class RailsScaffoldGeneratorTest < Test::Unit::TestCase + + include GeneratorTestHelper + + def setup + ActiveRecord::Base.pluralize_table_names = true + Dir.mkdir("#{RAILS_ROOT}/app") unless File.exist?("#{RAILS_ROOT}/app") + Dir.mkdir("#{RAILS_ROOT}/app/views") unless File.exist?("#{RAILS_ROOT}/app/views") + Dir.mkdir("#{RAILS_ROOT}/app/views/layouts") unless File.exist?("#{RAILS_ROOT}/app/views/layouts") + Dir.mkdir("#{RAILS_ROOT}/config") unless File.exist?("#{RAILS_ROOT}/config") + Dir.mkdir("#{RAILS_ROOT}/db") unless File.exist?("#{RAILS_ROOT}/db") + Dir.mkdir("#{RAILS_ROOT}/test") unless File.exist?("#{RAILS_ROOT}/test") + Dir.mkdir("#{RAILS_ROOT}/test/fixtures") unless File.exist?("#{RAILS_ROOT}/test/fixtures") + Dir.mkdir("#{RAILS_ROOT}/public") unless File.exist?("#{RAILS_ROOT}/public") + Dir.mkdir("#{RAILS_ROOT}/public/stylesheets") unless File.exist?("#{RAILS_ROOT}/public/stylesheets") + File.open("#{RAILS_ROOT}/config/routes.rb", 'w') do |f| + f<<"ActionController::Routing::Routes.draw do |map|\n\nend\n" + end end - end - def teardown - FileUtils.rm_rf "#{RAILS_ROOT}/app" - FileUtils.rm_rf "#{RAILS_ROOT}/test" - FileUtils.rm_rf "#{RAILS_ROOT}/config" - FileUtils.rm_rf "#{RAILS_ROOT}/db" - FileUtils.rm_rf "#{RAILS_ROOT}/public" - end + def teardown + FileUtils.rm_rf "#{RAILS_ROOT}/app" + FileUtils.rm_rf "#{RAILS_ROOT}/test" + FileUtils.rm_rf "#{RAILS_ROOT}/config" + FileUtils.rm_rf "#{RAILS_ROOT}/db" + FileUtils.rm_rf "#{RAILS_ROOT}/public" + end - def test_scaffolded_names - g = Rails::Generator::Base.instance('scaffold', %w(ProductLine)) - assert_equal "ProductLines", g.controller_name - assert_equal "ProductLines", g.controller_class_name - assert_equal "ProductLine", g.controller_singular_name - assert_equal "product_lines", g.controller_plural_name - assert_equal "product_lines", g.controller_file_name - assert_equal "product_lines", g.controller_table_name - end + def test_scaffolded_names + g = Rails::Generator::Base.instance('scaffold', %w(ProductLine)) + assert_equal "ProductLines", g.controller_name + assert_equal "ProductLines", g.controller_class_name + assert_equal "ProductLine", g.controller_singular_name + assert_equal "product_lines", g.controller_plural_name + assert_equal "product_lines", g.controller_file_name + assert_equal "product_lines", g.controller_table_name + end - def test_scaffold_generates_resources + def test_scaffold_generates_resources - run_generator('scaffold', %w(Product)) + run_generator('scaffold', %w(Product name:string)) - assert_generated_controller_for :products do |f| + assert_generated_controller_for :products do |f| - assert_has_method f, :index do |name, m| - assert_match /@products = Product\.find\(:all\)/, m, "#{name} should query products table" - end + assert_has_method f, :index do |name, m| + assert_match /@products = Product\.find\(:all\)/, m, "#{name} should query products table" + end - assert_has_method f, :show, :edit, :update, :destroy do |name, m| - assert_match /@product = Product\.find\(params\[:id\]\)/, m, "#{name.to_s} should query products table" - end + assert_has_method f, :show, :edit, :update, :destroy do |name, m| + assert_match /@product = Product\.find\(params\[:id\]\)/, m, "#{name.to_s} should query products table" + end - assert_has_method f, :new do |name, m| - assert_match /@product = Product\.new/, m, "#{name.to_s} should instantiate a product" - end + assert_has_method f, :new do |name, m| + assert_match /@product = Product\.new/, m, "#{name.to_s} should instantiate a product" + end + + assert_has_method f, :create do |name, m| + assert_match /@product = Product\.new\(params\[:product\]\)/, m, "#{name.to_s} should instantiate a product" + assert_match /format.xml \{ render :xml => @product.errors, :status => :unprocessable_entity \}/, m, "#{name.to_s} should set status to :unprocessable_entity code for xml" + end - assert_has_method f, :create do |name, m| - assert_match /@product = Product\.new\(params\[:product\]\)/, m, "#{name.to_s} should instantiate a product" - assert_match /format.xml \{ render :xml => @product.errors, :status => :unprocessable_entity \}/, m, "#{name.to_s} should set status to :unprocessable_entity code for xml" end + assert_generated_model_for :product + assert_generated_functional_test_for :products + assert_generated_unit_test_for :product + assert_generated_fixtures_for :products + assert_generated_helper_for :products + assert_generated_stylesheet :scaffold + assert_generated_views_for :products, "index.html.erb", "new.html.erb", "edit.html.erb", "show.html.erb" + + assert_generated_migration :create_products + assert_added_route_for :products end - assert_generated_model_for :product - assert_generated_functional_test_for :products - assert_generated_unit_test_for :product - assert_generated_fixtures_for :products - assert_generated_helper_for :products - assert_generated_stylesheet :scaffold - assert_generated_views_for :products, "index.html.erb", "new.html.erb", "edit.html.erb", "show.html.erb" - assert_generated_migration :create_products - assert_added_route_for :products - end + def test_scaffold_skip_migration_skips_migration + run_generator('scaffold', %w(Product name:string --skip-migration)) + + assert_generated_model_for :product + assert_generated_functional_test_for :products + assert_generated_unit_test_for :product + assert_generated_fixtures_for :products + assert_generated_helper_for :products + assert_generated_stylesheet :scaffold + assert_generated_views_for :products, "index.html.erb","new.html.erb","edit.html.erb","show.html.erb" + assert_skipped_migration :create_products + assert_added_route_for :products + end - def test_scaffold_skip_migration_skips_migration - run_generator('scaffold', %w(Product --skip-migration)) - - assert_generated_model_for :product - assert_generated_functional_test_for :products - assert_generated_unit_test_for :product - assert_generated_fixtures_for :products - assert_generated_helper_for :products - assert_generated_stylesheet :scaffold - assert_generated_views_for :products, "index.html.erb","new.html.erb","edit.html.erb","show.html.erb" - assert_skipped_migration :create_products - assert_added_route_for :products - end + def test_scaffold_generates_resources_with_attributes + run_generator('scaffold', %w(Product name:string supplier_id:integer created_at:timestamp)) - def test_scaffold_generates_resources_with_attributes - run_generator('scaffold', %w(Product name:string supplier_id:integer created_at:timestamp)) + assert_generated_controller_for :products do |f| - assert_generated_controller_for :products do |f| + assert_has_method f, :index do |name, m| + assert_match /@products = Product\.find\(:all\)/, m, "#{name} should query products table" + end - assert_has_method f, :index do |name, m| - assert_match /@products = Product\.find\(:all\)/, m, "#{name} should query products table" - end + assert_has_method f, :show, :edit, :update, :destroy do |name, m| + assert_match /@product = Product\.find\(params\[:id\]\)/, m, "#{name.to_s} should query products table" + end - assert_has_method f, :show, :edit, :update, :destroy do |name, m| - assert_match /@product = Product\.find\(params\[:id\]\)/, m, "#{name.to_s} should query products table" - end + assert_has_method f, :new do |name, m| + assert_match /@product = Product\.new/, m, "#{name.to_s} should instantiate a product" + end - assert_has_method f, :new do |name, m| - assert_match /@product = Product\.new/, m, "#{name.to_s} should instantiate a product" - end + assert_has_method f, :create do |name, m| + assert_match /@product = Product\.new\(params\[:product\]\)/, m, "#{name.to_s} should instantiate a product" + assert_match /format.xml \{ render :xml => @product.errors, :status => :unprocessable_entity \}/, m, "#{name.to_s} should set status to :unprocessable_entity code for xml" + end - assert_has_method f, :create do |name, m| - assert_match /@product = Product\.new\(params\[:product\]\)/, m, "#{name.to_s} should instantiate a product" - assert_match /format.xml \{ render :xml => @product.errors, :status => :unprocessable_entity \}/, m, "#{name.to_s} should set status to :unprocessable_entity code for xml" end - end + assert_generated_model_for :product + assert_generated_functional_test_for :products + assert_generated_unit_test_for :product + assert_generated_fixtures_for :products + assert_generated_helper_for :products + assert_generated_stylesheet :scaffold + assert_generated_views_for :products, "index.html.erb", "new.html.erb", "edit.html.erb", "show.html.erb" + + assert_generated_migration :create_products do |t| + assert_generated_column t, :name, :string + assert_generated_column t, :supplier_id, :integer + assert_generated_column t, :created_at, :timestamp + end - assert_generated_model_for :product - assert_generated_functional_test_for :products - assert_generated_unit_test_for :product - assert_generated_fixtures_for :products - assert_generated_helper_for :products - assert_generated_stylesheet :scaffold - assert_generated_views_for :products, "index.html.erb", "new.html.erb", "edit.html.erb", "show.html.erb" - assert_generated_migration :create_products do |t| - assert_generated_column t, :name, :string - assert_generated_column t, :supplier_id, :integer - assert_generated_column t, :created_at, :timestamp + assert_added_route_for :products end - assert_added_route_for :products end - -end +end
\ No newline at end of file diff --git a/railties/test/rails_generator_test.rb b/railties/test/rails_generator_test.rb index 51d02312bc..63dd501614 100644 --- a/railties/test/rails_generator_test.rb +++ b/railties/test/rails_generator_test.rb @@ -33,8 +33,7 @@ else end $LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib" -require 'rails_generator' - +require 'generators/generator_test_helper' class RailsGeneratorTest < Test::Unit::TestCase BUILTINS = %w(controller integration_test mailer migration model observer plugin resource scaffold session_migration) @@ -46,7 +45,7 @@ class RailsGeneratorTest < Test::Unit::TestCase def test_sources expected = [:lib, :vendor, - :plugins, :plugins, # <plugin>/generators and <plugin>/rails_generators + "plugins (vendor/plugins)".to_sym, # <plugin>/generators and <plugin>/rails_generators :user, :RubyGems, :RubyGems, # gems named <x>_generator, gems containing /rails_generator/ folder :builtin] @@ -74,9 +73,9 @@ class RailsGeneratorTest < Test::Unit::TestCase end def test_lookup_missing_generator - assert_raise(MissingSourceFile) { + assert_raise Rails::Generator::GeneratorError do Rails::Generator::Base.lookup('missing_generator').klass - } + end end def test_lookup_missing_class @@ -98,7 +97,7 @@ class RailsGeneratorTest < Test::Unit::TestCase def test_generator_spec spec = Rails::Generator::Base.lookup('working') assert_equal 'working', spec.name - assert_equal "#{RAILS_ROOT}/lib/generators/working", spec.path + assert_match(/#{spec.path}$/, "#{RAILS_ROOT}/lib/generators/working") assert_equal :lib, spec.source assert_nothing_raised { assert_match(/WorkingGenerator$/, spec.klass.name) } end |