plugin_generator_test.rb 13.8 KB
Newer Older
1
require 'generators/generators_test_helper'
S
schneems 已提交
2
require 'rails/generators/rails/plugin/plugin_generator'
3
require 'generators/shared_generator_tests'
4 5 6 7 8

DEFAULT_PLUGIN_FILES = %w(
  .gitignore
  Gemfile
  Rakefile
9
  README.rdoc
10 11 12 13
  bukkits.gemspec
  MIT-LICENSE
  lib
  lib/bukkits.rb
14
  lib/tasks/bukkits_tasks.rake
15
  lib/bukkits/version.rb
16 17 18 19 20
  test/bukkits_test.rb
  test/test_helper.rb
  test/dummy
)

S
schneems 已提交
21
class PluginGeneratorTest < Rails::Generators::TestCase
22 23 24
  include GeneratorsTestHelper
  destination File.join(Rails.root, "tmp/bukkits")
  arguments [destination_root]
25 26

  # brings setup, teardown, and some tests
27
  include SharedGeneratorTests
28 29

  def test_invalid_plugin_name_raises_an_error
30 31 32 33 34
    content = capture(:stderr){ run_generator [File.join(destination_root, "things-43")] }
    assert_equal "Invalid plugin name things-43. Please give a name which use only alphabetic or numeric or \"_\" characters.\n", content

    content = capture(:stderr){ run_generator [File.join(destination_root, "things4.3")] }
    assert_equal "Invalid plugin name things4.3. Please give a name which use only alphabetic or numeric or \"_\" characters.\n", content
35

36 37
    content = capture(:stderr){ run_generator [File.join(destination_root, "43things")] }
    assert_equal "Invalid plugin name 43things. Please give a name which does not start with numbers.\n", content
38 39 40 41 42 43

    content = capture(:stderr){ run_generator [File.join(destination_root, "plugin")] }
    assert_equal "Invalid plugin name plugin. Please give a name which does not match one of the reserved rails words.\n", content

    content = capture(:stderr){ run_generator [File.join(destination_root, "Digest")] }
    assert_equal "Invalid plugin name Digest, constant Digest is already in use. Please choose another plugin name.\n", content
44 45
  end

L
lest 已提交
46 47 48 49 50 51
  def test_camelcase_plugin_name_underscores_filenames
    run_generator [File.join(destination_root, "CamelCasedName")]
    assert_no_file "CamelCasedName/lib/CamelCasedName.rb"
    assert_file "CamelCasedName/lib/camel_cased_name.rb", /module CamelCasedName/
  end

52
  def test_generating_without_options
53
    run_generator
S
Santiago Pastorino 已提交
54
    assert_file "README.rdoc", /Bukkits/
55
    assert_no_file "config/routes.rb"
56 57 58 59
    assert_file "test/test_helper.rb"
    assert_file "test/bukkits_test.rb", /assert_kind_of Module, Bukkits/
  end

60 61 62 63
  def test_generating_test_files_in_full_mode
    run_generator [destination_root, "--full"]
    assert_directory "test/integration/"

64
    assert_file "test/integration/navigation_test.rb", /ActionDispatch::IntegrationTest/
65 66
  end

67 68 69 70 71 72 73 74 75 76 77
  def test_inclusion_of_debugger
    run_generator [destination_root, '--full']
    if defined?(JRUBY_VERSION)
      assert_file "Gemfile" do |content|
        assert_no_match(/debugger/, content)
      end
    else
      assert_file "Gemfile", /# gem 'debugger'/
    end
  end

78 79 80 81 82
  def test_generating_test_files_in_full_mode_without_unit_test_files
    run_generator [destination_root, "-T", "--full"]

    assert_no_directory "test/integration/"
    assert_no_file "test"
83 84 85
    assert_file "Rakefile" do |contents|
      assert_no_match(/APP_RAKEFILE/, contents)
    end
86 87
  end

88 89
  def test_generating_adds_dummy_app_rake_tasks_without_unit_test_files
    run_generator [destination_root, "-T", "--mountable", '--dummy-path', 'my_dummy_app']
90 91 92 93 94
    assert_file "Rakefile", /APP_RAKEFILE/
  end

  def test_generating_adds_dummy_app_without_javascript_and_assets_deps
    run_generator [destination_root]
95

96 97 98 99 100
    assert_file "test/dummy/app/assets/stylesheets/application.css"

    assert_file "test/dummy/app/assets/javascripts/application.js" do |contents|
      assert_no_match(/jquery/, contents)
    end
101 102
  end

103 104
  def test_ensure_that_plugin_options_are_not_passed_to_app_generator
    FileUtils.cd(Rails.root)
