app_generator_test.rb 14.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 32
  log
  test/fixtures
M
Mike Moore 已提交
33 34 35 36
  test/controllers
  test/models
  test/helpers
  test/mailers
37 38
  test/integration
  vendor
J
José Valim 已提交
39
  vendor/assets
40
  tmp/cache
J
José Valim 已提交
41
  tmp/cache/assets
42 43
)

44 45
class AppGeneratorTest < Rails::Generators::TestCase
  include GeneratorsTestHelper
46
  arguments [destination_root]
47 48

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

51 52
  def default_files
    ::DEFAULT_APP_FILES
53 54
  end

55
  def test_assets
56
    run_generator
57 58 59 60

    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")
61 62
  end

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

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

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

100 101 102 103 104 105 106 107
  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")
108
    Rails.application.stubs(:is_a?).returns(Rails::Application)
109 110 111

    FileUtils.mv(app_root, app_moved_root)

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

120 121 122
  def test_rails_update_generates_correct_session_key
    app_root = File.join(destination_root, 'myapp')
    run_generator [app_root]
123

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

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

134 135
  def test_application_names_are_not_singularized
    run_generator [File.join(destination_root, "hats")]
136
    assert_file "hats/config/environment.rb", /Rails\.application\.initialize!/
137 138
  end

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

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

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
  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

185
  def test_skip_turbolinks_when_it_is_not_on_gemfile
186 187 188 189 190 191 192 193
    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
194 195

    assert_file "app/views/layouts/application.html.erb" do |contents|
196 197
      assert_no_match 'turbolinks', contents
    end
198

199
    assert_file "app/views/layouts/application.html.erb" do |contents|
200 201 202 203 204
      assert_no_match(/stylesheet_link_tag\s+"application", media: "all", "data-turbolinks-track" => true/, contents)
      assert_no_match(/javascript_include_tag\s+"application", "data-turbolinks-track" => true/, contents)
    end

    assert_file "app/assets/javascripts/application.js" do |contents|
205
      assert_no_match %r{^//= require turbolinks}, contents
206 207 208 209 210 211
    end
  ensure
    template.close
    template.unlink
  end

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

222 223 224 225 226
  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

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

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

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

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

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

262 263 264 265
  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 已提交
266
      assert_gem "activerecord-jdbcsqlite3-adapter"
267 268 269
    end
  end

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

279 280 281 282 283
  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

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

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

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

322
  def test_creation_of_a_test_directory
J
José Valim 已提交
323
    run_generator
324 325 326
    assert_file 'test'
  end

327 328 329 330 331
  def test_creation_of_app_assets_images_directory
    run_generator
    assert_file "app/assets/images"
  end

332 333 334 335 336
  def test_creation_of_vendor_assets_javascripts_directory
    run_generator
    assert_file "vendor/assets/javascripts"
  end

337 338 339 340 341
  def test_creation_of_vendor_assets_stylesheets_directory
    run_generator
    assert_file "vendor/assets/stylesheets"
  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
348
    assert_file "Gemfile", /^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)
373
    end
374
  end
375

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

S
Santiago Pastorino 已提交
381
  def test_inclusion_of_debugger
382
    run_generator
O
Oscar Del Ben 已提交
383
    assert_file "Gemfile", /# gem 'debugger'/
384 385
  end

X
Xavier Noria 已提交
386 387
  def test_inclusion_of_lazy_loaded_sdoc
    run_generator
A
Arun Agrawal 已提交
388
    assert_file 'Gemfile', /gem 'sdoc', \s+group: :doc, require: false/
X
Xavier Noria 已提交
389 390
  end

391
  def test_template_from_dir_pwd
392
    FileUtils.cd(Rails.root)
393
    assert_match(/It works from file!/, run_generator([destination_root, "-m", "lib/template.rb"]))
394 395
  end

J
José Valim 已提交
396 397 398 399 400 401
  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
402
    Rails::Generators::AppGenerator.expects(:usage_path).returns(nil)
403
    assert_match(/Create rails files for app generator/, Rails::Generators::AppGenerator.desc)
J
José Valim 已提交
404 405 406
  end

  def test_default_namespace
407
    assert_match "rails:app", Rails::Generators::AppGenerator.namespace
J
José Valim 已提交
408 409
  end

410 411 412 413 414
  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

415 416
  def test_test_unit_is_removed_from_frameworks_if_skip_test_unit_is_given
    run_generator [destination_root, "--skip-test-unit"]
417 418 419 420 421 422 423
    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["']/
424 425
  end

426 427 428
  def test_new_hash_style
    run_generator [destination_root]
    assert_file "config/initializers/session_store.rb" do |file|
429
      assert_match(/config.session_store :cookie_store, key: '_.+_session'/, file)
430 431 432
    end
  end

433 434 435 436 437
  def test_pretend_option
    output = run_generator [File.join(destination_root, "myapp"), "--pretend"]
    assert_no_match(/run  bundle install/, output)
  end

438 439 440 441 442 443 444 445 446 447
  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

448
protected
J
José Valim 已提交
449

450
  def action(*args, &block)
451
    silence(:stdout) { generator.send(*args, &block) }
452
  end
O
Oscar Del Ben 已提交
453 454 455 456

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