Skip to content

Commit 559d00f

Browse files
committed
Refactor railties tests to not rely on generic route
Ref: rails#53964 Having a generic route automatically defined make it hard to remove that deprecated feature.
1 parent b78637e commit 559d00f

File tree

5 files changed

+70
-37
lines changed

5 files changed

+70
-37
lines changed

railties/test/application/configuration_test.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4090,7 +4090,6 @@ class Post < ActiveRecord::Base
40904090
output = rails("routes", "-g", "active_storage")
40914091
assert_equal <<~MESSAGE, output
40924092
Prefix Verb URI Pattern Controller#Action
4093-
/:controller(/:action(/:id))(.:format) :controller#:action
40944093
rails_service_blob GET /files/blobs/redirect/:signed_id/*filename(.:format) active_storage/blobs/redirect#show
40954094
rails_service_blob_proxy GET /files/blobs/proxy/:signed_id/*filename(.:format) active_storage/blobs/proxy#show
40964095
GET /files/blobs/:signed_id/*filename(.:format) active_storage/blobs/redirect#show

railties/test/application/middleware/exceptions_test.rb

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ def teardown
1717
end
1818

1919
test "show exceptions middleware filter backtrace before logging" do
20-
controller :foo, <<-RUBY
20+
routes <<~'RUBY'
21+
get "/foo" => "foo#index"
22+
RUBY
23+
controller :foo, <<~'RUBY'
2124
class FooController < ActionController::Base
2225
def index
2326
raise 'oops'
@@ -35,7 +38,10 @@ def index
3538
end
3639

3740
test "renders active record exceptions as 404" do
38-
controller :foo, <<-RUBY
41+
routes <<~'RUBY'
42+
get "/foo" => "foo#index"
43+
RUBY
44+
controller :foo, <<~'RUBY'
3945
class FooController < ActionController::Base
4046
def index
4147
raise ActiveRecord::RecordNotFound
@@ -53,10 +59,7 @@ def index
5359
end
5460

5561
test "renders unknown http methods as 405 when routes are used as the custom exceptions app" do
56-
app_file "config/routes.rb", <<-RUBY
57-
Rails.application.routes.draw do
58-
end
59-
RUBY
62+
routes ""
6063

6164
add_to_config "config.exceptions_app = self.routes"
6265

@@ -67,7 +70,7 @@ def index
6770
end
6871

6972
test "renders unknown http formats as 406 when routes are used as the custom exceptions app" do
70-
controller :foo, <<-RUBY
73+
controller :foo, <<~'RUBY'
7174
class FooController < ActionController::Base
7275
def index
7376
render plain: "rendering index!"
@@ -79,12 +82,10 @@ def not_acceptable
7982
end
8083
RUBY
8184

82-
app_file "config/routes.rb", <<-RUBY
83-
Rails.application.routes.draw do
84-
get "/foo", to: "foo#index"
85-
post "/foo", to: "foo#index"
86-
match "/406", to: "foo#not_acceptable", via: :all
87-
end
85+
routes <<~'RUBY'
86+
get "/foo", to: "foo#index"
87+
post "/foo", to: "foo#index"
88+
match "/406", to: "foo#not_acceptable", via: :all
8889
RUBY
8990

9091
add_to_config "config.exceptions_app = self.routes"
@@ -109,7 +110,7 @@ def not_acceptable
109110
end
110111

111112
test "uses custom exceptions app" do
112-
add_to_config <<-RUBY
113+
add_to_config <<~'RUBY'
113114
config.exceptions_app = lambda do |env|
114115
[404, { "Content-Type" => "text/plain" }, ["YOU FAILED"]]
115116
end
@@ -123,7 +124,10 @@ def not_acceptable
123124
end
124125

125126
test "URL generation error when action_dispatch.show_exceptions is set raises an exception" do
126-
controller :foo, <<-RUBY
127+
routes <<~'RUBY'
128+
get "/foo" => "foo#index"
129+
RUBY
130+
controller :foo, <<~'RUBY'
127131
class FooController < ActionController::Base
128132
def index
129133
raise ActionController::UrlGenerationError
@@ -141,7 +145,7 @@ def index
141145
app.config.action_dispatch.show_exceptions = :none
142146

143147
assert_raise(ActionController::RoutingError) do
144-
get("/foo", {}, "HTTPS" => "on")
148+
get("/does-not-exist", {}, "HTTPS" => "on")
145149
end
146150
end
147151

@@ -165,10 +169,8 @@ def index
165169
end
166170

167171
test "routing to a nonexistent controller when action_dispatch.show_exceptions and consider_all_requests_local are set shows diagnostics" do
168-
app_file "config/routes.rb", <<-RUBY
169-
Rails.application.routes.draw do
170-
resources :articles
171-
end
172+
routes <<~'RUBY'
173+
resources :articles
172174
RUBY
173175

174176
app.config.action_dispatch.show_exceptions = :all
@@ -179,7 +181,10 @@ def index
179181
end
180182

181183
test "displays diagnostics message when exception raised in template that contains UTF-8" do
182-
controller :foo, <<-RUBY
184+
routes <<~'RUBY'
185+
get "/foo" => "foo#index"
186+
RUBY
187+
controller :foo, <<~'RUBY'
183188
class FooController < ActionController::Base
184189
def index
185190
end
@@ -200,7 +205,11 @@ def index
200205
end
201206

202207
test "displays diagnostics message when malformed query parameters are provided" do
203-
controller :foo, <<-RUBY
208+
routes <<~'RUBY'
209+
get "/foo" => "foo#index"
210+
RUBY
211+
212+
controller :foo, <<~'RUBY'
204213
class FooController < ActionController::Base
205214
def index
206215
end
@@ -216,7 +225,10 @@ def index
216225
end
217226

218227
test "displays diagnostics message when too deep query parameters are provided" do
219-
controller :foo, <<-RUBY
228+
routes <<~'RUBY'
229+
get "/foo" => "foo#index"
230+
RUBY
231+
controller :foo, <<~'RUBY'
220232
class FooController < ActionController::Base
221233
def index
222234
end
@@ -235,7 +247,10 @@ def index
235247
end
236248

237249
test "displays statement invalid template correctly" do
238-
controller :foo, <<-RUBY
250+
routes <<~'RUBY'
251+
get "/foo" => "foo#index"
252+
RUBY
253+
controller :foo, <<~'RUBY'
239254
class FooController < ActionController::Base
240255
def index
241256
raise ActiveRecord::StatementInvalid
@@ -258,7 +273,10 @@ def index
258273
end
259274

260275
test "show_exceptions :rescuable with a rescuable error" do
261-
controller :foo, <<-RUBY
276+
routes <<~'RUBY'
277+
get "/foo" => "foo#index"
278+
RUBY
279+
controller :foo, <<~'RUBY'
262280
class FooController < ActionController::Base
263281
def index
264282
raise AbstractController::ActionNotFound
@@ -273,7 +291,10 @@ def index
273291
end
274292

275293
test "show_exceptions :rescuable with a non-rescuable error" do
276-
controller :foo, <<-RUBY
294+
routes <<~'RUBY'
295+
get "/foo" => "foo#index"
296+
RUBY
297+
controller :foo, <<~'RUBY'
277298
class FooController < ActionController::Base
278299
def index
279300
raise 'oops'

railties/test/application/test_test.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ def test_truth
6666
end
6767

6868
test "integration test" do
69+
routes <<~'RUBY'
70+
get "/posts" => "posts#index"
71+
RUBY
6972
controller "posts", <<-RUBY
7073
class PostsController < ActionController::Base
7174
end

railties/test/isolation/abstract_unit.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,6 @@ def build_app(options = {})
121121
end
122122
end
123123

124-
routes = File.read("#{app_path}/config/routes.rb")
125-
if routes =~ /(\n\s*end\s*)\z/
126-
File.open("#{app_path}/config/routes.rb", "w") do |f|
127-
f.puts $` + "\nActionDispatch.deprecator.silence { match ':controller(/:action(/:id))(.:format)', via: :all }\n" + $1
128-
end
129-
end
130-
131124
File.open("#{app_path}/config/database.yml", "w") do |f|
132125
if options[:multi_db]
133126
f.puts multi_db_database_configs
@@ -483,6 +476,14 @@ def controller(name, contents)
483476
app_file("app/controllers/#{name}_controller.rb", contents)
484477
end
485478

479+
def routes(routes)
480+
app_file("config/routes.rb", <<~RUBY)
481+
Rails.application.routes.draw do
482+
#{routes}
483+
end
484+
RUBY
485+
end
486+
486487
def use_frameworks(arr)
487488
to_remove = [:actionmailer, :activerecord, :activestorage, :activejob, :actionmailbox] - arr
488489

railties/test/railties/engine_test.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ class EngineTest < ActiveSupport::TestCase
1212
def setup
1313
build_app({ multi_db: true })
1414

15+
routes <<~'RUBY'
16+
mount Bukkits::Engine, at: "/"
17+
RUBY
18+
1519
@plugin = engine "bukkits" do |plugin|
1620
plugin.write "lib/bukkits.rb", <<-RUBY
1721
module Bukkits
@@ -326,7 +330,7 @@ class AddFirstNameToUsers < ActiveRecord::Migration::Current
326330
test "can draw routes in app routes from engines" do
327331
@plugin.write "config/routes/testing.rb", <<~RUBY
328332
Rails.application.routes.draw do
329-
get "/testing", to: "test#action", as: :testing
333+
get "/testing", to: "testing#index", as: :testing
330334
end
331335
RUBY
332336

@@ -808,13 +812,18 @@ class Engine < ::Rails::Engine
808812
end
809813
RUBY
810814

815+
routes <<~'RUBY'
816+
mount Bukkits::Engine, at: "/engine"
817+
get "/other" => "other#index"
818+
RUBY
819+
811820
boot_rails
812821

813-
env = Rack::MockRequest.env_for("/")
822+
env = Rack::MockRequest.env_for("/engine")
814823
Bukkits::Engine.call(env)
815824
assert_equal Bukkits::Engine.routes, env["action_dispatch.routes"]
816825

817-
env = Rack::MockRequest.env_for("/")
826+
env = Rack::MockRequest.env_for("/other")
818827
Rails.application.call(env)
819828
assert_equal Rails.application.routes, env["action_dispatch.routes"]
820829
end

0 commit comments

Comments
 (0)