Changeset 9176
- Timestamp:
- 04/01/08 00:29:24 (4 months ago)
- Files:
-
- trunk/railties/lib/rails_generator/commands.rb (modified) (4 diffs)
- trunk/railties/test/generators/generator_test_helper.rb (modified) (7 diffs)
- trunk/railties/test/generators/rails_model_generator_test.rb (modified) (3 diffs)
- trunk/railties/test/generators/rails_resource_generator_test.rb (modified) (3 diffs)
- trunk/railties/test/generators/rails_scaffold_generator_test.rb (modified) (2 diffs)
- trunk/railties/test/rails_generator_test.rb (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/railties/lib/rails_generator/commands.rb
r9122 r9176 187 187 # :shebang sets the #!/usr/bin/ruby line for scripts 188 188 # file 'bin/generate.rb', 'script/generate', :chmod => 0755, :shebang => '/usr/bin/env ruby' 189 # :collision sets the collision option only for the destination file: 189 # :collision sets the collision option only for the destination file: 190 190 # file 'settings/server.yml', 'config/server.yml', :collision => :skip 191 191 # … … 201 201 # If source and destination are identical then we're done. 202 202 if destination_exists and identical?(source, destination, &block) 203 return logger.identical(relative_destination) 203 return logger.identical(relative_destination) 204 204 end 205 205 … … 301 301 else 302 302 logger.create relative_path 303 unless options[:pretend]304 FileUtils.mkdir_p(path)305 # git doesn't require adding the paths, adding the files later will306 # automatically do a path add.307 308 # Subversion doesn't do path adds, so we need to add309 # each directory individually.310 # So stack up the directory tree and add the paths to311 # subversion in order without recursion.312 if options[:svn]313 stack=[relative_path]314 until File.dirname(stack.last) == stack.last # dirname('.') == '.'315 stack.push File.dirname(stack.last)316 end317 stack.reverse_each do |rel_path|318 svn_path = destination_path(rel_path)319 system("svn add -N #{svn_path}") unless File.directory?(File.join(svn_path, '.svn'))320 end321 end303 unless options[:pretend] 304 FileUtils.mkdir_p(path) 305 # git doesn't require adding the paths, adding the files later will 306 # automatically do a path add. 307 308 # Subversion doesn't do path adds, so we need to add 309 # each directory individually. 310 # So stack up the directory tree and add the paths to 311 # subversion in order without recursion. 312 if options[:svn] 313 stack = [relative_path] 314 until File.dirname(stack.last) == stack.last # dirname('.') == '.' 315 stack.push File.dirname(stack.last) 316 end 317 stack.reverse_each do |rel_path| 318 svn_path = destination_path(rel_path) 319 system("svn add -N #{svn_path}") unless File.directory?(File.join(svn_path, '.svn')) 320 end 321 end 322 322 end 323 323 end … … 545 545 logger.readme args.join(', ') 546 546 end 547 547 548 548 def migration_template(relative_source, relative_destination, options = {}) 549 549 migration_directory relative_destination trunk/railties/test/generators/generator_test_helper.rb
r8365 r9176 1 require 'initializer' 2 3 # Mocks out the configuration 4 module Rails 5 def self.configuration 6 Rails::Configuration.new 7 end 8 end 9 10 require 'rails_generator' 11 12 1 13 module GeneratorTestHelper 2 14 # Instantiates the Generator … … 52 64 # It takes a name or symbol without the <tt>_controller_test</tt> part and an optional super class. 53 65 # the contents of the class source file is passed to a block. 54 def assert_generated_functional_test_for(name,parent=" Test::Unit::TestCase")66 def assert_generated_functional_test_for(name,parent="ActionController::TestCase") 55 67 assert_generated_class "test/functional/#{name.to_s.underscore}_controller_test",parent do |body| 56 68 yield body if block_given? … … 61 73 # It takes a name or symbol without the <tt>_test</tt> part and an optional super class. 62 74 # the contents of the class source file is passed to a block. 63 def assert_generated_unit_test_for(name,parent=" Test::Unit::TestCase")75 def assert_generated_unit_test_for(name,parent="ActiveSupport::TestCase") 64 76 assert_generated_class "test/unit/#{name.to_s.underscore}_test",parent do |body| 65 77 yield body if block_given? … … 78 90 # asserts that the given file exists 79 91 def assert_file_exists(path) 80 assert File.exist?("#{RAILS_ROOT}/#{path}"),"The file '#{ path}' should exist"92 assert File.exist?("#{RAILS_ROOT}/#{path}"),"The file '#{RAILS_ROOT}/#{path}' should exist" 81 93 end 82 94 … … 129 141 def assert_generated_fixtures_for(name) 130 142 assert_generated_yaml "test/fixtures/#{name.to_s.underscore}" do |yaml| 131 assert_generated_timestamps(yaml)132 143 yield yaml if block_given? 133 144 end … … 145 156 end 146 157 147 # asserts that the given migration file was generated.148 # It takes the name of the migration as a parameter.149 # The migration body is passed to a block.150 158 def assert_generated_migration(name,parent="ActiveRecord::Migration") 151 assert_generated_class "db/migrate/001_#{name.to_s.underscore}",parent do |body| 152 assert body=~/timestamps/, "should have timestamps defined" 153 yield body if block_given? 159 file = 160 Dir.glob("#{RAILS_ROOT}/db/migrate/*_#{name.to_s.underscore}.rb").first 161 file = file.match(/db\/migrate\/[0-9]+_#{name.to_s.underscore}/).to_s 162 assert_generated_class file,parent do |body| 163 assert body=~/timestamps/, "should have timestamps defined" 164 yield body if block_given? 165 end 154 166 end 155 end156 167 157 168 # Asserts that the given migration file was not generated. … … 183 194 assert body=~/t\.#{type.to_s} :#{name.to_s}/, "should have column #{name.to_s} defined" 184 195 end 185 186 private187 # asserts that the default timestamps are created in the fixture188 def assert_generated_timestamps(yaml)189 yaml.values.each do |v|190 ["created_at", "updated_at"].each do |field|191 assert v.keys.include?(field), "should have #{field} field by default"192 end193 end194 end195 196 end trunk/railties/test/generators/rails_model_generator_test.rb
r8569 r9176 49 49 Dir.mkdir(RAILS_ROOT) unless File.exist?(RAILS_ROOT) 50 50 51 51 52 $LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../lib" 52 require 'rails_generator' 53 53 54 require 'generators/generator_test_helper' 54 55 … … 81 82 82 83 def test_model_generates_resources 83 run_generator('model', %w(Product ))84 run_generator('model', %w(Product name:string)) 84 85 85 86 assert_generated_model_for :product … … 89 90 90 91 def test_model_skip_migration_skips_migration 91 run_generator('model', %w(Product --skip-migration))92 run_generator('model', %w(Product name:string --skip-migration)) 92 93 93 94 assert_generated_model_for :product trunk/railties/test/generators/rails_resource_generator_test.rb
r8569 r9176 50 50 51 51 $LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../lib" 52 require 'rails_generator'53 52 require 'generators/generator_test_helper' 54 53 … … 81 80 82 81 def test_resource_generates_resources 83 run_generator(' scaffold', %w(Product))82 run_generator('resource', %w(Product name:string)) 84 83 85 84 assert_generated_controller_for :products … … 93 92 94 93 def test_resource_skip_migration_skips_migration 95 run_generator('resource', %w(Product --skip-migration))94 run_generator('resource', %w(Product name:string --skip-migration)) 96 95 97 96 assert_generated_controller_for :products trunk/railties/test/generators/rails_scaffold_generator_test.rb
r8569 r9176 1 require ' test/unit'1 require 'abstract_unit' 2 2 3 3 # Optionally load RubyGems. … … 52 52 53 53 $LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../lib" 54 require 'rails_generator' 54 55 55 require 'generators/generator_test_helper' 56 56 57 class RailsScaffoldGeneratorTest < Test::Unit::TestCase 57 uses_mocha "Scaffold Generator Tests" do 58 class RailsScaffoldGeneratorTest < Test::Unit::TestCase 58 59 59 include GeneratorTestHelper60 include GeneratorTestHelper 60 61 61 def setup 62 ActiveRecord::Base.pluralize_table_names = true 63 Dir.mkdir("#{RAILS_ROOT}/app") unless File.exist?("#{RAILS_ROOT}/app") 64 Dir.mkdir("#{RAILS_ROOT}/app/views") unless File.exist?("#{RAILS_ROOT}/app/views") 65 Dir.mkdir("#{RAILS_ROOT}/app/views/layouts") unless File.exist?("#{RAILS_ROOT}/app/views/layouts") 66 Dir.mkdir("#{RAILS_ROOT}/config") unless File.exist?("#{RAILS_ROOT}/config") 67 Dir.mkdir("#{RAILS_ROOT}/db") unless File.exist?("#{RAILS_ROOT}/db") 68 Dir.mkdir("#{RAILS_ROOT}/test") unless File.exist?("#{RAILS_ROOT}/test") 69 Dir.mkdir("#{RAILS_ROOT}/test/fixtures") unless File.exist?("#{RAILS_ROOT}/test/fixtures") 70 Dir.mkdir("#{RAILS_ROOT}/public") unless File.exist?("#{RAILS_ROOT}/public") 71 Dir.mkdir("#{RAILS_ROOT}/public/stylesheets") unless File.exist?("#{RAILS_ROOT}/public/stylesheets") 72 File.open("#{RAILS_ROOT}/config/routes.rb", 'w') do |f| 73 f<<"ActionController::Routing::Routes.draw do |map|\n\nend\n" 62 def setup 63 ActiveRecord::Base.pluralize_table_names = true 64 Dir.mkdir("#{RAILS_ROOT}/app") unless File.exist?("#{RAILS_ROOT}/app") 65 Dir.mkdir("#{RAILS_ROOT}/app/views") unless File.exist?("#{RAILS_ROOT}/app/views") 66 Dir.mkdir("#{RAILS_ROOT}/app/views/layouts") unless File.exist?("#{RAILS_ROOT}/app/views/layouts") 67 Dir.mkdir("#{RAILS_ROOT}/config") unless File.exist?("#{RAILS_ROOT}/config") 68 Dir.mkdir("#{RAILS_ROOT}/db") unless File.exist?("#{RAILS_ROOT}/db") 69 Dir.mkdir("#{RAILS_ROOT}/test") unless File.exist?("#{RAILS_ROOT}/test") 70 Dir.mkdir("#{RAILS_ROOT}/test/fixtures") unless File.exist?("#{RAILS_ROOT}/test/fixtures") 71 Dir.mkdir("#{RAILS_ROOT}/public") unless File.exist?("#{RAILS_ROOT}/public") 72 Dir.mkdir("#{RAILS_ROOT}/public/stylesheets") unless File.exist?("#{RAILS_ROOT}/public/stylesheets") 73 File.open("#{RAILS_ROOT}/config/routes.rb", 'w') do |f| 74 f<<"ActionController::Routing::Routes.draw do |map|\n\nend\n" 75 end 74 76 end 75 end76 77 77 def teardown78 FileUtils.rm_rf "#{RAILS_ROOT}/app"79 FileUtils.rm_rf "#{RAILS_ROOT}/test"80 FileUtils.rm_rf "#{RAILS_ROOT}/config"81 FileUtils.rm_rf "#{RAILS_ROOT}/db"82 FileUtils.rm_rf "#{RAILS_ROOT}/public"83 end78 def teardown 79 FileUtils.rm_rf "#{RAILS_ROOT}/app" 80 FileUtils.rm_rf "#{RAILS_ROOT}/test" 81 FileUtils.rm_rf "#{RAILS_ROOT}/config" 82 FileUtils.rm_rf "#{RAILS_ROOT}/db" 83 FileUtils.rm_rf "#{RAILS_ROOT}/public" 84 end 84 85 85 def test_scaffolded_names86 g = Rails::Generator::Base.instance('scaffold', %w(ProductLine))87 assert_equal "ProductLines", g.controller_name88 assert_equal "ProductLines", g.controller_class_name89 assert_equal "ProductLine", g.controller_singular_name90 assert_equal "product_lines", g.controller_plural_name91 assert_equal "product_lines", g.controller_file_name92 assert_equal "product_lines", g.controller_table_name93 end86 def test_scaffolded_names 87 g = Rails::Generator::Base.instance('scaffold', %w(ProductLine)) 88 assert_equal "ProductLines", g.controller_name 89 assert_equal "ProductLines", g.controller_class_name 90 assert_equal "ProductLine", g.controller_singular_name 91 assert_equal "product_lines", g.controller_plural_name 92 assert_equal "product_lines", g.controller_file_name 93 assert_equal "product_lines", g.controller_table_name 94 end 94 95 95 def test_scaffold_generates_resources96 def test_scaffold_generates_resources 96 97 97 run_generator('scaffold', %w(Product))98 run_generator('scaffold', %w(Product name:string)) 98 99 99 assert_generated_controller_for :products do |f|100 assert_generated_controller_for :products do |f| 100 101 101 assert_has_method f, :index do |name, m| 102 assert_match /@products = Product\.find\(:all\)/, m, "#{name} should query products table" 102 assert_has_method f, :index do |name, m| 103 assert_match /@products = Product\.find\(:all\)/, m, "#{name} should query products table" 104 end 105 106 assert_has_method f, :show, :edit, :update, :destroy do |name, m| 107 assert_match /@product = Product\.find\(params\[:id\]\)/, m, "#{name.to_s} should query products table" 108 end 109 110 assert_has_method f, :new do |name, m| 111 assert_match /@product = Product\.new/, m, "#{name.to_s} should instantiate a product" 112 end 113 114 assert_has_method f, :create do |name, m| 115 assert_match /@product = Product\.new\(params\[:product\]\)/, m, "#{name.to_s} should instantiate a product" 116 assert_match /format.xml \{ render :xml => @product.errors, :status => :unprocessable_entity \}/, m, "#{name.to_s} should set status to :unprocessable_entity code for xml" 117 end 118 103 119 end 104 120 105 assert_has_method f, :show, :edit, :update, :destroy do |name, m| 106 assert_match /@product = Product\.find\(params\[:id\]\)/, m, "#{name.to_s} should query products table" 121 assert_generated_model_for :product 122 assert_generated_functional_test_for :products 123 assert_generated_unit_test_for :product 124 assert_generated_fixtures_for :products 125 assert_generated_helper_for :products 126 assert_generated_stylesheet :scaffold 127 assert_generated_views_for :products, "index.html.erb", "new.html.erb", "edit.html.erb", "show.html.erb" 128 129 assert_generated_migration :create_products 130 assert_added_route_for :products 131 end 132 133 def test_scaffold_skip_migration_skips_migration 134 run_generator('scaffold', %w(Product name:string --skip-migration)) 135 136 assert_generated_model_for :product 137 assert_generated_functional_test_for :products 138 assert_generated_unit_test_for :product 139 assert_generated_fixtures_for :products 140 assert_generated_helper_for :products 141 assert_generated_stylesheet :scaffold 142 assert_generated_views_for :products, "index.html.erb","new.html.erb","edit.html.erb","show.html.erb" 143 assert_skipped_migration :create_products 144 assert_added_route_for :products 145 end 146 147 def test_scaffold_generates_resources_with_attributes 148 run_generator('scaffold', %w(Product name:string supplier_id:integer created_at:timestamp)) 149 150 assert_generated_controller_for :products do |f| 151 152 assert_has_method f, :index do |name, m| 153 assert_match /@products = Product\.find\(:all\)/, m, "#{name} should query products table" 154 end 155 156 assert_has_method f, :show, :edit, :update, :destroy do |name, m| 157 assert_match /@product = Product\.find\(params\[:id\]\)/, m, "#{name.to_s} should query products table" 158 end 159 160 assert_has_method f, :new do |name, m| 161 assert_match /@product = Product\.new/, m, "#{name.to_s} should instantiate a product" 162 end 163 164 assert_has_method f, :create do |name, m| 165 assert_match /@product = Product\.new\(params\[:product\]\)/, m, "#{name.to_s} should instantiate a product" 166 assert_match /format.xml \{ render :xml => @product.errors, :status => :unprocessable_entity \}/, m, "#{name.to_s} should set status to :unprocessable_entity code for xml" 167 end 168 107 169 end 108 170 109 assert_has_method f, :new do |name, m| 110 assert_match /@product = Product\.new/, m, "#{name.to_s} should instantiate a product" 171 assert_generated_model_for :product 172 assert_generated_functional_test_for :products 173 assert_generated_unit_test_for :product 174 assert_generated_fixtures_for :products 175 assert_generated_helper_for :products 176 assert_generated_stylesheet :scaffold 177 assert_generated_views_for :products, "index.html.erb", "new.html.erb", "edit.html.erb", "show.html.erb" 178 179 assert_generated_migration :create_products do |t| 180 assert_generated_column t, :name, :string 181 assert_generated_column t, :supplier_id, :integer 182 assert_generated_column t, :created_at, :timestamp 111 183 end 112 184 113 assert_has_method f, :create do |name, m| 114 assert_match /@product = Product\.new\(params\[:product\]\)/, m, "#{name.to_s} should instantiate a product" 115 assert_match /format.xml \{ render :xml => @product.errors, :status => :unprocessable_entity \}/, m, "#{name.to_s} should set status to :unprocessable_entity code for xml" 116 end 117 185 assert_added_route_for :products 118 186 end 119 187 120 assert_generated_model_for :product121 assert_generated_functional_test_for :products122 assert_generated_unit_test_for :product123 assert_generated_fixtures_for :products124 assert_generated_helper_for :products125 assert_generated_stylesheet :scaffold126 assert_generated_views_for :products, "index.html.erb", "new.html.erb", "edit.html.erb", "show.html.erb"127 assert_generated_migration :create_products128 assert_added_route_for :products129 188 end 130 131 def test_scaffold_skip_migration_skips_migration132 run_generator('scaffold', %w(Product --skip-migration))133 134 assert_generated_model_for :product135 assert_generated_functional_test_for :products136 assert_generated_unit_test_for :product137 assert_generated_fixtures_for :products138 assert_generated_helper_for :products139 assert_generated_stylesheet :scaffold140 assert_generated_views_for :products, "index.html.erb","new.html.erb","edit.html.erb","show.html.erb"141 assert_skipped_migration :create_products142 assert_added_route_for :products143 end144 145 def test_scaffold_generates_resources_with_attributes146 run_generator('scaffold', %w(Product name:string supplier_id:integer created_at:timestamp))147 148 assert_generated_controller_for :products do |f|149 150 assert_has_method f, :index do |name, m|151 assert_match /@products = Product\.find\(:all\)/, m, "#{name} should query products table"152 end153 154 assert_has_method f, :show, :edit, :update, :destroy do |name, m|155 assert_match /@product = Product\.find\(params\[:id\]\)/, m, "#{name.to_s} should query products table"156 end157 158 assert_has_method f, :new do |name, m|159 assert_match /@product = Product\.new/, m, "#{name.to_s} should instantiate a product"160 end161 162 assert_has_method f, :create do |name, m|163 assert_match /@product = Product\.new\(params\[:product\]\)/, m, "#{name.to_s} should instantiate a product"164 assert_match /format.xml \{ render :xml => @product.errors, :status => :unprocessable_entity \}/, m, "#{name.to_s} should set status to :unprocessable_entity code for xml"165 end166 167 end168 169 assert_generated_model_for :product170 assert_generated_functional_test_for :products171 assert_generated_unit_test_for :product172 assert_generated_fixtures_for :products173 assert_generated_helper_for :products174 assert_generated_stylesheet :scaffold175 assert_generated_views_for :products, "index.html.erb", "new.html.erb", "edit.html.erb", "show.html.erb"176 assert_generated_migration :create_products do |t|177 assert_generated_column t, :name, :string178 assert_generated_column t, :supplier_id, :integer179 assert_generated_column t, :created_at, :timestamp180 end181 182 assert_added_route_for :products183 end184 185 189 end trunk/railties/test/rails_generator_test.rb
r7593 r9176 34 34 35 35 $LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib" 36 require 'rails_generator' 37 36 require 'generators/generator_test_helper' 38 37 39 38 class RailsGeneratorTest < Test::Unit::TestCase … … 47 46 def test_sources 48 47 expected = [:lib, :vendor, 49 :plugins, :plugins, # <plugin>/generators and <plugin>/rails_generators48 "plugins (vendor/plugins)".to_sym, # <plugin>/generators and <plugin>/rails_generators 50 49 :user, 51 50 :RubyGems, :RubyGems, # gems named <x>_generator, gems containing /rails_generator/ folder … … 75 74 76 75 def test_lookup_missing_generator 77 assert_raise (MissingSourceFile) {76 assert_raise Rails::Generator::GeneratorError do 78 77 Rails::Generator::Base.lookup('missing_generator').klass 79 }78 end 80 79 end 81 80 … … 99 98 spec = Rails::Generator::Base.lookup('working') 100 99 assert_equal 'working', spec.name 101 assert_ equal "#{RAILS_ROOT}/lib/generators/working", spec.path100 assert_match(/#{spec.path}$/, "#{RAILS_ROOT}/lib/generators/working") 102 101 assert_equal :lib, spec.source 103 102 assert_nothing_raised { assert_match(/WorkingGenerator$/, spec.klass.name) }