From 5adc3f6651ee8efd707ce6a71e77b38a9322c036 Mon Sep 17 00:00:00 2001
From: tohosaku <tohosaku@users.osdn.me>
Date: Tue, 12 Dec 2023 01:26:37 +0000
Subject: [PATCH 2/2] Extract plugin autoload test.

Tests that verify autoloading of plugins are difficult to run with
 other tests and should be independent as test:autoload.
The mechanism to change the plugin directory by RAILS_ENV should
 be reverted.
---
 config/application.rb                       |  3 --
 config/environments/test.rb                 |  3 --
 lib/redmine/plugin_loader.rb                |  2 +-
 lib/tasks/testing.rake                      |  7 +++++
 test/autoload/plugin_autoload_test.rb       | 31 +++++++++++++++++++++
 test/integration/routing/plugins_test.rb    | 20 +++++++++++++
 test/unit/lib/redmine/plugin_loader_test.rb |  6 +---
 test/unit/lib/redmine/plugin_test.rb        |  2 +-
 8 files changed, 61 insertions(+), 13 deletions(-)
 create mode 100644 test/autoload/plugin_autoload_test.rb

diff --git a/config/application.rb b/config/application.rb
index f5062dfe7..48e855c9b 100644
--- a/config/application.rb
+++ b/config/application.rb
@@ -85,9 +85,6 @@ module RedmineApp
     # for more options (same options as config.cache_store).
     config.redmine_search_cache_store = :memory_store
 
-    # Sets default plugin directory
-    config.redmine_plugin_directory = 'plugins'
-
     # Configure log level here so that additional environment file
     # can change it (environments/ENV.rb would take precedence over it)
     config.log_level = Rails.env.production? ? :info : :debug
diff --git a/config/environments/test.rb b/config/environments/test.rb
index e67cea76c..199f1bb27 100644
--- a/config/environments/test.rb
+++ b/config/environments/test.rb
@@ -18,9 +18,6 @@ Rails.application.configure do
   # preloads Rails for running tests, you may have to set it to true.
   config.eager_load = false
 
