app_generator_test.rb 15.5 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 25 26 27 28 29
  config/environments
  config/initializers
  config/locales
  db
  lib
  lib/tasks
30
  lib/assets
31
  log
A
Arun Agrawal 已提交
32
  test/test_helper.rb
33
  test/fixtures
M
Mike Moore 已提交
34 35 36 37
  test/controllers
  test/models
  test/helpers
  test/mailers
38 39
  test/integration
  vendor
J
José Valim 已提交
40
  vendor/assets
A
Arun Agrawal 已提交
41 42
  vendor/assets/stylesheets
  vendor/assets/javascripts
43
  tmp/cache
J
José Valim 已提交
44
  tmp/cache/assets
45 46
)

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

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

54 55
  def default_files
    ::DEFAULT_APP_FILES
56 57
  end

58
  def test_assets
59
    run_generator
60 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/)
    assert_file("app/assets/stylesheets/application.css")
A
Arun Agrawal 已提交
64
    assert_file("app/assets/javascripts/application.js")
65 66
  end

67
  def test_invalid_application_name_raises_an_error
68
    content = capture(:stderr){ run_generator [File.join(destination_root, "43-things")] }
69 70 71 72
    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
73
    run_generator [File.join(destination_root, "things-43")]
74
    assert_file "things-43/config/environment.rb", /Rails\.application\.initialize!/
75
    assert_file "things-43/config/application.rb", /^module Things43$/
76 77
  end

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

94 95 96 97 98 99
  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
100
    assert_match(/rails new APP_PATH \[options\]/, output)
101 102 103
    assert_equal true, $?.success?
  end

104 105 106 107 108 109 110 111
  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")
112
    Rails.application.stubs(:is_a?).returns(Rails::Application)
113 114 115

    FileUtils.mv(app_root, app_moved_root)

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

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

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

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

135
    generator = Rails::Generators::AppGenerator.new ["rails"], { with_dispatchers: true }, destination_root: app_root, shell: @shell
136
    generator.send(:app_const)
137
    quietly { generator.send(:create_config_files) }
138 139
    assert_file "myapp/config/initializers/session_store.rb", /_myapp_session/
  end
140

141 142
  def test_application_names_are_not_singularized
    run_generator [File.join(destination_root, "hats")]
143
    assert_file "hats/config/environment.rb", /Rails\.application\.initialize!/
144 145
  end

146 147 148 149 150
  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 已提交
151
        assert_no_match %r{/^[ \t]+$/}, line
152 153 154 155
      end
    end
  end

J
José Valim 已提交
156 157 158
  def test_config_database_is_added_by_default
    run_generator
    assert_file "config/database.yml", /sqlite3/
159
    unless defined?(JRUBY_VERSION)
O
Oscar Del Ben 已提交
160
      assert_gem "sqlite3"
161
    else
O
Oscar Del Ben 已提交
162
      assert_gem "activerecord-jdbcsqlite3-adapter"
163
    end
164 165
  end

166 167 168 169 170 171 172 173 174
  def test_arbitrary_code
    output = Tempfile.open('my_template') do |template|
      template.puts 'puts "You are using Rails version #{Rails::VERSION::STRING}."'
      template.close
      run_generator([destination_root, "-m", template.path])
    end
    assert_match 'You are using', output
  end

175
  def test_add_gemfile_entry
176 177 178 179
    Tempfile.open('my_template') do |template|
      template.puts 'gemfile_entry "tenderlove"'
      template.flush
      template.close
180
      run_generator([destination_root, "-n", template.path])
181 182
      assert_file "Gemfile", /tenderlove/
    end
183 184 185
  end

  def test_add_skip_entry
186 187
    Tempfile.open 'my_template' do |template|
      template.puts 'add_gem_entry_filter { |gem| gem.name != "jbuilder" }'
188
      template.close
189

190 191 192 193 194 195 196 197 198 199 200 201 202
      run_generator([destination_root, "-n", template.path])
      assert_file "Gemfile" do |contents|
        assert_no_match 'jbuilder', contents
      end
    end
  end

  def test_remove_gem
    Tempfile.open 'my_template' do |template|
      template.puts 'remove_gem "jbuilder"'
      template.close

      run_generator([destination_root, "-n", template.path])
203 204 205
      assert_file "Gemfile" do |contents|
        assert_no_match 'jbuilder', contents
      end
206 207 208
    end
  end

209
  def test_skip_turbolinks_when_it_is_not_on_gemfile