105
    assert_no_match(/It works from file!.*It works_from_file/, run_generator([destination_root, "-m", "lib/template.rb"]))
106 107
  end

108 109 110 111 112 113 114
  def test_ensure_that_test_dummy_can_be_generated_from_a_template
    FileUtils.cd(Rails.root)
    run_generator([destination_root, "-m", "lib/create_test_dummy_template.rb", "--skip-test-unit"])
    assert_file "spec/dummy"
    assert_no_file "test"
  end

115
  def test_database_entry_is_generated_for_sqlite3_by_default_in_full_mode
116 117
    run_generator([destination_root, "--full"])
    assert_file "test/dummy/config/database.yml", /sqlite/
V
Vishnu Atrai 已提交
118
    assert_file "bukkits.gemspec", /sqlite3/
119 120 121 122 123
  end

  def test_config_another_database
    run_generator([destination_root, "-d", "mysql", "--full"])
    assert_file "test/dummy/config/database.yml", /mysql/
V
Vishnu Atrai 已提交
124
    assert_file "bukkits.gemspec", /mysql/
125 126
  end

127 128 129 130 131 132 133 134
  def test_dont_generate_development_dependency
    run_generator [destination_root, "--skip-active-record"]

    assert_file "bukkits.gemspec" do |contents|
      assert_no_match(/s.add_development_dependency "sqlite3"/, contents)
    end
  end

135 136 137 138 139
  def test_active_record_is_removed_from_frameworks_if_skip_active_record_is_given
    run_generator [destination_root, "--skip-active-record"]
    assert_file "test/dummy/config/application.rb", /#\s+require\s+["']active_record\/railtie["']/
  end

140 141 142
  def test_ensure_that_skip_active_record_option_is_passed_to_app_generator
    run_generator [destination_root, "--skip_active_record"]
    assert_no_file "test/dummy/config/database.yml"
143
    assert_file "test/test_helper.rb" do |contents|
V
Vipul A M 已提交
144
      assert_no_match(/ActiveRecord/, contents)
145
    end
146 147
  end

148 149 150 151 152
  def test_ensure_that_database_option_is_passed_to_app_generator
    run_generator [destination_root, "--database", "postgresql"]
    assert_file "test/dummy/config/database.yml", /postgres/
  end

153
  def test_generation_runs_bundle_install_with_full_and_mountable
154 155 156 157 158
    result = run_generator [destination_root, "--mountable", "--full", "--dev"]
    assert_file "#{destination_root}/Gemfile.lock" do |contents|
      assert_match(/bukkits/, contents)
    end
    assert_match(/run  bundle install/, result)
159
    assert_match(/Using bukkits \(?0\.0\.1\)?/, result)
160
    assert_match(/Your bundle is complete/, result)
161 162 163
    assert_equal 1, result.scan("Your bundle is complete").size
  end

164 165
  def test_skipping_javascripts_without_mountable_option
    run_generator
R
Ryan Bates 已提交
166
    assert_no_file "app/assets/javascripts/bukkits/application.js"
167 168 169 170
  end

  def test_javascripts_generation
    run_generator [destination_root, "--mountable"]
R
Ryan Bates 已提交
171
    assert_file "app/assets/javascripts/bukkits/application.js"
172 173 174 175
  end

  def test_skip_javascripts
    run_generator [destination_root, "--skip-javascript", "--mountable"]
R
Ryan Bates 已提交
176
    assert_no_file "app/assets/javascripts/bukkits/application.js"
177 178
  end

179 180
  def test_template_from_dir_pwd
    FileUtils.cd(Rails.root)
181
    assert_match(/It works from file!/, run_generator([destination_root, "-m", "lib/template.rb"]))
182 183
  end

184
  def test_ensure_that_tests_work
P
Piotr Sarnacki 已提交
185 186
    run_generator
    FileUtils.cd destination_root
187
    quietly { system 'bundle install' }
S
schneems 已提交
188
    assert_match(/1 runs, 1 assertions, 0 failures, 0 errors/, `bundle exec rake test 2>&1`)
P
Piotr Sarnacki 已提交
189 190
  end

191
  def test_ensure_that_tests_works_in_full_mode
192
    run_generator [destination_root, "--full", "--skip_active_record"]
193
    FileUtils.cd destination_root
194
    quietly { system 'bundle install' }
S
schneems 已提交
195
    assert_match(/1 runs, 1 assertions, 0 failures, 0 errors/, `bundle exec rake test 2>&1`)
196 197
  end

