PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/railties/test/railties/plugin_test.rb

https://bitbucket.org/druly/rails_cherry_pick
Ruby | 123 lines | 110 code | 13 blank | 0 comment | 1 complexity | 7a68003fa086b2e7810eeda174217231 MD5 | raw file
  1. require "isolation/abstract_unit"
  2. require "railties/shared_tests"
  3. module RailtiesTest
  4. class PluginTest < Test::Unit::TestCase
  5. include ActiveSupport::Testing::Isolation
  6. include SharedTests
  7. def setup
  8. build_app
  9. @plugin = plugin "bukkits", "::LEVEL = config.log_level" do |plugin|
  10. plugin.write "lib/bukkits.rb", "class Bukkits; end"
  11. plugin.write "lib/another.rb", "class Another; end"
  12. end
  13. end
  14. def teardown
  15. teardown_app
  16. end
  17. test "Rails::Plugin itself does not respond to config" do
  18. boot_rails
  19. assert !Rails::Plugin.respond_to?(:config)
  20. end
  21. test "cannot inherit from Rails::Plugin" do
  22. boot_rails
  23. assert_raise RuntimeError do
  24. class Foo < Rails::Plugin; end
  25. end
  26. end
  27. test "plugin can load the file with the same name in lib" do
  28. boot_rails
  29. require "bukkits"
  30. assert_equal "Bukkits", Bukkits.name
  31. end
  32. test "plugin gets added to dependency list" do
  33. boot_rails
  34. assert_equal "Another", Another.name
  35. end
  36. test "plugin constants get reloaded if config.reload_plugins is set to true" do
  37. add_to_config <<-RUBY
  38. config.reload_plugins = true
  39. RUBY
  40. boot_rails
  41. assert_equal "Another", Another.name
  42. ActiveSupport::Dependencies.clear
  43. @plugin.delete("lib/another.rb")
  44. assert_raises(NameError) { Another }
  45. end
  46. test "plugin constants are not reloaded by default" do
  47. boot_rails
  48. assert_equal "Another", Another.name
  49. ActiveSupport::Dependencies.clear
  50. @plugin.delete("lib/another.rb")
  51. assert_nothing_raised { Another }
  52. end
  53. test "it loads the plugin's init.rb file" do
  54. boot_rails
  55. assert_equal "loaded", BUKKITS
  56. end
  57. test "the init.rb file has access to the config object" do
  58. boot_rails
  59. assert_equal :debug, LEVEL
  60. end
  61. test "plugin_init_is_run_before_application_ones" do
  62. plugin "foo", "$foo = true" do |plugin|
  63. plugin.write "lib/foo.rb", "module Foo; end"
  64. end
  65. app_file 'config/initializers/foo.rb', <<-RUBY
  66. raise "no $foo" unless $foo
  67. raise "no Foo" unless Foo
  68. RUBY
  69. boot_rails
  70. assert $foo
  71. end
  72. test "plugin should work without init.rb" do
  73. @plugin.delete("init.rb")
  74. boot_rails
  75. require "bukkits"
  76. assert_nothing_raised { Bukkits }
  77. end
  78. test "plugin cannot declare an engine for it" do
  79. @plugin.write "lib/bukkits.rb", <<-RUBY
  80. class Bukkits
  81. class Engine < Rails::Engine
  82. end
  83. end
  84. RUBY
  85. @plugin.write "init.rb", <<-RUBY
  86. require "bukkits"
  87. RUBY
  88. rescued = false
  89. begin
  90. boot_rails
  91. rescue Exception => e
  92. rescued = true
  93. assert_equal '"bukkits" is a Railtie/Engine and cannot be installed as a plugin', e.message
  94. end
  95. assert rescued, "Expected boot rails to fail"
  96. end
  97. end
  98. end