require 'test/unit' require 'rails/all' require 'rails/generators' require 'rails/generators/test_case' require 'rspec_for_generators/rails_spec_helper' # Call configure to load the settings from # Rails.application.config.generators to Rails::Generators class Array def args flatten.map(&:to_s) end end # require the generators def require_generators *generator_list req = RSpec::Generator::Require generator_list.each do |name, generators| case name when Hash req.parse_generator_hash name when Symbol if !generators || generators.empty? req.require_generator! name else req.parse_generator_list name, generators end else raise ArgumentError, "required generator(s) must be stated either using a symbol or array of symbols" end end end alias :require_generator :require_generators module RSpec module Generator module Require class << self def parse_generator_hash hash parse_generator_list hash.keys.first, hash.values.flatten end def parse_generator_list name, generators if generators.empty? require_generator! name else generators.each do |generator_name| require_generator! name, generator_name end end end def require_generator! name, generator=nil file = (require_file_name name, generator) puts "Loading generator: #{file}" if RSpec::Generator.debug? require file end def require_file_name name, name_space=nil File.join('generators', name.to_s, "#{name_space}", "#{name_space || name}_generator") end end end class << self attr_accessor :generator, :test_method_name, :remove_temp_dir, :debug def debug? debug end def remove_rails_dir! FileUtils.rm_rf ::TmpRails.root end def configure_root_dir path, options = {} ::Rails.application.configure do config.root_dir = options == :custom ? TmpRails.root_dir(File.dirname(path) + '/../tmp', :custom) : TmpRails.root_dir(path) end ::RSpec.configure do |config| config.after(:suite) do ::RSpec::Generator.remove_rails_dir! if remove_temp_dir end end ::RSpec::Generators::TestCase.destination ::Rails.root ::Rails::Generators.configure! end def run_generator *args, &block generator.run_generator *args if block block.arity < 1 ? generator.instance_eval(&block) : block.call(generator, self) end end def check(&block) if block block.arity < 1 ? self.instance_eval(&block) : block.call(self) end end def with(generator, &block) if block block.arity < 1 ? generator.instance_eval(&block) : block.call(generator, self, generator.class) end end def with_generator &block with(get_generator, &block) end def setup_generator test_method_name=nil, &block clean! if test_method_name generator = get_generator(test_method_name).extend(RSpec::Generator::Helpers) if block block.arity < 1 ? generator.class.instance_eval(&block) : block.call(generator.class) end end protected def clean! if generator generator.class.generator_class = nil end @generator = nil end def get_generator test_method_name=nil @generator ||= RSpec::Generators::TestCase.new(test_method_name + '_spec') end end # class self end end module RSpec::Core class ExampleGroup def with_generator &block RSpec::Generator.with_generator &block end def setup_generator test_method_name=nil, &block RSpec::Generator.setup_generator test_method_name, &block end end end