app_generator_test.rb 13.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.rb'
J
José Valim 已提交
4

5 6 7 8 9
DEFAULT_APP_FILES = %w(
  .gitignore
  Gemfile
  Rakefile
  config.ru
J
José Valim 已提交
10 11
  app/assets/javascripts
  app/assets/stylesheets
12
  app/assets/images
13 14
  app/controllers
  app/helpers
15
  app/mailers
16 17 18 19 20 21 22 23 24
  app/models
  app/views/layouts
  config/environments
  config/initializers
  config/locales
  db
  doc
  lib
  lib/tasks
25
  lib/assets
26 27 28 29 30 31 32 33
  log
  script/rails
  test/fixtures
  test/functional
  test/integration
  test/performance
  test/unit
  vendor
J
José Valim 已提交
34
  vendor/assets
35
  tmp/cache
J
José Valim 已提交
36
  tmp/cache/assets
37 38
)

39 40
class AppGeneratorTest < Rails::Generators::TestCase
  include GeneratorsTestHelper
41
  arguments [destination_root]
42 43

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

46 47
  def default_files
    ::DEFAULT_APP_FILES
48 49
  end

50
  def test_assets
51
    run_generator
J
José Valim 已提交
52 53 54
    assert_file "app/views/layouts/application.html.erb", /stylesheet_link_tag\s+"application"/
    assert_file "app/views/layouts/application.html.erb", /javascript_include_tag\s+"application"/
    assert_file "app/assets/stylesheets/application.css"
55
    assert_file "config/application.rb", /config\.assets\.enabled = true/
56
    assert_file "public/index.html", /url\("assets\/rails.png"\);/
57 58
  end

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

70
  def test_application_new_exits_with_non_zero_code_on_invalid_application_name
71
    quietly { system 'rails new test' }
72
    assert_equal false, $?.success?
73 74 75 76 77 78 79 80 81 82 83
  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?
84 85
  end

86 87 88 89 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
    assert_match /rails new APP_PATH \[options\]/, output
    assert_equal true, $?.success?
  end

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

    FileUtils.mv(app_root, app_moved_root)

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

116 117 118
  def test_rails_update_generates_correct_session_key
    app_root = File.join(destination_root, 'myapp')
    run_generator [app_root]
119

120 121 122 123 124 125
    Rails.application.config.root = app_root
    Rails.application.class.stubs(:name).returns("Myapp")
    Rails.application.stubs(:is_a?).returns(Rails::Application)

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

130 131 132 133 134
  def test_application_names_are_not_singularized
    run_generator [File.join(destination_root, "hats")]
    assert_file "hats/config/environment.rb", /Hats::Application\.initialize!/
  end

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

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

  def test_config_another_database
    run_generator([destination_root, "-d", "mysql"])
    assert_file "config/database.yml", /mysql/
158
    unless defined?(JRUBY_VERSION)
O
Oscar Del Ben 已提交
159
      assert_gem "mysql2"
160
    else
O
Oscar Del Ben 已提交
161
      assert_gem "activerecord-jdbcmysql-adapter"
162
    end
J
José Valim 已提交
163 164
  end

165 166 167 168
  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 已提交
169
      assert_gem "pg"
170
    else
O
Oscar Del Ben 已提交
171
      assert_gem "activerecord-jdbcpostgresql-adapter"
172 173 174
    end
  end

175 176
  def test_config_jdbcmysql_database
    run_generator([destination_root, "-d", "jdbcmysql"])
177
    assert_file "config/database.yml", /mysql/
O
Oscar Del Ben 已提交
178
    assert_gem "activerecord-jdbcmysql-adapter"
179 180
    # TODO: When the JRuby guys merge jruby-openssl in
    # jruby this will be removed
O
Oscar Del Ben 已提交
181
    assert_gem "jruby-openssl" if defined?(JRUBY_VERSION)
182 183
  end

184 185
  def test_config_jdbcsqlite3_database
    run_generator([destination_root, "-d", "jdbcsqlite3"])
