Skip to content

Commit b9cb81c

Browse files
committed
[test] a sample rails 7.2 app for integration testing
1 parent 8b4a130 commit b9cb81c

File tree

25 files changed

+417
-7
lines changed

25 files changed

+417
-7
lines changed

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

Lines changed: 74 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@
8585

8686
shared_examples_for 'a rails app', :shared => true do
8787

88-
let(:servlet_context) { new_servlet_context(base_path) }
88+
let(:servlet_context) do
89+
new_servlet_context(base_path).tap { |servlet_context| prepare_servlet_context(servlet_context) }
90+
end
8991

9092
it "initializes pooling when min/max set" do
9193
servlet_context.addInitParameter('jruby.min.runtimes', '1')
@@ -127,10 +129,66 @@
127129
rack_factory.should be_a(RackApplicationFactory)
128130
rack_factory.should be_a(SharedRackApplicationFactory)
129131
rack_factory.realFactory.should be_a(RailsRackApplicationFactory)
132+
end
130133

131-
rack_factory.getApplication.should be_a(DefaultRackApplication)
134+
end
135+
136+
describe 'rails 7.2', lib: :rails72 do
137+
138+
before(:all) do name = :rails72 # copy_gemfile :
139+
FileUtils.cp File.join(GEMFILES_DIR, "#{name}.gemfile"), File.join(STUB_DIR, "#{name}/Gemfile")
140+
FileUtils.cp File.join(GEMFILES_DIR, "#{name}.gemfile.lock"), File.join(STUB_DIR, "#{name}/Gemfile.lock")
141+
Dir.chdir File.join(STUB_DIR, name.to_s)
132142
end
133143

144+
def base_path; "#{STUB_DIR}/rails72" end
145+
146+
it_should_behave_like 'a rails app'
147+
148+
context "initialized" do
149+
150+
before(:all) do
151+
initialize_rails('production', "file://#{base_path}") do |servlet_context, _|
152+
prepare_servlet_context(servlet_context)
153+
end
154+
end
155+
after(:all) { restore_rails }
156+
157+
it "loaded rack ~> 2.2" do
158+
@runtime = @rack_factory.getApplication.getRuntime
159+
should_eval_as_not_nil "defined?(Rack.release)"
160+
should_eval_as_eql_to "Rack.release.to_s[0, 3]", '2.2'
161+
end
162+
163+
it "booted with a servlet logger" do
164+
@runtime = @rack_factory.getApplication.getRuntime
165+
should_eval_as_not_nil "defined?(Rails)"
166+
should_eval_as_not_nil "Rails.logger"
167+
168+
# Rails 7.x wraps the default in a ActiveSupport::BroadcastLogger
169+
should_eval_as_eql_to "Rails.logger.is_a? ActiveSupport::BroadcastLogger", true
170+
should_eval_as_eql_to "Rails.logger.broadcasts.size", 1
171+
should_eval_as_eql_to "Rails.logger.broadcasts.first.is_a? JRuby::Rack::Logger", true
172+
# NOTE: TaggedLogging is a module that extends the logger instance:
173+
should_eval_as_eql_to "Rails.logger.broadcasts.first.is_a? ActiveSupport::TaggedLogging", true
174+
175+
# production.rb: config.log_level = 'info'
176+
should_eval_as_eql_to "Rails.logger.level", Logger::INFO
177+
should_eval_as_eql_to "Rails.logger.broadcasts.first.level", Logger::INFO
178+
179+
unwrap_logger = "logger = Rails.logger.broadcasts.first;"
180+
# sanity check logger-silence works:
181+
should_eval_as_eql_to "#{unwrap_logger} logger.silence { logger.warn('from-integration-spec') }", true
182+
183+
should_eval_as_eql_to "#{unwrap_logger} logger.real_logger.is_a?(org.jruby.rack.logging.ServletContextLogger)", true
184+
end
185+
186+
it "sets up public_path" do
187+
@runtime = @rack_factory.getApplication.getRuntime
188+
should_eval_as_eql_to "Rails.public_path.to_s", "#{base_path}/public"
189+
end
190+
191+
end
134192
end
135193

136194
def expect_to_have_monkey_patched_chunked
@@ -146,8 +204,6 @@ def expect_to_have_monkey_patched_chunked
146204
should_eval_as_eql_to script, "1\nsecond"
147205
end
148206

