Skip to content

Commit c84aade

Browse files
committed
[test] Generate Rails stub tests for 5.0 through 8.0
1 parent 0ec2cc4 commit c84aade

File tree

157 files changed

+2838
-66
lines changed

Some content is hidden

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

157 files changed

+2838
-66
lines changed

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

Lines changed: 106 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -82,77 +82,73 @@
8282

8383
end
8484

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

94+
base_path = "file://#{STUB_DIR}/#{CURRENT_LIB.to_s}"
95+
8796
let(:servlet_context) do
88-
new_servlet_context(base_path).tap { |servlet_context| prepare_servlet_context(servlet_context) }
97+
new_servlet_context(base_path).tap { |servlet_context| prepare_servlet_context(servlet_context, base_path) }
8998
end
9099

91-
it "initializes pooling when min/max set" do
92-
servlet_context.addInitParameter('jruby.min.runtimes', '1')
93-
servlet_context.addInitParameter('jruby.max.runtimes', '2')
100+
context "runtime" do
94101

95-
listener = org.jruby.rack.rails.RailsServletContextListener.new
96-
listener.contextInitialized javax.servlet.ServletContextEvent.new(servlet_context)
102+
it "initializes pooling when min/max set" do
103+
servlet_context.addInitParameter('jruby.min.runtimes', '1')
104+
servlet_context.addInitParameter('jruby.max.runtimes', '2')
97105

98-
rack_factory = servlet_context.getAttribute("rack.factory")
99-
expect(rack_factory).to be_a(RackApplicationFactory)
100-
expect(rack_factory).to be_a(PoolingRackApplicationFactory)
101-
expect(rack_factory.realFactory).to be_a(RailsRackApplicationFactory)
106+
listener = org.jruby.rack.rails.RailsServletContextListener.new
107+
listener.contextInitialized javax.servlet.ServletContextEvent.new(servlet_context)
102108

103-
expect(servlet_context.getAttribute("rack.context")).to be_a(RackContext)
104-
expect(servlet_context.getAttribute("rack.context")).to be_a(ServletRackContext)
109+
rack_factory = servlet_context.getAttribute("rack.factory")
110+
expect(rack_factory).to be_a(RackApplicationFactory)
111+
expect(rack_factory).to be_a(PoolingRackApplicationFactory)
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 shared (thread-safe) by default" do
110-
listener = org.jruby.rack.rails.RailsServletContextListener.new
111-
listener.contextInitialized javax.servlet.ServletContextEvent.new(servlet_context)
112-
113-
rack_factory = servlet_context.getAttribute("rack.factory")
114-
expect(rack_factory).to be_a(RackApplicationFactory)
115-
expect(rack_factory).to be_a(SharedRackApplicationFactory)
116-
expect(rack_factory.realFactory).to be_a(RailsRackApplicationFactory)
117-
118-
expect(rack_factory.getApplication).to be_a(DefaultRackApplication)
119-
end
120-
121-
it "initializes shared (thread-safe) whem max runtimes is 1" do
122-
servlet_context.addInitParameter('jruby.max.runtimes', '1')
117+
expect(rack_factory.getApplication).to be_a(DefaultRackApplication)
118+
end
123119

124-
listener = org.jruby.rack.rails.RailsServletContextListener.new
125-
listener.contextInitialized javax.servlet.ServletContextEvent.new(servlet_context)
120+
it "initializes shared (thread-safe) by default" do
121+
listener = org.jruby.rack.rails.RailsServletContextListener.new
122+
listener.contextInitialized javax.servlet.ServletContextEvent.new(servlet_context)
126123

127-
rack_factory = servlet_context.getAttribute("rack.factory")
128-
expect(rack_factory).to be_a(RackApplicationFactory)
129-
expect(rack_factory).to be_a(SharedRackApplicationFactory)
130-
expect(rack_factory.realFactory).to be_a(RailsRackApplicationFactory)
131-
end
124+
rack_factory = servlet_context.getAttribute("rack.factory")
125+
expect(rack_factory).to be_a(RackApplicationFactory)
126+
expect(rack_factory).to be_a(SharedRackApplicationFactory)
127+
expect(rack_factory.realFactory).to be_a(RailsRackApplicationFactory)
132128

