app_generator_test.rb 11.8 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
  app/controllers
F
Francesco Rodriguez 已提交
14
  app/controllers/concerns
15
  app/helpers
16
  app/mailers
17
  app/models
F
Francesco Rodriguez 已提交
18
  app/models/concerns
19
  app/views/layouts
20 21 22
  bin/bundle
  bin/rails
  bin/rake
23 24 25 26 27 28
  config/environments
  config/initializers
  config/locales
  db
  lib
  lib/tasks
29
  lib/assets
30 31
  log
  test/fixtures
M
Mike Moore 已提交
32 33 34 35
  test/controllers
  test/models
  test/helpers
  test/mailers
36 37
  test/integration
  vendor
J
José Valim 已提交
38
  vendor/assets
39
  tmp/cache
J
José Valim 已提交
40
  tmp/cache/assets
41 42
)

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

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

50 51
  def default_files
    ::DEFAULT_APP_FILES
52 53
  end

54
  def test_assets
55
    run_generator
J
José Valim 已提交
56 57 58
    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"
59 60
  end

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

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

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

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

    FileUtils.mv(app_root, app_moved_root)

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

118 119 120
  def test_rails_update_generates_correct_session_key
    app_root = File.join(destination_root, 'myapp')
    run_generator [app_root]
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)

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

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

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

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

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

167 168 169 170 171
  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

172 173 174 175
  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 已提交
176
      assert_gem "pg"
177
    else
O
Oscar Del Ben 已提交
178
      assert_gem "activerecord-jdbcpostgresql-adapter"
179 180 181
    end
  end

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

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

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

A
Arun Agrawal 已提交
203 204 205
  def test_config_jdbc_database
    run_generator([destination_root, "-d", "jdbc"])
    assert_file "config/database.yml", /jdbc/
206
    assert_file "config/database.yml", /mssql/
O
Oscar Del Ben 已提交
207
    assert_gem "activerecord-jdbc-adapter"
A
Arun Agrawal 已提交
208 209
  end

210 211 212 213
  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 已提交
214
      assert_gem "activerecord-jdbcsqlite3-adapter"
215 216 217
    end
  end

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

227
  def test_generator_if_skip_sprockets_is_given
228 229
    run_generator [destination_root, "--skip-sprockets"]
    assert_file "config/application.rb" do |content|
230
      assert_match(/#\s+require\s+["']sprockets\/railtie["']/, content)
231
      assert_match(/config\.assets\.enabled = false/, content)
232
    end
233 234 235
    assert_file "Gemfile" do |content|
      assert_no_match(/sass-rails/, content)
      assert_no_match(/uglifier/, content)
236
      assert_match(/coffee-rails/, content)
237
    end
238 239 240 241 242
    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 已提交
243 244
      assert_no_match(/config\.assets\.js_compressor = :uglifier/, content)
      assert_no_match(/config\.assets\.css_compressor = :sass/, content)
245
      assert_no_match(/config\.assets\.version = '1\.0'/, content)
246
    end
247
  end
J
José Valim 已提交
248

249
  def test_inclusion_of_javascript_runtime
250
    run_generator([destination_root])
251
    if defined?(JRUBY_VERSION)
O
Oscar Del Ben 已提交
252
      assert_gem "therubyrhino"
253
    else
254
      assert_file "Gemfile", /# gem\s+["']therubyracer["']+, platforms: :ruby$/
255 256 257
    end
  end

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

263 264 265 266 267
  def test_creation_of_vendor_assets_javascripts_directory
    run_generator
    assert_file "vendor/assets/javascripts"
  end

268 269 270 271 272
  def test_creation_of_vendor_assets_stylesheets_directory
    run_generator
    assert_file "vendor/assets/stylesheets"
  end

273 274 275 276 277 278
  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
279
    assert_file "Gemfile", /^gem 'jquery-rails'/
280 281 282 283 284 285 286 287
  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 已提交
288
    assert_gem "prototype-rails"
J
José Valim 已提交
289
  end
290

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

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

X
Xavier Noria 已提交
306 307 308 309 310
  def test_inclusion_of_lazy_loaded_sdoc
    run_generator
    assert_file 'Gemfile', /gem 'sdoc', require: false/
  end

311
  def test_template_from_dir_pwd
312
    FileUtils.cd(Rails.root)
313
    assert_match(/It works from file!/, run_generator([destination_root, "-m", "lib/template.rb"]))
314 315
  end

J
José Valim 已提交
316 317 318 319 320 321
  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
322
    Rails::Generators::AppGenerator.expects(:usage_path).returns(nil)
323
    assert_match(/Create rails files for app generator/, Rails::Generators::AppGenerator.desc)
J
José Valim 已提交
324 325 326
  end

  def test_default_namespace
327
    assert_match "rails:app", Rails::Generators::AppGenerator.namespace
J
José Valim 已提交
328 329
  end

330 331 332 333 334
  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

335 336
  def test_test_unit_is_removed_from_frameworks_if_skip_test_unit_is_given
    run_generator [destination_root, "--skip-test-unit"]
337 338 339 340 341 342 343
    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["']/
344 345
  end

346 347 348
  def test_new_hash_style
    run_generator [destination_root]
    assert_file "config/initializers/session_store.rb" do |file|
349
      assert_match(/config.session_store :cookie_store, key: '_.+_session'/, file)
350 351 352
    end
  end

353 354 355 356 357
  def test_pretend_option
    output = run_generator [File.join(destination_root, "myapp"), "--pretend"]
    assert_no_match(/run  bundle install/, output)
  end

358
protected
J
José Valim 已提交
359

360
  def action(*args, &block)
361
    silence(:stdout) { generator.send(*args, &block) }
362
  end
O
Oscar Del Ben 已提交
363 364 365 366

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