app_generator_test.rb 15.7 KB
Newer Older
J
José Valim 已提交
1
require 'generators/generators_test_helper'
2
require 'rails/generators/rails/app/app_generator'
3
require 'generators/shared_generator_tests'
J
José Valim 已提交
4

5 6
DEFAULT_APP_FILES = %w(
  .gitignore
A
Arun Agrawal 已提交
7
  README.rdoc
8 9 10
  Gemfile
  Rakefile
  config.ru
J
José Valim 已提交
11 12
  app/assets/javascripts
  app/assets/stylesheets
13
  app/assets/images
14
  app/controllers
F
Francesco Rodriguez 已提交
15
  app/controllers/concerns
16
  app/helpers
17
  app/mailers
18
  app/models
F
Francesco Rodriguez 已提交
19
  app/models/concerns
20
  app/views/layouts
21 22 23
  bin/bundle
  bin/rails
  bin/rake
24
  bin/setup
25 26 27 28 29 30
  config/environments
  config/initializers
  config/locales
  db
  lib
  lib/tasks
31
  lib/assets
32
  log
A
Arun Agrawal 已提交
33
  test/test_helper.rb
34
  test/fixtures
M
Mike Moore 已提交
35 36 37 38
  test/controllers
  test/models
  test/helpers
  test/mailers
39 40
  test/integration
  vendor
J
José Valim 已提交
41
  vendor/assets
A
Arun Agrawal 已提交
42 43
  vendor/assets/stylesheets
  vendor/assets/javascripts
44
  tmp/cache
J
José Valim 已提交
45
  tmp/cache/assets
46 47
)

48 49
class AppGeneratorTest < Rails::Generators::TestCase
  include GeneratorsTestHelper
50
  arguments [destination_root]
51 52

  # brings setup, teardown, and some tests
53
  include SharedGeneratorTests
J
José Valim 已提交
54

55 56
  def default_files
    ::DEFAULT_APP_FILES
57 58
  end

59
  def test_assets
60
    run_generator
61

62 63
    assert_file("app/views/layouts/application.html.erb", /stylesheet_link_tag\s+'application', media: 'all', 'data-turbolinks-track' => true/)
    assert_file("app/views/layouts/application.html.erb", /javascript_include_tag\s+'application', 'data-turbolinks-track' => true/)
64
    assert_file("app/assets/stylesheets/application.css")
A
Arun Agrawal 已提交
65
    assert_file("app/assets/javascripts/application.js")
66 67
  end

68
  def test_invalid_application_name_raises_an_error
69
    content = capture(:stderr){ run_generator [File.join(destination_root, "43-things")] }
70 71 72 73
    assert_equal "Invalid application name 43-things. Please give a name which does not start with numbers.\n", content
  end

  def test_invalid_application_name_is_fixed
74
    run_generator [File.join(destination_root, "things-43")]
75
    assert_file "things-43/config/environment.rb", /Rails\.application\.initialize!/
76
    assert_file "things-43/config/application.rb", /^module Things43$/
77 78
  end

79
  def test_application_new_exits_with_non_zero_code_on_invalid_application_name
A
Akira Matsuda 已提交
80
    quietly { system 'rails new test --no-rc' }
81
    assert_equal false, $?.success?
82 83 84 85 86 87 88 89 90 91 92
  end

  def test_application_new_exits_with_message_and_non_zero_code_when_generating_inside_existing_rails_directory
    app_root = File.join(destination_root, 'myfirstapp')
    run_generator [app_root]
    output = nil
    Dir.chdir(app_root) do
      output = `rails new mysecondapp`
    end
    assert_equal "Can't initialize a new Rails application within the directory of another, please change to a non-Rails directory first.\nType 'rails' for help.\n", output
    assert_equal false, $?.success?
93 94
  end

95 96 97 98 99 100
  def test_application_new_show_help_message_inside_existing_rails_directory
    app_root = File.join(destination_root, 'myfirstapp')
    run_generator [app_root]
    output = Dir.chdir(app_root) do
      `rails new --help`
    end
101
    assert_match(/rails new APP_PATH \[options\]/, output)
102 103 104
    assert_equal true, $?.success?
  end

