diff --git a/lib/puma/configuration.rb b/lib/puma/configuration.rb index 1aea28bb67..735c796efc 100644 --- a/lib/puma/configuration.rb +++ b/lib/puma/configuration.rb @@ -189,22 +189,22 @@ def puma_default_options end def load + config_files.each { |config_file| @file_dsl._load_from(config_file) } + + @options + end + + def config_files files = @options.all_of(:config_files) - if files.empty? - imp = %W(config/puma/#{@options[:environment]}.rb config/puma.rb).find { |f| - File.exist?(f) - } + return [] if files == ['-'] + return files if files.any? - files << imp - elsif files == ["-"] - files = [] + first_default_file = %W(config/puma/#{environment_str}.rb config/puma.rb).find do |f| + File.exist?(f) end - files.each do |f| - @file_dsl._load_from(f) - end - @options + [first_default_file] end # Call once all configuration (included from rackup files) @@ -264,6 +264,10 @@ def environment @options[:environment] end + def environment_str + environment.respond_to?(:call) ? environment.call : environment + end + def load_plugin(name) @plugins.create name end diff --git a/test/test_config.rb b/test/test_config.rb index 87c1be44f0..b62a21817c 100644 --- a/test/test_config.rb +++ b/test/test_config.rb @@ -3,6 +3,11 @@ require "puma/configuration" class TestConfigFile < Minitest::Test + def setup + FileUtils.mkpath("config/puma") + File.write("config/puma/fake-env.rb", "") + end + def test_app_from_rackup conf = Puma::Configuration.new do |c| c.rackup "test/rackup/hello-bind.ru" @@ -84,6 +89,56 @@ def test_parameters_overwrite_files assert_equal 5, conf.options[:max_threads] end + def test_config_files_default + conf = Puma::Configuration.new do + end + + assert_equal [nil], conf.config_files + end + + def test_config_files_with_dash + conf = Puma::Configuration.new(config_files: ['-']) do + end + + assert_equal [], conf.config_files + end + + def test_config_files_with_existing_path + conf = Puma::Configuration.new(config_files: ['test/config/settings.rb']) do + end + + assert_equal ['test/config/settings.rb'], conf.config_files + end + + def test_config_files_with_non_existing_path + conf = Puma::Configuration.new(config_files: ['test/config/typo/settings.rb']) do + end + + assert_equal ['test/config/typo/settings.rb'], conf.config_files + end + + def test_config_files_with_rack_env + with_env('RACK_ENV' => 'fake-env') do + conf = Puma::Configuration.new do + end + + assert_equal ['config/puma/fake-env.rb'], conf.config_files + end + end + + def test_config_files_with_specified_environment + conf = Puma::Configuration.new do + end + + conf.options[:environment] = 'fake-env' + + assert_equal ['config/puma/fake-env.rb'], conf.config_files + end + + def teardown + FileUtils.rm_r("config/puma") + end + private def with_env(env = {})