app_generator_test.rb 14.0 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
    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/)
63
    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(:update_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(:update_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 166 167 168
  end

  def test_config_another_database
    run_generator([destination_root, "-d", "mysql"])
    assert_file "config/database.yml", /mysql/
169
    unless defined?(JRUBY_VERSION)
O
Oscar Del Ben 已提交
170
      assert_gem "mysql2"
171
    else
O
Oscar Del Ben 已提交
172
      assert_gem "activerecord-jdbcmysql-adapter"
173
    end
J
José Valim 已提交
174 175
  end

176 177 178 179 180
  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

181 182 183 184
  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 已提交
185
      assert_gem "pg"
186
    else
O
Oscar Del Ben 已提交
187
      assert_gem "activerecord-jdbcpostgresql-adapter"
188 189 190
    end
  end

191 192
  def test_config_jdbcmysql_database
    run_generator([destination_root, "-d", "jdbcmysql"])
193
    assert_file "config/database.yml", /mysql/
O
Oscar Del Ben 已提交
194
    assert_gem "activerecord-jdbcmysql-adapter"
195 196
  end

197 198
  def test_config_jdbcsqlite3_database
    run_generator([destination_root, "-d", "jdbcsqlite3"])
199
    assert_file "config/database.yml", /sqlite3/
O
Oscar Del Ben 已提交
200
    assert_gem "activerecord-jdbcsqlite3-adapter"
201 202
  end

203 204
  def test_config_jdbcpostgresql_database
    run_generator([destination_root, "-d", "jdbcpostgresql"])
205
    assert_file "config/database.yml", /postgresql/
O
Oscar Del Ben 已提交
206
    assert_gem "activerecord-jdbcpostgresql-adapter"
207 208
  end

A
Arun Agrawal 已提交
209 210 211
  def test_config_jdbc_database
    run_generator([destination_root, "-d", "jdbc"])
    assert_file "config/database.yml", /jdbc/
212
    assert_file "config/database.yml", /mssql/
O
Oscar Del Ben 已提交
213
    assert_gem "activerecord-jdbc-adapter"
A
Arun Agrawal 已提交
214 215
  end

216 217 218 219
  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 已提交
220
      assert_gem "activerecord-jdbcsqlite3-adapter"
221 222 223
    end
  end

224
  def test_generator_if_skip_active_record_is_given
225
    run_generator [destination_root, "--skip-active-record"]
J
José Valim 已提交
226
    assert_no_file "config/database.yml"
227
    assert_file "config/application.rb", /#\s+require\s+["']active_record\/railtie["']/
228
    assert_file "test/test_helper.rb" do |helper_content|
229
      assert_no_match(/fixtures :all/, helper_content)
230
    end
J
José Valim 已提交
231 232
  end

233 234 235 236 237
  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

238
  def test_generator_if_skip_sprockets_is_given
239
    run_generator [destination_root, "--skip-sprockets"]
240
    assert_no_file "config/initializers/assets.rb"
241
    assert_file "config/application.rb" do |content|
242
      assert_match(/#\s+require\s+["']sprockets\/railtie["']/, content)
243
    end
244 245 246
    assert_file "Gemfile" do |content|
      assert_no_match(/sass-rails/, content)
      assert_no_match(/uglifier/, content)
247
      assert_match(/coffee-rails/, content)
248
    end
249 250 251 252 253
    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 已提交
254 255
      assert_no_match(/config\.assets\.js_compressor = :uglifier/, content)
      assert_no_match(/config\.assets\.css_compressor = :sass/, content)
256
    end
257
  end
J
José Valim 已提交
258

259
  def test_inclusion_of_javascript_runtime
260
    run_generator([destination_root])
261
    if defined?(JRUBY_VERSION)
O
Oscar Del Ben 已提交
262
      assert_gem "therubyrhino"
263
    else
A
Arun Agrawal 已提交
264
      assert_file "Gemfile", /# gem\s+["']therubyracer["']+, \s+platforms: :ruby$/
265 266 267
    end
  end

268 269 270 271 272 273
  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 已提交
274
    assert_gem "jquery-rails"
275 276 277 278 279 280 281 282
  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 已提交
283
    assert_gem "prototype-rails"
J
José Valim 已提交
284
  end
285

286 287
  def test_javascript_is_skipped_if_required
    run_generator [destination_root, "--skip-javascript"]
288 289 290 291

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

292
    assert_file "app/views/layouts/application.html.erb" do |contents|
293 294
      assert_match(/stylesheet_link_tag\s+'application', media: 'all' %>/, contents)
      assert_no_match(/javascript_include_tag\s+'application' \%>/, contents)
295
    end
296

297
    assert_file "Gemfile" do |content|
298
      assert_no_match(/coffee-rails/, content)
A
Arun Agrawal 已提交
299
      assert_no_match(/jquery-rails/, content)
300
    end
301
  end
302

A
Arun Agrawal 已提交
303 304 305 306 307
  def test_inclusion_of_jbuilder
    run_generator
    assert_file "Gemfile", /gem 'jbuilder'/
  end

308
  def test_inclusion_of_a_debugger
309
    run_generator
A
Arun Agrawal 已提交
310 311
    if defined?(JRUBY_VERSION)
      assert_file "Gemfile" do |content|
312
        assert_no_match(/byebug/, content)
A
Arun Agrawal 已提交
313 314
        assert_no_match(/debugger/, content)
      end
315
    elsif RUBY_VERSION < '2.0.0'
A
Arun Agrawal 已提交
316
      assert_file "Gemfile", /# gem 'debugger'/
317 318
    else
      assert_file "Gemfile", /# gem 'byebug'/
A
Arun Agrawal 已提交
319
    end
320 321
  end

X
Xavier Noria 已提交
322
  def test_inclusion_of_doc
X
Xavier Noria 已提交
323
    run_generator
X
Xavier Noria 已提交
324
    assert_file 'Gemfile', /gem 'sdoc',\s+'~> 0.4.0',\s+group: :doc/
X
Xavier Noria 已提交
325 326
  end

327
  def test_template_from_dir_pwd
328
    FileUtils.cd(Rails.root)
329
    assert_match(/It works from file!/, run_generator([destination_root, "-m", "lib/template.rb"]))
330 331
  end

J
José Valim 已提交
332 333 334 335 336 337
  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
338
    Rails::Generators::AppGenerator.expects(:usage_path).returns(nil)
339
    assert_match(/Create rails files for app generator/, Rails::Generators::AppGenerator.desc)
J
José Valim 已提交
340 341 342
  end

  def test_default_namespace
343
    assert_match "rails:app", Rails::Generators::AppGenerator.namespace
J
José Valim 已提交
344 345
  end

346 347 348 349 350
  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

351 352
  def test_test_unit_is_removed_from_frameworks_if_skip_test_unit_is_given
    run_generator [destination_root, "--skip-test-unit"]
353 354 355 356 357 358 359
    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["']/
360 361
  end

362 363 364
  def test_new_hash_style
    run_generator [destination_root]
    assert_file "config/initializers/session_store.rb" do |file|
365
      assert_match(/config.session_store :cookie_store, key: '_.+_session'/, file)
366 367 368
    end
  end

369 370 371 372 373
  def test_pretend_option
    output = run_generator [File.join(destination_root, "myapp"), "--pretend"]
    assert_no_match(/run  bundle install/, output)
  end

374 375 376 377 378 379 380 381 382 383
  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

384 385
  def test_spring
    run_generator
386
    assert_file "Gemfile", /gem 'spring', \s+group: :development/
387 388 389
  end

  def test_spring_binstubs
390
    jruby_skip "spring doesn't run on JRuby"
391 392 393 394 395 396
    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
397
    jruby_skip "spring doesn't run on JRuby"
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
    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

414 415 416 417 418 419 420 421 422 423 424 425
  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|
426
      assert_no_match(/sqlite/i, content)
427 428 429 430 431 432 433
    end
  end

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

    assert_file '.gitignore' do |content|
434
      assert_no_match(/sqlite/i, content)
435 436 437 438
    end
  end

  protected
J
José Valim 已提交
439

440
  def action(*args, &block)
441
    silence(:stdout) { generator.send(*args, &block) }
442
  end
O
Oscar Del Ben 已提交
443 444 445 446

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