J
Jake Varghese 已提交
198 199 200 201
  def test_ensure_that_migration_tasks_work_with_mountable_option
    run_generator [destination_root, "--mountable"]
    FileUtils.cd destination_root
    quietly { system 'bundle install' }
S
schneems 已提交
202 203
    output = `bundle exec rake db:migrate 2>&1`
    assert $?.success?, "Command failed: #{output}"
J
Jake Varghese 已提交
204 205
  end

206 207
  def test_creating_engine_in_full_mode
    run_generator [destination_root, "--full"]
R
Ryan Bates 已提交
208 209 210
    assert_file "app/assets/javascripts/bukkits"
    assert_file "app/assets/stylesheets/bukkits"
    assert_file "app/assets/images/bukkits"
211 212 213 214
    assert_file "app/models"
    assert_file "app/controllers"
    assert_file "app/views"
    assert_file "app/helpers"
215
    assert_file "app/mailers"
216
    assert_file "bin/rails"
217
    assert_file "config/routes.rb", /Rails.application.routes.draw do/
S
Stefan Sprenger 已提交
218
    assert_file "lib/bukkits/engine.rb", /module Bukkits\n  class Engine < ::Rails::Engine\n  end\nend/
219 220 221
    assert_file "lib/bukkits.rb", /require "bukkits\/engine"/
  end

222
  def test_being_quiet_while_creating_dummy_application
223
    assert_no_match(/create\s+config\/application.rb/, run_generator)
224 225
  end

226 227
  def test_create_mountable_application_with_mountable_option
    run_generator [destination_root, "--mountable"]
R
Ryan Bates 已提交
228 229 230
    assert_file "app/assets/javascripts/bukkits"
    assert_file "app/assets/stylesheets/bukkits"
    assert_file "app/assets/images/bukkits"
231 232 233
    assert_file "config/routes.rb", /Bukkits::Engine.routes.draw do/
    assert_file "lib/bukkits/engine.rb", /isolate_namespace Bukkits/
    assert_file "test/dummy/config/routes.rb", /mount Bukkits::Engine => "\/bukkits"/
234
    assert_file "app/controllers/bukkits/application_controller.rb", /module Bukkits\n  class ApplicationController < ActionController::Base/
235
    assert_file "app/helpers/bukkits/application_helper.rb", /module Bukkits\n  module ApplicationHelper/
R
Ryan Bates 已提交
236 237
    assert_file "app/views/layouts/bukkits/application.html.erb" do |contents|
      assert_match "<title>Bukkits</title>", contents
