PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/change-image/dell/openstack_manager/vendor/rails/railties/test/generators/generator_test_helper.rb

https://github.com/socialengineers/crowbar
Ruby | 310 lines | 223 code | 37 blank | 50 comment | 17 complexity | 612aeb0632cfcb3de28d8e2332cdf8c2 MD5 | raw file
  1. require 'test/unit'
  2. require 'fileutils'
  3. # Mock out what we need from AR::Base
  4. module ActiveRecord
  5. class Base
  6. class << self
  7. attr_accessor :pluralize_table_names, :timestamped_migrations
  8. end
  9. self.pluralize_table_names = true
  10. self.timestamped_migrations = true
  11. end
  12. module ConnectionAdapters
  13. class Column
  14. attr_reader :name, :default, :type, :limit, :null, :sql_type, :precision, :scale
  15. def initialize(name, default, sql_type = nil)
  16. @name = name
  17. @default = default
  18. @type = @sql_type = sql_type
  19. end
  20. def human_name
  21. @name.humanize
  22. end
  23. end
  24. end
  25. end
  26. # Mock up necessities from ActionView
  27. module ActionView
  28. module Helpers
  29. module ActionRecordHelper; end
  30. class InstanceTag; end
  31. end
  32. end
  33. # Set RAILS_ROOT appropriately fixture generation
  34. tmp_dir = "#{File.dirname(__FILE__)}/../fixtures/tmp"
  35. if defined? RAILS_ROOT
  36. RAILS_ROOT.replace tmp_dir
  37. else
  38. RAILS_ROOT = tmp_dir
  39. end
  40. FileUtils.mkdir_p RAILS_ROOT
  41. $LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../lib"
  42. require 'initializer'
  43. # Mocks out the configuration
  44. module Rails
  45. def self.configuration
  46. Rails::Configuration.new
  47. end
  48. end
  49. require 'rails_generator'
  50. class GeneratorTestCase < Test::Unit::TestCase
  51. include FileUtils
  52. def setup
  53. ActiveRecord::Base.pluralize_table_names = true
  54. mkdir_p "#{RAILS_ROOT}/app/views/layouts"
  55. mkdir_p "#{RAILS_ROOT}/config"
  56. mkdir_p "#{RAILS_ROOT}/db"
  57. mkdir_p "#{RAILS_ROOT}/test/fixtures"
  58. mkdir_p "#{RAILS_ROOT}/public/stylesheets"
  59. File.open("#{RAILS_ROOT}/config/routes.rb", 'w') do |f|
  60. f << "ActionController::Routing::Routes.draw do |map|\n\nend"
  61. end
  62. end
  63. def teardown
  64. rm_rf "#{RAILS_ROOT}/app"
  65. rm_rf "#{RAILS_ROOT}/test"
  66. rm_rf "#{RAILS_ROOT}/config"
  67. rm_rf "#{RAILS_ROOT}/db"
  68. rm_rf "#{RAILS_ROOT}/public"
  69. end
  70. def test_truth
  71. # don't complain, test/unit
  72. end
  73. # Instantiates the Generator.
  74. def build_generator(name, params)
  75. Rails::Generator::Base.instance(name, params)
  76. end
  77. # Runs the +create+ command (like the command line does).
  78. def run_generator(name, params)
  79. silence_generator do
  80. build_generator(name, params).command(:create).invoke!
  81. end
  82. end
  83. # Silences the logger temporarily and returns the output as a String.
  84. def silence_generator
  85. logger_original = Rails::Generator::Base.logger
  86. myout = StringIO.new
  87. Rails::Generator::Base.logger = Rails::Generator::SimpleLogger.new(myout)
  88. yield if block_given?
  89. Rails::Generator::Base.logger = logger_original
  90. myout.string
  91. end
  92. # Asserts that the given controller was generated.
  93. # It takes a name or symbol without the <tt>_controller</tt> part and an optional super class.
  94. # The contents of the class source file is passed to a block.
  95. def assert_generated_controller_for(name, parent = "ApplicationController")
  96. assert_generated_class "app/controllers/#{name.to_s.underscore}_controller", parent do |body|
  97. yield body if block_given?
  98. end
  99. end
  100. # Asserts that the given model was generated.
  101. # It takes a name or symbol and an optional super class.
  102. # The contents of the class source file is passed to a block.
  103. def assert_generated_model_for(name, parent = "ActiveRecord::Base")
  104. assert_generated_class "app/models/#{name.to_s.underscore}", parent do |body|
  105. yield body if block_given?
  106. end
  107. end
  108. # Asserts that the given helper was generated.
  109. # It takes a name or symbol without the <tt>_helper</tt> part.
  110. # The contents of the module source file is passed to a block.
  111. def assert_generated_helper_for(name)
  112. assert_generated_module "app/helpers/#{name.to_s.underscore}_helper" do |body|
  113. yield body if block_given?
  114. end
  115. end
  116. # Asserts that the given functional test was generated.
  117. # It takes a name or symbol without the <tt>_controller_test</tt> part and an optional super class.
  118. # The contents of the class source file is passed to a block.
  119. def assert_generated_functional_test_for(name, parent = "ActionController::TestCase")
  120. assert_generated_class "test/functional/#{name.to_s.underscore}_controller_test",parent do |body|
  121. yield body if block_given?
  122. end
  123. end
  124. # Asserts that the given helper test test was generated.
  125. # It takes a name or symbol without the <tt>_helper_test</tt> part and an optional super class.
  126. # The contents of the class source file is passed to a block.
  127. def assert_generated_helper_test_for(name, parent = "ActionView::TestCase")
  128. path = "test/unit/helpers/#{name.to_s.underscore}_helper_test"
  129. # Have to pass the path without the "test/" part so that class_name_from_path will return a correct result
  130. class_name = class_name_from_path(path.gsub(/^test\//, ''))
  131. assert_generated_class path,parent,class_name do |body|
  132. yield body if block_given?
  133. end
  134. end
  135. # Asserts that the given unit test was generated.
  136. # It takes a name or symbol without the <tt>_test</tt> part and an optional super class.
  137. # The contents of the class source file is passed to a block.
  138. def assert_generated_unit_test_for(name, parent = "ActiveSupport::TestCase")
  139. assert_generated_class "test/unit/#{name.to_s.underscore}_test", parent do |body|
  140. yield body if block_given?
  141. end
  142. end
  143. # Asserts that the given file was generated.
  144. # The contents of the file is passed to a block.
  145. def assert_generated_file(path)
  146. assert_file_exists(path)
  147. File.open("#{RAILS_ROOT}/#{path}") do |f|
  148. yield f.read if block_given?
  149. end
  150. end
  151. # asserts that the given file exists
  152. def assert_file_exists(path)
  153. assert File.exist?("#{RAILS_ROOT}/#{path}"),
  154. "The file '#{RAILS_ROOT}/#{path}' should exist"
  155. end
  156. # Asserts that the given class source file was generated.
  157. # It takes a path without the <tt>.rb</tt> part and an optional super class.
  158. # The contents of the class source file is passed to a block.
  159. def assert_generated_class(path, parent = nil, class_name = class_name_from_path(path))
  160. assert_generated_file("#{path}.rb") do |body|
  161. assert_match /class #{class_name}#{parent.nil? ? '':" < #{parent}"}/, body, "the file '#{path}.rb' should be a class"
  162. yield body if block_given?
  163. end
  164. end
  165. def class_name_from_path(path)
  166. # FIXME: Sucky way to detect namespaced classes
  167. if path.split('/').size > 3
  168. path =~ /\/?(\d+_)?(\w+)\/(\w+)$/
  169. "#{$2.camelize}::#{$3.camelize}"
  170. else
  171. path =~ /\/?(\d+_)?(\w+)$/
  172. $2.camelize
  173. end
  174. end
  175. # Asserts that the given module source file was generated.
  176. # It takes a path without the <tt>.rb</tt> part.
  177. # The contents of the class source file is passed to a block.
  178. def assert_generated_module(path)
  179. # FIXME: Sucky way to detect namespaced modules
  180. if path.split('/').size > 3
  181. path =~ /\/?(\w+)\/(\w+)$/
  182. module_name = "#{$1.camelize}::#{$2.camelize}"
  183. else
  184. path =~ /\/?(\w+)$/
  185. module_name = $1.camelize
  186. end
  187. assert_generated_file("#{path}.rb") do |body|
  188. assert_match /module #{module_name}/, body, "the file '#{path}.rb' should be a module"
  189. yield body if block_given?
  190. end
  191. end
  192. # Asserts that the given CSS stylesheet file was generated.
  193. # It takes a path without the <tt>.css</tt> part.
  194. # The contents of the stylesheet source file is passed to a block.
  195. def assert_generated_stylesheet(path)
  196. assert_generated_file("public/stylesheets/#{path}.css") do |body|
  197. yield body if block_given?
  198. end
  199. end
  200. # Asserts that the given YAML file was generated.
  201. # It takes a path without the <tt>.yml</tt> part.
  202. # The parsed YAML tree is passed to a block.
  203. def assert_generated_yaml(path)
  204. assert_generated_file("#{path}.yml") do |body|
  205. yaml = YAML.load(body)
  206. assert yaml, 'YAML data missing'
  207. yield yaml if block_given?
  208. end
  209. end
  210. # Asserts that the given fixtures YAML file was generated.
  211. # It takes a fixture name without the <tt>.yml</tt> part.
  212. # The parsed YAML tree is passed to a block.
  213. def assert_generated_fixtures_for(name)
  214. assert_generated_yaml "test/fixtures/#{name.to_s.underscore}" do |yaml|
  215. yield yaml if block_given?
  216. end
  217. end
  218. # Asserts that the given views were generated.
  219. # It takes a controller name and a list of views (including extensions).
  220. # The body of each view is passed to a block.
  221. def assert_generated_views_for(name, *actions)
  222. actions.each do |action|
  223. assert_generated_file("app/views/#{name.to_s.underscore}/#{action}") do |body|
  224. yield body if block_given?
  225. end
  226. end
  227. end
  228. def assert_generated_migration(name, parent = "ActiveRecord::Migration")
  229. file = Dir.glob("#{RAILS_ROOT}/db/migrate/*_#{name.to_s.underscore}.rb").first
  230. assert !file.nil?, "should have generated the migration file but didn't"
  231. file = file.match(/db\/migrate\/[0-9]+_\w+/).to_s
  232. assert_generated_class file, parent do |body|
  233. assert_match /timestamps/, body, "should have timestamps defined"
  234. yield body if block_given?
  235. end
  236. end
  237. # Asserts that the given migration file was not generated.
  238. # It takes the name of the migration as a parameter.
  239. def assert_skipped_migration(name)
  240. migration_file = "#{RAILS_ROOT}/db/migrate/001_#{name.to_s.underscore}.rb"
  241. assert !File.exist?(migration_file), "should not create migration #{migration_file}"
  242. end
  243. # Asserts that the given resource was added to the routes.
  244. def assert_added_route_for(name)
  245. assert_generated_file("config/routes.rb") do |body|
  246. assert_match /map.resources :#{name.to_s.underscore}/, body,
  247. "should add route for :#{name.to_s.underscore}"
  248. end
  249. end
  250. # Asserts that the given methods are defined in the body.
  251. # This does assume standard rails code conventions with regards to the source code.
  252. # The body of each individual method is passed to a block.
  253. def assert_has_method(body, *methods)
  254. methods.each do |name|
  255. assert body =~ /^ def #{name}(\(.+\))?\n((\n| .*\n)*) end/, "should have method #{name}"
  256. yield(name, $2) if block_given?
  257. end
  258. end
  259. # Asserts that the given column is defined in the migration.
  260. def assert_generated_column(body, name, type)
  261. assert_match /t\.#{type.to_s} :#{name.to_s}/, body, "should have column #{name.to_s} defined"
  262. end
  263. # Asserts that the given table is defined in the migration.
  264. def assert_generated_table(body, name)
  265. assert_match /create_table :#{name.to_s} do/, body, "should have table #{name.to_s} defined"
  266. end
  267. end