Skip to content

Commit 592e439

Browse files
committed
Include capybara puma workaround
1 parent 160f606 commit 592e439

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

example_app_generator/generate_app.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
'ci_retry_bundle_install.sh'
1212
)
1313
function_script_file = File.join(rspec_rails_repo_path, 'script/functions.sh')
14+
capybara_backport_path = File.join(rspec_rails_repo_path, 'example_app_generator/spec/support/capybara.rb')
1415

1516
in_root do
1617
prepend_to_file "Rakefile", "require 'active_support/all'"
@@ -64,6 +65,8 @@
6465
bundle_install_path
6566
chmod 'ci_retry_bundle_install.sh', 0755
6667

68+
copy_file capybara_backport_path, 'spec/support/capybara.rb'
69+
6770
if Rails::VERSION::STRING > '7'
6871
create_file 'app/assets/config/manifest.js' do
6972
"//= link application.css"
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# This is a backport of a fix that was included in capybara 3.40.0 which was also Ruby version locked to 3.0+
2+
if RUBY_VERSION.to_f < 3 && Rails::VERSION::STRING.to_f >= 7.1
3+
Capybara.register_server :puma do |app, port, host, **options|
4+
begin
5+
require 'rackup'
6+
rescue LoadError # rubocop:disable Lint/SuppressedException
7+
end
8+
begin
9+
require 'rack/handler/puma'
10+
rescue LoadError
11+
raise LoadError, 'Capybara is unable to load `puma` for its server, please add `puma` to your project or specify a different server via something like `Capybara.server = :webrick`.'
12+
end
13+
puma_rack_handler = defined?(Rackup::Handler::Puma) ? Rackup::Handler::Puma : Rack::Handler::Puma
14+
15+
unless puma_rack_handler.respond_to?(:config)
16+
raise LoadError, 'Capybara requires `puma` version 3.8.0 or higher, please upgrade `puma` or register and specify your own server block'
17+
end
18+
19+
# If we just run the Puma Rack handler it installs signal handlers which prevent us from being able to interrupt tests.
20+
# Therefore construct and run the Server instance ourselves.
21+
# puma_rack_handler.run(app, { Host: host, Port: port, Threads: "0:4", workers: 0, daemon: false }.merge(options))
22+
default_options = { Host: host, Port: port, Threads: '0:4', workers: 0, daemon: false }
23+
options = default_options.merge(options)
24+
25+
conf = puma_rack_handler.config(app, options)
26+
conf.clamp
27+
28+
puma_ver = Gem::Version.new(Puma::Const::PUMA_VERSION)
29+
require_relative 'patches/puma_ssl' if Gem::Requirement.new('>=4.0.0', '< 4.1.0').satisfied_by?(puma_ver)
30+
31+
logger = (defined?(Puma::LogWriter) ? Puma::LogWriter : Puma::Events).then do |cls|
32+
conf.options[:Silent] ? cls.strings : cls.stdio
33+
end
34+
conf.options[:log_writer] = logger
35+
36+
logger.log 'Capybara starting Puma...'
37+
logger.log "* Version #{Puma::Const::PUMA_VERSION}, codename: #{Puma::Const::CODE_NAME}"
38+
logger.log "* Min threads: #{conf.options[:min_threads]}, max threads: #{conf.options[:max_threads]}"
39+
40+
Puma::Server.new(
41+
conf.app,
42+
defined?(Puma::LogWriter) ? nil : logger,
43+
conf.options
44+
).tap do |s|
45+
s.binder.parse conf.options[:binds], (s.log_writer rescue s.events) # rubocop:disable Style/RescueModifier
46+
s.min_threads, s.max_threads = conf.options[:min_threads], conf.options[:max_threads] if s.respond_to? :min_threads=
47+
end.run.join
48+
end
49+
end

features/support/env.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ def with_unbundled_env
5959
# We want fresh `example_app` project with empty `spec` dir except helpers.
6060
# FileUtils.cp_r on Ruby 1.9.2 doesn't preserve permissions.
6161
system('cp', '-r', example_app_dir, aruba_dir)
62-
helpers = %w[spec/spec_helper.rb spec/rails_helper.rb]
63-
Dir["#{aruba_dir}/spec/*"].each do |path|
62+
helpers = %w[spec/spec_helper.rb spec/rails_helper.rb spec/support/capybara.rb]
63+
Dir["#{aruba_dir}/spec/**/*"].each do |path|
64+
next if File.directory?(path)
6465
next if helpers.any? { |helper| path.end_with?(helper) }
6566

6667
FileUtils.rm_rf(path)

0 commit comments

Comments
 (0)