2008-04-18 22:10:58 +00:00
|
|
|
require 'test/unit'
|
|
|
|
require 'fileutils'
|
|
|
|
|
2009-07-03 10:32:46 +00:00
|
|
|
fixtures = File.expand_path(File.join(File.dirname(__FILE__), '..', 'fixtures'))
|
|
|
|
if defined?(RAILS_ROOT)
|
|
|
|
RAILS_ROOT.replace fixtures
|
|
|
|
else
|
|
|
|
RAILS_ROOT = fixtures
|
|
|
|
end
|
|
|
|
|
2009-07-03 12:55:37 +00:00
|
|
|
$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../lib"
|
2009-08-10 16:29:20 +00:00
|
|
|
$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../../activerecord/lib"
|
|
|
|
$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../../actionpack/lib"
|
2009-07-03 12:55:37 +00:00
|
|
|
require 'generators'
|
2009-08-10 16:29:20 +00:00
|
|
|
require 'activerecord'
|
|
|
|
require 'action_dispatch'
|
2009-07-03 12:55:37 +00:00
|
|
|
|
2009-07-15 09:33:36 +00:00
|
|
|
CURRENT_PATH = File.expand_path(Dir.pwd)
|
2009-07-15 18:16:37 +00:00
|
|
|
Rails::Generators.no_color!
|
2009-07-15 09:33:36 +00:00
|
|
|
|
2009-06-25 08:18:00 +00:00
|
|
|
class GeneratorsTestCase < Test::Unit::TestCase
|
2008-04-18 22:10:58 +00:00
|
|
|
include FileUtils
|
2008-05-13 20:49:41 +00:00
|
|
|
|
2009-06-23 17:10:42 +00:00
|
|
|
def destination_root
|
2009-08-10 16:29:20 +00:00
|
|
|
@destination_root ||= File.expand_path(File.join(File.dirname(__FILE__),
|
2009-08-07 13:34:10 +00:00
|
|
|
'..', 'fixtures', 'tmp'))
|
2008-04-18 22:10:58 +00:00
|
|
|
end
|
2008-04-01 00:29:24 +00:00
|
|
|
|
2009-06-23 17:10:42 +00:00
|
|
|
def setup
|
2009-07-15 09:33:36 +00:00
|
|
|
cd CURRENT_PATH
|
2009-06-25 08:44:46 +00:00
|
|
|
rm_rf(destination_root)
|
2009-06-25 09:56:18 +00:00
|
|
|
mkdir_p(destination_root)
|
2009-06-25 08:44:46 +00:00
|
|
|
end
|
|
|
|
|
2008-04-18 22:10:58 +00:00
|
|
|
def test_truth
|
|
|
|
# don't complain, test/unit
|
|
|
|
end
|
2008-05-13 20:49:41 +00:00
|
|
|
|
2009-06-23 17:10:42 +00:00
|
|
|
def capture(stream)
|
|
|
|
begin
|
|
|
|
stream = stream.to_s
|
|
|
|
eval "$#{stream} = StringIO.new"
|
|
|
|
yield
|
|
|
|
result = eval("$#{stream}").string
|
|
|
|
ensure
|
|
|
|
eval("$#{stream} = #{stream.upcase}")
|
2008-05-13 20:49:41 +00:00
|
|
|
end
|
|
|
|
|
2009-06-23 17:10:42 +00:00
|
|
|
result
|
2007-05-23 22:42:24 +00:00
|
|
|
end
|
2009-06-23 17:10:42 +00:00
|
|
|
alias :silence :capture
|
2007-06-23 18:26:58 +00:00
|
|
|
|
2009-06-27 11:03:07 +00:00
|
|
|
def assert_file(relative, *contents)
|
2009-06-23 17:10:42 +00:00
|
|
|
absolute = File.join(destination_root, relative)
|
2009-06-28 09:56:44 +00:00
|
|
|
assert File.exists?(absolute), "Expected file #{relative.inspect} to exist, but does not"
|
2007-06-23 18:26:58 +00:00
|
|
|
|
2009-06-28 17:46:34 +00:00
|
|
|
read = File.read(absolute) if block_given? || !contents.empty?
|
|
|
|
yield read if block_given?
|
|
|
|
|
2009-06-27 11:03:07 +00:00
|
|
|
contents.each do |content|
|
|
|
|
case content
|
|
|
|
when String
|
2009-06-28 09:56:44 +00:00
|
|
|
assert_equal content, read
|
2009-06-27 11:03:07 +00:00
|
|
|
when Regexp
|
2009-06-28 09:56:44 +00:00
|
|
|
assert_match content, read
|
2009-06-27 11:03:07 +00:00
|
|
|
end
|
2007-05-23 22:42:24 +00:00
|
|
|
end
|
|
|
|
end
|
2007-06-23 18:26:58 +00:00
|
|
|
|
2009-06-28 09:56:44 +00:00
|
|
|
def assert_no_file(relative)
|
2009-06-23 17:10:42 +00:00
|
|
|
absolute = File.join(destination_root, relative)
|
2009-06-28 09:56:44 +00:00
|
|
|
assert !File.exists?(absolute), "Expected file #{relative.inspect} to not exist, but does"
|
|
|
|
end
|
|
|
|
|
2009-06-28 17:46:34 +00:00
|
|
|
def assert_migration(relative, *contents, &block)
|
2009-06-28 09:56:44 +00:00
|
|
|
file_name = migration_file_name(relative)
|
|
|
|
assert file_name, "Expected migration #{relative} to exist, but was not found"
|
2009-06-28 17:46:34 +00:00
|
|
|
assert_file File.join(File.dirname(relative), file_name), *contents, &block
|
2009-06-28 09:56:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def assert_no_migration(relative)
|
|
|
|
file_name = migration_file_name(relative)
|
|
|
|
assert_nil file_name, "Expected migration #{relative} to not exist, but found #{file_name}"
|
|
|
|
end
|
|
|
|
|
2009-06-28 17:46:34 +00:00
|
|
|
def assert_class_method(content, method, &block)
|
|
|
|
assert_instance_method content, "self.#{method}", &block
|
2009-06-28 11:16:14 +00:00
|
|
|
end
|
|
|
|
|
2009-06-28 17:46:34 +00:00
|
|
|
def assert_instance_method(content, method)
|
2009-07-01 18:12:29 +00:00
|
|
|
assert content =~ /def #{method}(\(.+\))?(.*?)\n end/m, "Expected to have method #{method}"
|
|
|
|
yield $2.strip if block_given?
|
2009-06-28 11:16:14 +00:00
|
|
|
end
|
2009-06-28 17:46:34 +00:00
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def migration_file_name(relative)
|
|
|
|
absolute = File.join(destination_root, relative)
|
|
|
|
dirname, file_name = File.dirname(absolute), File.basename(absolute).sub(/\.rb$/, '')
|
|
|
|
|
|
|
|
migration = Dir.glob("#{dirname}/[0-9]*_*.rb").grep(/\d+_#{file_name}.rb$/).first
|
|
|
|
File.basename(migration) if migration
|
|
|
|
end
|
2007-06-23 18:26:58 +00:00
|
|
|
end
|