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 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)

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

124 125 126
  def test_rails_update_generates_correct_session_key
    app_root = File.join(destination_root, 'myapp')
    run_generator [app_root]
127

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

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

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

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

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

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
  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

189
  def test_skip_turbolinks_when_it_is_not_on_gemfile
190 191 192 193 194 195 196 197
    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
198 199

    assert_file "app/views/layouts/application.html.erb" do |contents|
200 201
      assert_no_match 'turbolinks', contents
    end
202

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

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

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

225 226 227 228 229
  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

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

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

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

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

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

265 266 267 268
  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 已提交
269
      assert_gem "activerecord-jdbcsqlite3-adapter"
270 271 272
    end
  end

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

282 283 284 285 286
  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

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

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

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

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

343 344
  def test_javascript_is_skipped_if_required
    run_generator [destination_root, "--skip-javascript"]
345 346 347 348

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

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

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

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

S
Santiago Pastorino 已提交
365
  def test_inclusion_of_debugger
366
    run_generator
O
Oscar Del Ben 已提交
367
    assert_file "Gemfile", /# gem 'debugger'/
368 369
  end

X
Xavier Noria 已提交
370 371
  def test_inclusion_of_lazy_loaded_sdoc
    run_generator
A
Arun Agrawal 已提交
372
    assert_file 'Gemfile', /gem 'sdoc', \s+group: :doc, require: false/
X
Xavier Noria 已提交
373 374
  end

375
  def test_template_from_dir_pwd
376
    FileUtils.cd(Rails.root)
377
    assert_match(/It works from file!/, run_generator([destination_root, "-m", "lib/template.rb"]))
378 379
  end

J
José Valim 已提交
380 381 382 383 384 385
  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
386
    Rails::Generators::AppGenerator.expects(:usage_path).returns(nil)
387
    assert_match(/Create rails files for app generator/, Rails::Generators::AppGenerator.desc)
J
José Valim 已提交
388 389 390
  end

  def test_default_namespace
391
    assert_match "rails:app", Rails::Generators::AppGenerator.namespace
J
José Valim 已提交
392 393
  end

394 395 396 397 398
  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

399 400
  def test_test_unit_is_removed_from_frameworks_if_skip_test_unit_is_given
    run_generator [destination_root, "--skip-test-unit"]
401 402 403 404 405 406 407
    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["']/
408 409
  end

410 411 412
  def test_new_hash_style
    run_generator [destination_root]
    assert_file "config/initializers/session_store.rb" do |file|
413
      assert_match(/config.session_store :cookie_store, key: '_.+_session'/, file)
414 415 416
    end
  end

417 418 419 420 421
  def test_pretend_option
    output = run_generator [File.join(destination_root, "myapp"), "--pretend"]
    assert_no_match(/run  bundle install/, output)
  end

422 423 424 425 426 427 428 429 430 431
  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

432
protected
J
José Valim 已提交
433

434
  def action(*args, &block)
435
    silence(:stdout) { generator.send(*args, &block) }
436
  end
O
Oscar Del Ben 已提交
437 438 439 440

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