diff options
Diffstat (limited to 'railties/test/generators/app_generator_test.rb')
-rw-r--r-- | railties/test/generators/app_generator_test.rb | 194 |
1 files changed, 115 insertions, 79 deletions
diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb index 058308aa13..9299b9ebea 100644 --- a/railties/test/generators/app_generator_test.rb +++ b/railties/test/generators/app_generator_test.rb @@ -1,6 +1,6 @@ -require 'generators/generators_test_helper' -require 'rails/generators/rails/app/app_generator' -require 'generators/shared_generator_tests' +require "generators/generators_test_helper" +require "rails/generators/rails/app/app_generator" +require "generators/shared_generator_tests" DEFAULT_APP_FILES = %w( .gitignore @@ -77,7 +77,7 @@ class AppGeneratorTest < Rails::Generators::TestCase end def test_invalid_application_name_raises_an_error - content = capture(:stderr){ run_generator [File.join(destination_root, "43-things")] } + content = capture(:stderr) { run_generator [File.join(destination_root, "43-things")] } assert_equal "Invalid application name 43-things. Please give a name which does not start with numbers.\n", content end @@ -88,12 +88,12 @@ class AppGeneratorTest < Rails::Generators::TestCase end def test_application_new_exits_with_non_zero_code_on_invalid_application_name - quietly { system 'rails new test --no-rc' } + quietly { system "rails new test --no-rc" } assert_equal false, $?.success? 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') + app_root = File.join(destination_root, "myfirstapp") run_generator [app_root] output = nil Dir.chdir(app_root) do @@ -104,7 +104,7 @@ class AppGeneratorTest < Rails::Generators::TestCase end def test_application_new_show_help_message_inside_existing_rails_directory - app_root = File.join(destination_root, 'myfirstapp') + app_root = File.join(destination_root, "myfirstapp") run_generator [app_root] output = Dir.chdir(app_root) do `rails new --help` @@ -131,20 +131,18 @@ class AppGeneratorTest < Rails::Generators::TestCase generator.send(:app_const) quietly { generator.send(:update_config_files) } assert_file "myapp_moved/config/environment.rb", /Rails\.application\.initialize!/ - assert_file "myapp_moved/config/initializers/session_store.rb", /_myapp_session/ end end end def test_rails_update_generates_correct_session_key - app_root = File.join(destination_root, 'myapp') + app_root = File.join(destination_root, "myapp") run_generator [app_root] stub_rails_application(app_root) do generator = Rails::Generators::AppGenerator.new ["rails"], [], destination_root: app_root, shell: @shell generator.send(:app_const) quietly { generator.send(:update_config_files) } - assert_file "myapp/config/initializers/session_store.rb", /_myapp_session/ end end @@ -157,11 +155,11 @@ class AppGeneratorTest < Rails::Generators::TestCase def test_new_application_not_include_api_initializers run_generator - assert_no_file 'config/initializers/cors.rb' + assert_no_file "config/initializers/cors.rb" end def test_rails_update_keep_the_cookie_serializer_if_it_is_already_configured - app_root = File.join(destination_root, 'myapp') + app_root = File.join(destination_root, "myapp") run_generator [app_root] stub_rails_application(app_root) do @@ -173,7 +171,7 @@ class AppGeneratorTest < Rails::Generators::TestCase end def test_rails_update_set_the_cookie_serializer_to_marshal_if_it_is_not_already_configured - app_root = File.join(destination_root, 'myapp') + app_root = File.join(destination_root, "myapp") run_generator [app_root] FileUtils.rm("#{app_root}/config/initializers/cookies_serializer.rb") @@ -188,7 +186,7 @@ class AppGeneratorTest < Rails::Generators::TestCase end def test_rails_update_dont_set_file_watcher - app_root = File.join(destination_root, 'myapp') + app_root = File.join(destination_root, "myapp") run_generator [app_root] stub_rails_application(app_root) do @@ -202,7 +200,7 @@ class AppGeneratorTest < Rails::Generators::TestCase end def test_rails_update_does_not_create_new_framework_defaults_by_default - app_root = File.join(destination_root, 'myapp') + app_root = File.join(destination_root, "myapp") run_generator [app_root] FileUtils.rm("#{app_root}/config/initializers/new_framework_defaults.rb") @@ -221,7 +219,7 @@ class AppGeneratorTest < Rails::Generators::TestCase end def test_rails_update_does_not_create_rack_cors - app_root = File.join(destination_root, 'myapp') + app_root = File.join(destination_root, "myapp") run_generator [app_root] stub_rails_application(app_root) do @@ -233,7 +231,7 @@ class AppGeneratorTest < Rails::Generators::TestCase end def test_rails_update_does_not_remove_rack_cors_if_already_present - app_root = File.join(destination_root, 'myapp') + app_root = File.join(destination_root, "myapp") run_generator [app_root] FileUtils.touch("#{app_root}/config/initializers/cors.rb") @@ -254,7 +252,7 @@ class AppGeneratorTest < Rails::Generators::TestCase def test_gemfile_has_no_whitespace_errors run_generator absolute = File.expand_path("Gemfile", destination_root) - File.open(absolute, 'r') do |f| + File.open(absolute, "r") do |f| f.each_line do |line| assert_no_match %r{/^[ \t]+$/}, line end @@ -358,6 +356,7 @@ class AppGeneratorTest < Rails::Generators::TestCase def test_generator_if_skip_active_record_is_given run_generator [destination_root, "--skip-active-record"] + assert_no_directory "db/" assert_no_file "config/database.yml" assert_no_file "app/models/application_record.rb" assert_file "config/application.rb", /#\s+require\s+["']active_record\/railtie["']/ @@ -382,13 +381,15 @@ class AppGeneratorTest < Rails::Generators::TestCase assert_file "config/environments/production.rb" do |content| assert_no_match(/config\.action_mailer/, content) end + assert_no_directory "app/mailers" + assert_no_directory "test/mailers" end def test_generator_has_assets_gems run_generator - assert_gem 'sass-rails' - assert_gem 'uglifier' + assert_gem "sass-rails" + assert_gem "uglifier" end def test_generator_if_skip_sprockets_is_given @@ -411,6 +412,9 @@ class AppGeneratorTest < Rails::Generators::TestCase assert_no_match(/config\.assets\.js_compressor = :uglifier/, content) assert_no_match(/config\.assets\.css_compressor = :sass/, content) end + assert_file "config/initializers/new_framework_defaults.rb" do |content| + assert_no_match(/unknown_asset_fallback/, content) + end end def test_generator_if_skip_action_cable_is_given @@ -448,7 +452,7 @@ class AppGeneratorTest < Rails::Generators::TestCase end def test_other_javascript_libraries - run_generator [destination_root, '-j', 'prototype'] + 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 @@ -470,12 +474,17 @@ class AppGeneratorTest < Rails::Generators::TestCase assert_file "Gemfile" do |content| assert_no_match(/coffee-rails/, content) assert_no_match(/jquery-rails/, content) + assert_no_match(/uglifier/, content) + end + + assert_file "config/environments/production.rb" do |content| + assert_no_match(/config\.assets\.js_compressor = :uglifier/, content) end end def test_inclusion_of_jbuilder run_generator - assert_gem 'jbuilder' + assert_gem "jbuilder" end def test_inclusion_of_a_debugger @@ -485,13 +494,13 @@ class AppGeneratorTest < Rails::Generators::TestCase assert_no_match(/byebug/, content) end else - assert_gem 'byebug' + assert_gem "byebug" end end def test_inclusion_of_listen_related_configuration_by_default run_generator - if RbConfig::CONFIG['host_os'] =~ /darwin|linux/ + if RbConfig::CONFIG["host_os"] =~ /darwin|linux/ assert_listen_related_configuration else assert_no_listen_related_configuration @@ -499,14 +508,14 @@ class AppGeneratorTest < Rails::Generators::TestCase end def test_non_inclusion_of_listen_related_configuration_if_skip_listen - run_generator [destination_root, '--skip-listen'] + run_generator [destination_root, "--skip-listen"] assert_no_listen_related_configuration end def test_evented_file_update_checker_config run_generator - assert_file 'config/environments/development.rb' do |content| - if RbConfig::CONFIG['host_os'] =~ /darwin|linux/ + assert_file "config/environments/development.rb" do |content| + if RbConfig::CONFIG["host_os"] =~ /darwin|linux/ assert_match(/^\s*config.file_watcher = ActiveSupport::EventedFileUpdateChecker/, content) else assert_match(/^\s*# config.file_watcher = ActiveSupport::EventedFileUpdateChecker/, content) @@ -536,8 +545,8 @@ class AppGeneratorTest < Rails::Generators::TestCase end 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' + action :file, "lib/test_file.rb", "heres test data" + assert_file "lib/test_file.rb", "heres test data" end def test_tests_are_removed_from_frameworks_if_skip_test_is_given @@ -552,13 +561,6 @@ class AppGeneratorTest < Rails::Generators::TestCase assert_file "config/application.rb", /\s+require\s+["']active_job\/railtie["']/ end - def test_new_hash_style - run_generator - assert_file "config/initializers/session_store.rb" do |file| - assert_match(/config.session_store :cookie_store, key: '_.+_session'/, file) - end - end - def test_pretend_option output = run_generator [File.join(destination_root, "myapp"), "--pretend"] assert_no_match(/run bundle install/, output) @@ -568,15 +570,14 @@ class AppGeneratorTest < Rails::Generators::TestCase path = File.join(destination_root, "foo bar") # This also applies to MySQL apps but not with SQLite - run_generator [path, "-d", 'postgresql'] + 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 def test_web_console run_generator - assert_gem 'web-console' + assert_gem "web-console" end def test_web_console_with_dev_option @@ -584,7 +585,7 @@ class AppGeneratorTest < Rails::Generators::TestCase assert_file "Gemfile" do |content| assert_match(/gem 'web-console',\s+github: 'rails\/web-console'/, content) - assert_no_match(/\Agem 'web-console'\z/, content) + assert_no_match(/\Agem 'web-console', '>= 3.3.0'\z/, content) end end @@ -593,13 +594,28 @@ class AppGeneratorTest < Rails::Generators::TestCase assert_file "Gemfile" do |content| assert_match(/gem 'web-console',\s+github: 'rails\/web-console'/, content) - assert_no_match(/\Agem 'web-console'\z/, content) + assert_no_match(/\Agem 'web-console', '>= 3.3.0'\z/, content) end end + def test_generation_runs_bundle_install + assert_generates_with_bundler + end + + def test_dev_option + assert_generates_with_bundler dev: true + rails_path = File.expand_path("../../..", Rails.root) + assert_file "Gemfile", /^gem\s+["']rails["'],\s+path:\s+["']#{Regexp.escape(rails_path)}["']$/ + end + + def test_edge_option + assert_generates_with_bundler edge: true + assert_file "Gemfile", %r{^gem\s+["']rails["'],\s+github:\s+["']#{Regexp.escape("rails/rails")}["']$} + end + def test_spring run_generator - assert_gem 'spring' + assert_gem "spring" end def test_spring_binstubs @@ -608,9 +624,9 @@ class AppGeneratorTest < Rails::Generators::TestCase @binstub_called ||= 0 case command - when 'install' + when "install" # Called when running bundle, we just want to stub it so nothing to do here. - when 'exec spring binstub --all' + when "exec spring binstub --all" @binstub_called += 1 assert_equal 1, @binstub_called, "exec spring binstub --all expected to be called once, but was called #{@install_called} times." end @@ -635,7 +651,7 @@ class AppGeneratorTest < Rails::Generators::TestCase def test_skip_spring run_generator [destination_root, "--skip-spring"] - assert_no_file 'config/spring.rb' + assert_no_file "config/spring.rb" assert_file "Gemfile" do |content| assert_no_match(/spring/, content) end @@ -666,15 +682,15 @@ class AppGeneratorTest < Rails::Generators::TestCase def test_gitignore_when_sqlite3 run_generator - assert_file '.gitignore' do |content| + assert_file ".gitignore" do |content| assert_match(/sqlite3/, content) end end def test_gitignore_when_no_active_record - run_generator [destination_root, '--skip-active-record'] + run_generator [destination_root, "--skip-active-record"] - assert_file '.gitignore' do |content| + assert_file ".gitignore" do |content| assert_no_match(/sqlite/i, content) end end @@ -682,7 +698,7 @@ class AppGeneratorTest < Rails::Generators::TestCase def test_gitignore_when_non_sqlite3_db run_generator([destination_root, "-d", "mysql"]) - assert_file '.gitignore' do |content| + assert_file ".gitignore" do |content| assert_no_match(/sqlite/i, content) end end @@ -725,17 +741,17 @@ class AppGeneratorTest < Rails::Generators::TestCase end def test_after_bundle_callback - path = 'http://example.org/rails_template' + path = "http://example.org/rails_template" template = %{ after_bundle { run 'echo ran after_bundle' } } template.instance_eval "def read; self; end" # Make the string respond to read check_open = -> *args do - assert_equal [ path, 'Accept' => 'application/x-thor-template' ], args + assert_equal [ path, "Accept" => "application/x-thor-template" ], args template end - sequence = ['install', 'exec spring binstub --all', 'echo ran after_bundle'] - @sequence_step ||= 0 + sequence = ["install", "exec spring binstub --all", "echo ran after_bundle"] + @sequence_step ||= 0 ensure_bundler_first = -> command do assert_equal sequence[@sequence_step], command, "commands should be called in sequence #{sequence}" @sequence_step += 1 @@ -754,41 +770,61 @@ class AppGeneratorTest < Rails::Generators::TestCase protected - def stub_rails_application(root) - Rails.application.config.root = root - Rails.application.class.stub(:name, "Myapp") do - yield + def stub_rails_application(root) + Rails.application.config.root = root + Rails.application.class.stub(:name, "Myapp") do + yield + end end - end - def action(*args, &block) - capture(:stdout) { generator.send(*args, &block) } - end + def action(*args, &block) + capture(:stdout) { generator.send(*args, &block) } + end - def assert_gem(gem, constraint = nil) - if constraint - assert_file "Gemfile", /^\s*gem\s+["']#{gem}["'], #{constraint}$*/ - else - assert_file "Gemfile", /^\s*gem\s+["']#{gem}["']$*/ + def assert_gem(gem, constraint = nil) + if constraint + assert_file "Gemfile", /^\s*gem\s+["']#{gem}["'], #{constraint}$*/ + else + assert_file "Gemfile", /^\s*gem\s+["']#{gem}["']$*/ + end end - end - def assert_listen_related_configuration - assert_gem 'listen' - assert_gem 'spring-watcher-listen' + def assert_listen_related_configuration + assert_gem "listen" + assert_gem "spring-watcher-listen" - assert_file 'config/environments/development.rb' do |content| - assert_match(/^\s*config.file_watcher = ActiveSupport::EventedFileUpdateChecker/, content) + assert_file "config/environments/development.rb" do |content| + assert_match(/^\s*config.file_watcher = ActiveSupport::EventedFileUpdateChecker/, content) + end end - end - def assert_no_listen_related_configuration - assert_file 'Gemfile' do |content| - assert_no_match(/listen/, content) + def assert_no_listen_related_configuration + assert_file "Gemfile" do |content| + assert_no_match(/listen/, content) + end + + assert_file "config/environments/development.rb" do |content| + assert_match(/^\s*# config.file_watcher = ActiveSupport::EventedFileUpdateChecker/, content) + end end - assert_file 'config/environments/development.rb' do |content| - assert_match(/^\s*# config.file_watcher = ActiveSupport::EventedFileUpdateChecker/, content) + def assert_generates_with_bundler(options = {}) + generator([destination_root], options) + + command_check = -> command do + @install_called ||= 0 + + case command + when "install" + @install_called += 1 + assert_equal 1, @install_called, "install expected to be called once, but was called #{@install_called} times" + when "exec spring binstub --all" + # Called when running tests with spring, let through unscathed. + end + end + + generator.stub :bundle_command, command_check do + quietly { generator.invoke_all } + end end - end end |