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

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

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

48 49
  def default_files
    ::DEFAULT_APP_FILES
50 51
  end

52
  def test_assets
53
    run_generator
J
José Valim 已提交
54 55 56
    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"
57
    assert_file "config/application.rb", /config\.assets\.enabled = true/
58 59
  end

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

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

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

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

    FileUtils.mv(app_root, app_moved_root)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

222
  def test_generator_if_skip_sprockets_is_given
223 224
    run_generator [destination_root, "--skip-sprockets"]
    assert_file "config/application.rb" do |content|
225
      assert_match(/#\s+require\s+["']sprockets\/railtie["']/, content)
226 227
      assert_no_match(/config\.assets\.enabled = true/, content)
    end
228 229 230 231 232
    assert_file "Gemfile" do |content|
      assert_no_match(/sass-rails/, content)
      assert_no_match(/coffee-rails/, content)
      assert_no_match(/uglifier/, content)
    end
233 234 235 236 237
    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 已提交
238 239
      assert_no_match(/config\.assets\.js_compressor = :uglifier/, content)
      assert_no_match(/config\.assets\.css_compressor = :sass/, content)
240
    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["']+, platforms: :ruby$/
250 251 252
    end
  end

253
  def test_creation_of_a_test_directory
J
José Valim 已提交
254
    run_generator
255 256 257
    assert_file 'test'
  end

258 259 260 261 262
  def test_creation_of_vendor_assets_javascripts_directory
    run_generator
    assert_file "vendor/assets/javascripts"
  end

263 264 265 266 267
  def test_creation_of_vendor_assets_stylesheets_directory
    run_generator
    assert_file "vendor/assets/stylesheets"
  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
O
Oscar Del Ben 已提交
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
    assert_file "app/assets/javascripts/application.js" do |contents|
      assert_no_match %r{^//=\s+require\s}, contents
    end
291
  end
292

S
Santiago Pastorino 已提交
293
  def test_inclusion_of_debugger
294
    run_generator
O
Oscar Del Ben 已提交
295
    assert_file "Gemfile", /# gem 'debugger'/
296 297
  end

298
  def test_template_from_dir_pwd
299
    FileUtils.cd(Rails.root)
300
    assert_match(/It works from file!/, run_generator([destination_root, "-m", "lib/template.rb"]))
301 302
  end

J
José Valim 已提交
303 304 305 306 307 308
  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
309
    Rails::Generators::AppGenerator.expects(:usage_path).returns(nil)
310
    assert_match(/Create rails files for app generator/, Rails::Generators::AppGenerator.desc)
J
José Valim 已提交
311 312 313
  end

  def test_default_namespace
314
    assert_match "rails:app", Rails::Generators::AppGenerator.namespace
J
José Valim 已提交
315 316
  end

317 318 319 320 321
  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

322 323
  def test_test_unit_is_removed_from_frameworks_if_skip_test_unit_is_given
    run_generator [destination_root, "--skip-test-unit"]
324 325 326 327 328 329 330
    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["']/
331 332
  end

333 334 335
  def test_new_hash_style
    run_generator [destination_root]
    assert_file "config/initializers/session_store.rb" do |file|
S
Santiago Pastorino 已提交
336
      assert_match(/config.session_store :encrypted_cookie_store, key: '_.+_session'/, file)
337 338 339
    end
  end

340 341
  def test_generated_environments_file_for_auto_explain
    run_generator [destination_root, "--skip-active-record"]
A
Arun Agrawal 已提交
342
    %w(development production).each do |env|
343 344 345 346 347 348
      assert_file "config/environments/#{env}.rb" do |file|
        assert_no_match %r(auto_explain_threshold_in_seconds), file
      end
    end
  end

349 350 351 352 353
  def test_pretend_option
    output = run_generator [File.join(destination_root, "myapp"), "--pretend"]
    assert_no_match(/run  bundle install/, output)
  end

354
protected
J
José Valim 已提交
355

356
  def action(*args, &block)
357
    silence(:stdout) { generator.send(*args, &block) }
358
  end
O
Oscar Del Ben 已提交
359 360 361 362

  def assert_gem(gem)
    assert_file "Gemfile", /^gem\s+["']#{gem}["']$/
  end
363 364 365 366 367 368 369
end

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

  arguments [destination_root]
370
  include SharedCustomGeneratorTests
371

372 373 374
protected
  def default_files
    ::DEFAULT_APP_FILES
375 376
  end

377 378
  def builders_dir
    "app_builders"
379 380
  end

381 382
  def builder_class
    :AppBuilder
383 384 385
  end

  def action(*args, &block)
386
    silence(:stdout) { generator.send(*args, &block) }
387 388
  end
end