Move all generators tests to use new test case syntax.

This commit is contained in:
José Valim 2010-01-03 16:34:32 +01:00
parent 441227a10f
commit 271e7c803f
21 changed files with 68 additions and 185 deletions

View File

@ -29,12 +29,13 @@ module Rails
class TestCase < ActiveSupport::TestCase
include FileUtils
extlib_inheritable_accessor :destination_root, :current_path, :instance_writer => false
extlib_inheritable_accessor :generator_class
extlib_inheritable_accessor :destination_root, :current_path, :generator_class,
:default_arguments, :instance_writer => false
# Generators frequently change the current path using +FileUtils.cd+.
# So we need to store the path at file load and revert back to it after each test.
self.current_path = File.expand_path(Dir.pwd)
self.default_arguments = []
setup :destination_root_is_set?, :ensure_current_path
teardown :ensure_current_path
@ -47,6 +48,15 @@ module Rails
self.generator_class = klass
end
# Sets default arguments on generator invocation. This can be overwritten when
# invoking it.
#
# arguments %w(app_name --skip-activerecord)
#
def self.arguments(array)
self.default_arguments = array
end
# Sets the destination of generator files:
#
# destination File.expand_path("../tmp", File.dirname(__FILE__))
@ -195,8 +205,13 @@ module Rails
#
# You can provide a configuration hash as second argument. This method returns the output
# printed by the generator.
def run_generator(args=[], config={})
capture(:stdout) { self.generator_class.start args, config.reverse_merge(:destination_root => destination_root) }
def run_generator(args=self.default_arguments, config={})
capture(:stdout) { self.generator_class.start(args, config.reverse_merge(:destination_root => destination_root)) }
end
# Instantiate the generator.
def generator(args=self.default_arguments, options={}, config={})
@generator ||= self.generator_class.new(args, options, config.reverse_merge(:destination_root => destination_root))
end
protected

View File

@ -1,8 +1,10 @@
require 'abstract_unit'
require 'generators/generators_test_helper'
require 'rails/generators/rails/app/app_generator'
class ActionsTest < GeneratorsTestCase
tests Rails::Generators::AppGenerator
arguments [destination_root]
def setup
super
@git_plugin_uri = 'git://github.com/technoweenie/restful-authentication.git'
@ -178,14 +180,6 @@ class ActionsTest < GeneratorsTestCase
protected
def run_generator
silence(:stdout) { Rails::Generators::AppGenerator.start [destination_root] }
end
def generator(config={})
@generator ||= Rails::Generators::Base.new([], {}, { :destination_root => destination_root }.merge!(config))
end
def action(*args, &block)
silence(:stdout){ generator.send(*args, &block) }
end

View File

