Skip to content

Commit 690b569

Browse files
committed
[test] Generate Rails stub tests for 5.0 through 7.1
(cherry-picked from 7e9bb85)
1 parent a4f2ac3 commit 690b569

File tree

140 files changed

+2491
-56
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

140 files changed

+2491
-56
lines changed

src/spec/ruby/jruby/rack/integration_spec.rb

Lines changed: 95 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -84,65 +84,64 @@
8484

8585
end
8686

87+
it "should have defined Rails stub tests" do
88+
expect(File.foreach(__FILE__).select { |line| line.include?("describe") }).to include(/^ describe.*lib: :#{CURRENT_LIB}/),
89+
"Expected rails stub tests to be defined for #{CURRENT_LIB} inside integration_spec.rb"
90+
expect(File.exist?(File.join(STUB_DIR, CURRENT_LIB.to_s))).to be(true),
91+
"Expected rails stub dir for #{CURRENT_LIB.to_s} to exist at #{File.join(STUB_DIR, CURRENT_LIB.to_s).inspect}"
92+
end if CURRENT_LIB.to_s.include?('rails')
93+
8794
shared_examples_for 'a rails app', :shared => true do
8895

96+
base_path = "file://#{STUB_DIR}/#{CURRENT_LIB.to_s}"
97+
8998
let(:servlet_context) do
90-
new_servlet_context(base_path).tap { |servlet_context| prepare_servlet_context(servlet_context) }
99+
new_servlet_context(base_path).tap { |servlet_context| prepare_servlet_context(servlet_context, base_path) }
91100
end
92101

93-
it "initializes (pooling by default)" do
94-
listener = org.jruby.rack.rails.RailsServletContextListener.new
95-
listener.contextInitialized javax.servlet.ServletContextEvent.new(servlet_context)
102+
context "runtime" do
96103

97-
rack_factory = servlet_context.getAttribute("rack.factory")
98-
expect(rack_factory).to be_a(RackApplicationFactory)
99-
expect(rack_factory).to be_a(PoolingRackApplicationFactory)
100-
expect(rack_factory).to respond_to(:realFactory)
101-
expect(rack_factory.realFactory).to be_a(RailsRackApplicationFactory)
104+
it "initializes (pooling by default)" do
105+
listener = org.jruby.rack.rails.RailsServletContextListener.new
106+
listener.contextInitialized javax.servlet.ServletContextEvent.new(servlet_context)
102107

103-
expect(servlet_context.getAttribute("rack.context")).to be_a(RackContext)
104-
expect(servlet_context.getAttribute("rack.context")).to be_a(ServletRackContext)
108+
rack_factory = servlet_context.getAttribute("rack.factory")
109+
expect(rack_factory).to be_a(RackApplicationFactory)
110+
expect(rack_factory).to be_a(PoolingRackApplicationFactory)
111+
expect(rack_factory).to respond_to(:realFactory)
112+
expect(rack_factory.realFactory).to be_a(RailsRackApplicationFactory)
105113

106-
expect(rack_factory.getApplication).to be_a(DefaultRackApplication)
107-
end
114+
expect(servlet_context.getAttribute("rack.context")).to be_a(RackContext)
115+
expect(servlet_context.getAttribute("rack.context")).to be_a(ServletRackContext)
108116

109-
it "initializes threadsafe!" do
110-
servlet_context.addInitParameter('jruby.max.runtimes', '1')
117+
expect(rack_factory.getApplication).to be_a(DefaultRackApplication)
118+
end
111119

112-
listener = org.jruby.rack.rails.RailsServletContextListener.new
113-
listener.contextInitialized javax.servlet.ServletContextEvent.new(servlet_context)
120+
it "initializes threadsafe!" do
121+
servlet_context.addInitParameter('jruby.max.runtimes', '1')
114122

115-
rack_factory = servlet_context.getAttribute("rack.factory")
116-
expect(rack_factory).to be_a(RackApplicationFactory)
117-
expect(rack_factory).to be_a(SharedRackApplicationFactory)
118-
expect(rack_factory.realFactory).to be_a(RailsRackApplicationFactory)
123+
listener = org.jruby.rack.rails.RailsServletContextListener.new
124+
listener.contextInitialized javax.servlet.ServletContextEvent.new(servlet_context)
119125

120-
expect(rack_factory.getApplication).to be_a(DefaultRackApplication)
121-
end
122-
end
123-
124-
describe 'rails 7.2', lib: :rails72 do
126+
rack_factory = servlet_context.getAttribute("rack.factory")
127+
expect(rack_factory).to be_a(RackApplicationFactory)
128+
expect(rack_factory).to be_a(SharedRackApplicationFactory)
129+
expect(rack_factory.realFactory).to be_a(RailsRackApplicationFactory)
125130

126-
before(:all) do
127-
name = :rails72 # copy_gemfile :
128-
raise "Environment variable BUNDLE_GEMFILE seems to not contain #{name.to_s}" unless ENV['BUNDLE_GEMFILE']&.include?(name.to_s)
129-
FileUtils.cp ENV['BUNDLE_GEMFILE'], File.join(STUB_DIR, "#{name}/Gemfile")
130-
FileUtils.cp "#{ENV['BUNDLE_GEMFILE']}.lock", File.join(STUB_DIR, "#{name}/Gemfile.lock")
131-
Dir.chdir File.join(STUB_DIR, name.to_s)
131+
expect(rack_factory.getApplication).to be_a(DefaultRackApplication)
132+
end
132133
end
133134

134-
def base_path; "#{STUB_DIR}/rails72" end
135-
136-
it_should_behave_like 'a rails app'
137-
138135
context "initialized" do
139136

137+
before(:all) { copy_gemfile }
138+
140139
before(:all) do
141140
initialize_rails('production', "file://#{base_path}") do |servlet_context, _|
142-
prepare_servlet_context(servlet_context)
141+
prepare_servlet_context(servlet_context, base_path)
143142
end
144143
end
145-
after(:all) { restore_rails }
144+
after(:all) { restore_rails }
146145

147146
it "loaded rack ~> 2.2.0" do
148147
@runtime = @rack_factory.getApplication.getRuntime
@@ -155,18 +154,27 @@ def base_path; "#{STUB_DIR}/rails72" end
155154
should_eval_as_not_nil "defined?(Rails)"
156155
should_eval_as_not_nil "Rails.logger"
157156

158-
# Rails 7.x wraps the default in a ActiveSupport::BroadcastLogger
159-
should_eval_as_eql_to "Rails.logger.is_a? ActiveSupport::BroadcastLogger", true
160-
should_eval_as_eql_to "Rails.logger.broadcasts.size", 1
161-
should_eval_as_eql_to "Rails.logger.broadcasts.first.is_a? JRuby::Rack::Logger", true
162-
# NOTE: TaggedLogging is a module that extends the logger instance:
163-
should_eval_as_eql_to "Rails.logger.broadcasts.first.is_a? ActiveSupport::TaggedLogging", true
164-
165157
# production.rb: config.log_level = 'info'
166158
should_eval_as_eql_to "Rails.logger.level", Logger::INFO
167-
should_eval_as_eql_to "Rails.logger.broadcasts.first.level", Logger::INFO
168159

169-
unwrap_logger = "logger = Rails.logger.broadcasts.first;"
160+
# Rails 7.1+ wraps the default in a ActiveSupport::BroadcastLogger
161+
if Rails::VERSION::STRING < '7.1'
162+
should_eval_as_eql_to "Rails.logger.is_a? JRuby::Rack::Logger", true
163+
should_eval_as_eql_to "Rails.logger.is_a? ActiveSupport::TaggedLogging", true
164+
unwrap_logger = "logger = Rails.logger;"
165+
else
166+
should_eval_as_not_nil "defined?(ActiveSupport::BroadcastLogger)"
167+
should_eval_as_eql_to "Rails.logger.is_a? ActiveSupport::BroadcastLogger", true
168+
should_eval_as_eql_to "Rails.logger.broadcasts.size", 1
169+
should_eval_as_eql_to "Rails.logger.broadcasts.first.is_a? JRuby::Rack::Logger", true
170+
# NOTE: TaggedLogging is a module that extends the logger instance:
171+
should_eval_as_eql_to "Rails.logger.broadcasts.first.is_a? ActiveSupport::TaggedLogging", true
172+
173+
should_eval_as_eql_to "Rails.logger.broadcasts.first.level", Logger::INFO
174+
175+
unwrap_logger = "logger = Rails.logger.broadcasts.first;"
176+
end
177+
170178
# sanity check logger-silence works:
171179
should_eval_as_eql_to "#{unwrap_logger} logger.silence { logger.warn('from-integration-spec') }", true
172180

@@ -178,9 +186,41 @@ def base_path; "#{STUB_DIR}/rails72" end
178186
should_eval_as_eql_to "Rails.public_path.to_s", "#{base_path}/public"
179187
end
180188

189+
it "disables rack's chunked support (by default)" do
190+
@runtime = @rack_factory.getApplication.getRuntime
191+
expect_to_have_monkey_patched_chunked
192+
end
181193
end
182194
end
183195

196+
describe 'rails 5.0', lib: :rails50 do
197+
it_should_behave_like 'a rails app'
198+
end
199+
200+
describe 'rails 5.2', lib: :rails52 do
201+
it_should_behave_like 'a rails app'
202+
end
203+
204+
describe 'rails 6.0', lib: :rails60 do
205+
it_should_behave_like 'a rails app'
206+
end
207+
208+
describe 'rails 6.1', lib: :rails61 do
209+
it_should_behave_like 'a rails app'
210+
end
211+
212+
describe 'rails 7.0', lib: :rails70 do
213+
it_should_behave_like 'a rails app'
214+
end
215+
216+
describe 'rails 7.1', lib: :rails71 do
217+
it_should_behave_like 'a rails app'
218+
end
219+
220+
describe 'rails 7.2', lib: :rails72 do
221+
it_should_behave_like 'a rails app'
222+
end
223+
184224
def expect_to_have_monkey_patched_chunked
185225
@runtime.evalScriptlet "require 'rack/chunked'"
186226
script = %{
@@ -215,11 +255,11 @@ def initialize_rails(env = nil, servlet_context = @servlet_context)
215255

216256
def new_servlet_context(base_path = nil)
217257
servlet_context = org.jruby.rack.mock.RackLoggingMockServletContext.new base_path
218-
servlet_context.logger = raise_logger(:WARN).tap { |logger| logger.setEnabled(false) }
258+
servlet_context.logger = raise_logger('WARN').tap { |logger| logger.setEnabled(false) }
219259
servlet_context
220260
end
221261

222-
def prepare_servlet_context(servlet_context)
262+
def prepare_servlet_context(servlet_context, base_path)
223263
set_compat_version servlet_context
224264
servlet_context.addInitParameter('rails.root', base_path)
225265
servlet_context.addInitParameter('jruby.rack.layout_class', 'FileSystemLayout')
@@ -232,10 +272,12 @@ def set_compat_version(servlet_context = @servlet_context); require 'jruby'
232272

233273
GEMFILES_DIR = File.expand_path('../../../gemfiles', STUB_DIR)
234274

235-
def copy_gemfile(name)
236-
# e.g. 'rails30'
237-
FileUtils.cp File.join(GEMFILES_DIR, "#{name}.gemfile"), File.join(STUB_DIR, "#{name}/WEB-INF/Gemfile")
238-
FileUtils.cp File.join(GEMFILES_DIR, "#{name}.gemfile.lock"), File.join(STUB_DIR, "#{name}/WEB-INF/Gemfile.lock")
275+
def copy_gemfile
276+
name = CURRENT_LIB.to_s
277+
raise "Environment variable BUNDLE_GEMFILE seems to not contain #{name}" unless ENV['BUNDLE_GEMFILE']&.include?(name)
278+
FileUtils.cp ENV['BUNDLE_GEMFILE'], File.join(STUB_DIR, "#{name}/Gemfile")
279+
FileUtils.cp "#{ENV['BUNDLE_GEMFILE']}.lock", File.join(STUB_DIR, "#{name}/Gemfile.lock")
280+
Dir.chdir File.join(STUB_DIR, name)
239281
end
240282

241283
ENV_COPY = ENV.to_h
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class ApplicationController < ActionController::Base
2+
protect_from_forgery with: :exception
3+
end
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module ApplicationHelper
2+
end
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class ApplicationJob < ActiveJob::Base
2+
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
require_relative 'boot'
2+
3+
require "rails"
4+
# Pick the frameworks you want:
5+
require "active_model/railtie"
6+
require "active_job/railtie"
7+
# require "active_record/railtie"
8+
require "action_controller/railtie"
9+
# require "action_mailer/railtie"
10+
require "action_view/railtie"
11+
# require "action_cable/engine"
12+
# require "sprockets/railtie"
13+
# require "rails/test_unit/railtie"
14+
15+
# Require the gems listed in Gemfile, including any gems
16+
# you've limited to :test, :development, or :production.
17+
Bundler.require(*Rails.groups)
18+
19+
module Rails50
20+
class Application < Rails::Application
21+
# Settings in config/environments/* take precedence over those specified here.
22+
# Application configuration should go into files in config/initializers
23+
# -- all .rb files in that directory are automatically loaded.
24+
end
25+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
2+
3+
require 'bundler/setup' # Set up gems listed in the Gemfile.
4+
5+
# workaround for https://github.com/ruby-concurrency/concurrent-ruby/issues/1077 since https://github.com/rails/rails/pull/54264 wont be backported earlier than 7.1.
6+
require "logger"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Load the Rails application.
2+
require_relative 'application'
3+
4+
# Initialize the Rails application.
5+
Rails.application.initialize!
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
Rails.application.configure do
2+
# Settings specified here will take precedence over those in config/application.rb.
3+
4+
# In the development environment your application's code is reloaded on
5+
# every request. This slows down response time but is perfect for development
6+
# since you don't have to restart the web server when you make code changes.
7+
config.cache_classes = false
8+
9+
# Do not eager load code on boot.
10+
config.eager_load = false
11+
12+
# Show full error reports.
13+
config.consider_all_requests_local = true
14+
15+
# Enable/disable caching. By default caching is disabled.
16+
if Rails.root.join('tmp/caching-dev.txt').exist?
17+
config.action_controller.perform_caching = true
18+
19+
config.cache_store = :memory_store
20+
config.public_file_server.headers = {
21+
'Cache-Control' => 'public, max-age=172800'
22+
}
23+
else
24+
config.action_controller.perform_caching = false
25+
26+
config.cache_store = :null_store
27+
end
28+
29+
# Print deprecation notices to the Rails logger.
30+
config.active_support.deprecation = :log
31+
32+
33+
# Raises error for missing translations
34+
# config.action_view.raise_on_missing_translations = true
35+
36+
# Use an evented file watcher to asynchronously detect changes in source code,
37+
# routes, locales, etc. This feature depends on the listen gem.
38+
# config.file_watcher = ActiveSupport::EventedFileUpdateChecker
39+
end
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
Rails.application.configure do
2+
# Settings specified here will take precedence over those in config/application.rb.
3+
4+
# Code is not reloaded between requests.
5+
config.cache_classes = true
6+
7+
# Eager load code on boot. This eager loads most of Rails and
8+
# your application in memory, allowing both threaded web servers
9+
# and those relying on copy on write to perform better.
10+
# Rake tasks automatically ignore this option for performance.
11+
config.eager_load = true
12+
13+
# Full error reports are disabled and caching is turned on.
14+
config.consider_all_requests_local = false
15+
config.action_controller.perform_caching = true
16+
17+
# Disable serving static files from the `/public` folder by default since
18+
# Apache or NGINX already handles this.
19+
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
20+
21+
22+
# Enable serving of images, stylesheets, and JavaScripts from an asset server.
23+
# config.action_controller.asset_host = 'http://assets.example.com'
24+
25+
# Specifies the header that your server uses for sending files.
26+
# config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache
27+
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX
28+
29+
30+
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
31+
# config.force_ssl = true
32+
33+
# Use the lowest log level to ensure availability of diagnostic information
34+
# when problems arise.
35+
config.log_level = :info
36+
37+
# Prepend all log lines with the following tags.
38+
config.log_tags = [ :request_id ]
39+
40+
# Use a different cache store in production.
41+
# config.cache_store = :mem_cache_store
42+
43+
# Use a real queuing backend for Active Job (and separate queues per environment)
44+
# config.active_job.queue_adapter = :resque
45+
# config.active_job.queue_name_prefix = "rails50_#{Rails.env}"
46+
47+
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
48+
# the I18n.default_locale when a translation cannot be found).
49+
config.i18n.fallbacks = true
50+
51+
# Send deprecation notices to registered listeners.
52+
config.active_support.deprecation = :notify
53+
54+
# Use default logging formatter so that PID and timestamp are not suppressed.
55+
config.log_formatter = ::Logger::Formatter.new
56+
57+
# Use a different logger for distributed setups.
58+
# require 'syslog/logger'
59+
# config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name')
60+
61+
if ENV["RAILS_LOG_TO_STDOUT"].present?
62+
logger = ActiveSupport::Logger.new(STDOUT)
63+
logger.formatter = config.log_formatter
64+
config.logger = ActiveSupport::TaggedLogging.new(logger)
65+
end
66+
end

0 commit comments

Comments
 (0)