105 106 107 108 109 110 111 112
  def test_application_name_is_detected_if_it_exists_and_app_folder_renamed
    app_root       = File.join(destination_root, "myapp")
    app_moved_root = File.join(destination_root, "myapp_moved")

    run_generator [app_root]

    Rails.application.config.root = app_moved_root
    Rails.application.class.stubs(:name).returns("Myapp")
113
    Rails.application.stubs(:is_a?).returns(Rails::Application)
114 115 116

    FileUtils.mv(app_root, app_moved_root)

A
Arun Agrawal 已提交
117 118 119
    # make sure we are in correct dir
    FileUtils.cd(app_moved_root)

120 121
    generator = Rails::Generators::AppGenerator.new ["rails"], { with_dispatchers: true },
                                                               destination_root: app_moved_root, shell: @shell
122
    generator.send(:app_const)
123
    quietly { generator.send(:update_config_files) }
124
    assert_file "myapp_moved/config/environment.rb", /Rails\.application\.initialize!/
125
    assert_file "myapp_moved/config/initializers/session_store.rb", /_myapp_session/
126
  end
127

128 129 130
  def test_rails_update_generates_correct_session_key
    app_root = File.join(destination_root, 'myapp')
    run_generator [app_root]
131

132 133 134 135
    Rails.application.config.root = app_root
    Rails.application.class.stubs(:name).returns("Myapp")
    Rails.application.stubs(:is_a?).returns(Rails::Application)

136
    generator = Rails::Generators::AppGenerator.new ["rails"], { with_dispatchers: true }, destination_root: app_root, shell: @shell
137
    generator.send(:app_const)
138
    quietly { generator.send(:update_config_files) }
139 140
    assert_file "myapp/config/initializers/session_store.rb", /_myapp_session/
  end
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
  def test_new_application_use_json_serialzier
    run_generator

    assert_file("config/initializers/cookies_serializer.rb", /Rails\.application\.config\.action_dispatch\.cookies_serializer = :json/)
  end

  def test_rails_update_keep_the_cookie_serializer_if_it_is_already_configured
    app_root = File.join(destination_root, 'myapp')
    run_generator [app_root]

    Rails.application.config.root = app_root
    Rails.application.class.stubs(:name).returns("Myapp")
    Rails.application.stubs(:is_a?).returns(Rails::Application)

    generator = Rails::Generators::AppGenerator.new ["rails"], { with_dispatchers: true }, destination_root: app_root, shell: @shell
    generator.send(:app_const)
    quietly { generator.send(:update_config_files) }
    assert_file("#{app_root}/config/initializers/cookies_serializer.rb", /Rails\.application\.config\.action_dispatch\.cookies_serializer = :json/)
  end

  def test_rails_update_set_the_cookie_serializer_to_marchal_if_it_is_not_already_configured
    app_root = File.join(destination_root, 'myapp')
    run_generator [app_root]

    FileUtils.rm("#{app_root}/config/initializers/cookies_serializer.rb")

    Rails.application.config.root = app_root
    Rails.application.class.stubs(:name).returns("Myapp")
    Rails.application.stubs(:is_a?).returns(Rails::Application)

    generator = Rails::Generators::AppGenerator.new ["rails"], { with_dispatchers: true }, destination_root: app_root, shell: @shell
    generator.send(:app_const)
    quietly { generator.send(:update_config_files) }
    assert_file("#{app_root}/config/initializers/cookies_serializer.rb", /Rails\.application\.config\.action_dispatch\.cookies_serializer = :marshal/)
  end

178 179
  def test_application_names_are_not_singularized
    run_generator [File.join(destination_root, "hats")]
180
    assert_file "hats/config/environment.rb", /Rails\.application\.initialize!/
181 182
  end

183 184 185 186 187
  def test_gemfile_has_no_whitespace_errors
    run_generator
    absolute = File.expand_path("Gemfile", destination_root)
    File.open(absolute, 'r') do |f|
      f.each_line do |line|
A
Arun Agrawal 已提交
188
        assert_no_match %r{/^[ \t]+$/}, line
189 190 191 192
      end
    end
  end

J
José Valim 已提交
193 194 195
  def test_config_database_is_added_by_default
    run_generator
    assert_file "config/database.yml", /sqlite3/
