mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Add a fixture replacement hook to TestUnit model generator.
This commit is contained in:
parent
8c5622f0d4
commit
acaeaf308a
7 changed files with 60 additions and 26 deletions
|
@ -3,15 +3,17 @@ require 'generators/actions'
|
||||||
module Rails
|
module Rails
|
||||||
module Generators
|
module Generators
|
||||||
DEFAULTS = {
|
DEFAULTS = {
|
||||||
:orm => 'active_record',
|
:fixture => true,
|
||||||
:helper => true,
|
:helper => true,
|
||||||
|
:orm => 'active_record',
|
||||||
:test_framework => 'test_unit',
|
:test_framework => 'test_unit',
|
||||||
:template_engine => 'erb'
|
:template_engine => 'erb'
|
||||||
}
|
}
|
||||||
|
|
||||||
ALIASES = {
|
ALIASES = {
|
||||||
|
:fixture_replacement => '-r',
|
||||||
|
:helper => '-l',
|
||||||
:orm => '-o',
|
:orm => '-o',
|
||||||
:helper => '-v',
|
|
||||||
:test_framework => '-t',
|
:test_framework => '-t',
|
||||||
:template_engine => '-e'
|
:template_engine => '-e'
|
||||||
}
|
}
|
||||||
|
@ -70,11 +72,13 @@ module Rails
|
||||||
#
|
#
|
||||||
# ruby script/generate controller Account --test-framework=test_unit
|
# ruby script/generate controller Account --test-framework=test_unit
|
||||||
#
|
#
|
||||||
# The controller generator will then invoke "test_unit:generators:controller".
|
# The controller generator will then try to invoke the following generators:
|
||||||
# If it can't be found it then tries to invoke only "test_unit".
|
|
||||||
#
|
#
|
||||||
# This allows any test framework to hook into Rails as long as it
|
# "rails:generators:test_unit", "test_unit:generators:controller", "test_unit"
|
||||||
# provides a "test_framework:generators:controller" generator.
|
#
|
||||||
|
# In this case, the "test_unit:generators:controller" is available and is
|
||||||
|
# invoked. This allows any test framework to hook into Rails as long as it
|
||||||
|
# provides any of the hooks above.
|
||||||
#
|
#
|
||||||
# Finally, if the user don't want to use any test framework, he can do:
|
# Finally, if the user don't want to use any test framework, he can do:
|
||||||
#
|
#
|
||||||
|
@ -84,12 +88,35 @@ module Rails
|
||||||
#
|
#
|
||||||
# ruby script/generate controller Account --no-test-framework
|
# ruby script/generate controller Account --no-test-framework
|
||||||
#
|
#
|
||||||
|
# ==== Another example
|
||||||
|
#
|
||||||
|
# Another example of invoke_for hooks is fixture replacement tools.
|
||||||
|
# Consider this TestUnit model generator:
|
||||||
|
#
|
||||||
|
# class ModelGenerator < TestUnit::Generators::Base
|
||||||
|
# invoke_for :fixture_replacement, :aliases => "-r"
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
# When invoked as:
|
||||||
|
#
|
||||||
|
# ruby script/generate test_unit:model Account -r fixjour
|
||||||
|
#
|
||||||
|
# It will lookup for any of these generators:
|
||||||
|
#
|
||||||
|
# "test_unit:generators:fixjour", "fixjour:generators:model", "fixjour"
|
||||||
|
#
|
||||||
|
# Fixjour can choose any of these hooks to implement. For example, if
|
||||||
|
# Fixjour has different output for rspec and test_unit, it will likely
|
||||||
|
# implement the first hook. However if the output is the same for both,
|
||||||
|
# it will implement the second or the third depending on the number of
|
||||||
|
# generators it has.
|
||||||
|
#
|
||||||
def self.invoke_for(*names)
|
def self.invoke_for(*names)
|
||||||
default_options = names.extract_options!
|
default_options = names.extract_options!
|
||||||
|
|
||||||
names.each do |name|
|
names.each do |name|
|
||||||
options = default_options.dup
|
options = default_options.dup
|
||||||
options[:desc] ||= "#{name.to_s.humanize} to be used"
|
options[:desc] ||= "#{name.to_s.humanize} to be invoked"
|
||||||
options[:banner] ||= "NAME"
|
options[:banner] ||= "NAME"
|
||||||
options[:aliases] ||= ALIASES[name]
|
options[:aliases] ||= ALIASES[name]
|
||||||
|
|
||||||
|
@ -100,13 +127,12 @@ module Rails
|
||||||
return unless options[#{name.inspect}]
|
return unless options[#{name.inspect}]
|
||||||
|
|
||||||
klass = Rails::Generators.find_by_namespace(options[#{name.inspect}],
|
klass = Rails::Generators.find_by_namespace(options[#{name.inspect}],
|
||||||
nil, self.class.generator_name)
|
self.class.base_name, self.class.generator_name)
|
||||||
|
|
||||||
if klass
|
if klass
|
||||||
invoke klass
|
invoke klass
|
||||||
else
|
else
|
||||||
task = "\#{options[#{name.inspect}]}:generators:\#{self.class.generator_name}"
|
say "Could not find and invoke '\#{options[#{name.inspect}]}'."
|
||||||
say "Could not find and invoke '\#{task}'."
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
METHOD
|
METHOD
|
||||||
|
@ -140,11 +166,7 @@ module Rails
|
||||||
default_options = names.extract_options!
|
default_options = names.extract_options!
|
||||||
|
|
||||||
names.each do |name|
|
names.each do |name|
|
||||||
options = default_options.dup
|
conditional_class_option name, default_options.dup
|
||||||
options[:desc] ||= "Indicates when to use #{name.to_s.humanize}"
|
|
||||||
|
|
||||||
# TODO Reverse --name to --skip-name if default is given.
|
|
||||||
class_option name, options.merge!(:type => :boolean, :default => DEFAULTS[name] || false)
|
|
||||||
|
|
||||||
class_eval <<-METHOD, __FILE__, __LINE__
|
class_eval <<-METHOD, __FILE__, __LINE__
|
||||||
def invoke_if_#{name}
|
def invoke_if_#{name}
|
||||||
|
@ -156,7 +178,7 @@ module Rails
|
||||||
if klass
|
if klass
|
||||||
invoke klass
|
invoke klass
|
||||||
else
|
else
|
||||||
say "Could not find and invoke '#{name.inspect}'."
|
say "Could not find and invoke '#{name}'."
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
METHOD
|
METHOD
|
||||||
|
@ -220,6 +242,14 @@ module Rails
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Creates a conditional class option with type boolean, default value
|
||||||
|
# lookup and default description.
|
||||||
|
#
|
||||||
|
def self.conditional_class_option(name, options={})
|
||||||
|
options[:desc] ||= "Indicates when to generate #{name.to_s.humanize.downcase}"
|
||||||
|
class_option name, options.merge!(:type => :boolean, :default => DEFAULTS[name] || false)
|
||||||
|
end
|
||||||
|
|
||||||
# Small macro to add ruby as an option to the generator with proper
|
# Small macro to add ruby as an option to the generator with proper
|
||||||
# default value plus an instance helper method called shebang.
|
# default value plus an instance helper method called shebang.
|
||||||
#
|
#
|
||||||
|
|
|
@ -4,18 +4,16 @@ module TestUnit
|
||||||
argument :attributes, :type => :hash, :default => {}, :banner => "field:type, field:type"
|
argument :attributes, :type => :hash, :default => {}, :banner => "field:type, field:type"
|
||||||
|
|
||||||
check_class_collision :suffix => "Test"
|
check_class_collision :suffix => "Test"
|
||||||
|
conditional_class_option :fixture
|
||||||
# TODO Add DEFAULTS support
|
|
||||||
class_option :skip_fixture, :type => :boolean, :default => false,
|
|
||||||
:desc => "Don't generate a fixture file"
|
|
||||||
|
|
||||||
def create_test_file
|
def create_test_file
|
||||||
template 'unit_test.rb', File.join('test/unit', class_path, "#{file_name}_test.rb")
|
template 'unit_test.rb', File.join('test/unit', class_path, "#{file_name}_test.rb")
|
||||||
end
|
end
|
||||||
|
|
||||||
# TODO Add fixture replacement support
|
invoke_for :fixture_replacement
|
||||||
|
|
||||||
def create_fixture_file
|
def create_fixture_file
|
||||||
unless options[:skip_fixture]
|
if options[:fixture] && options[:fixture_replacement].nil?
|
||||||
template 'fixtures.yml', File.join('test/fixtures', "#{table_name}.yml")
|
template 'fixtures.yml', File.join('test/fixtures', "#{table_name}.yml")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -20,7 +20,7 @@ class HelperGeneratorTest < GeneratorsTestCase
|
||||||
|
|
||||||
def test_logs_if_the_test_framework_cannot_be_found
|
def test_logs_if_the_test_framework_cannot_be_found
|
||||||
content = run_generator ["admin", "--test-framework=unknown"]
|
content = run_generator ["admin", "--test-framework=unknown"]
|
||||||
assert_match /Could not find and invoke 'unknown:generators:helper'/, content
|
assert_match /Could not find and invoke 'unknown'/, content
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_check_class_collision
|
def test_check_class_collision
|
||||||
|
|
|
@ -36,7 +36,7 @@ class MailerGeneratorTest < GeneratorsTestCase
|
||||||
|
|
||||||
def test_logs_if_the_template_engine_cannot_be_found
|
def test_logs_if_the_template_engine_cannot_be_found
|
||||||
content = run_generator ["notifier", "foo", "bar", "--template-engine=unknown"]
|
content = run_generator ["notifier", "foo", "bar", "--template-engine=unknown"]
|
||||||
assert_match /Could not find and invoke 'unknown:generators:mailer'/, content
|
assert_match /Could not find and invoke 'unknown'/, content
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_actions_are_turned_into_methods
|
def test_actions_are_turned_into_methods
|
||||||
|
|
|
@ -22,6 +22,12 @@ class ModelGeneratorTest < GeneratorsTestCase
|
||||||
assert_no_file "test/fixtures/accounts.yml"
|
assert_no_file "test/fixtures/accounts.yml"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_fixtures_are_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
|
def test_check_class_collision
|
||||||
content = capture(:stderr){ run_generator ["object"] }
|
content = capture(:stderr){ run_generator ["object"] }
|
||||||
assert_match /The name 'Object' is either already used in your application or reserved/, content
|
assert_match /The name 'Object' is either already used in your application or reserved/, content
|
||||||
|
|
|
@ -23,7 +23,7 @@ class ObserverGeneratorTest < GeneratorsTestCase
|
||||||
|
|
||||||
def test_logs_if_the_test_framework_cannot_be_found
|
def test_logs_if_the_test_framework_cannot_be_found
|
||||||
content = run_generator ["account", "--test-framework=unknown"]
|
content = run_generator ["account", "--test-framework=unknown"]
|
||||||
assert_match /Could not find and invoke 'unknown:generators:observer'/, content
|
assert_match /Could not find and invoke 'unknown'/, content
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
|
@ -27,7 +27,7 @@ class PluginGeneratorTest < GeneratorsTestCase
|
||||||
|
|
||||||
def test_logs_if_the_test_framework_cannot_be_found
|
def test_logs_if_the_test_framework_cannot_be_found
|
||||||
content = run_generator ["plugin_fu", "--test-framework=unknown"]
|
content = run_generator ["plugin_fu", "--test-framework=unknown"]
|
||||||
assert_match /Could not find and invoke 'unknown:generators:plugin'/, content
|
assert_match /Could not find and invoke 'unknown'/, content
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_creates_tasks_if_required
|
def test_creates_tasks_if_required
|
||||||
|
|
Loading…
Reference in a new issue