210 211 212
    Tempfile.open 'my_template' do |template|
      template.puts 'add_gem_entry_filter { |gem| gem.name != "turbolinks" }'
      template.flush
213

214
      run_generator([destination_root, "-n", template.path])
215 216 217
      assert_file "Gemfile" do |contents|
        assert_no_match 'turbolinks', contents
      end
218

219 220 221
      assert_file "app/views/layouts/application.html.erb" do |contents|
        assert_no_match 'turbolinks', contents
      end
222

223 224 225
      assert_file "app/views/layouts/application.html.erb" do |contents|
        assert_no_match('data-turbolinks-track', contents)
      end
226

227 228 229
      assert_file "app/assets/javascripts/application.js" do |contents|
        assert_no_match 'turbolinks', contents
      end
230 231 232
    end
  end

233 234 235
  def test_config_another_database
    run_generator([destination_root, "-d", "mysql"])
    assert_file "config/database.yml", /mysql/
236
    unless defined?(JRUBY_VERSION)
O
Oscar Del Ben 已提交
237
      assert_gem "mysql2"
238
    else
O
Oscar Del Ben 已提交
239
      assert_gem "activerecord-jdbcmysql-adapter"
240
    end
J
José Valim 已提交
241 242
  end

243 244 245 246 247
  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

248 249 250 251
  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 已提交
252
      assert_gem "pg"
253
    else
O
Oscar Del Ben 已提交
254
      assert_gem "activerecord-jdbcpostgresql-adapter"
255 256 257
    end
  end

258 259
  def test_config_jdbcmysql_database
    run_generator([destination_root, "-d", "jdbcmysql"])
260
    assert_file "config/database.yml", /mysql/
O
Oscar Del Ben 已提交
261
    assert_gem "activerecord-jdbcmysql-adapter"
262 263
  end

264 265
  def test_config_jdbcsqlite3_database
    run_generator([destination_root, "-d", "jdbcsqlite3"])
266
    assert_file "config/database.yml", /sqlite3/
O
Oscar Del Ben 已提交
267
    assert_gem "activerecord-jdbcsqlite3-adapter"
268 269
  end

270 271
  def test_config_jdbcpostgresql_database
    run_generator([destination_root, "-d", "jdbcpostgresql"])
272
    assert_file "config/database.yml", /postgresql/
O
Oscar Del Ben 已提交
273
    assert_gem "activerecord-jdbcpostgresql-adapter"
274 275
  end

A
Arun Agrawal 已提交
276 277 278
  def test_config_jdbc_database
    run_generator([destination_root, "-d", "jdbc"])
    assert_file "config/database.yml", /jdbc/
279
    assert_file "config/database.yml", /mssql/
O
Oscar Del Ben 已提交
280
    assert_gem "activerecord-jdbc-adapter"
A
Arun Agrawal 已提交
281 282
  end

283 284 285 286
  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 已提交
287
      assert_gem "activerecord-jdbcsqlite3-adapter"
288 289 290
    end
  end

291
  def test_generator_if_skip_active_record_is_given
292
    run_generator [destination_root, "--skip-active-record"]
J
José Valim 已提交
293
    assert_no_file "config/database.yml"
294
    assert_file "config/application.rb", /#\s+require\s+["']active_record\/railtie["']/
295
    assert_file "test/test_helper.rb" do |helper_content|
296
      assert_no_match(/fixtures :all/, helper_content)
297
    end
J
José Valim 已提交
298 299
  end

300 301 302 303 304
  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

305
  def test_generator_if_skip_sprockets_is_given
306 307
    run_generator [destination_root, "--skip-sprockets"]
    assert_file "config/application.rb" do |content|