196
    unless defined?(JRUBY_VERSION)
O
Oscar Del Ben 已提交
197
      assert_gem "sqlite3"
198
    else
O
Oscar Del Ben 已提交
199
      assert_gem "activerecord-jdbcsqlite3-adapter"
200
    end
201 202 203 204 205
  end

  def test_config_another_database
    run_generator([destination_root, "-d", "mysql"])
    assert_file "config/database.yml", /mysql/
206
    unless defined?(JRUBY_VERSION)
O
Oscar Del Ben 已提交
207
      assert_gem "mysql2"
208
    else
O
Oscar Del Ben 已提交
209
      assert_gem "activerecord-jdbcmysql-adapter"
210
    end
J
José Valim 已提交
211 212
  end

213 214 215 216 217
  def test_config_database_app_name_with_period
    run_generator [File.join(destination_root, "common.usage.com"), "-d", "postgresql"]
    assert_file "common.usage.com/config/database.yml", /common_usage_com/
  end

218 219 220 221
  def test_config_postgresql_database
    run_generator([destination_root, "-d", "postgresql"])
    assert_file "config/database.yml", /postgresql/
    unless defined?(JRUBY_VERSION)
O
Oscar Del Ben 已提交
222
      assert_gem "pg"
223
    else
O
Oscar Del Ben 已提交
224
      assert_gem "activerecord-jdbcpostgresql-adapter"
225 226 227
    end
  end

228 229
  def test_config_jdbcmysql_database
    run_generator([destination_root, "-d", "jdbcmysql"])
230
    assert_file "config/database.yml", /mysql/
O
Oscar Del Ben 已提交
231
    assert_gem "activerecord-jdbcmysql-adapter"
232 233
  end

234 235
  def test_config_jdbcsqlite3_database
    run_generator([destination_root, "-d", "jdbcsqlite3"])
236
    assert_file "config/database.yml", /sqlite3/
O
Oscar Del Ben 已提交
237
    assert_gem "activerecord-jdbcsqlite3-adapter"
238 239
  end

240 241
  def test_config_jdbcpostgresql_database
    run_generator([destination_root, "-d", "jdbcpostgresql"])
242
    assert_file "config/database.yml", /postgresql/
O
Oscar Del Ben 已提交
243
    assert_gem "activerecord-jdbcpostgresql-adapter"
244 245
  end

A
Arun Agrawal 已提交
246 247 248
  def test_config_jdbc_database
    run_generator([destination_root, "-d", "jdbc"])
    assert_file "config/database.yml", /jdbc/
249
    assert_file "config/database.yml", /mssql/
O
Oscar Del Ben 已提交
250
    assert_gem "activerecord-jdbc-adapter"
A
Arun Agrawal 已提交
251 252
  end

253 254 255 256
  def test_config_jdbc_database_when_no_option_given
    if defined?(JRUBY_VERSION)
      run_generator([destination_root])
      assert_file "config/database.yml", /sqlite3/
O
Oscar Del Ben 已提交
257
      assert_gem "activerecord-jdbcsqlite3-adapter"
258 259 260
    end
  end

261
  def test_generator_if_skip_active_record_is_given
262
    run_generator [destination_root, "--skip-active-record"]
J
José Valim 已提交
263
    assert_no_file "config/database.yml"
264
    assert_file "config/application.rb", /#\s+require\s+["']active_record\/railtie["']/
265
    assert_file "test/test_helper.rb" do |helper_content|
266
      assert_no_match(/fixtures :all/, helper_content)
267
    end
J
José Valim 已提交
268 269
  end

270 271 272 273 274
  def test_generator_if_skip_action_view_is_given
    run_generator [destination_root, "--skip-action-view"]
    assert_file "config/application.rb", /#\s+require\s+["']action_view\/railtie["']/
  end

275
  def test_generator_if_skip_sprockets_is_given
276
    run_generator [destination_root, "--skip-sprockets"]
277
    assert_no_file "config/initializers/assets.rb"
278
    assert_file "config/application.rb" do |content|