149-
ENV_COPY = ENV.to_h
150-
151207
def initialize_rails(env = nil, servlet_context = @servlet_context)
152208
if ! servlet_context || servlet_context.is_a?(String)
153209
base = servlet_context.is_a?(String) ? servlet_context : nil
@@ -160,19 +216,23 @@ def initialize_rails(env = nil, servlet_context = @servlet_context)
160216
servlet_context.addInitParameter("jruby.runtime.env", the_env)
161217

162218
yield(servlet_context, listener) if block_given?
219+
163220
listener.contextInitialized javax.servlet.ServletContextEvent.new(servlet_context)
164221
@rack_context = servlet_context.getAttribute("rack.context")
165222
@rack_factory = servlet_context.getAttribute("rack.factory")
166-
@servlet_context ||= servlet_context
223+
@servlet_context = servlet_context
167224
end
168225

169226
def new_servlet_context(base_path = nil)
170227
servlet_context = org.jruby.rack.mock.RackLoggingMockServletContext.new base_path
171-
servlet_context.logger = raise_logger
228+
servlet_context.logger = raise_logger(:WARN).tap { |logger| logger.setEnabled(false) }
172229
servlet_context
173230
end
174231

175-
private
232+
def prepare_servlet_context(servlet_context)
233+
servlet_context.addInitParameter('rails.root', base_path)
234+
servlet_context.addInitParameter('jruby.rack.layout_class', 'FileSystemLayout')
235+
end
176236

177237
GEMFILES_DIR = File.expand_path('../../../gemfiles', STUB_DIR)
178238

@@ -181,4 +241,11 @@ def copy_gemfile(name) # e.g. 'rails30'
181241
FileUtils.cp File.join(GEMFILES_DIR, "#{name}.gemfile.lock"), File.join(STUB_DIR, "#{name}/WEB-INF/Gemfile.lock")
182242
end
183243

244+
ENV_COPY = ENV.to_h
245+
246+
def restore_rails
247+
ENV['RACK_ENV'] = ENV_COPY['RACK_ENV'] if ENV.key?('RACK_ENV')
248+
ENV['RAILS_ENV'] = ENV_COPY['RAILS_ENV'] if ENV.key?('RAILS_ENV')
249+
end
250+
184251
end

src/spec/stub/rails72/Rakefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Add your own tasks in files placed in lib/tasks ending in .rake,
2+
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3+
4+
require_relative "config/application"
5+
6+
Rails.application.load_tasks
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class ApplicationController < ActionController::API
2+
end

src/spec/stub/rails72/app/controllers/concerns/.keep

Whitespace-only changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class SampleController < ApplicationController
2+
def index
3+
end
4+
end
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module SampleHelper
2+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# class ApplicationRecord < ActiveRecord::Base
2+
# primary_abstract_class
3+
# end
4+
5+
class ApplicationRecord
6+
end

src/spec/stub/rails72/app/models/concerns/.keep

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<h1>Sample#index</h1>
2+
<p>Find me in app/views/sample/index.html.erb</p>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 "active_storage/engine"
9+
require "action_controller/railtie"
10+
#require "action_mailer/railtie"
11+
#require "action_mailbox/engine"
12+
#require "action_text/engine"
13+
require "action_view/railtie"
14+
#require "action_cable/engine"
15+
#require "rails/test_unit/railtie"
16+
17+
# Require the gems listed in Gemfile, including any gems
18+
# you've limited to :test, :development, or :production.
19+
Bundler.require(*Rails.groups)
20+
21+
module Rails72
22+
class Application < Rails::Application
23+
# Initialize configuration defaults for originally generated Rails version.
24+
config.load_defaults 7.2
25+
26+
# Please, add to the `ignore` list any other `lib` subdirectories that do
27+
# not contain `.rb` files, or that should not be reloaded or eager loaded.
28+
# Common ones are `templates`, `generators`, or `middleware`, for example.
29+
config.autoload_lib(ignore: %w[assets tasks])
30+
31+
# Configuration for the application, engines, and railties goes here.
32+
#
33+
# These settings can be overridden in specific environments using the files
34+
# in config/environments, which are processed later.
35+
#
36+
# config.time_zone = "Central Time (US & Canada)"
37+
# config.eager_load_paths << Rails.root.join("extras")
38+
39+
# Only loads a smaller set of middleware suitable for API only apps.
40+
# Middleware like session, flash, cookies can be added back manually.
41+
# Skip views, helpers and assets when generating a new resource.
42+
#config.api_only = true
43+
end
44+
end

0 commit comments

Comments
 (0)