app_generator_test.rb 14.8 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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
  def test_add_gemfile_entry
    template = Tempfile.open 'my_template'
    template.puts 'gemfile_entry "tenderlove"'
    template.flush

    run_generator([destination_root, "-m", template.path])
    assert_file "Gemfile", /tenderlove/
  ensure
    template.close
    template.unlink
  end

  def test_add_skip_entry
    template = Tempfile.open 'my_template'
    template.puts 'add_gem_entry_filter { |gem| gem.name != "jbuilder" }'
    template.flush

    run_generator([destination_root, "-m", template.path])
    assert_file "Gemfile" do |contents|
      assert_no_match 'jbuilder', contents
    end
  ensure
    template.close
    template.unlink
  end

192
  def test_skip_turbolinks_when_it_is_not_on_gemfile
193 194 195 196 197 198 199 200
    template = Tempfile.open 'my_template'
    template.puts 'add_gem_entry_filter { |gem| gem.name != "turbolinks" }'
    template.flush

    run_generator([destination_root, "-m", template.path])
    assert_file "Gemfile" do |contents|
      assert_no_match 'turbolinks', contents
    end
201 202

    assert_file "app/views/layouts/application.html.erb" do |contents|
203 204
      assert_no_match 'turbolinks', contents
    end
205

206
    assert_file "app/views/layouts/application.html.erb" do |contents|
207
      assert_no_match('data-turbolinks-track', contents)
208 209 210
    end

    assert_file "app/assets/javascripts/application.js" do |contents|
211
      assert_no_match 'turbolinks', contents
212 213 214 215 216 217
    end
  ensure
    template.close
    template.unlink
  end

218 219 220
  def test_config_another_database
    run_generator([destination_root, "-d", "mysql"])
    assert_file "config/database.yml", /mysql/
221
    unless defined?(JRUBY_VERSION)
O
Oscar Del Ben 已提交
222
      assert_gem "mysql2"
223
    else
O
Oscar Del Ben 已提交
224
      assert_gem "activerecord-jdbcmysql-adapter"
225
    end
J
José Valim 已提交
226 227
  end

228 229 230 231 232
  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

233 234 235 236
  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 已提交
237
      assert_gem "pg"
238
    else
O
Oscar Del Ben 已提交
239
      assert_gem "activerecord-jdbcpostgresql-adapter"
240 241 242
    end
  end

243 244
  def test_config_jdbcmysql_database
    run_generator([destination_root, "-d", "jdbcmysql"])
245
    assert_file "config/database.yml", /mysql/
O
Oscar Del Ben 已提交
246
    assert_gem "activerecord-jdbcmysql-adapter"
247 248
  end

249 250
  def test_config_jdbcsqlite3_database
    run_generator([destination_root, "-d", "jdbcsqlite3"])
251
    assert_file "config/database.yml", /sqlite3/
O
Oscar Del Ben 已提交
252
    assert_gem "activerecord-jdbcsqlite3-adapter"
253 254
  end

255 256
  def test_config_jdbcpostgresql_database
    run_generator([destination_root, "-d", "jdbcpostgresql"])
257
    assert_file "config/database.yml", /postgresql/
O
Oscar Del Ben 已提交
258
    assert_gem "activerecord-jdbcpostgresql-adapter"
259 260
  end

A
Arun Agrawal 已提交
261 262 263
  def test_config_jdbc_database
    run_generator([destination_root, "-d", "jdbc"])
    assert_file "config/database.yml", /jdbc/
264
    assert_file "config/database.yml", /mssql/
O
Oscar Del Ben 已提交
265
    assert_gem "activerecord-jdbc-adapter"
A
Arun Agrawal 已提交
266 267
  end

268 269 270 271
  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 已提交
272
      assert_gem "activerecord-jdbcsqlite3-adapter"
273 274 275
    end
  end

276
  def test_generator_if_skip_active_record_is_given
277
    run_generator [destination_root, "--skip-active-record"]
J
José Valim 已提交
278
    assert_no_file "config/database.yml"
279
    assert_file "config/application.rb", /#\s+require\s+["']active_record\/railtie["']/
280
    assert_file "test/test_helper.rb" do |helper_content|
281
      assert_no_match(/fixtures :all/, helper_content)
282
    end
J
José Valim 已提交
283 284
  end

285 286 287 288 289
  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

290
  def test_generator_if_skip_sprockets_is_given
291 292
    run_generator [destination_root, "--skip-sprockets"]
    assert_file "config/application.rb" do |content|
