From 2b394fa93c07509f76c985e0119551c0d20af0f8 Mon Sep 17 00:00:00 2001 From: Tore Darell Date: Mon, 9 Mar 2009 17:32:08 +0100 Subject: [PATCH] Make GemPlugin recognise Rails-specific directories inside the "rails" directory --- railties/lib/rails/plugin.rb | 33 +++++ .../engines/gemlike_engine/lib/gemlike_engine.rb | 2 + .../app/controllers/gemlike_engine_controller.rb | 2 + .../rails/app/helpers/gemlike_helper.rb | 2 + .../rails/app/metal/gemlike_metal.rb | 5 + .../rails/app/models/gemlike_engine_model.rb | 2 + .../rails/app/views/gemlike/gemlike.html.erb | 1 + .../engines/gemlike_engine/rails/config/routes.rb | 1 + .../plugins/engines/gemlike_engine/rails/init.rb | 7 + railties/test/gem_plugin_test.rb | 147 ++++++++++++++++++++ railties/test/initializer_test.rb | 8 +- railties/test/plugin_loader_test.rb | 14 +- railties/test/plugin_locator_test.rb | 2 +- 13 files changed, 214 insertions(+), 12 deletions(-) create mode 100644 railties/test/fixtures/plugins/engines/gemlike_engine/lib/gemlike_engine.rb create mode 100644 railties/test/fixtures/plugins/engines/gemlike_engine/rails/app/controllers/gemlike_engine_controller.rb create mode 100644 railties/test/fixtures/plugins/engines/gemlike_engine/rails/app/helpers/gemlike_helper.rb create mode 100644 railties/test/fixtures/plugins/engines/gemlike_engine/rails/app/metal/gemlike_metal.rb create mode 100644 railties/test/fixtures/plugins/engines/gemlike_engine/rails/app/models/gemlike_engine_model.rb create mode 100644 railties/test/fixtures/plugins/engines/gemlike_engine/rails/app/views/gemlike/gemlike.html.erb create mode 100644 railties/test/fixtures/plugins/engines/gemlike_engine/rails/config/routes.rb create mode 100644 railties/test/fixtures/plugins/engines/gemlike_engine/rails/init.rb create mode 100644 railties/test/gem_plugin_test.rb diff --git a/railties/lib/rails/plugin.rb b/railties/lib/rails/plugin.rb index 80deb73..406fc10 100644 --- a/railties/lib/rails/plugin.rb +++ b/railties/lib/rails/plugin.rb @@ -163,5 +163,38 @@ module Rails def init_path File.join(directory, 'rails', 'init.rb') end + + def view_path + rails_or_default_path('app', 'views') + end + + def controller_path + rails_or_default_path('app', 'controllers') + end + + def metal_path + rails_or_default_path('app', 'metal') + end + + def routing_file + File.join(rails_or_default_path('config'), 'routes.rb') + end + + def app_paths + [ rails_or_default_path('app', 'models'), rails_or_default_path('app', 'helpers'), controller_path, metal_path ] + end + + private + + def has_app_directory? + File.directory?(rails_or_default_path('app')) + end + + #If the directory exists inside the "rails" directory, return its path + #Otherwise, return the normal path + def rails_or_default_path(*parts) + rails_path = File.join(directory, 'rails', *parts) + File.directory?(rails_path) ? rails_path : File.join(directory, *parts) + end end end diff --git a/railties/test/fixtures/plugins/engines/gemlike_engine/lib/gemlike_engine.rb b/railties/test/fixtures/plugins/engines/gemlike_engine/lib/gemlike_engine.rb new file mode 100644 index 0000000..d08e7c3 --- /dev/null +++ b/railties/test/fixtures/plugins/engines/gemlike_engine/lib/gemlike_engine.rb @@ -0,0 +1,2 @@ +module GemlikeEngine +end diff --git a/railties/test/fixtures/plugins/engines/gemlike_engine/rails/app/controllers/gemlike_engine_controller.rb b/railties/test/fixtures/plugins/engines/gemlike_engine/rails/app/controllers/gemlike_engine_controller.rb new file mode 100644 index 0000000..df0455d --- /dev/null +++ b/railties/test/fixtures/plugins/engines/gemlike_engine/rails/app/controllers/gemlike_engine_controller.rb @@ -0,0 +1,2 @@ +class GemlikeEngineController +end diff --git a/railties/test/fixtures/plugins/engines/gemlike_engine/rails/app/helpers/gemlike_helper.rb b/railties/test/fixtures/plugins/engines/gemlike_engine/rails/app/helpers/gemlike_helper.rb new file mode 100644 index 0000000..0984751 --- /dev/null +++ b/railties/test/fixtures/plugins/engines/gemlike_engine/rails/app/helpers/gemlike_helper.rb @@ -0,0 +1,2 @@ +module GemlikeHelper +end diff --git a/railties/test/fixtures/plugins/engines/gemlike_engine/rails/app/metal/gemlike_metal.rb b/railties/test/fixtures/plugins/engines/gemlike_engine/rails/app/metal/gemlike_metal.rb new file mode 100644 index 0000000..7fe0b6d --- /dev/null +++ b/railties/test/fixtures/plugins/engines/gemlike_engine/rails/app/metal/gemlike_metal.rb @@ -0,0 +1,5 @@ +class GemlikeMetal + def call(e) + [200, {"Content-Type" => "text/plain"}, "Hello"] + end +end diff --git a/railties/test/fixtures/plugins/engines/gemlike_engine/rails/app/models/gemlike_engine_model.rb b/railties/test/fixtures/plugins/engines/gemlike_engine/rails/app/models/gemlike_engine_model.rb new file mode 100644 index 0000000..cc4de8b --- /dev/null +++ b/railties/test/fixtures/plugins/engines/gemlike_engine/rails/app/models/gemlike_engine_model.rb @@ -0,0 +1,2 @@ +class GemlikeEngineModel +end diff --git a/railties/test/fixtures/plugins/engines/gemlike_engine/rails/app/views/gemlike/gemlike.html.erb b/railties/test/fixtures/plugins/engines/gemlike_engine/rails/app/views/gemlike/gemlike.html.erb new file mode 100644 index 0000000..a99f4a8 --- /dev/null +++ b/railties/test/fixtures/plugins/engines/gemlike_engine/rails/app/views/gemlike/gemlike.html.erb @@ -0,0 +1 @@ +gemlike diff --git a/railties/test/fixtures/plugins/engines/gemlike_engine/rails/config/routes.rb b/railties/test/fixtures/plugins/engines/gemlike_engine/rails/config/routes.rb new file mode 100644 index 0000000..5e6b006 --- /dev/null +++ b/railties/test/fixtures/plugins/engines/gemlike_engine/rails/config/routes.rb @@ -0,0 +1 @@ +ActionController::Routing::Routes.draw{|m|} diff --git a/railties/test/fixtures/plugins/engines/gemlike_engine/rails/init.rb b/railties/test/fixtures/plugins/engines/gemlike_engine/rails/init.rb new file mode 100644 index 0000000..2e6ec27 --- /dev/null +++ b/railties/test/fixtures/plugins/engines/gemlike_engine/rails/init.rb @@ -0,0 +1,7 @@ +# I have access to my directory and the Rails config. +raise 'directory expected but undefined in init.rb' unless defined? directory +raise 'config expected but undefined in init.rb' unless defined? config + +# My lib/ dir must be in the load path. +require 'gemlike_engine' +raise 'missing mixin from my lib/ dir' unless defined? GemlikeEngine diff --git a/railties/test/gem_plugin_test.rb b/railties/test/gem_plugin_test.rb new file mode 100644 index 0000000..eeb8cee --- /dev/null +++ b/railties/test/gem_plugin_test.rb @@ -0,0 +1,147 @@ +require 'plugin_test_helper' + +class GemPluginTest < Test::Unit::TestCase + + def setup + @initializer = Rails::Initializer.new(Rails::Configuration.new) + @normal_engine_path = plugin_fixture_path('engines/engine') + @gemlike_engine_path = plugin_fixture_path('engines/gemlike_engine') + end + + + #Normal engine tests (could still be a gem, but doesn't use "rails" directory) + + def test_should_discover_controller_path_in_root_directory + assert_equal expected_controller_path, normal_engine.controller_path + end + + def test_should_discover_view_path_in_root_directory + assert_equal expected_view_path, normal_engine.view_path + end + + def test_should_discover_metal_path_in_root_directory + assert_equal expected_metal_path, normal_engine.metal_path + end + + def test_should_discover_app_paths_in_root_directory + assert_equal expected_app_paths, normal_engine.send(:app_paths) + end + + def test_should_see_app_directory_in_root_directory + assert normal_engine.send(:has_app_directory?) + end + + def test_should_find_routing_file_in_root_config_directory + assert_equal expected_routing_file_path, normal_engine.routing_file + end + + + #Gemlike engine tests (uses "rails" directory for everything rails-specific) + + def test_should_discover_controller_path_in_rails_app_directory + assert_equal expected_gemlike_controller_path, gemlike_engine.controller_path + end + + def test_should_discover_view_path_in_rails_app_directory + assert_equal expected_gemlike_view_path, gemlike_engine.view_path + end + + def test_should_discover_metal_path_in_rails_app_directory + assert_equal expected_gemlike_metal_path, gemlike_engine.metal_path + end + + def test_should_discover_app_paths_in_rails_directory + assert_equal expected_gemlike_app_paths, gemlike_engine.send(:app_paths) + end + + def test_should_see_app_directory_in_rails_directory + assert gemlike_engine.send(:has_app_directory?) + end + + def test_should_find_routing_file_in_rails_config_directory + assert_equal expected_gemlike_routing_file_path, gemlike_engine.routing_file + end + + +private + + def plugin_for(path) + spec = stub("spec", :full_gem_path => path, :name => path.split('/').last) + gem = stub("gem") + Rails::GemPlugin.new(spec, gem) + end + + def normal_engine + plugin_for(@normal_engine_path) + end + + def gemlike_engine + plugin_for(@gemlike_engine_path) + end + + def app_path(*directories) + File.join(@normal_engine_path, 'app', *directories) + end + + def expected_controller_path + app_path('controllers') + end + + def expected_view_path + app_path('views') + end + + def expected_model_path + app_path('models') + end + + def expected_helper_path + app_path('helpers') + end + + def expected_metal_path + app_path('metal') + end + + def expected_app_paths + [expected_model_path, expected_helper_path, expected_controller_path, expected_metal_path] + end + + def expected_routing_file_path + File.join(@normal_engine_path, 'config', 'routes.rb') + end + + + def gemlike_app_path(*directories) + File.join(@gemlike_engine_path, 'rails', 'app', *directories) + end + + def expected_gemlike_controller_path + gemlike_app_path('controllers') + end + + def expected_gemlike_view_path + gemlike_app_path('views') + end + + def expected_gemlike_model_path + gemlike_app_path('models') + end + + def expected_gemlike_helper_path + gemlike_app_path('helpers') + end + + def expected_gemlike_metal_path + gemlike_app_path('metal') + end + + def expected_gemlike_app_paths + [expected_gemlike_model_path, expected_gemlike_helper_path, expected_gemlike_controller_path, expected_gemlike_metal_path] + end + + def expected_gemlike_routing_file_path + File.join(@gemlike_engine_path, 'rails', 'config', 'routes.rb') + end + +end diff --git a/railties/test/initializer_test.rb b/railties/test/initializer_test.rb index 561f7b8..ba5bd68 100644 --- a/railties/test/initializer_test.rb +++ b/railties/test/initializer_test.rb @@ -208,7 +208,7 @@ class InitializerPluginLoadingTests < Test::Unit::TestCase def test_all_plugins_are_loaded_when_registered_plugin_list_is_untouched failure_tip = "It's likely someone has added a new plugin fixture without updating this list" load_plugins! - assert_plugins [:a, :acts_as_chunky_bacon, :engine, :gemlike, :plugin_with_no_lib_dir, :stubby], @initializer.loaded_plugins, failure_tip + assert_plugins [:a, :acts_as_chunky_bacon, :engine, :gemlike, :gemlike_engine, :plugin_with_no_lib_dir, :stubby], @initializer.loaded_plugins, failure_tip end def test_all_plugins_loaded_when_all_is_used @@ -216,7 +216,7 @@ class InitializerPluginLoadingTests < Test::Unit::TestCase only_load_the_following_plugins! plugin_names load_plugins! failure_tip = "It's likely someone has added a new plugin fixture without updating this list" - assert_plugins [:stubby, :acts_as_chunky_bacon, :a, :engine, :gemlike, :plugin_with_no_lib_dir], @initializer.loaded_plugins, failure_tip + assert_plugins [:stubby, :acts_as_chunky_bacon, :a, :engine, :gemlike, :gemlike_engine, :plugin_with_no_lib_dir], @initializer.loaded_plugins, failure_tip end def test_all_plugins_loaded_after_all @@ -224,7 +224,7 @@ class InitializerPluginLoadingTests < Test::Unit::TestCase only_load_the_following_plugins! plugin_names load_plugins! failure_tip = "It's likely someone has added a new plugin fixture without updating this list" - assert_plugins [:stubby, :a, :engine, :gemlike, :plugin_with_no_lib_dir, :acts_as_chunky_bacon], @initializer.loaded_plugins, failure_tip + assert_plugins [:stubby, :a, :engine, :gemlike, :gemlike_engine, :plugin_with_no_lib_dir, :acts_as_chunky_bacon], @initializer.loaded_plugins, failure_tip end def test_plugin_names_may_be_strings @@ -396,4 +396,4 @@ class RailsRootTest < Test::Unit::TestCase def test_rails_dot_root_should_be_a_pathname assert_equal File.join(RAILS_ROOT, 'app', 'controllers'), Rails.root.join('app', 'controllers').to_s end -end \ No newline at end of file +end diff --git a/railties/test/plugin_loader_test.rb b/railties/test/plugin_loader_test.rb index b270748..9ca2c33 100644 --- a/railties/test/plugin_loader_test.rb +++ b/railties/test/plugin_loader_test.rb @@ -50,17 +50,17 @@ class TestPluginLoader < Test::Unit::TestCase assert_equal [], @loader.plugins end - def test_should_find_all_availble_plugins_and_return_as_all_plugins - assert_plugins [ :engine, :stubby, :plugin_with_no_lib_dir, :gemlike, :acts_as_chunky_bacon, :a], @loader.all_plugins.reverse, @failure_tip + def test_should_find_all_available_plugins_and_return_as_all_plugins + assert_plugins [:gemlike_engine, :engine, :stubby, :plugin_with_no_lib_dir, :gemlike, :acts_as_chunky_bacon, :a], @loader.all_plugins.reverse, @failure_tip end def test_should_return_all_plugins_as_plugins_when_registered_plugin_list_is_untouched - assert_plugins [:a, :acts_as_chunky_bacon, :engine, :gemlike, :plugin_with_no_lib_dir, :stubby], @loader.plugins, @failure_tip + assert_plugins [:a, :acts_as_chunky_bacon, :engine, :gemlike, :gemlike_engine, :plugin_with_no_lib_dir, :stubby], @loader.plugins, @failure_tip end def test_should_return_all_plugins_as_plugins_when_registered_plugin_list_is_nil @configuration.plugins = nil - assert_plugins [:a, :acts_as_chunky_bacon, :engine, :gemlike, :plugin_with_no_lib_dir, :stubby], @loader.plugins, @failure_tip + assert_plugins [:a, :acts_as_chunky_bacon, :engine, :gemlike, :gemlike_engine, :plugin_with_no_lib_dir, :stubby], @loader.plugins, @failure_tip end def test_should_return_specific_plugins_named_in_config_plugins_array_if_set @@ -77,17 +77,17 @@ class TestPluginLoader < Test::Unit::TestCase def test_should_load_all_plugins_in_natural_order_when_all_is_used only_load_the_following_plugins! [:all] - assert_plugins [:a, :acts_as_chunky_bacon, :engine, :gemlike, :plugin_with_no_lib_dir, :stubby], @loader.plugins, @failure_tip + assert_plugins [:a, :acts_as_chunky_bacon, :engine, :gemlike, :gemlike_engine, :plugin_with_no_lib_dir, :stubby], @loader.plugins, @failure_tip end def test_should_load_specified_plugins_in_order_and_then_all_remaining_plugins_when_all_is_used only_load_the_following_plugins! [:stubby, :acts_as_chunky_bacon, :all] - assert_plugins [:stubby, :acts_as_chunky_bacon, :a, :engine, :gemlike, :plugin_with_no_lib_dir], @loader.plugins, @failure_tip + assert_plugins [:stubby, :acts_as_chunky_bacon, :a, :engine, :gemlike, :gemlike_engine, :plugin_with_no_lib_dir], @loader.plugins, @failure_tip end def test_should_be_able_to_specify_loading_of_plugins_loaded_after_all only_load_the_following_plugins! [:stubby, :all, :acts_as_chunky_bacon] - assert_plugins [:stubby, :a, :engine, :gemlike, :plugin_with_no_lib_dir, :acts_as_chunky_bacon], @loader.plugins, @failure_tip + assert_plugins [:stubby, :a, :engine, :gemlike, :gemlike_engine, :plugin_with_no_lib_dir, :acts_as_chunky_bacon], @loader.plugins, @failure_tip end def test_should_accept_plugin_names_given_as_strings diff --git a/railties/test/plugin_locator_test.rb b/railties/test/plugin_locator_test.rb index 471d9fc..eeabdd4 100644 --- a/railties/test/plugin_locator_test.rb +++ b/railties/test/plugin_locator_test.rb @@ -42,7 +42,7 @@ class PluginFileSystemLocatorTest < Test::Unit::TestCase end def test_should_return_all_plugins_found_under_the_set_plugin_paths - assert_equal ["a", "acts_as_chunky_bacon", "engine", "gemlike", "plugin_with_no_lib_dir", "stubby"].sort, @locator.plugins.map(&:name).sort + assert_equal ["a", "acts_as_chunky_bacon", "engine", "gemlike", "gemlike_engine", "plugin_with_no_lib_dir", "stubby"].sort, @locator.plugins.map(&:name).sort end def test_should_find_plugins_only_under_the_plugin_paths_set_in_configuration -- 1.5.4.3