133-
end
129+
expect(rack_factory.getApplication).to be_a(DefaultRackApplication)
130+
end
134131

135-
describe 'rails 7.2', lib: :rails72 do
132+
it "initializes shared (thread-safe) whem max runtimes is 1" do
133+
servlet_context.addInitParameter('jruby.max.runtimes', '1')
136134

137-
before(:all) do
138-
name = :rails72 # copy_gemfile :
139-
raise "Environment variable BUNDLE_GEMFILE seems to not contain #{name.to_s}" unless ENV['BUNDLE_GEMFILE']&.include?(name.to_s)
140-
FileUtils.cp ENV['BUNDLE_GEMFILE'], File.join(STUB_DIR, "#{name}/Gemfile")
141-
FileUtils.cp "#{ENV['BUNDLE_GEMFILE']}.lock", File.join(STUB_DIR, "#{name}/Gemfile.lock")
142-
Dir.chdir File.join(STUB_DIR, name.to_s)
143-
end
135+
listener = org.jruby.rack.rails.RailsServletContextListener.new
136+
listener.contextInitialized javax.servlet.ServletContextEvent.new(servlet_context)
144137

145-
def base_path
146-
"#{STUB_DIR}/rails72"
138+
rack_factory = servlet_context.getAttribute("rack.factory")
139+
expect(rack_factory).to be_a(RackApplicationFactory)
140+
expect(rack_factory).to be_a(SharedRackApplicationFactory)
141+
expect(rack_factory.realFactory).to be_a(RailsRackApplicationFactory)
142+
end
147143
end
148144

149-
it_should_behave_like 'a rails app'
150-
151145
context "initialized" do
152146

147+
before(:all) { copy_gemfile }
148+
153149
before(:all) do
154150
initialize_rails('production', "file://#{base_path}") do |servlet_context, _|
155-
prepare_servlet_context(servlet_context)
151+
prepare_servlet_context(servlet_context, base_path)
156152
end
157153
end
158154
after(:all) { restore_rails }
@@ -168,18 +164,27 @@ def base_path
168164
should_eval_as_not_nil "defined?(Rails)"
169165
should_eval_as_not_nil "Rails.logger"
170166

171-
# Rails 7.x wraps the default in a ActiveSupport::BroadcastLogger
172-
should_eval_as_eql_to "Rails.logger.is_a? ActiveSupport::BroadcastLogger", true
173-
should_eval_as_eql_to "Rails.logger.broadcasts.size", 1
174-
should_eval_as_eql_to "Rails.logger.broadcasts.first.is_a? JRuby::Rack::Logger", true
175-
# NOTE: TaggedLogging is a module that extends the logger instance:
176-
should_eval_as_eql_to "Rails.logger.broadcasts.first.is_a? ActiveSupport::TaggedLogging", true
177-
178167
# production.rb: config.log_level = 'info'
179168
should_eval_as_eql_to "Rails.logger.level", Logger::INFO
180-
should_eval_as_eql_to "Rails.logger.broadcasts.first.level", Logger::INFO
181169

