mirror of
https://github.com/puma/puma.git
synced 2022-11-09 13:48:40 -05:00
Fix environment file loader (#1340)
* Extract Puma::Configuration#config_files method and add few unit tests * Add TestConfigFile#test_config_files_with_rack_env test * Fix configuration loading based on RACK_ENV * Add test case when config files files are not specified * Refactor Puma::Configuration#config_files method * Fix backwards compatibility issue. Environment could be a String or Proc
This commit is contained in:
parent
b9485b13f4
commit
ae6b1cef9a
2 changed files with 70 additions and 11 deletions
|
@ -189,22 +189,22 @@ module Puma
|
||||||
end
|
end
|
||||||
|
|
||||||
def load
|
def load
|
||||||
|
config_files.each { |config_file| @file_dsl._load_from(config_file) }
|
||||||
|
|
||||||
|
@options
|
||||||
|
end
|
||||||
|
|
||||||
|
def config_files
|
||||||
files = @options.all_of(:config_files)
|
files = @options.all_of(:config_files)
|
||||||
|
|
||||||
if files.empty?
|
return [] if files == ['-']
|
||||||
imp = %W(config/puma/#{@options[:environment]}.rb config/puma.rb).find { |f|
|
return files if files.any?
|
||||||
|
|
||||||
|
first_default_file = %W(config/puma/#{environment_str}.rb config/puma.rb).find do |f|
|
||||||
File.exist?(f)
|
File.exist?(f)
|
||||||
}
|
|
||||||
|
|
||||||
files << imp
|
|
||||||
elsif files == ["-"]
|
|
||||||
files = []
|
|
||||||
end
|
end
|
||||||
|
|
||||||
files.each do |f|
|
[first_default_file]
|
||||||
@file_dsl._load_from(f)
|
|
||||||
end
|
|
||||||
@options
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Call once all configuration (included from rackup files)
|
# Call once all configuration (included from rackup files)
|
||||||
|
@ -264,6 +264,10 @@ module Puma
|
||||||
@options[:environment]
|
@options[:environment]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def environment_str
|
||||||
|
environment.respond_to?(:call) ? environment.call : environment
|
||||||
|
end
|
||||||
|
|
||||||
def load_plugin(name)
|
def load_plugin(name)
|
||||||
@plugins.create name
|
@plugins.create name
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,6 +3,11 @@ require_relative "helper"
|
||||||
require "puma/configuration"
|
require "puma/configuration"
|
||||||
|
|
||||||
class TestConfigFile < Minitest::Test
|
class TestConfigFile < Minitest::Test
|
||||||
|
def setup
|
||||||
|
FileUtils.mkpath("config/puma")
|
||||||
|
File.write("config/puma/fake-env.rb", "")
|
||||||
|
end
|
||||||
|
|
||||||
def test_app_from_rackup
|
def test_app_from_rackup
|
||||||
conf = Puma::Configuration.new do |c|
|
conf = Puma::Configuration.new do |c|
|
||||||
c.rackup "test/rackup/hello-bind.ru"
|
c.rackup "test/rackup/hello-bind.ru"
|
||||||
|
@ -84,6 +89,56 @@ class TestConfigFile < Minitest::Test
|
||||||
assert_equal 5, conf.options[:max_threads]
|
assert_equal 5, conf.options[:max_threads]
|
||||||
end
|
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
|
private
|
||||||
|
|
||||||
def with_env(env = {})
|
def with_env(env = {})
|
||||||
|
|
Loading…
Add table
Reference in a new issue