238 239
      assert_match(/stylesheet_link_tag\s+['"]bukkits\/application['"]/, contents)
      assert_match(/javascript_include_tag\s+['"]bukkits\/application['"]/, contents)
R
Ryan Bates 已提交
240
    end
241 242
  end

243
  def test_creating_gemspec
244
    run_generator
V
Vishnu Atrai 已提交
245
    assert_file "bukkits.gemspec", /s.name\s+= "bukkits"/
246
    assert_file "bukkits.gemspec", /s.files = Dir\["\{app,config,db,lib\}\/\*\*\/\*", "MIT-LICENSE", "Rakefile", "README\.rdoc"\]/
247
    assert_file "bukkits.gemspec", /s.test_files = Dir\["test\/\*\*\/\*"\]/
V
Vishnu Atrai 已提交
248
    assert_file "bukkits.gemspec", /s.version\s+ = Bukkits::VERSION/
249 250
  end

251
  def test_usage_of_engine_commands
252
    run_generator [destination_root, "--full"]
253 254 255 256
    assert_file "bin/rails", /ENGINE_PATH = File.expand_path\('..\/..\/lib\/bukkits\/engine', __FILE__\)/
    assert_file "bin/rails", /ENGINE_ROOT = File.expand_path\('..\/..', __FILE__\)/
    assert_file "bin/rails", /require 'rails\/all'/
    assert_file "bin/rails", /require 'rails\/engine\/commands'/
257 258
  end

259
  def test_shebang
260
    run_generator [destination_root, "--full"]
261
    assert_file "bin/rails", /#!\/usr\/bin\/env ruby/
262 263
  end

264 265 266 267 268 269
  def test_passing_dummy_path_as_a_parameter
    run_generator [destination_root, "--dummy_path", "spec/dummy"]
    assert_file "spec/dummy"
    assert_file "spec/dummy/config/application.rb"
    assert_no_file "test/dummy"
  end
270

P
Piotr Sarnacki 已提交
271 272 273 274 275 276 277
  def test_creating_dummy_application_with_different_name
    run_generator [destination_root, "--dummy_path", "spec/fake"]
    assert_file "spec/fake"
    assert_file "spec/fake/config/application.rb"
    assert_no_file "test/dummy"
  end

278 279 280 281 282
  def test_creating_dummy_without_tests_but_with_dummy_path
    run_generator [destination_root, "--dummy_path", "spec/dummy", "--skip-test-unit"]
    assert_file "spec/dummy"
    assert_file "spec/dummy/config/application.rb"
    assert_no_file "test"
283 284 285
    assert_file '.gitignore' do |contents|
      assert_match(/spec\/dummy/, contents)
    end
286
  end
287

288 289
  def test_ensure_that_gitignore_can_be_generated_from_a_template_for_dummy_path
    FileUtils.cd(Rails.root)
290
    run_generator([destination_root, "--dummy_path", "spec/dummy", "--skip-test-unit"])
291 292 293 294
    assert_file ".gitignore" do |contents|
      assert_match(/spec\/dummy/, contents)
    end
  end
295

296 297 298
  def test_skipping_test_unit
    run_generator [destination_root, "--skip-test-unit"]
    assert_no_file "test"
S
Stefan Sprenger 已提交
299
    assert_file "bukkits.gemspec" do |contents|
300
      assert_no_match(/s.test_files = Dir\["test\/\*\*\/\*"\]/, contents)
S
Stefan Sprenger 已提交
301
    end
302 303 304
    assert_file '.gitignore' do |contents|
      assert_no_match(/test\dummy/, contents)
    end
305 306
  end

307 308 309
  def test_skipping_gemspec
    run_generator [destination_root, "--skip-gemspec"]
    assert_no_file "bukkits.gemspec"
310 311
    assert_file "Gemfile" do |contents|
      assert_no_match('gemspec', contents)
312
      assert_match(/gem "rails", "~> #{Rails.version}"/, contents)
313
      assert_match_sqlite3(contents)
314 315 316 317 318 319 320 321 322
      assert_no_match(/# gem "jquery-rails"/, contents)
    end
  end

  def test_skipping_gemspec_in_full_mode
    run_generator [destination_root, "--skip-gemspec", "--full"]
    assert_no_file "bukkits.gemspec"
    assert_file "Gemfile" do |contents|
      assert_no_match('gemspec', contents)
323
      assert_match(/gem "rails", "~> #{Rails.version}"/, contents)
324
      assert_match_sqlite3(contents)
325
    end
326 327
  end

328
  def test_creating_plugin_in_app_directory_adds_gemfile_entry
329
    # simulate application existence
330 331 332 333 334 335
    gemfile_path = "#{Rails.root}/Gemfile"
    Object.const_set('APP_PATH', Rails.root)
    FileUtils.touch gemfile_path

    run_generator [destination_root]

336
    assert_file gemfile_path, /gem 'bukkits', path: 'tmp\/bukkits'/
337 338 339 340 341 342
  ensure
    Object.send(:remove_const, 'APP_PATH')
    FileUtils.rm gemfile_path
  end

  def test_skipping_gemfile_entry
343
    # simulate application existence
344 345 346 347 348 349 350
    gemfile_path = "#{Rails.root}/Gemfile"
    Object.const_set('APP_PATH', Rails.root)
    FileUtils.touch gemfile_path

    run_generator [destination_root, "--skip-gemfile-entry"]

    assert_file gemfile_path do |contents|
351
      assert_no_match(/gem 'bukkits', path: 'tmp\/bukkits'/, contents)
352 353 354 355 356 357
    end
  ensure
    Object.send(:remove_const, 'APP_PATH')
    FileUtils.rm gemfile_path
  end

358 359 360 361 362 363 364 365 366 367 368 369
  def test_generating_controller_inside_mountable_engine
    run_generator [destination_root, "--mountable"]

    capture(:stdout) do
      `#{destination_root}/bin/rails g controller admin/dashboard foo`
    end

    assert_file "config/routes.rb" do |contents|
      assert_match(/namespace :admin/, contents)
      assert_no_match(/namespace :bukkit/, contents)
    end
  end
370

371 372 373 374 375
protected
  def action(*args, &block)
    silence(:stdout){ generator.send(*args, &block) }
  end

376 377 378
  def default_files
    ::DEFAULT_PLUGIN_FILES
  end
379 380 381 382 383 384 385 386

  def assert_match_sqlite3(contents)
    unless defined?(JRUBY_VERSION)
      assert_match(/group :development do\n  gem "sqlite3"\nend/, contents)
    else
      assert_match(/group :development do\n  gem "activerecord-jdbcsqlite3-adapter"\nend/, contents)
    end
  end
387
end