293
      assert_match(/#\s+require\s+["']sprockets\/railtie["']/, content)
294
    end
295 296 297
    assert_file "Gemfile" do |content|
      assert_no_match(/sass-rails/, content)
      assert_no_match(/uglifier/, content)
298
      assert_match(/coffee-rails/, content)
299
    end
300 301 302 303 304
    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 已提交
305 306
      assert_no_match(/config\.assets\.js_compressor = :uglifier/, content)
      assert_no_match(/config\.assets\.css_compressor = :sass/, content)
307
      assert_no_match(/config\.assets\.version = '1\.0'/, content)
308
    end
309
  end
J
José Valim 已提交
310

311
  def test_inclusion_of_javascript_runtime
312
    run_generator([destination_root])
313
    if defined?(JRUBY_VERSION)
O
Oscar Del Ben 已提交
314
      assert_gem "therubyrhino"
315
    else
A
Arun Agrawal 已提交
316
      assert_file "Gemfile", /# gem\s+["']therubyracer["']+, \s+platforms: :ruby$/
317 318 319
    end
  end

A
Arun Agrawal 已提交
320 321 322 323 324 325 326
  def test_inclusion_of_plateform_dependent_gems
    run_generator([destination_root])
    if RUBY_ENGINE == 'rbx'
      assert_gem 'rubysl'
    end
  end

327 328 329 330 331 332
  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 已提交
333
    assert_gem "jquery-rails"
334 335 336 337 338 339 340 341
  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 已提交
342
    assert_gem "prototype-rails"
J
José Valim 已提交
343
  end
344

345 346
  def test_javascript_is_skipped_if_required
    run_generator [destination_root, "--skip-javascript"]
347 348 349 350

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

351 352
    assert_file "app/views/layouts/application.html.erb" do |contents|
      assert_match(/stylesheet_link_tag\s+"application", media: "all" %>/, contents)
353
      assert_no_match(/javascript_include_tag\s+"application" \%>/, contents)
354
    end
355

356
    assert_file "Gemfile" do |content|
357
      assert_no_match(/coffee-rails/, content)
A
Arun Agrawal 已提交
358
      assert_no_match(/jquery-rails/, content)
359
    end
360
  end
361

A
Arun Agrawal 已提交
362 363 364 365 366
  def test_inclusion_of_jbuilder
    run_generator
    assert_file "Gemfile", /gem 'jbuilder'/
  end

S
Santiago Pastorino 已提交
367
  def test_inclusion_of_debugger
368
    run_generator
A
Arun Agrawal 已提交
369 370 371 372 373 374 375
    if defined?(JRUBY_VERSION)
      assert_file "Gemfile" do |content|
        assert_no_match(/debugger/, content)
      end
    else
      assert_file "Gemfile", /# gem 'debugger'/
    end
376 377
  end

X
Xavier Noria 已提交
378 379
  def test_inclusion_of_lazy_loaded_sdoc
    run_generator
A
Arun Agrawal 已提交
380
    assert_file 'Gemfile', /gem 'sdoc', \s+group: :doc, require: false/
X
Xavier Noria 已提交
381 382
  end

383
  def test_template_from_dir_pwd
384
    FileUtils.cd(Rails.root)
385
    assert_match(/It works from file!/, run_generator([destination_root, "-m", "lib/template.rb"]))
386 387
  end

J
José Valim 已提交
388 389 390 391 392 393
  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
394
    Rails::Generators::AppGenerator.expects(:usage_path).returns(nil)
395
    assert_match(/Create rails files for app generator/, Rails::Generators::AppGenerator.desc)
J
José Valim 已提交
396 397 398
  end

  def test_default_namespace
399
    assert_match "rails:app", Rails::Generators::AppGenerator.namespace
J
José Valim 已提交
400 401
  end

402 403 404 405 406
  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

407 408
  def test_test_unit_is_removed_from_frameworks_if_skip_test_unit_is_given
    run_generator [destination_root, "--skip-test-unit"]
409 410 411 412 413 414 415
    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["']/
416 417
  end

418 419 420
  def test_new_hash_style
    run_generator [destination_root]
    assert_file "config/initializers/session_store.rb" do |file|
421
      assert_match(/config.session_store :cookie_store, key: '_.+_session'/, file)
422 423 424
    end
  end

425 426 427 428 429
  def test_pretend_option
    output = run_generator [File.join(destination_root, "myapp"), "--pretend"]
    assert_no_match(/run  bundle install/, output)
  end

430 431 432 433 434 435 436 437 438 439
  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

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
  def test_spring
    run_generator
    assert_file "Gemfile", /gem 'spring'/
  end

  def test_spring_binstubs
    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
    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

468
protected
J
José Valim 已提交
469

470
  def action(*args, &block)
471
    silence(:stdout) { generator.send(*args, &block) }
472
  end
O
Oscar Del Ben 已提交
473 474 475 476

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