Generators should use Rails.root instead of Dir.pwd [#3408 status:resolved]
Signed-off-by: Yehuda Katz <wycats@gmail.com>
This commit is contained in:
parent
8dcf91ca11
commit
a288b74f1c
@ -12,6 +12,16 @@ class Base < Thor::Group
|
||||
|
||||
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.
|
||||
#
|
||||
def self.source_root
|
||||
@ -247,6 +257,13 @@ def class_collisions(*class_names) #:nodoc:
|
||||
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.
|
||||
#
|
||||
def self.banner
|
||||
|
@ -182,7 +182,7 @@ def run_generator
|
||||
end
|
||||
|
||||
def generator(config={})
|
||||
@generator ||= Rails::Generators::Base.new([], {}, { :destination_root => destination_root }.merge!(config))
|
||||
@generator ||= Rails::Generators::Base.new([], {}, config)
|
||||
end
|
||||
|
||||
def action(*args, &block)
|
||||
|
@ -126,7 +126,7 @@ def test_rails_is_frozen
|
||||
|
||||
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(["-m", "../lib/template.rb"])
|
||||
end
|
||||
|
||||
def test_template_raises_an_error_with_invalid_path
|
||||
@ -170,7 +170,7 @@ def run_generator(args=[])
|
||||
end
|
||||
|
||||
def generator(options={})
|
||||
@generator ||= Rails::Generators::AppGenerator.new([destination_root], options, :destination_root => destination_root)
|
||||
@generator ||= Rails::Generators::AppGenerator.new([destination_root], options)
|
||||
end
|
||||
|
||||
def action(*args, &block)
|
||||
|
@ -74,7 +74,7 @@ def test_actions_are_turned_into_methods
|
||||
protected
|
||||
|
||||
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
|
||||
|
@ -20,7 +20,7 @@ def test_generator_skeleton_is_created
|
||||
protected
|
||||
|
||||
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
|
||||
|
@ -1,5 +1,5 @@
|
||||
# TODO: Fix this RAILS_ENV stuff
|
||||
RAILS_ENV = 'test'
|
||||
RAILS_ENV = 'test' unless defined?(RAILS_ENV)
|
||||
|
||||
require 'abstract_unit'
|
||||
Rails.application.config.root = File.expand_path(File.join(File.dirname(__FILE__), '..', 'fixtures'))
|
||||
@ -11,12 +11,17 @@
|
||||
CURRENT_PATH = File.expand_path(Dir.pwd)
|
||||
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
|
||||
include FileUtils
|
||||
|
||||
def destination_root
|
||||
@destination_root ||= File.expand_path(File.join(File.dirname(__FILE__),
|
||||
'..', 'fixtures', 'tmp'))
|
||||
Rails.root
|
||||
end
|
||||
|
||||
def setup
|
||||
|
@ -54,7 +54,7 @@ def test_namespaced_and_not_namespaced_helpers
|
||||
protected
|
||||
|
||||
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
|
||||
|
@ -12,7 +12,7 @@ def test_integration_test_skeleton_is_created
|
||||
protected
|
||||
|
||||
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
|
||||
|
@ -46,7 +46,7 @@ def test_actions_are_turned_into_methods
|
||||
protected
|
||||
|
||||
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
|
||||
|
@ -17,7 +17,7 @@ def test_check_class_collision
|
||||
protected
|
||||
|
||||
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
|
||||
|
@ -53,7 +53,7 @@ def test_remove_migration_with_attributes
|
||||
protected
|
||||
|
||||
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
|
||||
|
@ -175,7 +175,7 @@ def test_check_class_collision
|
||||
protected
|
||||
|
||||
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
|
||||
|
@ -27,7 +27,7 @@ def test_logs_if_the_test_framework_cannot_be_found
|
||||
protected
|
||||
|
||||
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
|
||||
|
@ -12,7 +12,7 @@ def test_performance_test_skeleton_is_created
|
||||
protected
|
||||
|
||||
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
|
||||
|
@ -50,7 +50,7 @@ def test_plugin_generator_on_revoke
|
||||
protected
|
||||
|
||||
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
|
||||
|
@ -100,7 +100,7 @@ def test_route_is_removed_on_revoke
|
||||
protected
|
||||
|
||||
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
|
||||
|
@ -139,7 +139,7 @@ def self.all(klass)
|
||||
protected
|
||||
|
||||
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
|
||||
|
@ -122,8 +122,7 @@ def test_scaffold_on_revoke
|
||||
|
||||
def run_generator(config={})
|
||||
silence(:stdout) do
|
||||
Rails::Generators::ScaffoldGenerator.start ["product_line", "title:string", "price:integer"],
|
||||
config.merge(:destination_root => destination_root)
|
||||
Rails::Generators::ScaffoldGenerator.start ["product_line", "title:string", "price:integer"], config
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -28,7 +28,7 @@ def test_session_migration_with_custom_table_name
|
||||
protected
|
||||
|
||||
def run_generator(args=[])
|
||||
silence(:stdout) { Rails::Generators::SessionMigrationGenerator.start args, :destination_root => destination_root }
|
||||
silence(:stdout) { Rails::Generators::SessionMigrationGenerator.start args }
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -18,7 +18,7 @@ def test_stylesheets_are_not_deleted_on_revoke
|
||||
protected
|
||||
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user