@ -3,6 +3,7 @@ require 'generators/generators_test_helper'
require 'rails/generators/rails/app/app_generator'
class AppGeneratorTest < GeneratorsTestCase
arguments [destination_root]
def setup
super
@ -49,7 +50,7 @@ class AppGeneratorTest < GeneratorsTestCase
end
def test_invalid_database_option_raises_an_error
content = capture(:stderr){ run_generator(["-d", "unknown"]) }
content = capture(:stderr){ run_generator([destination_root, "-d", "unknown"]) }
assert_match /Invalid value for \-\-database option/, content
end
@ -69,7 +70,7 @@ class AppGeneratorTest < GeneratorsTestCase
end
def test_config_database_is_not_added_if_skip_activerecord_is_given
run_generator ["--skip-activerecord"]
run_generator [destination_root, "--skip-activerecord"]
assert_no_file "config/database.yml"
end
@ -86,13 +87,13 @@ class AppGeneratorTest < GeneratorsTestCase
end
def test_prototype_and_test_unit_are_skipped_if_required
run_generator ["--skip-prototype", "--skip-testunit"]
run_generator [destination_root, "--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"]
run_generator [destination_root, "--ruby", "foo/bar/baz"]
%w(
about
@ -107,7 +108,7 @@ class AppGeneratorTest < GeneratorsTestCase
end
def test_shebang_when_is_the_same_as_default_use_env
run_generator ["--ruby", Thor::Util.ruby_command]
run_generator [destination_root, "--ruby", Thor::Util.ruby_command]
%w(
about
@ -123,11 +124,11 @@ class AppGeneratorTest < GeneratorsTestCase
def test_template_from_dir_pwd
FileUtils.cd(Rails.root)
assert_match /It works from file!/, run_generator(["-m", "lib/template.rb"])
assert_match /It works from file!/, run_generator([destination_root, "-m", "lib/template.rb"])
end
def test_template_raises_an_error_with_invalid_path
content = capture(:stderr){ run_generator(["-m", "non/existant/path"]) }
content = capture(:stderr){ run_generator([destination_root, "-m", "non/existant/path"]) }
assert_match /The template \[.*\] could not be loaded/, content
assert_match /non\/existant\/path/, content
end
@ -137,7 +138,7 @@ class AppGeneratorTest < GeneratorsTestCase
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)
generator([destination_root], :template => path, :database => "sqlite3").expects(:open).with(path).returns(template)
assert_match /It works!/, silence(:stdout){ generator.invoke }
end
@ -161,28 +162,20 @@ class AppGeneratorTest < GeneratorsTestCase
end
def test_dev_option
run_generator ["--dev"]
run_generator [destination_root, "--dev"]
rails_path = File.expand_path('../../..', Rails.root)
dev_gem = %(gem "rails", :path => #{rails_path.inspect})
assert_file 'Gemfile', /^#{Regexp.escape(dev_gem)}$/
end
def test_edge_option
run_generator ["--edge"]
run_generator [destination_root, "--edge"]
edge_gem = %(gem "rails", :git => "git://github.com/rails/rails.git")
assert_file 'Gemfile', /^#{Regexp.escape(edge_gem)}$/
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, :destination_root => destination_root)
end
def action(*args, &block)
silence(:stdout){ generator.send(*args, &block) }
end

View File

@ -1,8 +1,8 @@
require 'abstract_unit'
require 'generators/generators_test_helper'
require 'rails/generators/rails/controller/controller_generator'
class ControllerGeneratorTest < GeneratorsTestCase
arguments %w(Account foo bar)
def test_help_does_not_show_invoked_generators_options_if_they_already_exist
content = run_generator ["--help"]
@ -70,11 +70,4 @@ class ControllerGeneratorTest < GeneratorsTestCase
assert_instance_method :bar, controller
end
end
protected
def run_generator(args=["Account", "foo", "bar"])
silence(:stdout) { Rails::Generators::ControllerGenerator.start args, :destination_root => destination_root }
end
end

View File

@ -1,8 +1,8 @@
require 'abstract_unit'
require 'generators/generators_test_helper'
require 'rails/generators/rails/generator/generator_generator'
class GeneratorGeneratorTest < GeneratorsTestCase
arguments %w(awesome)
def test_generator_skeleton_is_created
run_generator
@ -16,11 +16,4 @@ class GeneratorGeneratorTest < GeneratorsTestCase
assert_file "lib/generators/awesome/awesome_generator.rb",
/class AwesomeGenerator < Rails::Generators::NamedBase/
end
protected
def run_generator(args=["awesome"], config={})
silence(:stdout) { Rails::Generators::GeneratorGenerator.start args, config.merge(:destination_root => destination_root) }
end
end

View File

@ -20,7 +20,9 @@ class GeneratorsTestCase < Rails::Generators::TestCase
destination File.join(Rails.root, "tmp")
setup :prepare_destination
def test_truth
# Don't cry test unit
def self.inherited(base)
base.tests Rails::Generators.const_get(base.name.sub(/Test$/, ''))
rescue
# Do nothing.
end
end

View File

@ -1,4 +1,3 @@
require 'abstract_unit'
require 'generators/generators_test_helper'
require 'rails/generators/rails/helper/helper_generator'
@ -6,6 +5,7 @@ ObjectHelper = Class.new
AnotherObjectHelperTest = Class.new
class HelperGeneratorTest < GeneratorsTestCase
arguments %w(admin)
def test_helper_skeleton_is_created
run_generator
@ -50,11 +50,4 @@ class HelperGeneratorTest < GeneratorsTestCase
end
end
end
protected
def run_generator(args=["admin"])
silence(:stdout) { Rails::Generators::HelperGenerator.start args, :destination_root => destination_root }
end
end

View File

@ -1,18 +1,11 @@
require 'abstract_unit'
require 'generators/generators_test_helper'
require 'rails/generators/rails/integration_test/integration_test_generator'
class IntegrationTestGeneratorTest < GeneratorsTestCase
arguments %w(integration)
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, :destination_root => destination_root }
end
end

View File

@ -1,8 +1,8 @@
require 'abstract_unit'
require 'generators/generators_test_helper'
require 'rails/generators/rails/mailer/mailer_generator'
class MailerGeneratorTest < GeneratorsTestCase
arguments %w(notifier foo bar)
def test_mailer_skeleton_is_created
run_generator
@ -42,11 +42,4 @@ class MailerGeneratorTest < GeneratorsTestCase
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, :destination_root => destination_root }
end
end