186
    assert_file "config/database.yml", /sqlite3/
O
Oscar Del Ben 已提交
187
    assert_gem "activerecord-jdbcsqlite3-adapter"
188 189
  end

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

A
Arun Agrawal 已提交
196 197 198
  def test_config_jdbc_database
    run_generator([destination_root, "-d", "jdbc"])
    assert_file "config/database.yml", /jdbc/
199
    assert_file "config/database.yml", /mssql/
O
Oscar Del Ben 已提交
200
    assert_gem "activerecord-jdbc-adapter"
A
Arun Agrawal 已提交
201 202
  end

203 204 205 206
  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 已提交
207
      assert_gem "activerecord-jdbcsqlite3-adapter"
208 209 210
    end
  end

211
  def test_generator_if_skip_active_record_is_given
212
    run_generator [destination_root, "--skip-active-record"]
J
José Valim 已提交
213
    assert_no_file "config/database.yml"
214
    assert_file "config/application.rb", /#\s+require\s+["']active_record\/railtie["']/
215
    assert_file "config/application.rb", /#\s+config\.active_record\.whitelist_attributes = true/
216
    assert_file "config/application.rb", /#\s+config\.active_record\.dependent_restrict_raises = false/
217
    assert_file "test/test_helper.rb" do |helper_content|
218
      assert_no_match(/fixtures :all/, helper_content)
219
    end
220
    assert_file "test/performance/browsing_test.rb"
J
José Valim 已提交
221 222
  end

223
  def test_generator_if_skip_sprockets_is_given
224 225
    run_generator [destination_root, "--skip-sprockets"]
    assert_file "config/application.rb" do |content|
226
      assert_match(/#\s+require\s+["']sprockets\/rails\/railtie["']/, content)
227 228
      assert_no_match(/config\.assets\.enabled = true/, content)
    end
229 230 231 232 233
    assert_file "Gemfile" do |content|
      assert_no_match(/sass-rails/, content)
      assert_no_match(/coffee-rails/, content)
      assert_no_match(/uglifier/, content)
    end
234 235 236 237 238 239 240
    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)
      assert_no_match(/config\.assets\.compress = true/, content)
    end
241
    assert_file "test/performance/browsing_test.rb"
242
  end
J
José Valim 已提交
243

244
  def test_inclusion_of_javascript_runtime
245
    run_generator([destination_root])
246
    if defined?(JRUBY_VERSION)
O
Oscar Del Ben 已提交
247
      assert_gem "therubyrhino"
248
    else
249
      assert_file "Gemfile", /# gem\s+["']therubyracer["']+, platform: :ruby$/
250 251 252
    end
  end

253 254 255 256 257 258 259
  def test_generator_if_skip_index_html_is_given
    run_generator [destination_root, "--skip-index-html"]
    assert_no_file "public/index.html"
    assert_no_file "app/assets/images/rails.png"
    assert_file "app/assets/images/.gitkeep"
  end

260
  def test_creation_of_a_test_directory
J
José Valim 已提交
261
    run_generator
262 263 264
    assert_file 'test'
  end

265 266 267 268 269
  def test_creation_of_vendor_assets_javascripts_directory
    run_generator
    assert_file "vendor/assets/javascripts"
  end

270 271 272 273 274
  def test_creation_of_vendor_assets_stylesheets_directory
    run_generator
    assert_file "vendor/assets/stylesheets"
  end

275 276 277 278 279 280
  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
O
Oscar Del Ben 已提交
281
    assert_gem "jquery-rails"
282 283 284 285 286 287 288 289
  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 已提交
290
    assert_gem "prototype-rails"
J
José Valim 已提交
291
  end
292

293 294
  def test_javascript_is_skipped_if_required
    run_generator [destination_root, "--skip-javascript"]
