提交 a288b74f 编写于 作者: J José Valim 提交者: Yehuda Katz

Generators should use Rails.root instead of Dir.pwd [#3408 status:resolved]

Signed-off-by: NYehuda Katz <wycats@gmail.com>
上级 8dcf91ca
...@@ -12,6 +12,16 @@ class Base < Thor::Group ...@@ -12,6 +12,16 @@ class Base < Thor::Group
add_runtime_options! add_runtime_options!
# Always move to rails source root.
#
def initialize(*args) #:nodoc:
if !invoked?(args) && defined?(Rails.root) && Rails.root
self.destination_root = Rails.root
FileUtils.cd(destination_root)
end
super
end
# Automatically sets the source root based on the class name. # Automatically sets the source root based on the class name.
# #
def self.source_root def self.source_root
...@@ -247,6 +257,13 @@ def class_collisions(*class_names) #:nodoc: ...@@ -247,6 +257,13 @@ def class_collisions(*class_names) #:nodoc:
end end
end end
# Check if this generator was invoked from another one by inspecting
# parameters.
#
def invoked?(args)
args.last.is_a?(Hash) && args.last.key?(:invocations)
end
# Use Rails default banner. # Use Rails default banner.
# #
def self.banner def self.banner
......
...@@ -182,7 +182,7 @@ def run_generator ...@@ -182,7 +182,7 @@ def run_generator
end end
def generator(config={}) def generator(config={})
@generator ||= Rails::Generators::Base.new([], {}, { :destination_root => destination_root }.merge!(config)) @generator ||= Rails::Generators::Base.new([], {}, config)
end end
def action(*args, &block) def action(*args, &block)
......
...@@ -126,7 +126,7 @@ def test_rails_is_frozen ...@@ -126,7 +126,7 @@ def test_rails_is_frozen
def test_template_from_dir_pwd def test_template_from_dir_pwd
FileUtils.cd(Rails.root) FileUtils.cd(Rails.root)
assert_match /It works from file!/, run_generator(["-m", "lib/template.rb"]) assert_match /It works from file!/, run_generator(["-m", "../lib/template.rb"])
end end
def test_template_raises_an_error_with_invalid_path def test_template_raises_an_error_with_invalid_path
...@@ -170,7 +170,7 @@ def run_generator(args=[]) ...@@ -170,7 +170,7 @@ def run_generator(args=[])
end end
def generator(options={}) def generator(options={})
@generator ||= Rails::Generators::AppGenerator.new([destination_root], options, :destination_root => destination_root) @generator ||= Rails::Generators::AppGenerator.new([destination_root], options)
end end
def action(*args, &block) def action(*args, &block)
......
...@@ -74,7 +74,7 @@ def test_actions_are_turned_into_methods ...@@ -74,7 +74,7 @@ def test_actions_are_turned_into_methods
protected protected
def run_generator(args=["Account", "foo", "bar"]) def run_generator(args=["Account", "foo", "bar"])
silence(:stdout) { Rails::Generators::ControllerGenerator.start args, :destination_root => destination_root } silence(:stdout) { Rails::Generators::ControllerGenerator.start args }
end end
end end
...@@ -20,7 +20,7 @@ def test_generator_skeleton_is_created ...@@ -20,7 +20,7 @@ def test_generator_skeleton_is_created
protected protected
def run_generator(args=["awesome"], config={}) def run_generator(args=["awesome"], config={})
silence(:stdout) { Rails::Generators::GeneratorGenerator.start args, config.merge(:destination_root => destination_root) } silence(:stdout) { Rails::Generators::GeneratorGenerator.start args, config }
end end
end end
# TODO: Fix this RAILS_ENV stuff # TODO: Fix this RAILS_ENV stuff
RAILS_ENV = 'test' RAILS_ENV = 'test' unless defined?(RAILS_ENV)
require 'abstract_unit' require 'abstract_unit'
Rails.application.config.root = File.expand_path(File.join(File.dirname(__FILE__), '..', 'fixtures')) Rails.application.config.root = File.expand_path(File.join(File.dirname(__FILE__), '..', 'fixtures'))
...@@ -11,12 +11,17 @@ ...@@ -11,12 +11,17 @@
CURRENT_PATH = File.expand_path(Dir.pwd) CURRENT_PATH = File.expand_path(Dir.pwd)
Rails::Generators.no_color! Rails::Generators.no_color!
module Rails
def self.root
@root ||= File.expand_path(File.join(File.dirname(__FILE__), '..', 'fixtures', 'tmp'))
end
end
class GeneratorsTestCase < Test::Unit::TestCase class GeneratorsTestCase < Test::Unit::TestCase
include FileUtils include FileUtils
def destination_root def destination_root
@destination_root ||= File.expand_path(File.join(File.dirname(__FILE__), Rails.root
'..', 'fixtures', 'tmp'))
end end
def setup def setup
......
...@@ -54,7 +54,7 @@ def test_namespaced_and_not_namespaced_helpers ...@@ -54,7 +54,7 @@ def test_namespaced_and_not_namespaced_helpers
protected protected
def run_generator(args=["admin"]) def run_generator(args=["admin"])
silence(:stdout) { Rails::Generators::HelperGenerator.start args, :destination_root => destination_root } silence(:stdout) { Rails::Generators::HelperGenerator.start args }
end end
end end
...@@ -12,7 +12,7 @@ def test_integration_test_skeleton_is_created ...@@ -12,7 +12,7 @@ def test_integration_test_skeleton_is_created
protected protected
def run_generator(args=["integration"]) def run_generator(args=["integration"])
silence(:stdout) { Rails::Generators::IntegrationTestGenerator.start args, :destination_root => destination_root } silence(:stdout) { Rails::Generators::IntegrationTestGenerator.start args }
end end
end end
...@@ -46,7 +46,7 @@ def test_actions_are_turned_into_methods ...@@ -46,7 +46,7 @@ def test_actions_are_turned_into_methods
protected protected
def run_generator(args=["notifier", "foo", "bar"]) def run_generator(args=["notifier", "foo", "bar"])
silence(:stdout) { Rails::Generators::MailerGenerator.start args, :destination_root => destination_root } silence(:stdout) { Rails::Generators::MailerGenerator.start args }
end end
end end
...@@ -17,7 +17,7 @@ def test_check_class_collision ...@@ -17,7 +17,7 @@ def test_check_class_collision
protected protected
def run_generator(args=["foo"]) def run_generator(args=["foo"])
silence(:stdout) { Rails::Generators::MetalGenerator.start args, :destination_root => destination_root } silence(:stdout) { Rails::Generators::MetalGenerator.start args }
end end
end end
...@@ -53,7 +53,7 @@ def test_remove_migration_with_attributes ...@@ -53,7 +53,7 @@ def test_remove_migration_with_attributes
protected protected
def run_generator(args=[@migration]) def run_generator(args=[@migration])
silence(:stdout) { Rails::Generators::MigrationGenerator.start args, :destination_root => destination_root } silence(:stdout) { Rails::Generators::MigrationGenerator.start args }
end end
end end
...@@ -175,7 +175,7 @@ def test_check_class_collision ...@@ -175,7 +175,7 @@ def test_check_class_collision
protected protected
def run_generator(args=["Account", "name:string", "age:integer"], config={}) def run_generator(args=["Account", "name:string", "age:integer"], config={})
silence(:stdout) { Rails::Generators::ModelGenerator.start args, config.merge(:destination_root => destination_root) } silence(:stdout) { Rails::Generators::ModelGenerator.start args, config }
end end
end end
...@@ -27,7 +27,7 @@ def test_logs_if_the_test_framework_cannot_be_found ...@@ -27,7 +27,7 @@ def test_logs_if_the_test_framework_cannot_be_found
protected protected
def run_generator(args=["account"]) def run_generator(args=["account"])
silence(:stdout) { Rails::Generators::ObserverGenerator.start args, :destination_root => destination_root } silence(:stdout) { Rails::Generators::ObserverGenerator.start args }
end end
end end
...@@ -12,7 +12,7 @@ def test_performance_test_skeleton_is_created ...@@ -12,7 +12,7 @@ def test_performance_test_skeleton_is_created
protected protected
def run_generator(args=["performance"]) def run_generator(args=["performance"])
silence(:stdout) { Rails::Generators::PerformanceTestGenerator.start args, :destination_root => destination_root } silence(:stdout) { Rails::Generators::PerformanceTestGenerator.start args }
end end
end end
...@@ -50,7 +50,7 @@ def test_plugin_generator_on_revoke ...@@ -50,7 +50,7 @@ def test_plugin_generator_on_revoke
protected protected
def run_generator(args=["plugin_fu"], config={}) def run_generator(args=["plugin_fu"], config={})
silence(:stdout) { Rails::Generators::PluginGenerator.start args, config.merge(:destination_root => destination_root) } silence(:stdout){ Rails::Generators::PluginGenerator.start args, config }
end end
end end
...@@ -100,7 +100,7 @@ def test_route_is_removed_on_revoke ...@@ -100,7 +100,7 @@ def test_route_is_removed_on_revoke
protected protected
def run_generator(args=["account"], config={}) def run_generator(args=["account"], config={})
silence(:stdout) { Rails::Generators::ResourceGenerator.start args, config.merge(:destination_root => destination_root) } silence(:stdout) { Rails::Generators::ResourceGenerator.start args, config }
end end
end end
...@@ -139,7 +139,7 @@ def self.all(klass) ...@@ -139,7 +139,7 @@ def self.all(klass)
protected protected
def run_generator(args=["User", "name:string", "age:integer"]) def run_generator(args=["User", "name:string", "age:integer"])
silence(:stdout) { Rails::Generators::ScaffoldControllerGenerator.start args, :destination_root => destination_root } silence(:stdout) { Rails::Generators::ScaffoldControllerGenerator.start args }
end end
end end
...@@ -122,8 +122,7 @@ def test_scaffold_on_revoke ...@@ -122,8 +122,7 @@ def test_scaffold_on_revoke
def run_generator(config={}) def run_generator(config={})
silence(:stdout) do silence(:stdout) do
Rails::Generators::ScaffoldGenerator.start ["product_line", "title:string", "price:integer"], Rails::Generators::ScaffoldGenerator.start ["product_line", "title:string", "price:integer"], config
config.merge(:destination_root => destination_root)
end end
end end
......
...@@ -28,7 +28,7 @@ def test_session_migration_with_custom_table_name ...@@ -28,7 +28,7 @@ def test_session_migration_with_custom_table_name
protected protected
def run_generator(args=[]) def run_generator(args=[])
silence(:stdout) { Rails::Generators::SessionMigrationGenerator.start args, :destination_root => destination_root } silence(:stdout) { Rails::Generators::SessionMigrationGenerator.start args }
end end
end end
...@@ -18,7 +18,7 @@ def test_stylesheets_are_not_deleted_on_revoke ...@@ -18,7 +18,7 @@ def test_stylesheets_are_not_deleted_on_revoke
protected protected
def run_generator(config={}) def run_generator(config={})
silence(:stdout) { Rails::Generators::StylesheetsGenerator.start [], config.merge(:destination_root => destination_root) } silence(:stdout) { Rails::Generators::StylesheetsGenerator.start [], config }
end end
end end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册