View File

@ -1,8 +1,8 @@
require 'abstract_unit'
require 'generators/generators_test_helper'
require 'rails/generators/rails/metal/metal_generator'
class MetalGeneratorTest < GeneratorsTestCase
arguments %w(foo)
def test_metal_skeleton_is_created
run_generator
@ -13,11 +13,4 @@ class MetalGeneratorTest < GeneratorsTestCase
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, :destination_root => destination_root }
end
end

View File

@ -1,26 +1,24 @@
require 'abstract_unit'
require 'generators/generators_test_helper'
require 'rails/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/
migration = "change_title_body_from_posts"
run_generator [migration]
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/
migration = "ChangeTitleBodyFromPosts"
run_generator [migration]
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"]
migration = "add_title_body_to_posts"
run_generator [migration, "title:string", "body:text"]
assert_migration "db/migrate/#{@migration}.rb" do |content|
assert_migration "db/migrate/#{migration}.rb" do |content|
assert_class_method :up, content do |up|
assert_match /add_column :posts, :title, :string/, up
assert_match /add_column :posts, :body, :text/, up
@ -34,10 +32,10 @@ class MigrationGeneratorTest < GeneratorsTestCase
end
def test_remove_migration_with_attributes
@migration = "remove_title_body_from_posts"
run_generator [@migration, "title:string", "body:text"]
migration = "remove_title_body_from_posts"
run_generator [migration, "title:string", "body:text"]
assert_migration "db/migrate/#{@migration}.rb" do |content|
assert_migration "db/migrate/#{migration}.rb" do |content|
assert_class_method :up, content do |up|
assert_match /remove_column :posts, :title/, up
assert_match /remove_column :posts, :body/, up
@ -49,11 +47,4 @@ class MigrationGeneratorTest < GeneratorsTestCase
end
end
end
protected
def run_generator(args=[@migration])
silence(:stdout) { Rails::Generators::MigrationGenerator.start args, :destination_root => destination_root }
end
end

View File

@ -1,8 +1,8 @@
require 'abstract_unit'
require 'generators/generators_test_helper'
require 'rails/generators/rails/model/model_generator'
class ModelGeneratorTest < GeneratorsTestCase
arguments %w(Account name:string age:integer)
def test_help_shows_invoked_generators_options
content = run_generator ["--help"]
@ -171,11 +171,4 @@ class ModelGeneratorTest < GeneratorsTestCase
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"], config={})
silence(:stdout) { Rails::Generators::ModelGenerator.start args, config.merge(:destination_root => destination_root) }
end
end

View File

@ -1,4 +1,3 @@
require 'abstract_unit'
require 'generators/generators_test_helper'
require 'rails/generators/rails/scaffold_controller/scaffold_controller_generator'
@ -13,9 +12,10 @@ module ActiveRecord
end
class NamedBaseTest < GeneratorsTestCase
tests Rails::Generators::ScaffoldControllerGenerator
def test_named_generator_attributes
g = Rails::Generators::ScaffoldControllerGenerator.new ["admin/foo"]
g = generator ["admin/foo"]
assert_equal 'admin/foo', g.name
assert_equal %w(admin), g.class_path
assert_equal 1, g.class_nesting_depth
@ -28,12 +28,12 @@ class NamedBaseTest < GeneratorsTestCase
def test_named_generator_attributes_without_pluralized
ActiveRecord::Base.pluralize_table_names = false
g = Rails::Generators::ScaffoldControllerGenerator.new ["admin/foo"]
g = generator ["admin/foo"]
assert_equal "admin_#{g.singular_name}", g.table_name
end
def test_scaffold_plural_names
g = Rails::Generators::ScaffoldControllerGenerator.new ["ProductLine"]
g = generator ["ProductLine"]
assert_equal "ProductLines", g.controller_name
assert_equal "ProductLines", g.controller_class_name
assert_equal "product_lines", g.controller_file_name

View File

@ -1,8 +1,8 @@
require 'abstract_unit'
require 'generators/generators_test_helper'
require 'rails/generators/rails/observer/observer_generator'
class ObserverGeneratorTest < GeneratorsTestCase
arguments %w(account)
def test_invokes_default_orm
run_generator
@ -23,11 +23,4 @@ class ObserverGeneratorTest < GeneratorsTestCase
content = run_generator ["account", "--test-framework=rspec"]
assert_match /rspec \[not found\]/, content
end
protected
def run_generator(args=["account"])
silence(:stdout) { Rails::Generators::ObserverGenerator.start args, :destination_root => destination_root }
end
end

View File