308
      assert_match(/#\s+require\s+["']sprockets\/railtie["']/, content)
309
    end
310 311 312
    assert_file "Gemfile" do |content|
      assert_no_match(/sass-rails/, content)
      assert_no_match(/uglifier/, content)
313
      assert_match(/coffee-rails/, content)
314
    end
315 316 317 318 319
    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 已提交
320 321
      assert_no_match(/config\.assets\.js_compressor = :uglifier/, content)
      assert_no_match(/config\.assets\.css_compressor = :sass/, content)
322
      assert_no_match(/config\.assets\.version = '1\.0'/, content)
323
    end
324
  end
J
José Valim 已提交
325

326
  def test_inclusion_of_javascript_runtime
327
    run_generator([destination_root])
328
    if defined?(JRUBY_VERSION)
O
Oscar Del Ben 已提交
329
      assert_gem "therubyrhino"
330
    else
A
Arun Agrawal 已提交
331
      assert_file "Gemfile", /# gem\s+["']therubyracer["']+, \s+platforms: :ruby$/
332 333 334
    end
  end

A
Arun Agrawal 已提交
335 336 337 338 339 340 341
  def test_inclusion_of_plateform_dependent_gems
    run_generator([destination_root])
    if RUBY_ENGINE == 'rbx'
      assert_gem 'rubysl'
    end
  end

342 343 344 345 346 347
  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 已提交
348
    assert_gem "jquery-rails"
349 350 351 352 353 354 355 356
  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 已提交
357
    assert_gem "prototype-rails"
J
José Valim 已提交
358
  end
359

360 361
  def test_javascript_is_skipped_if_required
    run_generator [destination_root, "--skip-javascript"]
362 363 364 365

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

366 367
    assert_file "app/views/layouts/application.html.erb" do |contents|
      assert_match(/stylesheet_link_tag\s+"application", media: "all" %>/, contents)
368
      assert_no_match(/javascript_include_tag\s+"application" \%>/, contents)
369
    end
370

371
    assert_file "Gemfile" do |content|
372
      assert_no_match(/coffee-rails/, content)
A
Arun Agrawal 已提交
373
      assert_no_match(/jquery-rails/, content)
374
    end
375
  end
376

A
Arun Agrawal 已提交
377 378 379 380 381
  def test_inclusion_of_jbuilder
    run_generator
    assert_file "Gemfile", /gem 'jbuilder'/
  end

S
Santiago Pastorino 已提交
382
  def test_inclusion_of_debugger
383
    run_generator
A
Arun Agrawal 已提交
384 385 386 387 388 389 390
    if defined?(JRUBY_VERSION)
      assert_file "Gemfile" do |content|
        assert_no_match(/debugger/, content)
      end
    else
      assert_file "Gemfile", /# gem 'debugger'/
    end
391 392
  end

X
Xavier Noria 已提交
393
  def test_inclusion_of_doc
X
Xavier Noria 已提交
394
    run_generator
X
Xavier Noria 已提交
395
    assert_file 'Gemfile', /gem 'sdoc',\s+'~> 0.4.0',\s+group: :doc/
X
Xavier Noria 已提交
396 397
  end

398
  def test_template_from_dir_pwd
399
    FileUtils.cd(Rails.root)
400
    assert_match(/It works from file!/, run_generator([destination_root, "-m", "lib/template.rb"]))
401 402
  end

J
José Valim 已提交
403 404 405 406 407 408
  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
409
    Rails::Generators::AppGenerator.expects(:usage_path).returns(nil)
410
    assert_match(/Create rails files for app generator/, Rails::Generators::AppGenerator.desc)
J
José Valim 已提交
411 412 413
  end

  def test_default_namespace
414
    assert_match "rails:app", Rails::Generators::AppGenerator.namespace
J
José Valim 已提交
415 416
  end

417 418 419 420 421
  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

422 423
  def test_test_unit_is_removed_from_frameworks_if_skip_test_unit_is_given
    run_generator [destination_root, "--skip-test-unit"]
424 425 426 427 428 429 430
    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["']/
431 432
  end

433 434 435
  def test_new_hash_style
    run_generator [destination_root]
    assert_file "config/initializers/session_store.rb" do |file|
436
      assert_match(/config.session_store :cookie_store, key: '_.+_session'/, file)
437 438 439
    end
  end

440 441 442 443 444
  def test_pretend_option
    output = run_generator [File.join(destination_root, "myapp"), "--pretend"]
    assert_no_match(/run  bundle install/, output)
  end

445 446 447 448 449 450 451 452 453 454
  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

455 456
  def test_spring
    run_generator
457
    assert_file "Gemfile", /gem 'spring', \s+group: :development/
458 459 460
  end

  def test_spring_binstubs
461
    skip "spring doesn't run on JRuby" if defined?(JRUBY_VERSION)
462 463 464 465 466 467
    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
468
    skip "spring doesn't run on JRuby" if defined?(JRUBY_VERSION)
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
    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

485
protected
J
José Valim 已提交
486

487
  def action(*args, &block)
488
    silence(:stdout) { generator.send(*args, &block) }
489
  end
O
Oscar Del Ben 已提交
490 491 492 493

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