-  # Change the plugin directory when testing to avoid clashes with real plugins.
-  config.redmine_plugin_directory = 'test/fixtures/plugins'
-
   # Configure public file server for tests with Cache-Control for performance.
   config.public_file_server.enabled = true
   config.public_file_server.headers = {
diff --git a/lib/redmine/plugin_loader.rb b/lib/redmine/plugin_loader.rb
index 04d7e4af0..e221c22cd 100644
--- a/lib/redmine/plugin_loader.rb
+++ b/lib/redmine/plugin_loader.rb
@@ -84,7 +84,7 @@ module Redmine
   class PluginLoader
     # Absolute path to the directory where plugins are located
     cattr_accessor :directory
-    self.directory = Rails.root.join Rails.application.config.redmine_plugin_directory
+    self.directory = Rails.root.join (ENV['REDMINE_PLUGINS_DIR'] || 'plugins')
 
     # Absolute path to the public directory where plugins assets are copied
     cattr_accessor :public_directory
diff --git a/lib/tasks/testing.rake b/lib/tasks/testing.rake
index 5b5cedb46..4a8bc4575 100644
--- a/lib/tasks/testing.rake
+++ b/lib/tasks/testing.rake
@@ -114,4 +114,11 @@ namespace :test do
     Rails::TestUnit::Runner.run_from_rake 'test', FileList['test/integration/routing/*_test.rb'] + FileList['test/integration/api_test/*_routing_test.rb']
   end
   Rake::Task['test:routing'].comment = "Run the routing tests"
+
+  task(:autoload) do |t|
+    $: << "test"
+    ENV["REDMINE_PLUGINS_DIR"] = "test/fixtures/plugins"
+    Rails::TestUnit::Runner.run_from_rake 'test', FileList['test/autoload/*_test.rb']
+  end
+  Rake::Task['test:autoload'].comment = "Run the plugin autoload tests"
 end
diff --git a/test/autoload/plugin_autoload_test.rb b/test/autoload/plugin_autoload_test.rb
new file mode 100644
index 000000000..47511a743
--- /dev/null
+++ b/test/autoload/plugin_autoload_test.rb
@@ -0,0 +1,31 @@
+# frozen_string_literal: true
+
+# Redmine - project management software
+# Copyright (C) 2006-2023  Jean-Philippe Lang
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+
+require_relative '../test_helper'
+
+class Redmine::PluginAutoloadTest < ActiveSupport::TestCase
+
+  if ENV['REDMINE_PLUGINS_DIR']
+    def test_autoload
+      assert_equal true, Object.const_defined?(:Foo)
+    end
+  else
+    puts 'Tests related to plugin autoloading should be run separately using "rails test:autoload"'
+  end
+end
diff --git a/test/integration/routing/plugins_test.rb b/test/integration/routing/plugins_test.rb
index 53b10cd0a..0748bb574 100644
--- a/test/integration/routing/plugins_test.rb
+++ b/test/integration/routing/plugins_test.rb
@@ -20,6 +20,26 @@
 require File.expand_path('../../test_helper', __dir__)
 
 class RoutingPluginsTest < Redmine::RoutingTest
+
+  def setup
+    # Change plugin loader's directory for testing
+    @original_plugin_dir = Redmine::PluginLoader.directory
+    Redmine::Plugin.clear
+    Redmine::PluginLoader.directory = Rails.root.join('test/fixtures/plugins')
+    Redmine::Plugin.directory = Rails.root.join('test/fixtures/plugins')
+    Redmine::PluginLoader.load
+    Redmine::PluginLoader.directories.each(&:run_initializer) # to define relative controllers
+    RedmineApp::Application.instance.routes_reloader.reload!
+  end
+
+  def teardown
+    Redmine::Plugin.clear
+    Redmine::PluginLoader.directory = @original_plugin_dir
+    Redmine::Plugin.directory = @original_plugin_dir
+    Redmine::PluginLoader.load
+    RedmineApp::Application.instance.routes_reloader.reload!
+  end
+
   def test_plugins
     should_route 'GET /plugin_articles' => 'plugin_articles#index'
     should_route 'GET /bar_plugin_articles' => 'bar_plugin_articles#index'
diff --git a/test/unit/lib/redmine/plugin_loader_test.rb b/test/unit/lib/redmine/plugin_loader_test.rb
index a0bec218e..05224452e 100644
--- a/test/unit/lib/redmine/plugin_loader_test.rb
+++ b/test/unit/lib/redmine/plugin_loader_test.rb
@@ -23,9 +23,9 @@ class Redmine::PluginLoaderTest < ActiveSupport::TestCase
   def setup
     clear_public
 
-    # Change plugin directory for testing to default in config/environments/tesr.rb.
     # plugins/foo => test/fixtures/plugins/foo
     @klass = Redmine::PluginLoader
+    @klass.directory = Rails.root.join('test/fixtures/plugins')
     @klass.public_directory = Rails.root.join('tmp/public/plugin_assets')
     @klass.load
   end
@@ -56,10 +56,6 @@ class Redmine::PluginLoaderTest < ActiveSupport::TestCase
     assert File.exist?("#{@klass.public_directory}/foo_plugin/stylesheets/foo.css")
   end
 
-  def test_autoload
-    assert_equal true, Object.const_defined?(:Foo)
-  end
-
   def clear_public
     FileUtils.rm_rf 'tmp/public'
   end
diff --git a/test/unit/lib/redmine/plugin_test.rb b/test/unit/lib/redmine/plugin_test.rb
index d27668247..36846b7db 100644
--- a/test/unit/lib/redmine/plugin_test.rb
+++ b/test/unit/lib/redmine/plugin_test.rb
@@ -22,8 +22,8 @@ require_relative '../../../test_helper'
 class Redmine::PluginTest < ActiveSupport::TestCase
   def setup
     @klass = Redmine::Plugin
-    # Change plugin directory for testing to default in config/environments/test.rb.
     # plugins/foo => test/fixtures/plugins/foo
+    @klass.directory = Rails.root.join('test/fixtures/plugins')
     # In case some real plugins are installed
     @klass.clear
 
-- 
2.39.2

