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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
|
require "isolation/abstract_unit"
require "railties/shared_tests"
require "stringio"
require "rack/test"
module RailtiesTest
class EngineTest < ActiveSupport::TestCase
include ActiveSupport::Testing::Isolation
include SharedTests
include Rack::Test::Methods
def setup
build_app
@plugin = engine "bukkits" do |plugin|
plugin.write "lib/bukkits.rb", <<-RUBY
module Bukkits
class Engine < ::Rails::Engine
railtie_name "bukkits"
end
end
RUBY
plugin.write "lib/another.rb", "class Another; end"
end
end
def teardown
teardown_app
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
module 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].each { |b| b.upcase! }
response
end
end
test "engine is a rack app and can have his own middleware stack" do
add_to_config("config.action_dispatch.show_exceptions = false")
@plugin.write "lib/bukkits.rb", <<-RUBY
module 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
get("/bukkits")
assert_equal "HELLO WORLD", last_response.body
end
test "pass the value of the segment" do
controller "foo", <<-RUBY
class FooController < ActionController::Base
def index
render :text => params[:username]
end
end
RUBY
@plugin.write "config/routes.rb", <<-RUBY
Bukkits::Engine.routes.draw do
root :to => "foo#index"
end
RUBY
app_file "config/routes.rb", <<-RUBY
Rails.application.routes.draw do
mount(Bukkits::Engine => "/:username")
end
RUBY
boot_rails
get("/arunagw")
assert_equal "arunagw", last_response.body
end
test "it provides routes as default endpoint" do
@plugin.write "lib/bukkits.rb", <<-RUBY
module 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
get("/bukkits/foo")
assert_equal "foo", last_response.body
end
test "it loads its environment file" do
@plugin.write "lib/bukkits.rb", <<-RUBY
module 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
module Bukkits
class Engine < ::Rails::Engine
endpoint lambda { |env| [200, {'Content-Type' => 'text/html'}, 'hello'] }
end
end
RUBY
require "rack/file"
boot_rails
env = Rack::MockRequest.env_for("/")
Bukkits::Engine.call(env)
assert_equal Bukkits::Engine.routes, env['action_dispatch.routes']
env = Rack::MockRequest.env_for("/")
Rails.application.call(env)
assert_equal Rails.application.routes, env['action_dispatch.routes']
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
isolate_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_namespace, Bukkits::Engine
assert ::Bukkits::MyMailer.method_defined?(:foo_path)
assert !::Bukkits::MyMailer.method_defined?(:bar_path)
get("/bukkits/from_app")
assert_equal "false", last_response.body
get("/bukkits/foo/show")
assert_equal "/bukkits/foo", last_response.body
get("/bukkits/foo")
assert_equal "Helped.", last_response.body
get("/bukkits/routes_helpers_in_view")
assert_equal "/bukkits/foo, /bar", last_response.body
get("/bukkits/polymorphic_path_without_namespace")
assert_equal "/bukkits/posts/1", last_response.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
isolate_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", <<-ERB
<%= form_for(Bukkits::Post.new) do |f| %>
<%= f.text_field :title %>
<% end %>
ERB
add_to_config("config.action_dispatch.show_exceptions = false")
boot_rails
get("/bukkits/posts/new")
assert_match(/name="post\[title\]"/, last_response.body)
end
test "isolated engine should set correct route module prefix for nested namespace" do
@plugin.write "lib/bukkits.rb", <<-RUBY
module Bukkits
module Awesome
class Engine < ::Rails::Engine
isolate_namespace Bukkits::Awesome
end
end
end
RUBY
app_file "config/routes.rb", <<-RUBY
AppTemplate::Application.routes.draw do
mount Bukkits::Awesome::Engine => "/bukkits", :as => "bukkits"
end
RUBY
@plugin.write "config/routes.rb", <<-RUBY
Bukkits::Awesome::Engine.routes.draw do
match "/foo" => "foo#index"
end
RUBY
@plugin.write "app/controllers/bukkits/awesome/foo_controller.rb", <<-RUBY
class Bukkits::Awesome::FooController < ActionController::Base
def index
render :text => "ok"
end
end
RUBY
add_to_config("config.action_dispatch.show_exceptions = false")
boot_rails
get("/bukkits/foo")
assert_equal "ok", last_response.body
end
test "loading seed data" do
@plugin.write "db/seeds.rb", <<-RUBY
Bukkits::Engine.config.bukkits_seeds_loaded = true
RUBY
app_file "db/seeds.rb", <<-RUBY
Rails.application.config.app_seeds_loaded = true
RUBY
boot_rails
Rails.application.load_seed
assert Rails.application.config.app_seeds_loaded
assert_raise(NoMethodError) { Bukkits::Engine.config.bukkits_seeds_loaded }
Bukkits::Engine.load_seed
assert Bukkits::Engine.config.bukkits_seeds_loaded
end
test "skips nonexistent seed data" do
FileUtils.rm "#{app_path}/db/seeds.rb"
boot_rails
assert_nil Rails.application.load_seed
end
test "using namespace more than once on one module should not overwrite railtie_namespace method" do
@plugin.write "lib/bukkits.rb", <<-RUBY
module AppTemplate
class Engine < ::Rails::Engine
isolate_namespace(AppTemplate)
end
end
RUBY
add_to_config "isolate_namespace AppTemplate"
app_file "config/routes.rb", <<-RUBY
AppTemplate::Application.routes.draw do end
RUBY
boot_rails
assert_equal AppTemplate.railtie_namespace, AppTemplate::Engine
end
test "properly reload routes" do
# when routes are inside application class definition
# they should not be reloaded when engine's routes
# file has changed
add_to_config <<-RUBY
routes do
mount lambda{|env| [200, {}, ["foo"]]} => "/foo"
mount Bukkits::Engine => "/bukkits"
end
RUBY
FileUtils.rm(File.join(app_path, "config/routes.rb"))
@plugin.write "config/routes.rb", <<-RUBY
Bukkits::Engine.routes.draw do
mount lambda{|env| [200, {}, ["bar"]]} => "/bar"
end
RUBY
@plugin.write "lib/bukkits.rb", <<-RUBY
module Bukkits
class Engine < ::Rails::Engine
isolate_namespace(Bukkits)
end
end
RUBY
boot_rails
require "#{rails_root}/config/environment"
get("/foo")
assert_equal "foo", last_response.body
get("/bukkits/bar")
assert_equal "bar", last_response.body
end
test "setting generators for engine and overriding app generator's" do
@plugin.write "lib/bukkits.rb", <<-RUBY
module Bukkits
class Engine < ::Rails::Engine
config.generators do |g|
g.orm :datamapper
g.template_engine :haml
g.test_framework :rspec
end
config.app_generators do |g|
g.orm :mongoid
g.template_engine :liquid
g.test_framework :shoulda
end
end
end
RUBY
add_to_config <<-RUBY
config.generators do |g|
g.test_framework :test_unit
end
RUBY
boot_rails
require "#{rails_root}/config/environment"
app_generators = Rails.application.config.generators.options[:rails]
assert_equal :mongoid , app_generators[:orm]
assert_equal :liquid , app_generators[:template_engine]
assert_equal :test_unit, app_generators[:test_framework]
generators = Bukkits::Engine.config.generators.options[:rails]
assert_equal :datamapper, generators[:orm]
assert_equal :haml , generators[:template_engine]
assert_equal :rspec , generators[:test_framework]
end
test "engine should get default generators with ability to overwrite them" do
@plugin.write "lib/bukkits.rb", <<-RUBY
module Bukkits
class Engine < ::Rails::Engine
config.generators.test_framework :rspec
end
end
RUBY
boot_rails
require "#{rails_root}/config/environment"
generators = Bukkits::Engine.config.generators.options[:rails]
assert_equal :active_record, generators[:orm]
assert_equal :rspec , generators[:test_framework]
app_generators = Rails.application.config.generators.options[:rails]
assert_equal :test_unit , app_generators[:test_framework]
end
test "do not create table_name_prefix method if it already exists" do
@plugin.write "lib/bukkits.rb", <<-RUBY
module Bukkits
def self.table_name_prefix
"foo"
end
class Engine < ::Rails::Engine
isolate_namespace(Bukkits)
end
end
RUBY
boot_rails
require "#{rails_root}/config/environment"
assert_equal "foo", Bukkits.table_name_prefix
end
test "fetching engine by path" do
@plugin.write "lib/bukkits.rb", <<-RUBY
module Bukkits
class Engine < ::Rails::Engine
end
end
RUBY
boot_rails
require "#{rails_root}/config/environment"
assert_equal Bukkits::Engine.instance, Rails::Engine.find(@plugin.path)
# check expanding paths
engine_dir = @plugin.path.chomp("/").split("/").last
engine_path = File.join(@plugin.path, '..', engine_dir)
assert_equal Bukkits::Engine.instance, Rails::Engine.find(engine_path)
end
test "gather isolated engine's helpers in Engine#helpers" do
@plugin.write "lib/bukkits.rb", <<-RUBY
module Bukkits
class Engine < ::Rails::Engine
isolate_namespace Bukkits
end
end
RUBY
app_file "app/helpers/some_helper.rb", <<-RUBY
module SomeHelper
def foo
'foo'
end
end
RUBY
@plugin.write "app/helpers/bukkits/engine_helper.rb", <<-RUBY
module Bukkits
module EngineHelper
def bar
'bar'
end
end
end
RUBY
@plugin.write "app/helpers/engine_helper.rb", <<-RUBY
module EngineHelper
def baz
'baz'
end
end
RUBY
add_to_config("config.action_dispatch.show_exceptions = false")
boot_rails
require "#{rails_root}/config/environment"
methods = Bukkits::Engine.helpers.public_instance_methods.collect(&:to_s).sort
expected = ["bar", "baz"]
assert_equal expected, methods
end
test "setting priority for engines with config.railties_order" do
@blog = engine "blog" do |plugin|
plugin.write "lib/blog.rb", <<-RUBY
module Blog
class Engine < ::Rails::Engine
end
end
RUBY
end
@plugin.write "lib/bukkits.rb", <<-RUBY
module Bukkits
class Engine < ::Rails::Engine
isolate_namespace Bukkits
end
end
RUBY
controller "main", <<-RUBY
class MainController < ActionController::Base
def foo
render :inline => '<%= render :partial => "shared/foo" %>'
end
def bar
render :inline => '<%= render :partial => "shared/bar" %>'
end
end
RUBY
app_file "config/routes.rb", <<-RUBY
Rails.application.routes.draw do
match "/foo" => "main#foo"
match "/bar" => "main#bar"
end
RUBY
@plugin.write "app/views/shared/_foo.html.erb", <<-RUBY
Bukkit's foo partial
RUBY
app_file "app/views/shared/_foo.html.erb", <<-RUBY
App's foo partial
RUBY
@blog.write "app/views/shared/_bar.html.erb", <<-RUBY
Blog's bar partial
RUBY
app_file "app/views/shared/_bar.html.erb", <<-RUBY
App's bar partial
RUBY
@plugin.write "app/assets/javascripts/foo.js", <<-RUBY
// Bukkit's foo js
RUBY
app_file "app/assets/javascripts/foo.js", <<-RUBY
// App's foo js
RUBY
@blog.write "app/assets/javascripts/bar.js", <<-RUBY
// Blog's bar js
RUBY
app_file "app/assets/javascripts/bar.js", <<-RUBY
// App's bar js
RUBY
add_to_config("config.railties_order = [:all, :main_app, Blog::Engine]")
boot_rails
require "#{rails_root}/config/environment"
get("/foo")
assert_equal "Bukkit's foo partial", last_response.body.strip
get("/bar")
assert_equal "App's bar partial", last_response.body.strip
get("/assets/foo.js")
assert_equal "// Bukkit's foo js\n;", last_response.body.strip
get("/assets/bar.js")
assert_equal "// App's bar js\n;", last_response.body.strip
end
test "railties_order adds :all with lowest priority if not given" do
@plugin.write "lib/bukkits.rb", <<-RUBY
module Bukkits
class Engine < ::Rails::Engine
end
end
RUBY
controller "main", <<-RUBY
class MainController < ActionController::Base
def foo
render :inline => '<%= render :partial => "shared/foo" %>'
end
end
RUBY
app_file "config/routes.rb", <<-RUBY
Rails.application.routes.draw do
match "/foo" => "main#foo"
end
RUBY
@plugin.write "app/views/shared/_foo.html.erb", <<-RUBY
Bukkit's foo partial
RUBY
app_file "app/views/shared/_foo.html.erb", <<-RUBY
App's foo partial
RUBY
add_to_config("config.railties_order = [Bukkits::Engine]")
boot_rails
require "#{rails_root}/config/environment"
get("/foo")
assert_equal "Bukkit's foo partial", last_response.body.strip
end
private
def app
Rails.application
end
end
end
|