295 296 297
    assert_file "app/assets/javascripts/application.js" do |contents|
      assert_no_match %r{^//=\s+require\s}, contents
    end
298
  end
299

S
Santiago Pastorino 已提交
300
  def test_inclusion_of_debugger
301
    run_generator
O
Oscar Del Ben 已提交
302
    assert_file "Gemfile", /# gem 'debugger'/
303 304
  end

305
  def test_template_from_dir_pwd
306
    FileUtils.cd(Rails.root)
307
    assert_match(/It works from file!/, run_generator([destination_root, "-m", "lib/template.rb"]))
308 309
  end

J
José Valim 已提交
310 311 312 313 314 315
  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
316
    Rails::Generators::AppGenerator.expects(:usage_path).returns(nil)
317
    assert_match(/Create rails files for app generator/, Rails::Generators::AppGenerator.desc)
J
José Valim 已提交
318 319 320
  end

  def test_default_namespace
321
    assert_match "rails:app", Rails::Generators::AppGenerator.namespace
J
José Valim 已提交
322 323
  end

324 325 326 327 328
  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

329 330
  def test_test_unit_is_removed_from_frameworks_if_skip_test_unit_is_given
    run_generator [destination_root, "--skip-test-unit"]
331 332 333 334 335 336 337
    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["']/
338 339
  end

340 341 342
  def test_new_hash_style
    run_generator [destination_root]
    assert_file "config/initializers/session_store.rb" do |file|
J
José Valim 已提交
343
      assert_match(/config.session_store :cookie_store, key: '_.+_session'/, file)
344 345 346
    end
  end

347 348
  def test_generated_environments_file_for_sanitizer
    run_generator [destination_root, "--skip-active-record"]
A
Arun Agrawal 已提交
349 350
    %w(development test).each do |env|
      assert_file "config/environments/#{env}.rb" do |file|
351 352 353 354 355
        assert_no_match(/config.active_record.mass_assignment_sanitizer = :strict/, file)
      end
    end
  end

356 357
  def test_generated_environments_file_for_auto_explain
    run_generator [destination_root, "--skip-active-record"]
A
Arun Agrawal 已提交
358
    %w(development production).each do |env|
359 360 361 362 363 364
      assert_file "config/environments/#{env}.rb" do |file|
        assert_no_match %r(auto_explain_threshold_in_seconds), file
      end
    end
  end

365 366 367 368 369
  def test_active_record_whitelist_attributes_is_present_application_config
    run_generator
    assert_file "config/application.rb", /config\.active_record\.whitelist_attributes = true/
  end

370 371 372 373 374
  def test_active_record_dependent_restrict_raises_is_present_application_config
    run_generator
    assert_file "config/application.rb", /config\.active_record\.dependent_restrict_raises = false/
  end

375 376 377 378 379
  def test_pretend_option
    output = run_generator [File.join(destination_root, "myapp"), "--pretend"]
    assert_no_match(/run  bundle install/, output)
  end

A
Arun Agrawal 已提交
380 381 382
  def test_humans_txt_file
    date = Date.today.strftime("%B %d, %Y")
    run_generator [File.join(destination_root, 'things-43')]
383
    assert_file "things-43/public/humans.txt", /Name: Things43/, /Software: Ruby on Rails/
A
Arun Agrawal 已提交
384 385
  end

386
protected
J
José Valim 已提交
387

388
  def action(*args, &block)
389
    silence(:stdout) { generator.send(*args, &block) }
390
  end
O
Oscar Del Ben 已提交
391 392 393 394

  def assert_gem(gem)
    assert_file "Gemfile", /^gem\s+["']#{gem}["']$/
  end
395 396 397 398 399 400 401
end

class CustomAppGeneratorTest < Rails::Generators::TestCase
  include GeneratorsTestHelper
  tests Rails::Generators::AppGenerator

  arguments [destination_root]
402
  include SharedCustomGeneratorTests
403

404 405 406
protected
  def default_files
    ::DEFAULT_APP_FILES
407 408
  end

409 410
  def builders_dir
    "app_builders"
411 412
  end

413 414
  def builder_class
    :AppBuilder
415 416 417
  end

  def action(*args, &block)
418
    silence(:stdout) { generator.send(*args, &block) }
419 420
  end
end