182-
unwrap_logger = "logger = Rails.logger.broadcasts.first;"
170+
# Rails 7.1+ wraps the default in a ActiveSupport::BroadcastLogger
171+
if Rails::VERSION::STRING < '7.1'
172+
should_eval_as_eql_to "Rails.logger.is_a? JRuby::Rack::Logger", true
173+
should_eval_as_eql_to "Rails.logger.is_a? ActiveSupport::TaggedLogging", true
174+
unwrap_logger = "logger = Rails.logger;"
175+
else
176+
should_eval_as_not_nil "defined?(ActiveSupport::BroadcastLogger)"
177+
should_eval_as_eql_to "Rails.logger.is_a? ActiveSupport::BroadcastLogger", true
178+
should_eval_as_eql_to "Rails.logger.broadcasts.size", 1
179+
should_eval_as_eql_to "Rails.logger.broadcasts.first.is_a? JRuby::Rack::Logger", true
180+
# NOTE: TaggedLogging is a module that extends the logger instance:
181+
should_eval_as_eql_to "Rails.logger.broadcasts.first.is_a? ActiveSupport::TaggedLogging", true
182+
183+
should_eval_as_eql_to "Rails.logger.broadcasts.first.level", Logger::INFO
184+
185+
unwrap_logger = "logger = Rails.logger.broadcasts.first;"
186+
end
187+
183188
# sanity check logger-silence works:
184189
should_eval_as_eql_to "#{unwrap_logger} logger.silence { logger.warn('from-integration-spec') }", true
185190

@@ -191,9 +196,45 @@ def base_path
191196
should_eval_as_eql_to "Rails.public_path.to_s", "#{base_path}/public"
192197
end
193198

199+
it "disables rack's chunked support (by default)" do
200+
@runtime = @rack_factory.getApplication.getRuntime
201+
expect_to_have_monkey_patched_chunked
202+
end
194203
end
195204
end
196205

206+
describe 'rails 5.0', lib: :rails50 do
207+
it_should_behave_like 'a rails app'
208+
end
209+
210+
describe 'rails 5.2', lib: :rails52 do
211+
it_should_behave_like 'a rails app'
212+
end
213+
214+
describe 'rails 6.0', lib: :rails60 do
215+
it_should_behave_like 'a rails app'
216+
end
217+
218+
describe 'rails 6.1', lib: :rails61 do
219+
it_should_behave_like 'a rails app'
220+
end
221+
222+
describe 'rails 7.0', lib: :rails70 do
223+
it_should_behave_like 'a rails app'
224+
end
225+
226+
describe 'rails 7.1', lib: :rails71 do
227+
it_should_behave_like 'a rails app'
228+
end
229+
230+
describe 'rails 7.2', lib: :rails72 do
231+
it_should_behave_like 'a rails app'
232+
end
233+
234+
describe 'rails 8.0', lib: :rails80 do
235+
it_should_behave_like 'a rails app'
236+
end
237+
197238
def expect_to_have_monkey_patched_chunked
198239
@runtime.evalScriptlet "require 'rack/chunked'"
199240
script = %{
@@ -232,17 +273,19 @@ def new_servlet_context(base_path = nil)
232273
servlet_context
233274
end
234275

235-
def prepare_servlet_context(servlet_context)
276+
def prepare_servlet_context(servlet_context, base_path)
236277
servlet_context.addInitParameter('rails.root', base_path)
237278
servlet_context.addInitParameter('jruby.rack.layout_class', 'FileSystemLayout')
238279
end
239280

240281
GEMFILES_DIR = File.expand_path('../../../gemfiles', STUB_DIR)
241282

242-
def copy_gemfile(name)
243-
# e.g. 'rails30'
244-
FileUtils.cp File.join(GEMFILES_DIR, "#{name}.gemfile"), File.join(STUB_DIR, "#{name}/WEB-INF/Gemfile")
245-
FileUtils.cp File.join(GEMFILES_DIR, "#{name}.gemfile.lock"), File.join(STUB_DIR, "#{name}/WEB-INF/Gemfile.lock")
283+
def copy_gemfile
284+
name = CURRENT_LIB.to_s
285+
raise "Environment variable BUNDLE_GEMFILE seems to not contain #{name}" unless ENV['BUNDLE_GEMFILE']&.include?(name)
286+
FileUtils.cp ENV['BUNDLE_GEMFILE'], File.join(STUB_DIR, "#{name}/Gemfile")
287+
FileUtils.cp "#{ENV['BUNDLE_GEMFILE']}.lock", File.join(STUB_DIR, "#{name}/Gemfile.lock")
288+
Dir.chdir File.join(STUB_DIR, name)
246289
end
247290

248291
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)