Skip to content

Commit

Permalink
CI: remove test/helpers/config_file.rb, which modifies ENV (#3550)
Browse files Browse the repository at this point in the history
  • Loading branch information
MSP-Greg authored Nov 12, 2024
1 parent 450784a commit 0255efd
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 141 deletions.
16 changes: 0 additions & 16 deletions test/helpers/config_file.rb

This file was deleted.

115 changes: 51 additions & 64 deletions test/test_config.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# frozen_string_literal: true

require_relative "helper"
require_relative "helpers/config_file"

require "puma/configuration"
require 'puma/log_writer'
require 'rack'

class TestConfigFile < TestConfigFileBase
class TestConfigFile < Minitest::Test
parallelize_me!

def test_default_max_threads
Expand Down Expand Up @@ -624,7 +623,7 @@ def assert_warning_for_hooks_defined_in_single_mode(hook_name)
end

# contains tests that cannot run parallel
class TestConfigFileSingle < TestConfigFileBase
class TestConfigFileSingle < Minitest::Test
def test_custom_logger_from_DSL
conf = Puma::Configuration.new { |c| c.load 'test/config/custom_logger.rb' }

Expand All @@ -636,109 +635,101 @@ def test_custom_logger_from_DSL
end

# Thread unsafe modification of ENV
class TestEnvModifificationConfig < TestConfigFileBase
class TestEnvModifificationConfig < Minitest::Test
def test_double_bind_port
port = (rand(10_000) + 30_000).to_s
with_env("PORT" => port) do
conf = Puma::Configuration.new do |user_config, file_config, default_config|
user_config.bind "tcp://#{Puma::Configuration::DEFAULTS[:tcp_host]}:#{port}"
file_config.load "test/config/app.rb"
end

conf.load
assert_equal ["tcp://0.0.0.0:#{port}"], conf.options[:binds]
env = { "PORT" => port }
conf = Puma::Configuration.new({}, {}, env) do |user_config, file_config, default_config|
user_config.bind "tcp://#{Puma::Configuration::DEFAULTS[:tcp_host]}:#{port}"
file_config.load "test/config/app.rb"
end

conf.load
assert_equal ["tcp://0.0.0.0:#{port}"], conf.options[:binds]
end
end

class TestConfigEnvVariables < TestConfigFileBase
class TestConfigEnvVariables < Minitest::Test
def test_config_loads_correct_min_threads
assert_equal 0, Puma::Configuration.new.options.default_options[:min_threads]

with_env("MIN_THREADS" => "7") do
conf = Puma::Configuration.new
assert_equal 7, conf.options.default_options[:min_threads]
end
env = { "MIN_THREADS" => "7" }
conf = Puma::Configuration.new({}, {}, env)
assert_equal 7, conf.options.default_options[:min_threads]

with_env("PUMA_MIN_THREADS" => "8") do
conf = Puma::Configuration.new
assert_equal 8, conf.options.default_options[:min_threads]
end
env = { "PUMA_MIN_THREADS" => "8" }
conf = Puma::Configuration.new({}, {}, env)
assert_equal 8, conf.options.default_options[:min_threads]
end

def test_config_loads_correct_max_threads
conf = Puma::Configuration.new

with_env("MAX_THREADS" => "7") do
conf = Puma::Configuration.new
assert_equal 7, conf.options.default_options[:max_threads]
end
env = { "MAX_THREADS" => "7" }
conf = Puma::Configuration.new({}, {}, env)
assert_equal 7, conf.options.default_options[:max_threads]

with_env("PUMA_MAX_THREADS" => "8") do
conf = Puma::Configuration.new
assert_equal 8, conf.options.default_options[:max_threads]
end
env = { "PUMA_MAX_THREADS" => "8" }
conf = Puma::Configuration.new({}, {}, env)
assert_equal 8, conf.options.default_options[:max_threads]
end

def test_config_loads_workers_from_env
with_env("WEB_CONCURRENCY" => "9") do
conf = Puma::Configuration.new
assert_equal 9, conf.options.default_options[:workers]
end
env = { "WEB_CONCURRENCY" => "9" }
conf = Puma::Configuration.new({}, {}, env)
assert_equal 9, conf.options.default_options[:workers]
end

def test_config_does_not_preload_app_if_not_using_workers
with_env("WEB_CONCURRENCY" => "0") do
conf = Puma::Configuration.new
assert_equal false, conf.options.default_options[:preload_app]
end
env = { "WEB_CONCURRENCY" => "0" }
conf = Puma::Configuration.new({}, {}, env)
assert_equal false, conf.options.default_options[:preload_app]
end

def test_config_preloads_app_if_using_workers
with_env("WEB_CONCURRENCY" => "2") do
preload = Puma.forkable?
conf = Puma::Configuration.new
assert_equal preload, conf.options.default_options[:preload_app]
end
env = { "WEB_CONCURRENCY" => "2" }
preload = Puma.forkable?
conf = Puma::Configuration.new({}, {}, env)
assert_equal preload, conf.options.default_options[:preload_app]
end
end

class TestConfigFileWithFakeEnv < TestConfigFileBase
class TestConfigFileWithFakeEnv < Minitest::Test
def setup
FileUtils.mkpath("config/puma")
File.write("config/puma/fake-env.rb", "")
end

def teardown
FileUtils.rm_r("config/puma")
end

def test_config_files_with_app_env
with_env('APP_ENV' => 'fake-env') do
conf = Puma::Configuration.new do
end
env = { 'APP_ENV' => 'fake-env' }

assert_equal ['config/puma/fake-env.rb'], conf.config_files
end
conf = Puma::Configuration.new({}, {}, env)

assert_equal ['config/puma/fake-env.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
env = { 'RACK_ENV' => 'fake-env' }

assert_equal ['config/puma/fake-env.rb'], conf.config_files
end
conf = Puma::Configuration.new({}, {}, env)

assert_equal ['config/puma/fake-env.rb'], conf.config_files
end

def test_config_files_with_rails_env
with_env('RAILS_ENV' => 'fake-env', 'RACK_ENV' => nil) do
conf = Puma::Configuration.new do
end
env = { 'RAILS_ENV' => 'fake-env', 'RACK_ENV' => nil }

assert_equal ['config/puma/fake-env.rb'], conf.config_files
end
conf = Puma::Configuration.new({}, {}, env)

assert_equal ['config/puma/fake-env.rb'], conf.config_files
end

def test_config_files_with_specified_environment
conf = Puma::Configuration.new do
end
conf = Puma::Configuration.new

conf.options[:environment] = 'fake-env'

Expand Down Expand Up @@ -769,8 +760,4 @@ def test_enable_keep_alives_false

assert_equal conf.options[:enable_keep_alives], false
end

def teardown
FileUtils.rm_r("config/puma")
end
end
118 changes: 57 additions & 61 deletions test/test_pumactl.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
require_relative "helper"
require_relative "helpers/config_file"
require_relative "helpers/ssl"

require 'pathname'
require 'puma/control_cli'

class TestPumaControlCli < TestConfigFileBase
class TestPumaControlCli < Minitest::Test
include SSLHelper

def setup
# use a pipe to get info across thread boundary
@wait, @ready = IO.pipe
end

def wait_booted
def wait_booted(log: nil)
@server_log = +''
begin
sleep 0.01 until @wait.wait_readable(0.01)
line = @wait.gets
STDOUT.syswrite "#{line}\n" if log
@server_log << line
end until line&.include?('Use Ctrl-C to stop')
end
Expand Down Expand Up @@ -53,84 +54,79 @@ def test_config_file
end

def test_app_env_without_environment
with_env('APP_ENV' => 'test') do
control_cli = Puma::ControlCLI.new ['halt']
assert_equal 'test', control_cli.instance_variable_get(:@environment)
end
env = { 'APP_ENV' => 'test' }
control_cli = Puma::ControlCLI.new ['halt'], env: env
assert_equal 'test', control_cli.instance_variable_get(:@environment)
end

def test_rack_env_without_environment
with_env("RACK_ENV" => "test") do
control_cli = Puma::ControlCLI.new ["halt"]
assert_equal "test", control_cli.instance_variable_get(:@environment)
end
env = { "RACK_ENV" => "test" }
control_cli = Puma::ControlCLI.new ["halt"], env: env
assert_equal "test", control_cli.instance_variable_get(:@environment)
end

def test_app_env_precedence
with_env('APP_ENV' => nil, 'RACK_ENV' => nil, 'RAILS_ENV' => 'production') do
control_cli = Puma::ControlCLI.new ['halt']
assert_equal 'production', control_cli.instance_variable_get(:@environment)
end
env = { 'APP_ENV' => nil, 'RACK_ENV' => nil, 'RAILS_ENV' => 'production' }
control_cli = Puma::ControlCLI.new ['halt'], env: env
assert_equal 'production', control_cli.instance_variable_get(:@environment)

with_env('APP_ENV' => nil, 'RACK_ENV' => 'test', 'RAILS_ENV' => 'production') do
control_cli = Puma::ControlCLI.new ['halt']
assert_equal 'test', control_cli.instance_variable_get(:@environment)
end
env = { 'APP_ENV' => nil, 'RACK_ENV' => 'test', 'RAILS_ENV' => 'production' }
control_cli = Puma::ControlCLI.new ['halt'], env: env
assert_equal 'test', control_cli.instance_variable_get(:@environment)

with_env('APP_ENV' => 'development', 'RACK_ENV' => 'test', 'RAILS_ENV' => 'production') do
control_cli = Puma::ControlCLI.new ['halt']
assert_equal 'development', control_cli.instance_variable_get(:@environment)
env = { 'APP_ENV' => 'development', 'RACK_ENV' => 'test', 'RAILS_ENV' => 'production' }
control_cli = Puma::ControlCLI.new ['halt'], env: env
assert_equal 'development', control_cli.instance_variable_get(:@environment)

control_cli = Puma::ControlCLI.new ['-e', 'test', 'halt']
assert_equal 'test', control_cli.instance_variable_get(:@environment)
end
control_cli = Puma::ControlCLI.new ['-e', 'test', 'halt'], env: env
assert_equal 'test', control_cli.instance_variable_get(:@environment)
end

def test_environment_without_app_env
with_env('APP_ENV' => nil, 'RACK_ENV' => nil, 'RAILS_ENV' => nil) do
control_cli = Puma::ControlCLI.new ['halt']
assert_nil control_cli.instance_variable_get(:@environment)
env = { 'APP_ENV' => nil, 'RACK_ENV' => nil, 'RAILS_ENV' => nil }

control_cli = Puma::ControlCLI.new ['-e', 'test', 'halt']
assert_equal 'test', control_cli.instance_variable_get(:@environment)
end
control_cli = Puma::ControlCLI.new ['halt'], env: env
assert_nil control_cli.instance_variable_get(:@environment)

control_cli = Puma::ControlCLI.new ['-e', 'test', 'halt'], env: env
assert_equal 'test', control_cli.instance_variable_get(:@environment)
end

def test_environment_without_rack_env
with_env("RACK_ENV" => nil, 'RAILS_ENV' => nil) do
control_cli = Puma::ControlCLI.new ["halt"]
assert_nil control_cli.instance_variable_get(:@environment)
env = { "RACK_ENV" => nil, 'RAILS_ENV' => nil }

control_cli = Puma::ControlCLI.new ["-e", "test", "halt"]
assert_equal "test", control_cli.instance_variable_get(:@environment)
end
control_cli = Puma::ControlCLI.new ["halt"], env: env
assert_nil control_cli.instance_variable_get(:@environment)

control_cli = Puma::ControlCLI.new ["-e", "test", "halt"]
assert_equal "test", control_cli.instance_variable_get(:@environment)
end

def test_environment_with_rack_env
with_env("RACK_ENV" => "production") do
control_cli = Puma::ControlCLI.new ["halt"]
assert_equal "production", control_cli.instance_variable_get(:@environment)
env = { "RACK_ENV" => "production" }

control_cli = Puma::ControlCLI.new ["-e", "test", "halt"]
assert_equal "test", control_cli.instance_variable_get(:@environment)
end
control_cli = Puma::ControlCLI.new ["halt"], env: env
assert_equal "production", control_cli.instance_variable_get(:@environment)

control_cli = Puma::ControlCLI.new ["-e", "test", "halt"], env: env
assert_equal "test", control_cli.instance_variable_get(:@environment)
end

def test_environment_specific_config_file_exist
port = UniquePort.call
puma_config_file = "config/puma.rb"
production_config_file = "config/puma/production.rb"

with_env("RACK_ENV" => nil) do
with_config_file(puma_config_file, port) do
control_cli = Puma::ControlCLI.new ["-e", "production", "halt"]
assert_equal puma_config_file, control_cli.instance_variable_get(:@config_file)
end
env = { "RACK_ENV" => nil }

with_config_file(production_config_file, port) do
control_cli = Puma::ControlCLI.new ["-e", "production", "halt"]
assert_equal production_config_file, control_cli.instance_variable_get(:@config_file)
end
with_config_file(puma_config_file, port) do
control_cli = Puma::ControlCLI.new ["-e", "production", "halt"], env: env
assert_equal puma_config_file, control_cli.instance_variable_get(:@config_file)
end

with_config_file(production_config_file, port) do
control_cli = Puma::ControlCLI.new ["-e", "production", "halt"], env: env
assert_equal production_config_file, control_cli.instance_variable_get(:@config_file)
end
end

Expand All @@ -139,16 +135,16 @@ def test_default_config_file_exist
puma_config_file = "config/puma.rb"
development_config_file = "config/puma/development.rb"

with_env("RACK_ENV" => nil, 'RAILS_ENV' => nil) do
with_config_file(puma_config_file, port) do
control_cli = Puma::ControlCLI.new ["halt"]
assert_equal puma_config_file, control_cli.instance_variable_get(:@config_file)
end
env = { "RACK_ENV" => nil, 'RAILS_ENV' => nil }

with_config_file(development_config_file, port) do
control_cli = Puma::ControlCLI.new ["halt"]
assert_equal development_config_file, control_cli.instance_variable_get(:@config_file)
end
with_config_file(puma_config_file, port) do
control_cli = Puma::ControlCLI.new ["halt"], env: env
assert_equal puma_config_file, control_cli.instance_variable_get(:@config_file)
end

with_config_file(development_config_file, port) do
control_cli = Puma::ControlCLI.new ["halt"], env: env
assert_equal development_config_file, control_cli.instance_variable_get(:@config_file)
end
end

Expand Down

0 comments on commit 0255efd

Please sign in to comment.