diff options
Diffstat (limited to 'railties/test')
26 files changed, 1081 insertions, 262 deletions
diff --git a/railties/test/boot_test.rb b/railties/test/boot_test.rb index 2dd68857c3..7bf420d4f2 100644 --- a/railties/test/boot_test.rb +++ b/railties/test/boot_test.rb @@ -1,6 +1,6 @@ require 'abstract_unit' require 'initializer' -require "#{File.dirname(__FILE__)}/../environments/boot" +require "#{File.dirname(__FILE__)}/../lib/generators/rails/app/templates/config/boot" require 'rails/gem_dependency' class BootTest < Test::Unit::TestCase diff --git a/railties/test/generators/actions_test.rb b/railties/test/generators/actions_test.rb new file mode 100644 index 0000000000..baf687336a --- /dev/null +++ b/railties/test/generators/actions_test.rb @@ -0,0 +1,185 @@ +require 'abstract_unit' +require 'generators/generators_test_helper' +require 'generators/rails/app/app_generator' + +class ActionsTest < GeneratorsTestCase + def setup + super + @git_plugin_uri = 'git://github.com/technoweenie/restful-authentication.git' + @svn_plugin_uri = 'svn://svnhub.com/technoweenie/restful-authentication/trunk' + end + + def test_apply_loads_and_evaluates_a_template + template = <<-TEMPLATE + @foo = "FOO" + TEMPLATE + template.instance_eval "def read; self; end" # Make the string respond to read + + generator.expects(:open).with("http://gist.github.com/103208.txt").returns(template) + action :apply, "http://gist.github.com/103208.txt" + assert_equal generator.instance_variable_get("@foo"), "FOO" + end + + def test_create_file_should_write_data_to_file_path + action :create_file, 'lib/test_file.rb', 'heres test data' + assert_file 'lib/test_file.rb', 'heres test data' + end + + def test_create_file_should_write_block_contents_to_file_path + action(:create_file, 'lib/test_file.rb'){ 'heres block data' } + assert_file 'lib/test_file.rb', 'heres block data' + end + + def test_plugin_with_git_option_should_run_plugin_install + generator.expects(:run).once.with("ruby script/plugin install #{@git_plugin_uri}", false) + action :plugin, 'restful-authentication', :git => @git_plugin_uri + end + + def test_plugin_with_svn_option_should_run_plugin_install + generator.expects(:run).once.with("ruby script/plugin install #{@svn_plugin_uri}", false) + action :plugin, 'restful-authentication', :svn => @svn_plugin_uri + end + + def test_plugin_with_git_option_and_submodule_should_use_git_scm + generator.expects(:run).with("git submodule add #{@git_plugin_uri} vendor/plugins/rest_auth", false) + action :plugin, 'rest_auth', :git => @git_plugin_uri, :submodule => true + end + + def test_plugin_with_no_options_should_skip_method + generator.expects(:run).never + action :plugin, 'rest_auth', {} + end + + def test_gem_should_put_gem_dependency_in_enviroment + run_generator + action :gem, 'will-paginate' + assert_file 'config/environment.rb', /config\.gem 'will\-paginate'/ + end + + def test_gem_with_options_should_include_options_in_gem_dependency_in_environment + run_generator + action :gem, 'mislav-will-paginate', :lib => 'will-paginate', :source => 'http://gems.github.com' + + regexp = /#{Regexp.escape("config.gem 'mislav-will-paginate', :lib => 'will-paginate', :source => 'http://gems.github.com'")}/ + assert_file 'config/environment.rb', regexp + end + + def test_gem_with_env_string_should_put_gem_dependency_in_specified_environment + run_generator + action :gem, 'rspec', :env => 'test' + assert_file 'config/environments/test.rb', /config\.gem 'rspec'/ + end + + def test_gem_with_env_array_should_put_gem_dependency_in_specified_environments + run_generator + action :gem, 'quietbacktrace', :env => %w[ development test ] + assert_file 'config/environments/development.rb', /config\.gem 'quietbacktrace'/ + assert_file 'config/environments/test.rb', /config\.gem 'quietbacktrace'/ + end + + def test_gem_with_lib_option_set_to_false_should_put_gem_dependency_in_enviroment_correctly + run_generator + action :gem, 'mislav-will-paginate', :lib => false + assert_file 'config/environment.rb', /config\.gem 'mislav\-will\-paginate'\, :lib => false/ + end + + def test_environment_should_include_data_in_environment_initializer_block + run_generator + load_paths = 'config.load_paths += %w["#{RAILS_ROOT}/app/extras"]' + action :environment, load_paths + assert_file 'config/environment.rb', /#{Regexp.escape(load_paths)}/ + end + + def test_environment_with_block_should_include_block_contents_in_environment_initializer_block + run_generator + + action :environment do + '# This wont be added' + '# This will be added' + end + + assert_file 'config/environment.rb', /# This will be added/ + end + + def test_git_with_symbol_should_run_command_using_git_scm + generator.expects(:run).once.with('git init') + action :git, :init + end + + def test_git_with_hash_should_run_each_command_using_git_scm + generator.expects(:run).times(2) + action :git, :rm => 'README', :add => '.' + end + + def test_vendor_should_write_data_to_file_in_vendor + action :vendor, 'vendor_file.rb', '# vendor data' + assert_file 'vendor/vendor_file.rb', '# vendor data' + end + + def test_lib_should_write_data_to_file_in_lib + action :lib, 'my_library.rb', 'class MyLibrary' + assert_file 'lib/my_library.rb', 'class MyLibrary' + end + + def test_rakefile_should_write_date_to_file_in_lib_tasks + action :rakefile, 'myapp.rake', 'task :run => [:environment]' + assert_file 'lib/tasks/myapp.rake', 'task :run => [:environment]' + end + + def test_initializer_should_write_date_to_file_in_config_initializers + action :initializer, 'constants.rb', 'MY_CONSTANT = 42' + assert_file 'config/initializers/constants.rb', 'MY_CONSTANT = 42' + end + + def test_generate_should_run_script_generate_with_argument_and_options + generator.expects(:run).once.with('ruby script/generate model MyModel', false) + action :generate, 'model', 'MyModel' + end + + def test_rake_should_run_rake_command_with_development_env + generator.expects(:run).once.with('rake log:clear RAILS_ENV=development', false) + action :rake, 'log:clear' + end + + def test_rake_with_env_option_should_run_rake_command_in_env + generator.expects(:run).once.with('rake log:clear RAILS_ENV=production', false) + action :rake, 'log:clear', :env => 'production' + end + + def test_rake_with_sudo_option_should_run_rake_command_with_sudo + generator.expects(:run).once.with('sudo rake log:clear RAILS_ENV=development', false) + action :rake, 'log:clear', :sudo => true + end + + def test_capify_should_run_the_capify_command + generator.expects(:run).once.with('capify .', false) + action :capify! + end + + def test_freeze_should_freeze_rails_edge + generator.expects(:run).once.with('rake rails:freeze:edge', false) + action :freeze! + end + + def test_route_should_add_data_to_the_routes_block_in_config_routes + run_generator + route_command = "map.route '/login', :controller => 'sessions', :action => 'new'" + action :route, route_command + assert_file 'config/routes.rb', /#{Regexp.escape(route_command)}/ + end + + protected + + def run_generator + silence(:stdout) { Rails::Generators::AppGenerator.start [destination_root] } + end + + def generator(config={}) + @generator ||= Rails::Generators::Base.new([], {}, { :root => destination_root }.merge!(config)) + end + + def action(*args, &block) + silence(:stdout){ generator.send(*args, &block) } + end + +end diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb new file mode 100644 index 0000000000..98994f6ad2 --- /dev/null +++ b/railties/test/generators/app_generator_test.rb @@ -0,0 +1,166 @@ +require 'abstract_unit' +require 'generators/generators_test_helper' +require 'generators/rails/app/app_generator' + +class AppGeneratorTest < GeneratorsTestCase + + def setup + super + Rails::Generators::AppGenerator.instance_variable_set('@desc', nil) + end + + def teardown + super + Rails::Generators::AppGenerator.instance_variable_set('@desc', nil) + end + + def test_application_skeleton_is_created + run_generator + + %w( + app/controllers + app/helpers + app/models + app/views/layouts + config/environments + config/initializers + config/locales + db + doc + lib + lib/tasks + log + public/images + public/javascripts + public/stylesheets + script/performance + test/fixtures + test/functional + test/integration + test/performance + test/unit + vendor + vendor/plugins + tmp/sessions + tmp/sockets + tmp/cache + tmp/pids + ).each{ |path| assert_file path } + end + + def test_invalid_database_option_raises_an_error + content = capture(:stderr){ run_generator(["-d", "unknown"]) } + assert_match /Invalid value for \-\-database option/, content + end + + def test_dispatchers_are_not_added_by_default + run_generator + assert_no_file "config.ru" + assert_no_file "public/dispatch.cgi" + assert_no_file "public/dispatch.fcgi" + end + + def test_dispatchers_are_added_if_required + run_generator ["--with-dispatchers"] + assert_file "config.ru" + assert_file "public/dispatch.cgi" + assert_file "public/dispatch.fcgi" + end + + def test_config_database_is_added_by_default + run_generator + assert_file "config/database.yml", /sqlite3/ + end + + def test_config_database_is_not_added_if_skip_activerecord_is_given + run_generator ["--skip-activerecord"] + assert_no_file "config/database.yml" + end + + def test_activerecord_is_removed_from_frameworks_if_skip_activerecord_is_given + run_generator ["--skip-activerecord"] + assert_file "config/environment.rb", /config\.frameworks \-= \[ :active_record \]/ + end + + def test_prototype_and_test_unit_are_added_by_default + run_generator + assert_file "public/javascripts/prototype.js" + assert_file "test" + end + + def test_prototype_and_test_unit_are_skipped_if_required + run_generator ["--skip-prototype", "--skip-testunit"] + assert_no_file "public/javascripts/prototype.js" + assert_no_file "test" + end + + def test_shebang_is_added_to_files + run_generator ["--ruby", "foo/bar/baz"] + + %w( + about + console + dbconsole + destroy + generate + plugin + runner + server + ).each { |path| assert_file "script/#{path}", /#!foo\/bar\/baz/ } + end + + def test_rails_is_frozen + generator(:freeze => true, :database => "sqlite3").expects(:run).with("rake rails:freeze:edge", false) + silence(:stdout){ generator.invoke } + assert_file 'config/environment.rb', /# RAILS_GEM_VERSION/ + end + + def test_template_raises_an_error_with_invalid_path + content = capture(:stderr){ run_generator(["-m", "non/existant/path"]) } + assert_match /The template \[.*\] could not be loaded/, content + assert_match /non\/existant\/path/, content + end + + def test_template_is_executed_when_supplied + path = "http://gist.github.com/103208.txt" + template = %{ say "It works!" } + template.instance_eval "def read; self; end" # Make the string respond to read + + generator(:template => path, :database => "sqlite3").expects(:open).with(path).returns(template) + assert_match /It works!/, silence(:stdout){ generator.invoke } + end + + def test_usage_read_from_file + File.expects(:read).returns("USAGE FROM FILE") + assert_equal "USAGE FROM FILE", Rails::Generators::AppGenerator.desc + end + + def test_default_usage + File.expects(:exist?).returns(false) + assert_match /Create rails files for app generator/, Rails::Generators::AppGenerator.desc + end + + def test_default_namespace + assert_match "rails:generators:app", Rails::Generators::AppGenerator.namespace + end + + def test_file_is_added_for_backwards_compatibility + action :file, 'lib/test_file.rb', 'heres test data' + assert_file 'lib/test_file.rb', 'heres test data' + end + + protected + + def run_generator(args=[]) + silence(:stdout) { Rails::Generators::AppGenerator.start [destination_root].concat(args) } + end + + def generator(options={}) + @generator ||= Rails::Generators::AppGenerator.new([destination_root], options, :root => destination_root) + end + + def action(*args, &block) + silence(:stdout){ generator.send(*args, &block) } + end + +end diff --git a/railties/test/generators/controller_generator_test.rb b/railties/test/generators/controller_generator_test.rb new file mode 100644 index 0000000000..e3fe2594b9 --- /dev/null +++ b/railties/test/generators/controller_generator_test.rb @@ -0,0 +1,83 @@ +require 'abstract_unit' +require 'generators/generators_test_helper' +require 'generators/erb/controller/controller_generator' +require 'generators/rails/controller/controller_generator' +require 'generators/rails/helper/helper_generator' +require 'generators/test_unit/controller/controller_generator' +require 'generators/test_unit/helper/helper_generator' + +ObjectController = Class.new + +class ControllerGeneratorTest < GeneratorsTestCase + + def test_help_does_not_show_invoked_generators_options_if_they_already_exist + content = run_generator ["--help"] + assert_no_match /Helper options:/, content + end + + def test_controller_skeleton_is_created + run_generator + assert_file "app/controllers/account_controller.rb", /class AccountController < ApplicationController/ + end + + def test_check_class_collision + content = capture(:stderr){ run_generator ["object"] } + assert_match /The name 'ObjectController' is either already used in your application or reserved/, content + end + + # No need to spec content since it's already spec'ed on helper generator. + # + def test_invokes_helper + run_generator + assert_file "app/helpers/account_helper.rb" + assert_file "test/unit/helpers/account_helper_test.rb" + end + + def test_does_not_invoke_helper_if_required + run_generator ["account", "--skip-helper"] + assert_no_file "app/helpers/account_helper.rb" + assert_no_file "test/unit/helpers/account_helper_test.rb" + end + + def test_invokes_default_test_framework + run_generator + assert_file "test/functional/account_controller_test.rb" + end + + def test_does_not_invoke_test_framework_if_required + run_generator ["account", "--no-test-framework"] + assert_no_file "test/functional/account_controller_test.rb" + end + + def test_invokes_default_template_engine + run_generator + assert_file "app/views/account/foo.html.erb", /app\/views\/account\/foo/ + assert_file "app/views/account/bar.html.erb", /app\/views\/account\/bar/ + end + + def test_invokes_default_template_engine_even_with_no_action + run_generator ["account"] + assert_file "app/views/account" + end + + def test_template_engine_with_class_path + run_generator ["admin/account"] + assert_file "app/views/admin/account" + end + + def test_actions_are_turned_into_methods + run_generator + + assert_file "app/controllers/account_controller.rb" do |controller| + assert_instance_method controller, :foo + assert_instance_method controller, :bar + end + end + + protected + + def run_generator(args=["Account", "foo", "bar"]) + silence(:stdout) { Rails::Generators::ControllerGenerator.start args, :root => destination_root } + end + +end diff --git a/railties/test/generators/generators_test_helper.rb b/railties/test/generators/generators_test_helper.rb new file mode 100644 index 0000000000..591da45c72 --- /dev/null +++ b/railties/test/generators/generators_test_helper.rb @@ -0,0 +1,88 @@ +require 'test/unit' +require 'fileutils' + +$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../lib" +require 'generators' + +class GeneratorsTestCase < Test::Unit::TestCase + include FileUtils + + def destination_root + @destination_root ||= File.expand_path("#{File.dirname(__FILE__)}/../fixtures/tmp") + end + + def setup + rm_rf(destination_root) + mkdir_p(destination_root) + end + + def test_truth + # don't complain, test/unit + end + + def capture(stream) + begin + stream = stream.to_s + eval "$#{stream} = StringIO.new" + yield + result = eval("$#{stream}").string + ensure + eval("$#{stream} = #{stream.upcase}") + end + + result + end + alias :silence :capture + + def assert_file(relative, *contents) + absolute = File.join(destination_root, relative) + assert File.exists?(absolute), "Expected file #{relative.inspect} to exist, but does not" + + read = File.read(absolute) if block_given? || !contents.empty? + yield read if block_given? + + contents.each do |content| + case content + when String + assert_equal content, read + when Regexp + assert_match content, read + end + end + end + + def assert_no_file(relative) + absolute = File.join(destination_root, relative) + assert !File.exists?(absolute), "Expected file #{relative.inspect} to not exist, but does" + end + + def assert_migration(relative, *contents, &block) + file_name = migration_file_name(relative) + assert file_name, "Expected migration #{relative} to exist, but was not found" + assert_file File.join(File.dirname(relative), file_name), *contents, &block + 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 + + def assert_class_method(content, method, &block) + assert_instance_method content, "self.#{method}", &block + end + + def assert_instance_method(content, method) + assert_match /def #{method}(.*?)end/m, content + yield content.match(/def #{method}(.*?)end/m)[1] if block_given? + end + + 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 +end diff --git a/railties/test/generators/helper_generator_test.rb b/railties/test/generators/helper_generator_test.rb new file mode 100644 index 0000000000..41e1e1dce2 --- /dev/null +++ b/railties/test/generators/helper_generator_test.rb @@ -0,0 +1,61 @@ +require 'abstract_unit' +require 'generators/generators_test_helper' +require 'generators/rails/helper/helper_generator' +require 'generators/test_unit/helper/helper_generator' + +ObjectHelper = Class.new +AnotherObjectHelperTest = Class.new + +class HelperGeneratorTest < GeneratorsTestCase + + def test_helper_skeleton_is_created + run_generator + assert_file "app/helpers/admin_helper.rb", /module AdminHelper/ + end + + def test_invokes_default_test_framework + run_generator + assert_file "test/unit/helpers/admin_helper_test.rb", /class AdminHelperTest < ActionView::TestCase/ + end + + def test_logs_if_the_test_framework_cannot_be_found + content = run_generator ["admin", "--test-framework=unknown"] + assert_match /Could not find and invoke 'unknown'/, content + end + + def test_check_class_collision + content = capture(:stderr){ run_generator ["object"] } + assert_match /The name 'ObjectHelper' is either already used in your application or reserved/, content + end + + def test_check_class_collision_on_tests + content = capture(:stderr){ run_generator ["another_object"] } + assert_match /The name 'AnotherObjectHelperTest' is either already used in your application or reserved/, content + end + + def test_namespaced_and_not_namespaced_helpers + run_generator ["products"] + + # We have to require the generated helper to show the problem because + # the test helpers just check for generated files and contents but + # do not actually load them. But they have to be loaded (as in a real environment) + # to make the second generator run fail + require "#{destination_root}/app/helpers/products_helper" + + assert_nothing_raised do + begin + run_generator ["admin::products"] + ensure + # cleanup + Object.send(:remove_const, :ProductsHelper) + end + end + end + + protected + + def run_generator(args=["admin"]) + silence(:stdout) { Rails::Generators::HelperGenerator.start args, :root => destination_root } + end + +end diff --git a/railties/test/generators/integration_test_generator_test.rb b/railties/test/generators/integration_test_generator_test.rb new file mode 100644 index 0000000000..1961a102b9 --- /dev/null +++ b/railties/test/generators/integration_test_generator_test.rb @@ -0,0 +1,18 @@ +require 'abstract_unit' +require 'generators/generators_test_helper' +require 'generators/rails/integration_test/integration_test_generator' + +class IntegrationTestGeneratorTest < GeneratorsTestCase + + def test_integration_test_skeleton_is_created + run_generator + assert_file "test/integration/integration_test.rb", /class IntegrationTest < ActionController::IntegrationTest/ + end + + protected + + def run_generator(args=["integration"]) + silence(:stdout) { Rails::Generators::IntegrationTestGenerator.start args, :root => destination_root } + end + +end diff --git a/railties/test/generators/mailer_generator_test.rb b/railties/test/generators/mailer_generator_test.rb new file mode 100644 index 0000000000..5d21da3dd1 --- /dev/null +++ b/railties/test/generators/mailer_generator_test.rb @@ -0,0 +1,54 @@ +require 'abstract_unit' +require 'generators/generators_test_helper' +require 'generators/erb/mailer/mailer_generator' +require 'generators/rails/mailer/mailer_generator' +require 'generators/test_unit/mailer/mailer_generator' + +class MailerGeneratorTest < GeneratorsTestCase + + def test_mailer_skeleton_is_created + run_generator + assert_file "app/models/notifier.rb", /class Notifier < ActionMailer::Base/ + end + + def test_check_class_collision + content = capture(:stderr){ run_generator ["object"] } + assert_match /The name 'Object' is either already used in your application or reserved/, content + end + + def test_invokes_default_test_framework + run_generator + assert_file "test/unit/notifier_test.rb", /class NotifierTest < ActionMailer::TestCase/ + assert_file "test/fixtures/notifier/foo", /app\/views\/notifier\/foo/ + assert_file "test/fixtures/notifier/bar", /app\/views\/notifier\/bar/ + end + + def test_invokes_default_template_engine + run_generator + assert_file "app/views/notifier/foo.erb", /app\/views\/notifier\/foo/ + assert_file "app/views/notifier/bar.erb", /app\/views\/notifier\/bar/ + end + + def test_invokes_default_template_engine_even_with_no_action + run_generator ["notifier"] + assert_file "app/views/notifier" + end + + def test_logs_if_the_template_engine_cannot_be_found + content = run_generator ["notifier", "foo", "bar", "--template-engine=unknown"] + assert_match /Could not find and invoke 'unknown'/, content + end + + def test_actions_are_turned_into_methods + run_generator + assert_file "app/models/notifier.rb", /def foo/ + assert_file "app/models/notifier.rb", /def bar/ + end + + protected + + def run_generator(args=["notifier", "foo", "bar"]) + silence(:stdout) { Rails::Generators::MailerGenerator.start args, :root => destination_root } + end + +end diff --git a/railties/test/generators/metal_generator_test.rb b/railties/test/generators/metal_generator_test.rb new file mode 100644 index 0000000000..4e73883feb --- /dev/null +++ b/railties/test/generators/metal_generator_test.rb @@ -0,0 +1,23 @@ +require 'abstract_unit' +require 'generators/generators_test_helper' +require 'generators/rails/metal/metal_generator' + +class MetalGeneratorTest < GeneratorsTestCase + + def test_metal_skeleton_is_created + run_generator + assert_file "app/metal/foo.rb", /class Foo/ + end + + def test_check_class_collision + content = capture(:stderr){ run_generator ["object"] } + assert_match /The name 'Object' is either already used in your application or reserved/, content + end + + protected + + def run_generator(args=["foo"]) + silence(:stdout) { Rails::Generators::MetalGenerator.start args, :root => destination_root } + end + +end diff --git a/railties/test/generators/migration_generator_test.rb b/railties/test/generators/migration_generator_test.rb new file mode 100644 index 0000000000..547ca6a9e3 --- /dev/null +++ b/railties/test/generators/migration_generator_test.rb @@ -0,0 +1,60 @@ +require 'abstract_unit' +require 'generators/generators_test_helper' +require 'generators/active_record/migration/migration_generator' +require 'generators/rails/migration/migration_generator' + +class MigrationGeneratorTest < GeneratorsTestCase + + def test_migration + @migration = "change_title_body_from_posts" + run_generator + assert_migration "db/migrate/#{@migration}.rb", /class ChangeTitleBodyFromPosts < ActiveRecord::Migration/ + end + + def test_migration_with_class_name + @migration = "ChangeTitleBodyFromPosts" + run_generator + assert_migration "db/migrate/change_title_body_from_posts.rb", /class #{@migration} < ActiveRecord::Migration/ + end + + def test_add_migration_with_attributes + @migration = "add_title_body_to_posts" + run_generator [@migration, "title:string", "body:text"] + + assert_migration "db/migrate/#{@migration}.rb" do |content| + assert_class_method content, :up do |up| + assert_match /add_column :posts, :title, :string/, up + assert_match /add_column :posts, :body, :text/, up + end + + assert_class_method content, :down do |down| + assert_match /remove_column :posts, :title/, down + assert_match /remove_column :posts, :body/, down + end + end + end + + def test_remove_migration_with_attributes + @migration = "remove_title_body_from_posts" + run_generator [@migration, "title:string", "body:text"] + + assert_migration "db/migrate/#{@migration}.rb" do |content| + assert_class_method content, :up do |up| + assert_match /remove_column :posts, :title/, up + assert_match /remove_column :posts, :body/, up + end + + assert_class_method content, :down do |down| + assert_match /add_column :posts, :title, :string/, down + assert_match /add_column :posts, :body, :text/, down + end + end + end + + protected + + def run_generator(args=[@migration]) + silence(:stdout) { Rails::Generators::MigrationGenerator.start args, :root => destination_root } + end + +end diff --git a/railties/test/generators/model_generator_test.rb b/railties/test/generators/model_generator_test.rb new file mode 100644 index 0000000000..14cafe7b0d --- /dev/null +++ b/railties/test/generators/model_generator_test.rb @@ -0,0 +1,110 @@ +require 'abstract_unit' +require 'generators/generators_test_helper' +require 'generators/active_record/model/model_generator' +require 'generators/rails/model/model_generator' +require 'generators/test_unit/model/model_generator' + +class ModelGeneratorTest < GeneratorsTestCase + + def test_help_shows_invoked_generators_options + content = run_generator ["--help"] + assert_match /ActiveRecord options:/, content + assert_match /TestUnit options:/, content + end + + def test_invokes_default_orm + run_generator + assert_file "app/models/account.rb", /class Account < ActiveRecord::Base/ + end + + def test_model_with_parent_option + run_generator ["account", "--parent", "Admin::Account"] + assert_file "app/models/account.rb", /class Account < Admin::Account/ + assert_no_migration "db/migrate/create_accounts.rb" + end + + def test_model_with_underscored_parent_option + run_generator ["account", "--parent", "admin/account"] + assert_file "app/models/account.rb", /class Account < Admin::Account/ + end + + def test_migration + run_generator + assert_migration "db/migrate/create_accounts.rb", /class CreateAccounts < ActiveRecord::Migration/ + end + + def test_migration_is_skipped + run_generator ["account", "--no-migration"] + assert_no_migration "db/migrate/create_accounts.rb" + end + + def test_migration_with_attributes + run_generator ["product", "name:string", "supplier_id:integer"] + + assert_migration "db/migrate/create_products.rb" do |m| + assert_class_method m, :up do |up| + assert_match /create_table :products/, up + assert_match /t\.string :name/, up + assert_match /t\.integer :supplier_id/, up + end + + assert_class_method m, :down do |down| + assert_match /drop_table :products/, down + end + end + end + + def test_model_with_references_attribute_generates_belongs_to_associations + run_generator ["product", "name:string", "supplier_id:references"] + assert_file "app/models/product.rb", /belongs_to :supplier/ + end + + def test_model_with_belongs_to_attribute_generates_belongs_to_associations + run_generator ["product", "name:string", "supplier_id:belongs_to"] + assert_file "app/models/product.rb", /belongs_to :supplier/ + end + + def test_migration_with_timestamps + run_generator + assert_migration "db/migrate/create_accounts.rb", /t.timestamps/ + end + + def test_migration_timestamps_are_skipped + run_generator ["account", "--no-timestamps"] + + assert_migration "db/migrate/create_accounts.rb" do |m| + assert_class_method m, :up do |up| + assert_no_match /t.timestamps/, up + end + end + end + + def test_invokes_default_test_framework + run_generator + assert_file "test/unit/account_test.rb", /class AccountTest < ActiveSupport::TestCase/ + assert_file "test/fixtures/accounts.yml", /name: MyString/, /age: 1/ + end + + def test_fixture_is_skipped + run_generator ["account", "--skip-fixture"] + assert_no_file "test/fixtures/accounts.yml" + end + + def test_fixture_is_skipped_if_fixture_replacement_is_given + content = run_generator ["account", "-r", "fixjour"] + assert_match /Could not find and invoke 'fixjour'/, content + assert_no_file "test/fixtures/accounts.yml" + end + + def test_check_class_collision + content = capture(:stderr){ run_generator ["object"] } + assert_match /The name 'Object' is either already used in your application or reserved/, content + end + + protected + + def run_generator(args=["Account", "name:string", "age:integer"]) + silence(:stdout) { Rails::Generators::ModelGenerator.start args, :root => destination_root } + end + +end diff --git a/railties/test/generators/observer_generator_test.rb b/railties/test/generators/observer_generator_test.rb new file mode 100644 index 0000000000..3707b70c7e --- /dev/null +++ b/railties/test/generators/observer_generator_test.rb @@ -0,0 +1,35 @@ +require 'abstract_unit' +require 'generators/generators_test_helper' +require 'generators/active_record/observer/observer_generator' +require 'generators/rails/observer/observer_generator' +require 'generators/test_unit/observer/observer_generator' + +class ObserverGeneratorTest < GeneratorsTestCase + + def test_invokes_default_orm + run_generator + assert_file "app/models/account_observer.rb", /class AccountObserver < ActiveRecord::Observer/ + end + + def test_invokes_default_orm_with_class_path + run_generator ["admin/account"] + assert_file "app/models/admin/account_observer.rb", /class Admin::AccountObserver < ActiveRecord::Observer/ + end + + def test_invokes_default_test_framework + run_generator + assert_file "test/unit/account_observer_test.rb", /class AccountObserverTest < ActiveSupport::TestCase/ + end + + def test_logs_if_the_test_framework_cannot_be_found + content = run_generator ["account", "--test-framework=unknown"] + assert_match /Could not find and invoke 'unknown'/, content + end + + protected + + def run_generator(args=["account"]) + silence(:stdout) { Rails::Generators::ObserverGenerator.start args, :root => destination_root } + end + +end diff --git a/railties/test/generators/performance_test_generator_test.rb b/railties/test/generators/performance_test_generator_test.rb new file mode 100644 index 0000000000..fdfbf9031c --- /dev/null +++ b/railties/test/generators/performance_test_generator_test.rb @@ -0,0 +1,18 @@ +require 'abstract_unit' +require 'generators/generators_test_helper' +require 'generators/rails/performance_test/performance_test_generator' + +class PerformanceTestGeneratorTest < GeneratorsTestCase + + def test_performance_test_skeleton_is_created + run_generator + assert_file "test/performance/performance_test.rb", /class PerformanceTest < ActionController::PerformanceTest/ + end + + protected + + def run_generator(args=["performance"]) + silence(:stdout) { Rails::Generators::PerformanceTestGenerator.start args, :root => destination_root } + end + +end diff --git a/railties/test/generators/plugin_generator_test.rb b/railties/test/generators/plugin_generator_test.rb new file mode 100644 index 0000000000..3388b2f6f5 --- /dev/null +++ b/railties/test/generators/plugin_generator_test.rb @@ -0,0 +1,53 @@ +require 'abstract_unit' +require 'generators/generators_test_helper' +require 'generators/rails/plugin/plugin_generator' +require 'generators/test_unit/plugin/plugin_generator' + +class PluginGeneratorTest < GeneratorsTestCase + + def test_plugin_skeleton_is_created + run_generator + + %w( + vendor/plugins + vendor/plugins/plugin_fu + vendor/plugins/plugin_fu/lib + ).each{ |path| assert_file path } + end + + def test_check_class_collision + content = capture(:stderr){ run_generator ["object"] } + assert_match /The name 'Object' is either already used in your application or reserved/, content + end + + def test_invokes_default_test_framework + run_generator + assert_file "vendor/plugins/plugin_fu/test/plugin_fu_test.rb", /class PluginFuTest < ActiveSupport::TestCase/ + assert_file "vendor/plugins/plugin_fu/test/test_helper.rb" + end + + def test_logs_if_the_test_framework_cannot_be_found + content = run_generator ["plugin_fu", "--test-framework=unknown"] + assert_match /Could not find and invoke 'unknown'/, content + end + + def test_creates_tasks_if_required + run_generator ["plugin_fu", "--with-tasks"] + assert_file "vendor/plugins/plugin_fu/tasks/plugin_fu_tasks.rake" + end + + def test_creates_generator_if_required + run_generator ["plugin_fu", "--with-generator"] + assert_file "vendor/plugins/plugin_fu/generators/plugin_fu/templates" + + flag = /class PluginFuGenerator < Rails::Generators::NamedBase/ + assert_file "vendor/plugins/plugin_fu/generators/plugin_fu/plugin_fu_generator.rb", flag + end + + protected + + def run_generator(args=["plugin_fu"]) + silence(:stdout) { Rails::Generators::PluginGenerator.start args, :root => destination_root } + end + +end diff --git a/railties/test/generators/rails_template_runner_test.rb b/railties/test/generators/rails_template_runner_test.rb deleted file mode 100644 index 2da6bd59b5..0000000000 --- a/railties/test/generators/rails_template_runner_test.rb +++ /dev/null @@ -1,216 +0,0 @@ -require 'abstract_unit' -require 'generators/generator_test_helper' - -class RailsTemplateRunnerTest < GeneratorTestCase - def setup - Rails::Generator::Base.use_application_sources! - run_generator('app', [RAILS_ROOT]) - # generate empty template - @template_path = File.join(RAILS_ROOT, 'template.rb') - File.open(File.join(@template_path), 'w') {|f| f << '' } - - @git_plugin_uri = 'git://github.com/technoweenie/restful-authentication.git' - @svn_plugin_uri = 'svn://svnhub.com/technoweenie/restful-authentication/trunk' - end - - def teardown - super - rm_rf "#{RAILS_ROOT}/README" - rm_rf "#{RAILS_ROOT}/Rakefile" - rm_rf "#{RAILS_ROOT}/doc" - rm_rf "#{RAILS_ROOT}/lib" - rm_rf "#{RAILS_ROOT}/log" - rm_rf "#{RAILS_ROOT}/script" - rm_rf "#{RAILS_ROOT}/vendor" - rm_rf "#{RAILS_ROOT}/tmp" - rm_rf "#{RAILS_ROOT}/Capfile" - rm_rf @template_path - end - - def test_initialize_should_load_template - Rails::TemplateRunner.any_instance.expects(:load_template).with(@template_path) - silence_generator do - Rails::TemplateRunner.new(@template_path, RAILS_ROOT) - end - end - - def test_initialize_should_raise_error_on_missing_template_file - assert_raise(RuntimeError) do - silence_generator do - Rails::TemplateRunner.new('non/existent/path/to/template.rb', RAILS_ROOT) - end - end - end - - def test_file_should_write_data_to_file_path - run_template_method(:file, 'lib/test_file.rb', 'heres test data') - assert_generated_file_with_data 'lib/test_file.rb', 'heres test data' - end - - def test_file_should_write_block_contents_to_file_path - run_template_method(:file, 'lib/test_file.rb') { 'heres block data' } - assert_generated_file_with_data 'lib/test_file.rb', 'heres block data' - end - - def test_plugin_with_git_option_should_run_plugin_install - expects_run_ruby_script_with_command("script/plugin install #{@git_plugin_uri}") - run_template_method(:plugin, 'restful-authentication', :git => @git_plugin_uri) - end - - def test_plugin_with_svn_option_should_run_plugin_install - expects_run_ruby_script_with_command("script/plugin install #{@svn_plugin_uri}") - run_template_method(:plugin, 'restful-authentication', :svn => @svn_plugin_uri) - end - - def test_plugin_with_git_option_and_submodule_should_use_git_scm - Rails::Git.expects(:run).with("submodule add #{@git_plugin_uri} vendor/plugins/rest_auth") - run_template_method(:plugin, 'rest_auth', :git => @git_plugin_uri, :submodule => true) - end - - def test_plugin_with_no_options_should_skip_method - Rails::TemplateRunner.any_instance.expects(:run).never - run_template_method(:plugin, 'rest_auth', {}) - end - - def test_gem_should_put_gem_dependency_in_enviroment - run_template_method(:gem, 'will-paginate') - assert_rails_initializer_includes("config.gem 'will-paginate'") - end - - def test_gem_with_options_should_include_options_in_gem_dependency_in_environment - run_template_method(:gem, 'mislav-will-paginate', :lib => 'will-paginate', :source => 'http://gems.github.com') - assert_rails_initializer_includes("config.gem 'mislav-will-paginate', :lib => 'will-paginate', :source => 'http://gems.github.com'") - end - - def test_gem_with_env_string_should_put_gem_dependency_in_specified_environment - run_template_method(:gem, 'rspec', :env => 'test') - assert_generated_file_with_data('config/environments/test.rb', "config.gem 'rspec'", 'test') - end - - def test_gem_with_env_array_should_put_gem_dependency_in_specified_environments - run_template_method(:gem, 'quietbacktrace', :env => %w[ development test ]) - assert_generated_file_with_data('config/environments/development.rb', "config.gem 'quietbacktrace'") - assert_generated_file_with_data('config/environments/test.rb', "config.gem 'quietbacktrace'") - end - - def test_gem_with_lib_option_set_to_false_should_put_gem_dependency_in_enviroment_correctly - run_template_method(:gem, 'mislav-will-paginate', :lib => false, :source => 'http://gems.github.com') - assert_rails_initializer_includes("config.gem 'mislav-will-paginate', :lib => false, :source => 'http://gems.github.com'") - end - - def test_environment_should_include_data_in_environment_initializer_block - load_paths = 'config.load_paths += %w["#{RAILS_ROOT}/app/extras"]' - run_template_method(:environment, load_paths) - assert_rails_initializer_includes(load_paths) - end - - def test_environment_with_block_should_include_block_contents_in_environment_initializer_block - run_template_method(:environment) do - '# This wont be added' - '# This will be added' - end - assert_rails_initializer_includes('# This will be added') - end - - def test_git_with_symbol_should_run_command_using_git_scm - Rails::Git.expects(:run).once.with('init') - run_template_method(:git, :init) - end - - def test_git_with_hash_should_run_each_command_using_git_scm - Rails::Git.expects(:run).times(2) - run_template_method(:git, {:init => '', :add => '.'}) - end - - def test_vendor_should_write_data_to_file_in_vendor - run_template_method(:vendor, 'vendor_file.rb', '# vendor data') - assert_generated_file_with_data('vendor/vendor_file.rb', '# vendor data') - end - - def test_lib_should_write_data_to_file_in_lib - run_template_method(:lib, 'my_library.rb', 'class MyLibrary') - assert_generated_file_with_data('lib/my_library.rb', 'class MyLibrary') - end - - def test_rakefile_should_write_date_to_file_in_lib_tasks - run_template_method(:rakefile, 'myapp.rake', 'task :run => [:environment]') - assert_generated_file_with_data('lib/tasks/myapp.rake', 'task :run => [:environment]') - end - - def test_initializer_should_write_date_to_file_in_config_initializers - run_template_method(:initializer, 'constants.rb', 'MY_CONSTANT = 42') - assert_generated_file_with_data('config/initializers/constants.rb', 'MY_CONSTANT = 42') - end - - def test_generate_should_run_script_generate_with_argument_and_options - expects_run_ruby_script_with_command('script/generate model MyModel') - run_template_method(:generate, 'model', 'MyModel') - end - - def test_rake_should_run_rake_command_with_development_env - expects_run_with_command('rake log:clear RAILS_ENV=development') - run_template_method(:rake, 'log:clear') - end - - def test_rake_with_env_option_should_run_rake_command_in_env - expects_run_with_command('rake log:clear RAILS_ENV=production') - run_template_method(:rake, 'log:clear', :env => 'production') - end - - def test_rake_with_sudo_option_should_run_rake_command_with_sudo - expects_run_with_command('sudo rake log:clear RAILS_ENV=development') - run_template_method(:rake, 'log:clear', :sudo => true) - end - - def test_capify_should_run_the_capify_command - expects_run_with_command('capify .') - run_template_method(:capify!) - end - - def test_freeze_should_freeze_rails_edge - expects_run_with_command('rake rails:freeze:edge') - run_template_method(:freeze!) - end - - def test_route_should_add_data_to_the_routes_block_in_config_routes - route_command = "map.route '/login', :controller => 'sessions', :action => 'new'" - run_template_method(:route, route_command) - assert_generated_file_with_data 'config/routes.rb', route_command - end - - def test_run_ruby_script_should_add_ruby_to_command_in_win32_environment - ruby_command = RUBY_PLATFORM =~ /win32/ ? 'ruby ' : '' - expects_run_with_command("#{ruby_command}script/generate model MyModel") - run_template_method(:generate, 'model', 'MyModel') - end - - protected - def run_template_method(method_name, *args, &block) - silence_generator do - @template_runner = Rails::TemplateRunner.new(@template_path, RAILS_ROOT) - @template_runner.send(method_name, *args, &block) - end - end - - def expects_run_with_command(command) - Rails::TemplateRunner.any_instance.stubs(:run).once.with(command, false) - end - - def expects_run_ruby_script_with_command(command) - Rails::TemplateRunner.any_instance.stubs(:run_ruby_script).once.with(command,false) - end - - def assert_rails_initializer_includes(data, message = nil) - message ||= "Rails::Initializer should include #{data}" - assert_generated_file 'config/environment.rb' do |body| - assert_match(/#{Regexp.escape("Rails::Initializer.run do |config|")}.+#{Regexp.escape(data)}.+end/m, body, message) - end - end - - def assert_generated_file_with_data(file, data, message = nil) - message ||= "#{file} should include '#{data}'" - assert_generated_file(file) do |file| - assert_match(/#{Regexp.escape(data)}/,file, message) - end - end -end
\ No newline at end of file diff --git a/railties/test/generators/resource_generator_test.rb b/railties/test/generators/resource_generator_test.rb new file mode 100644 index 0000000000..456e6ff3ea --- /dev/null +++ b/railties/test/generators/resource_generator_test.rb @@ -0,0 +1,95 @@ +require 'abstract_unit' +require 'generators/generators_test_helper' +require 'generators/rails/resource/resource_generator' + +# Model +require 'generators/active_record/model/model_generator' +require 'generators/rails/model/model_generator' +require 'generators/test_unit/model/model_generator' + +# Controller +require 'generators/erb/controller/controller_generator' +require 'generators/rails/controller/controller_generator' +require 'generators/rails/helper/helper_generator' +require 'generators/test_unit/controller/controller_generator' +require 'generators/test_unit/helper/helper_generator' + +class ResourceGeneratorTest < GeneratorsTestCase + + def setup + super + routes = Rails::Generators::ResourceGenerator.source_root + routes = File.join(routes, "..", "..", "app", "templates", "config", "routes.rb") + destination = File.join(destination_root, "config") + + FileUtils.mkdir_p(destination) + FileUtils.cp File.expand_path(routes), destination + end + + def test_help_with_inherited_options + content = run_generator ["--help"] + assert_match /ActiveRecord options:/, content + assert_match /TestUnit options:/, content + end + + def test_files_from_inherited_invocation + run_generator + + %w( + app/models/account.rb + test/unit/account_test.rb + test/fixtures/accounts.yml + ).each { |path| assert_file path } + + assert_migration "db/migrate/create_accounts.rb" + end + + def test_inherited_invocations_with_attributes + run_generator ["account", "name:string"] + assert_migration "db/migrate/create_accounts.rb", /t.string :name/ + end + + def test_resource_controller_with_pluralized_class_name + run_generator + assert_file "app/controllers/accounts_controller.rb", /class AccountsController < ApplicationController/ + assert_file "test/functional/accounts_controller_test.rb", /class AccountsControllerTest < ActionController::TestCase/ + + assert_file "app/helpers/accounts_helper.rb", /module AccountsHelper/ + assert_file "test/unit/helpers/accounts_helper_test.rb", /class AccountsHelperTest < ActionView::TestCase/ + end + + def test_resource_controller_with_actions + run_generator ["account", "--actions", "index", "new"] + + assert_file "app/controllers/accounts_controller.rb" do |controller| + assert_instance_method controller, :index + assert_instance_method controller, :new + end + + assert_file "app/views/accounts/index.html.erb" + assert_file "app/views/accounts/new.html.erb" + end + + def test_resource_routes_are_added + run_generator + + assert_file "config/routes.rb" do |route| + assert_match /map\.resources :accounts$/, route + end + end + + def test_singleton_resource + run_generator ["account", "--singleton"] + + assert_file "config/routes.rb" do |route| + assert_match /map\.resource :account$/, route + end + end + + protected + + def run_generator(args=["account"]) + silence(:stdout) { Rails::Generators::ResourceGenerator.start args, :root => destination_root } + end + +end diff --git a/railties/test/generators/session_migration_generator_test.rb b/railties/test/generators/session_migration_generator_test.rb new file mode 100644 index 0000000000..05b51b1566 --- /dev/null +++ b/railties/test/generators/session_migration_generator_test.rb @@ -0,0 +1,24 @@ +require 'abstract_unit' +require 'generators/generators_test_helper' +require 'generators/active_record/session_migration/session_migration_generator' +require 'generators/rails/session_migration/session_migration_generator' + +class SessionMigrationGeneratorTest < GeneratorsTestCase + + def test_session_migration_with_default_name + run_generator + assert_migration "db/migrate/add_sessions_table.rb", /class AddSessionsTable < ActiveRecord::Migration/ + end + + def test_session_migration_with_given_name + run_generator ["create_session_table"] + assert_migration "db/migrate/create_session_table.rb", /class CreateSessionTable < ActiveRecord::Migration/ + end + + protected + + def run_generator(args=[]) + silence(:stdout) { Rails::Generators::SessionMigrationGenerator.start args, :root => destination_root } + end + +end diff --git a/railties/test/generators/generator_test_helper.rb b/railties/test/rails_generator/generator_test_helper.rb index 01bf1c90bd..01bf1c90bd 100644 --- a/railties/test/generators/generator_test_helper.rb +++ b/railties/test/rails_generator/generator_test_helper.rb diff --git a/railties/test/generators/rails_controller_generator_test.rb b/railties/test/rails_generator/rails_controller_generator_test.rb index 43fbe972e2..6cbb6bf2bd 100644 --- a/railties/test/generators/rails_controller_generator_test.rb +++ b/railties/test/rails_generator/rails_controller_generator_test.rb @@ -1,4 +1,4 @@ -require 'generators/generator_test_helper' +require 'rails_generator/generator_test_helper' module Admin end diff --git a/railties/test/generators/rails_helper_generator_test.rb b/railties/test/rails_generator/rails_helper_generator_test.rb index 8d05f555e6..8d05f555e6 100644 --- a/railties/test/generators/rails_helper_generator_test.rb +++ b/railties/test/rails_generator/rails_helper_generator_test.rb diff --git a/railties/test/generators/rails_mailer_generator_test.rb b/railties/test/rails_generator/rails_mailer_generator_test.rb index de61e6736d..f580fe27ab 100644 --- a/railties/test/generators/rails_mailer_generator_test.rb +++ b/railties/test/rails_generator/rails_mailer_generator_test.rb @@ -1,4 +1,4 @@ -require 'generators/generator_test_helper' +require 'rails_generator/generator_test_helper' class RailsMailerGeneratorTest < GeneratorTestCase diff --git a/railties/test/generators/rails_model_generator_test.rb b/railties/test/rails_generator/rails_model_generator_test.rb index aea2abafba..ae3c2e316e 100644 --- a/railties/test/generators/rails_model_generator_test.rb +++ b/railties/test/rails_generator/rails_model_generator_test.rb @@ -1,4 +1,4 @@ -require 'generators/generator_test_helper' +require 'rails_generator/generator_test_helper' class RailsModelGeneratorTest < GeneratorTestCase diff --git a/railties/test/generators/rails_model_subclass_generator_test.rb b/railties/test/rails_generator/rails_model_subclass_generator_test.rb index 30066b5a3c..4f20de6eba 100644 --- a/railties/test/generators/rails_model_subclass_generator_test.rb +++ b/railties/test/rails_generator/rails_model_subclass_generator_test.rb @@ -1,4 +1,4 @@ -require 'generators/generator_test_helper' +require 'rails_generator/generator_test_helper' class RailsModelSubclassGeneratorTest < GeneratorTestCase @@ -12,4 +12,4 @@ class RailsModelSubclassGeneratorTest < GeneratorTestCase def test_model_subclass_must_have_a_parent_class_name assert_raise(Rails::Generator::UsageError) { run_generator('model_subclass', %w(Car)) } end -end
\ No newline at end of file +end diff --git a/railties/test/generators/rails_resource_generator_test.rb b/railties/test/rails_generator/rails_resource_generator_test.rb index 1f5bd0ef1e..b9a432cbff 100644 --- a/railties/test/generators/rails_resource_generator_test.rb +++ b/railties/test/rails_generator/rails_resource_generator_test.rb @@ -1,4 +1,4 @@ -require 'generators/generator_test_helper' +require 'rails_generator/generator_test_helper' class RailsResourceGeneratorTest < GeneratorTestCase def test_resource_generates_resources diff --git a/railties/test/generators/rails_scaffold_generator_test.rb b/railties/test/rails_generator/rails_scaffold_generator_test.rb index 70829a77fd..ffbeff79fb 100644 --- a/railties/test/generators/rails_scaffold_generator_test.rb +++ b/railties/test/rails_generator/rails_scaffold_generator_test.rb @@ -1,4 +1,4 @@ -require 'generators/generator_test_helper' +require 'rails_generator/generator_test_helper' require 'abstract_unit' class RailsScaffoldGeneratorTest < GeneratorTestCase diff --git a/railties/test/secret_key_generation_test.rb b/railties/test/secret_key_generation_test.rb deleted file mode 100644 index 2c7c3d5dfe..0000000000 --- a/railties/test/secret_key_generation_test.rb +++ /dev/null @@ -1,38 +0,0 @@ -require 'abstract_unit' - -# Must set before requiring generator libs. -if defined?(RAILS_ROOT) - RAILS_ROOT.replace "#{File.dirname(__FILE__)}/fixtures" -else - RAILS_ROOT = "#{File.dirname(__FILE__)}/fixtures" -end - -$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib" - -require 'initializer' - -# Mocks out the configuration -module Rails - def self.configuration - Rails::Configuration.new - end -end - -require 'rails_generator' -require 'rails_generator/secret_key_generator' -require 'rails_generator/generators/applications/app/app_generator' - -class SecretKeyGenerationTest < ActiveSupport::TestCase - SECRET_KEY_MIN_LENGTH = 128 - APP_NAME = "foo" - - def setup - @generator = Rails::SecretKeyGenerator.new(APP_NAME) - end - - def test_secret_key_generation - assert_deprecated /ActiveSupport::SecureRandom\.hex\(64\)/ do - assert @generator.generate_secret.length >= SECRET_KEY_MIN_LENGTH - end - end -end |