@ -1,18 +1,11 @@
require 'abstract_unit'
require 'generators/generators_test_helper'
require 'rails/generators/rails/performance_test/performance_test_generator'
class PerformanceTestGeneratorTest < GeneratorsTestCase
arguments %w(performance)
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, :destination_root => destination_root }
end
end

View File

@ -1,8 +1,8 @@
require 'abstract_unit'
require 'generators/generators_test_helper'
require 'rails/generators/rails/plugin/plugin_generator'
class PluginGeneratorTest < GeneratorsTestCase
arguments %w(plugin_fu)
def test_plugin_skeleton_is_created
run_generator
@ -46,11 +46,4 @@ class PluginGeneratorTest < GeneratorsTestCase
run_generator
run_generator ["plugin_fu"], :behavior => :revoke
end
protected
def run_generator(args=["plugin_fu"], config={})
silence(:stdout) { Rails::Generators::PluginGenerator.start args, config.merge(:destination_root => destination_root) }
end
end

View File

@ -1,8 +1,8 @@
require 'abstract_unit'
require 'generators/generators_test_helper'
require 'rails/generators/rails/resource/resource_generator'
class ResourceGeneratorTest < GeneratorsTestCase
arguments %w(account)
def setup
super
@ -96,11 +96,4 @@ class ResourceGeneratorTest < GeneratorsTestCase
assert_no_match /resources :accounts$/, route
end
end
protected
def run_generator(args=["account"], config={})
silence(:stdout) { Rails::Generators::ResourceGenerator.start args, config.merge(:destination_root => destination_root) }
end
end

View File

@ -1,4 +1,3 @@
require 'abstract_unit'
require 'generators/generators_test_helper'
require 'rails/generators/rails/scaffold_controller/scaffold_controller_generator'
@ -8,6 +7,7 @@ module Unknown
end
class ScaffoldControllerGeneratorTest < GeneratorsTestCase
arguments %w(User name:string age:integer)
def test_controller_skeleton_is_created
run_generator
@ -135,11 +135,4 @@ class ScaffoldControllerGeneratorTest < GeneratorsTestCase
ensure
Unknown::Generators.send :remove_const, :ActiveModel
end
protected
def run_generator(args=["User", "name:string", "age:integer"])
silence(:stdout) { Rails::Generators::ScaffoldControllerGenerator.start args, :destination_root => destination_root }
end
end

View File

@ -1,8 +1,8 @@
require 'abstract_unit'
require 'generators/generators_test_helper'
require 'rails/generators/rails/scaffold/scaffold_generator'
class ScaffoldGeneratorTest < GeneratorsTestCase
arguments %w(product_line title:string price:integer)
def setup
super
@ -89,7 +89,7 @@ class ScaffoldGeneratorTest < GeneratorsTestCase
def test_scaffold_on_revoke
run_generator
run_generator :behavior => :revoke
run_generator ["product_line"], :behavior => :revoke
# Model
assert_no_file "app/models/product_line.rb"
@ -117,14 +117,4 @@ class ScaffoldGeneratorTest < GeneratorsTestCase
# Stylesheets (should not be removed)
assert_file "public/stylesheets/scaffold.css"
end
protected
def run_generator(config={})
silence(:stdout) do
Rails::Generators::ScaffoldGenerator.start ["product_line", "title:string", "price:integer"],
config.merge(:destination_root => destination_root)
end
end
end

View File

@ -1,9 +1,7 @@
require 'abstract_unit'
require 'generators/generators_test_helper'
require 'rails/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/
@ -24,11 +22,4 @@ class SessionMigrationGeneratorTest < GeneratorsTestCase
ensure
ActiveRecord::SessionStore::Session.table_name = "sessions"
end
protected
def run_generator(args=[])
silence(:stdout) { Rails::Generators::SessionMigrationGenerator.start args, :destination_root => destination_root }
end
end

View File

@ -1,9 +1,7 @@
require 'abstract_unit'
require 'generators/generators_test_helper'
require 'rails/generators/rails/stylesheets/stylesheets_generator'
class StylesheetsGeneratorTest < GeneratorsTestCase
def test_copy_stylesheets
run_generator
assert_file "public/stylesheets/scaffold.css"
@ -11,14 +9,7 @@ class StylesheetsGeneratorTest < GeneratorsTestCase
def test_stylesheets_are_not_deleted_on_revoke
run_generator
run_generator :behavior => :revoke
run_generator [], :behavior => :revoke
assert_file "public/stylesheets/scaffold.css"
end
protected
def run_generator(config={})
silence(:stdout) { Rails::Generators::StylesheetsGenerator.start [], config.merge(:destination_root => destination_root) }
end
end