279
      assert_match(/#\s+require\s+["']sprockets\/railtie["']/, content)
280
    end
281 282 283
    assert_file "Gemfile" do |content|
      assert_no_match(/sass-rails/, content)
      assert_no_match(/uglifier/, content)
284
      assert_match(/coffee-rails/, content)
285
    end
286 287 288 289 290
    assert_file "config/environments/development.rb" do |content|
      assert_no_match(/config\.assets\.debug = true/, content)
    end
    assert_file "config/environments/production.rb" do |content|
      assert_no_match(/config\.assets\.digest = true/, content)
J
Joshua Peek 已提交
291 292
      assert_no_match(/config\.assets\.js_compressor = :uglifier/, content)
      assert_no_match(/config\.assets\.css_compressor = :sass/, content)
293
    end
294
  end
J
José Valim 已提交
295

296
  def test_inclusion_of_javascript_runtime
297
    run_generator([destination_root])
298
    if defined?(JRUBY_VERSION)
O
Oscar Del Ben 已提交
299
      assert_gem "therubyrhino"
300
    else
A
Arun Agrawal 已提交
301
      assert_file "Gemfile", /# gem\s+["']therubyracer["']+, \s+platforms: :ruby$/
302 303 304
    end
  end

305 306 307 308 309 310
  def test_jquery_is_the_default_javascript_library
    run_generator
    assert_file "app/assets/javascripts/application.js" do |contents|
      assert_match %r{^//= require jquery}, contents
      assert_match %r{^//= require jquery_ujs}, contents
    end
A
Arun Agrawal 已提交
311
    assert_gem "jquery-rails"
312 313 314 315 316 317 318 319
  end

  def test_other_javascript_libraries
    run_generator [destination_root, '-j', 'prototype']
    assert_file "app/assets/javascripts/application.js" do |contents|
      assert_match %r{^//= require prototype}, contents
      assert_match %r{^//= require prototype_ujs}, contents
    end
O
Oscar Del Ben 已提交
320
    assert_gem "prototype-rails"
J
José Valim 已提交
321
  end
322

323 324
  def test_javascript_is_skipped_if_required
    run_generator [destination_root, "--skip-javascript"]
325 326 327 328

    assert_no_file "app/assets/javascripts"
    assert_no_file "vendor/assets/javascripts"

329
    assert_file "app/views/layouts/application.html.erb" do |contents|
330 331
      assert_match(/stylesheet_link_tag\s+'application', media: 'all' %>/, contents)
      assert_no_match(/javascript_include_tag\s+'application' \%>/, contents)
332
    end
333

334
    assert_file "Gemfile" do |content|
335
      assert_no_match(/coffee-rails/, content)
A
Arun Agrawal 已提交
336
      assert_no_match(/jquery-rails/, content)
337
    end
338
  end
339

A
Arun Agrawal 已提交
340 341 342 343 344
  def test_inclusion_of_jbuilder
    run_generator
    assert_file "Gemfile", /gem 'jbuilder'/
  end

345
  def test_inclusion_of_a_debugger
346
    run_generator
A
Arun Agrawal 已提交
347 348
    if defined?(JRUBY_VERSION)
      assert_file "Gemfile" do |content|
349
        assert_no_match(/byebug/, content)
A
Arun Agrawal 已提交
350 351
        assert_no_match(/debugger/, content)
      end
352
    elsif RUBY_VERSION < '2.0.0'
A
Arun Agrawal 已提交
353
      assert_file "Gemfile", /# gem 'debugger'/
354 355
    else
      assert_file "Gemfile", /# gem 'byebug'/
A
Arun Agrawal 已提交
356
    end
357 358
  end

X
Xavier Noria 已提交
359
  def test_inclusion_of_doc
X
Xavier Noria 已提交
360
    run_generator
X
Xavier Noria 已提交
361
    assert_file 'Gemfile', /gem 'sdoc',\s+'~> 0.4.0',\s+group: :doc/
X
Xavier Noria 已提交
362 363
  end

364
  def test_template_from_dir_pwd
365
    FileUtils.cd(Rails.root)
366
    assert_match(/It works from file!/, run_generator([destination_root, "-m", "lib/template.rb"]))
367 368
  end

J
José Valim 已提交
369 370 371 372 373 374
  def test_usage_read_from_file
    File.expects(:read).returns("USAGE FROM FILE")
    assert_equal "USAGE FROM FILE", Rails::Generators::AppGenerator.desc
  end

  def test_default_usage
375
    Rails::Generators::AppGenerator.expects(:usage_path).returns(nil)
376
    assert_match(/Create rails files for app generator/, Rails::Generators::AppGenerator.desc)
J
José Valim 已提交
377 378 379
  end

  def test_default_namespace
380
    assert_match "rails:app", Rails::Generators::AppGenerator.namespace
J
José Valim 已提交
381 382
  end

383 384 385 386 387
  def test_file_is_added_for_backwards_compatibility
    action :file, 'lib/test_file.rb', 'heres test data'
    assert_file 'lib/test_file.rb', 'heres test data'
  end

388 389
  def test_test_unit_is_removed_from_frameworks_if_skip_test_unit_is_given
    run_generator [destination_root, "--skip-test-unit"]
390 391 392 393 394 395 396
    assert_file "config/application.rb", /#\s+require\s+["']rails\/test_unit\/railtie["']/
  end

  def test_no_active_record_or_test_unit_if_skips_given
    run_generator [destination_root, "--skip-test-unit", "--skip-active-record"]
    assert_file "config/application.rb", /#\s+require\s+["']rails\/test_unit\/railtie["']/
    assert_file "config/application.rb", /#\s+require\s+["']active_record\/railtie["']/
397 398
  end

399 400 401
  def test_new_hash_style
    run_generator [destination_root]
    assert_file "config/initializers/session_store.rb" do |file|
402
      assert_match(/config.session_store :cookie_store, key: '_.+_session'/, file)
403 404 405
    end
  end

406 407 408 409 410
  def test_pretend_option
    output = run_generator [File.join(destination_root, "myapp"), "--pretend"]
    assert_no_match(/run  bundle install/, output)
  end

411 412 413 414 415 416 417 418 419 420
  def test_application_name_with_spaces
    path = File.join(destination_root, "foo bar".shellescape)

    # This also applies to MySQL apps but not with SQLite
    run_generator [path, "-d", 'postgresql']

    assert_file "foo bar/config/database.yml", /database: foo_bar_development/
    assert_file "foo bar/config/initializers/session_store.rb", /key: '_foo_bar/
  end

421 422
  def test_spring
    run_generator
423
    assert_file "Gemfile", /gem 'spring', \s+group: :development/
424 425 426
  end

  def test_spring_binstubs
427
    jruby_skip "spring doesn't run on JRuby"
428 429 430 431 432 433
    generator.stubs(:bundle_command).with('install')
    generator.expects(:bundle_command).with('exec spring binstub --all').once
    quietly { generator.invoke_all }
  end

  def test_spring_no_fork
434
    jruby_skip "spring doesn't run on JRuby"
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
    Process.stubs(:respond_to?).with(:fork).returns(false)
    run_generator

    assert_file "Gemfile" do |content|
      assert_no_match(/spring/, content)
    end
  end

  def test_skip_spring
    run_generator [destination_root, "--skip-spring"]

    assert_file "Gemfile" do |content|
      assert_no_match(/spring/, content)
    end
  end

451 452 453 454 455 456 457 458 459 460 461 462
  def test_gitignore_when_sqlite3
    run_generator

    assert_file '.gitignore' do |content|
      assert_match(/sqlite3/, content)
    end
  end

  def test_gitignore_when_no_active_record
    run_generator [destination_root, '--skip-active-record']

    assert_file '.gitignore' do |content|
463
      assert_no_match(/sqlite/i, content)
464 465 466 467 468 469 470
    end
  end

  def test_gitignore_when_non_sqlite3_db
    run_generator([destination_root, "-d", "mysql"])

    assert_file '.gitignore' do |content|
471
      assert_no_match(/sqlite/i, content)
472 473 474 475
    end
  end

  protected
J
José Valim 已提交
476

477
  def action(*args, &block)
478
    silence(:stdout) { generator.send(*args, &block) }
479
  end
O
Oscar Del Ben 已提交
480 481 482 483

  def assert_gem(gem)
    assert_file "Gemfile", /^gem\s+["']#{gem}["']$/
  end
484
end