Permalink
Browse files

Update generator tests. Closes #11487 [thechrisoshow]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9176 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 0a2e980 commit 2681d55e407233c126ff8f0a5956c302b3acfb91 @jeremy jeremy committed Apr 1, 2008
@@ -186,7 +186,7 @@ def class_collisions(*class_names)
# 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 @@ def file(relative_source, relative_destination, file_options = {}, &block)
# 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 @@ def directory(relative_path)
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 @@ def directory(relative_path)
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
@@ -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 @@ def assert_generated_helper_for(name)
# 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 @@ def assert_generated_functional_test_for(name,parent="Test::Unit::TestCase")
# 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 @@ def assert_generated_file(path)
# 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 @@ def assert_generated_yaml(path)
# 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 @@ def assert_generated_views_for(name,*actions)
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 @@ def assert_has_method(body,*methods)
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
@@ -48,8 +48,9 @@ class InstanceTag; end
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,15 +81,15 @@ def teardown
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
assert_generated_migration :create_products
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
@@ -49,7 +49,6 @@ class InstanceTag; 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 @@ def teardown
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 @@ def test_resource_generates_resources
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
Oops, something went wrong.

0 comments on commit 2681d55

Please sign in to comment.