1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
|
require "isolation/abstract_unit"
require "railties/shared_tests"
require 'stringio'
require 'active_support/core_ext/kernel/reporting'
module RailtiesTest
class EngineTest < Test::Unit::TestCase
include ActiveSupport::Testing::Isolation
include SharedTests
def setup
build_app
@plugin = engine "bukkits" do |plugin|
plugin.write "lib/bukkits.rb", <<-RUBY
class Bukkits
class Engine < ::Rails::Engine
railtie_name "bukkits"
end
end
RUBY
plugin.write "lib/another.rb", "class Another; end"
end
end
test "Rails::Engine itself does not respond to config" do
boot_rails
assert !Rails::Engine.respond_to?(:config)
end
test "initializers are executed after application configuration initializers" do
@plugin.write "lib/bukkits.rb", <<-RUBY
class Bukkits
class Engine < ::Rails::Engine
initializer "dummy_initializer" do
end
end
end
RUBY
boot_rails
initializers = Rails.application.initializers.tsort
index = initializers.index { |i| i.name == "dummy_initializer" }
selection = initializers[(index-3)..(index)].map(&:name).map(&:to_s)
assert_equal %w(
load_config_initializers
load_config_initializers
engines_blank_point
dummy_initializer
), selection
assert index < initializers.index { |i| i.name == :build_middleware_stack }
end
class Upcaser
def initialize(app)
@app = app
end
def call(env)
response = @app.call(env)
response[2].upcase!
response
end
end
test "engine is a rack app and can have his own middleware stack" do
@plugin.write "lib/bukkits.rb", <<-RUBY
class Bukkits
class Engine < ::Rails::Engine
endpoint lambda { |env| [200, {'Content-Type' => 'text/html'}, 'Hello World'] }
config.middleware.use ::RailtiesTest::EngineTest::Upcaser
end
end
RUBY
app_file "config/routes.rb", <<-RUBY
AppTemplate::Application.routes.draw do
mount(Bukkits::Engine => "/bukkits")
end
RUBY
boot_rails
env = Rack::MockRequest.env_for("/bukkits")
response = Rails.application.call(env)
assert_equal "HELLO WORLD", response[2]
end
test "it provides routes as default endpoint" do
@plugin.write "lib/bukkits.rb", <<-RUBY
class Bukkits
class Engine < ::Rails::Engine
end
end
RUBY
@plugin.write "config/routes.rb", <<-RUBY
Bukkits::Engine.routes.draw do
match "/foo" => lambda { |env| [200, {'Content-Type' => 'text/html'}, 'foo'] }
end
RUBY
app_file "config/routes.rb", <<-RUBY
Rails.application.routes.draw do
mount(Bukkits::Engine => "/bukkits")
end
RUBY
boot_rails
env = Rack::MockRequest.env_for("/bukkits/foo")
response = Rails.application.call(env)
assert_equal "foo", response[2]
end
test "engine can load its own plugins" do
@plugin.write "lib/bukkits.rb", <<-RUBY
class Bukkits
class Engine < ::Rails::Engine
end
end
RUBY
@plugin.write "vendor/plugins/yaffle/init.rb", <<-RUBY
config.yaffle_loaded = true
RUBY
boot_rails
assert Bukkits::Engine.config.yaffle_loaded
end
test "engine does not load plugins that already exists in application" do
@plugin.write "lib/bukkits.rb", <<-RUBY
class Bukkits
class Engine < ::Rails::Engine
end
end
RUBY
@plugin.write "vendor/plugins/yaffle/init.rb", <<-RUBY
config.engine_yaffle_loaded = true
RUBY
app_file "vendor/plugins/yaffle/init.rb", <<-RUBY
config.app_yaffle_loaded = true
RUBY
warnings = capture(:stderr) { boot_rails }
assert !warnings.empty?
assert !Bukkits::Engine.config.respond_to?(:engine_yaffle_loaded)
assert Rails.application.config.app_yaffle_loaded
end
test "it loads its environment file" do
@plugin.write "lib/bukkits.rb", <<-RUBY
class Bukkits
class Engine < ::Rails::Engine
end
end
RUBY
@plugin.write "config/environments/development.rb", <<-RUBY
Bukkits::Engine.configure do
config.environment_loaded = true
end
RUBY
boot_rails
assert Bukkits::Engine.config.environment_loaded
end
test "it passes router in env" do
@plugin.write "lib/bukkits.rb", <<-RUBY
class Bukkits
class Engine < ::Rails::Engine
endpoint lambda { |env| [200, {'Content-Type' => 'text/html'}, 'hello'] }
end
end
RUBY
boot_rails
env = Rack::MockRequest.env_for("/")
response = Bukkits::Engine.call(env)
assert_equal Bukkits::Engine.routes, env['action_dispatch.routes']
env = Rack::MockRequest.env_for("/")
response = Rails.application.call(env)
assert_equal Rails.application.routes, env['action_dispatch.routes']
end
test "it allows to set asset_path" do
@plugin.write "lib/bukkits.rb", <<-RUBY
class Bukkits
class Engine < ::Rails::Engine
end
end
RUBY
@plugin.write "config/routes.rb", <<-RUBY
Bukkits::Engine.routes.draw do
match "/foo" => "foo#index"
end
RUBY
@plugin.write "app/controllers/foo_controller.rb", <<-RUBY
class FooController < ActionController::Base
def index
render :index
end
end
RUBY
@plugin.write "app/views/foo/index.html.erb", <<-RUBY
<%= compute_public_path("/foo", "") %>
<%= image_path("foo.png") %>
<%= javascript_include_tag("foo") %>
<%= stylesheet_link_tag("foo") %>
RUBY
app_file "app/controllers/bar_controller.rb", <<-RUBY
class BarController < ActionController::Base
def index
render :index
end
end
RUBY
app_file "app/views/bar/index.html.erb", <<-RUBY
<%= compute_public_path("/foo", "") %>
RUBY
add_to_config 'config.asset_path = "/omg%s"'
@plugin.write 'public/touch.txt', <<-RUBY
touch
RUBY
boot_rails
# should set asset_path with engine name by default
assert_equal "/bukkits_engine%s", ::Bukkits::Engine.config.asset_path
::Bukkits::Engine.config.asset_path = "/bukkits%s"
env = Rack::MockRequest.env_for("/foo")
response = Bukkits::Engine.call(env)
stripped_body = response[2].body.split("\n").map(&:strip).join("\n")
expected = "/omg/bukkits/foo\n" +
"/omg/bukkits/images/foo.png\n" +
"<script src=\"/omg/bukkits/javascripts/foo.js\" type=\"text/javascript\"></script>\n" +
"<link href=\"/omg/bukkits/stylesheets/foo.css\" media=\"screen\" rel=\"stylesheet\" type=\"text/css\" />"
assert_equal expected, stripped_body
end
test "default application's asset_path" do
@plugin.write "config/routes.rb", <<-RUBY
Bukkits::Engine.routes.draw do
match "/foo" => "foo#index"
end
RUBY
@plugin.write "app/controllers/foo_controller.rb", <<-RUBY
class FooController < ActionController::Base
def index
end
end
RUBY
@plugin.write "app/views/foo/index.html.erb", <<-RUBY
<%= compute_public_path("/foo", "") %>
RUBY
boot_rails
env = Rack::MockRequest.env_for("/foo")
response = Bukkits::Engine.call(env)
stripped_body = response[2].body.strip
expected = "/bukkits/foo"
assert_equal expected, stripped_body
end
test "engine's files are served via ActionDispatch::Static" do
add_to_config "config.serve_static_assets = true"
@plugin.write "lib/bukkits.rb", <<-RUBY
class Bukkits
class Engine < ::Rails::Engine
engine_name :bukkits
end
end
RUBY
@plugin.write "public/bukkits.html", "/bukkits/bukkits.html"
app_file "public/app.html", "/app.html"
app_file "public/bukkits/file_from_app.html", "/bukkits/file_from_app.html"
boot_rails
env = Rack::MockRequest.env_for("/app.html")
response = Rails.application.call(env)
assert_equal rack_body(response[2]), rack_body(File.open(File.join(app_path, "public/app.html")))
env = Rack::MockRequest.env_for("/bukkits/bukkits.html")
response = Rails.application.call(env)
assert_equal rack_body(response[2]), rack_body(File.open(File.join(@plugin.path, "public/bukkits.html")))
env = Rack::MockRequest.env_for("/bukkits/file_from_app.html")
response = Rails.application.call(env)
assert_equal rack_body(response[2]), rack_body(File.open(File.join(app_path, "public/bukkits/file_from_app.html")))
end
def rack_body(obj)
buffer = ""
obj.each do |part|
buffer << part
end
buffer
end
test "shared engine should include application's helpers and own helpers" do
app_file "config/routes.rb", <<-RUBY
AppTemplate::Application.routes.draw do
match "/foo" => "bukkits/foo#index", :as => "foo"
match "/foo/show" => "bukkits/foo#show"
match "/foo/bar" => "bukkits/foo#bar"
end
RUBY
app_file "app/helpers/some_helper.rb", <<-RUBY
module SomeHelper
def something
"Something... Something... Something..."
end
end
RUBY
@plugin.write "app/helpers/bar_helper.rb", <<-RUBY
module BarHelper
def bar
"It's a bar."
end
end
RUBY
@plugin.write "app/controllers/bukkits/foo_controller.rb", <<-RUBY
class Bukkits::FooController < ActionController::Base
def index
render :inline => "<%= something %>"
end
def show
render :text => foo_path
end
def bar
render :inline => "<%= bar %>"
end
end
RUBY
boot_rails
env = Rack::MockRequest.env_for("/foo")
response = Rails.application.call(env)
assert_equal "Something... Something... Something...", response[2].body
env = Rack::MockRequest.env_for("/foo/show")
response = Rails.application.call(env)
assert_equal "/foo", response[2].body
env = Rack::MockRequest.env_for("/foo/bar")
response = Rails.application.call(env)
assert_equal "It's a bar.", response[2].body
end
test "isolated engine should include only its own routes and helpers" do
@plugin.write "lib/bukkits.rb", <<-RUBY
module Bukkits
class Engine < ::Rails::Engine
namespace Bukkits
end
end
RUBY
@plugin.write "app/models/bukkits/post.rb", <<-RUBY
module Bukkits
class Post
extend ActiveModel::Naming
def to_param
"1"
end
end
end
RUBY
app_file "config/routes.rb", <<-RUBY
AppTemplate::Application.routes.draw do
match "/bar" => "bar#index", :as => "bar"
mount Bukkits::Engine => "/bukkits", :as => "bukkits"
end
RUBY
@plugin.write "config/routes.rb", <<-RUBY
Bukkits::Engine.routes.draw do
match "/foo" => "foo#index", :as => "foo"
match "/foo/show" => "foo#show"
match "/from_app" => "foo#from_app"
match "/routes_helpers_in_view" => "foo#routes_helpers_in_view"
match "/polymorphic_path_without_namespace" => "foo#polymorphic_path_without_namespace"
resources :posts
end
RUBY
app_file "app/helpers/some_helper.rb", <<-RUBY
module SomeHelper
def something
"Something... Something... Something..."
end
end
RUBY
@plugin.write "app/helpers/engine_helper.rb", <<-RUBY
module EngineHelper
def help_the_engine
"Helped."
end
end
RUBY
@plugin.write "app/controllers/bukkits/foo_controller.rb", <<-RUBY
class Bukkits::FooController < ActionController::Base
def index
render :inline => "<%= help_the_engine %>"
end
def show
render :text => foo_path
end
def from_app
render :inline => "<%= (self.respond_to?(:bar_path) || self.respond_to?(:something)) %>"
end
def routes_helpers_in_view
render :inline => "<%= foo_path %>, <%= main_app.bar_path %>"
end
def polymorphic_path_without_namespace
render :text => polymorphic_path(Post.new)
end
end
RUBY
@plugin.write "app/mailers/bukkits/my_mailer.rb", <<-RUBY
module Bukkits
class MyMailer < ActionMailer::Base
end
end
RUBY
add_to_config("config.action_dispatch.show_exceptions = false")
boot_rails
assert_equal "bukkits_", Bukkits.table_name_prefix
assert_equal "bukkits", Bukkits::Engine.engine_name
assert_equal Bukkits._railtie, Bukkits::Engine
assert ::Bukkits::MyMailer.method_defined?(:foo_path)
assert !::Bukkits::MyMailer.method_defined?(:bar_path)
env = Rack::MockRequest.env_for("/bukkits/from_app")
response = AppTemplate::Application.call(env)
assert_equal "false", response[2].body
env = Rack::MockRequest.env_for("/bukkits/foo/show")
response = AppTemplate::Application.call(env)
assert_equal "/bukkits/foo", response[2].body
env = Rack::MockRequest.env_for("/bukkits/foo")
response = AppTemplate::Application.call(env)
assert_equal "Helped.", response[2].body
env = Rack::MockRequest.env_for("/bukkits/routes_helpers_in_view")
response = AppTemplate::Application.call(env)
assert_equal "/bukkits/foo, /bar", response[2].body
env = Rack::MockRequest.env_for("/bukkits/polymorphic_path_without_namespace")
response = AppTemplate::Application.call(env)
assert_equal "/bukkits/posts/1", response[2].body
end
test "isolated engine should avoid namespace in names if that's possible" do
@plugin.write "lib/bukkits.rb", <<-RUBY
module Bukkits
class Engine < ::Rails::Engine
namespace Bukkits
end
end
RUBY
@plugin.write "app/models/bukkits/post.rb", <<-RUBY
module Bukkits
class Post
extend ActiveModel::Naming
include ActiveModel::Conversion
attr_accessor :title
def to_param
"1"
end
def persisted?
false
end
end
end
RUBY
app_file "config/routes.rb", <<-RUBY
AppTemplate::Application.routes.draw do
mount Bukkits::Engine => "/bukkits", :as => "bukkits"
end
RUBY
@plugin.write "config/routes.rb", <<-RUBY
Bukkits::Engine.routes.draw do
resources :posts
end
RUBY
@plugin.write "app/controllers/bukkits/posts_controller.rb", <<-RUBY
class Bukkits::PostsController < ActionController::Base
def new
end
end
RUBY
@plugin.write "app/views/bukkits/posts/new.html.erb", <<-RUBY
<%= form_for(Bukkits::Post.new) do |f| %>
<%= f.text_field :title %>
<% end %>
RUBY
add_to_config("config.action_dispatch.show_exceptions = false")
boot_rails
env = Rack::MockRequest.env_for("/bukkits/posts/new")
response = AppTemplate::Application.call(env)
p rack_body(response[2])
assert rack_body(response[2]) =~ /name="post\[title\]"/
end
test "creating symlinks" do
@plugin.write "lib/bukkits.rb", <<-RUBY
module Bukkits
class Engine < ::Rails::Engine
namespace(Bukkits)
end
end
RUBY
@plugin.write "public/hello.txt", "foo"
@plugin.write "alternate_public/hello.txt", "bar"
Dir.chdir(app_path) do
output = `rake railties:create_symlinks`
assert_match /Created symlink/, output
assert_match /#{app_path}\/public\/bukkits/, output
assert_match /#{@plugin.path}\/public/, output
assert File.symlink?(File.join(app_path, 'public/bukkits'))
assert_equal "foo\n", File.read(File.join(app_path, 'public/bukkits/hello.txt'))
@plugin.write "lib/bukkits.rb", <<-RUBY
module Bukkits
class Engine < ::Rails::Engine
namespace(Bukkits)
config.paths.public = "#{File.join(@plugin.path, "alternate_public")}"
end
end
RUBY
output = `rake railties:create_symlinks`
assert_match /Created symlink/, output
assert_match /#{app_path}\/public\/bukkits/, output
assert_match /#{@plugin.path}\/alternate_public/, output
assert File.symlink?(File.join(app_path, 'public/bukkits'))
assert_equal "bar\n", File.read(File.join(app_path, 'public/bukkits/hello.txt'))
@plugin.write "lib/bukkits.rb", <<-RUBY
module Bukkits
class Engine < ::Rails::Engine
namespace(Bukkits)
config.paths.public = "#{File.join(@plugin.path, "not_existing")}"
end
end
RUBY
FileUtils.rm File.join(app_path, 'public/bukkits')
output = `rake railties:create_symlinks`
assert_no_match /Created symlink/, output
assert !File.exist?(File.join(app_path, 'public/bukkits'))
end
end
end
end
|