Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix environment file loader #1340

Merged
merged 8 commits into from Oct 16, 2017
26 changes: 15 additions & 11 deletions lib/puma/configuration.rb
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
55 changes: 55 additions & 0 deletions test/test_config.rb
Expand Up @@ -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"
Expand Down Expand Up @@ -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 = {})
Expand Down