Skip to content

Commit

Permalink
Fix environment file loader (#1340)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ViliusLuneckas authored and nateberkopec committed Oct 16, 2017
1 parent b9485b1 commit ae6b1ce
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 11 deletions.
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

0 comments on commit ae6b1ce

Please sign in to comment.