1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/railties/test/application/rake_test.rb

322 lines
9.2 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require "isolation/abstract_unit"
require "env_helpers"
module ApplicationTests
2012-01-05 20:30:17 -05:00
class RakeTest < ActiveSupport::TestCase
include ActiveSupport::Testing::Isolation, EnvHelpers
def setup
build_app
end
def teardown
teardown_app
end
def test_gems_tasks_are_loaded_first_than_application_ones
app_file "lib/tasks/app.rake", <<-RUBY
$task_loaded = Rake::Task.task_defined?("db:create:all")
RUBY
require "#{app_path}/config/environment"
::Rails.application.load_tasks
assert $task_loaded
end
test "framework tasks are evaluated only once" do
assert_equal ["Rails version"], rails("about").scan(/^Rails version/)
end
test "tasks can invoke framework tasks via Rails::Command.invoke" do
add_to_config <<~RUBY
rake_tasks do
task :invoke_about do
Rails::Command.invoke :about
end
end
RUBY
assert_match(/^Rails version/, rails("invoke_about"))
end
test "help arguments describe rake tasks" do
task_description = <<~DESC
rails db:migrate
Migrate the database (options: VERSION=x, VERBOSE=false, SCOPE=blog).
DESC
assert_match task_description, rails("db:migrate", "-h")
end
test "task backtrace is silenced" do
add_to_config <<-RUBY
rake_tasks do
task :boom do
raise "boom"
end
end
RUBY
backtrace = rails("boom", allow_failure: true).lines.grep(/:\d+:in /)
app_lines, framework_lines = backtrace.partition { |line| line.start_with?(app_path) }
assert_not_empty app_lines
assert_empty framework_lines
end
test "task is protected when previous migration was production" do
with_rails_env "production" do
rails "generate", "model", "product", "name:string"
rails "db:create", "db:migrate"
output = rails("db:test:prepare", allow_failure: true)
assert_match(/ActiveRecord::ProtectedEnvironmentError/, output)
end
end
def test_not_protected_when_previous_migration_was_not_production
with_rails_env "test" do
rails "generate", "model", "product", "name:string"
rails "db:create", "db:migrate"
output = rails("db:test:prepare", "test")
assert_no_match(/ActiveRecord::ProtectedEnvironmentError/, output)
end
end
def test_environment_is_required_in_rake_tasks
app_file "config/environment.rb", <<-RUBY
SuperMiddleware = Struct.new(:app)
Rails.application.configure do
config.middleware.use SuperMiddleware
end
Rails.application.initialize!
RUBY
assert_match("SuperMiddleware", rails("middleware"))
end
def test_initializers_are_executed_in_rake_tasks
add_to_config <<-RUBY
initializer "do_something" do
puts "Doing something..."
end
rake_tasks do
2012-10-14 06:03:39 -04:00
task do_nothing: :environment do
end
end
RUBY
output = rails("do_nothing")
assert_match "Doing something...", output
end
def test_does_not_explode_when_accessing_a_model
add_to_config <<-RUBY
rake_tasks do
2012-10-14 06:03:39 -04:00
task do_nothing: :environment do
Hello.new.world
end
end
RUBY
app_file "app/models/hello.rb", <<-RUBY
class Hello
def world
puts 'Hello world'
end
end
RUBY
output = rails("do_nothing")
assert_match "Hello world", output
end
2017-02-27 16:55:30 -05:00
def test_should_not_eager_load_model_for_rake_when_rake_eager_load_is_false
add_to_config <<-RUBY
rake_tasks do
task do_nothing: :environment do
puts 'There is nothing'
end
end
RUBY
add_to_env_config "production", <<-RUBY
config.eager_load = true
RUBY
app_file "app/models/hello.rb", <<-RUBY
raise 'should not be pre-required for rake even eager_load=true'
RUBY
output = rails("do_nothing", "RAILS_ENV=production")
assert_match "There is nothing", output
end
2017-02-27 16:55:30 -05:00
def test_should_eager_load_model_for_rake_when_rake_eager_load_is_true
add_to_config <<-RUBY
rake_tasks do
task do_something: :environment do
puts "Answer: " + Hello::TEST.to_s
end
end
RUBY
add_to_env_config "production", <<-RUBY
config.rake_eager_load = true
RUBY
app_file "app/models/hello.rb", <<-RUBY
class Hello
TEST = 42
end
RUBY
output = Dir.chdir(app_path) { `bin/rails do_something RAILS_ENV=production` }
assert_equal "Answer: 42\n", output
end
def test_code_statistics
Replace webpack with importmapped Hotwire as default js (#42999) * Turbolinks is being replaced with Hotwire * Make --webpack opt-in * Don't use specific webpacker installers any more in preparation for next Webpacker * Update railties/lib/rails/app_updater.rb Co-authored-by: Alex Ghiculescu <alex@tanda.co> * Trailing whitespace * Convert to Turbo data attribute for tracking * Default is no webpack, no hotwire * Swap out turbolinks references for hotwire * Drop explicit return * Only generate package.json if using webpack * Only create package.json in webpack mode * Only create app/javascript in webpack mode * Generate correct style/js links based on js mode * Fix tests from changed output format Not sure why these are showing up in this PR, though. * Rubocopping * Stick with webpack for the test app for now * Adjust tests * Replace minitest-reporters with minitest-ci (#43016) minitest-reporters is used to create junit xml reports on CI. But when it loads before rails minitest plugin makes `Rails::TestUnitReporter` not being added as a reporter. minitest-ci is now only loaded at ci and does not interferes with rails minitest plugins. And keeps junit reports workings * Too heavy handed to actually run bundle Just like we don't auto-migrate * Pin js frameworks in importmap Instead of having importmap preconfigure it. * Match updated app/javascript path * No need for the explaining comment * Fixes test cases for replace webpack with importmapped Hotwire as default js (#42999) * Fix rubocop issues * Fix more railities test cases * Fix plugin generator railties shared test cases * Fix Action Text install generator asset pipeline spec * They're modules, not files * Let dev use the latest release as well So we don't have to replace unexisting dev releases with latest release * Make Webpack responsible for generating all the JS files it needs Webpacker 6 has already moved from app/javascript to app/packs. * Don't add rails/ujs by default any longer All the ajax/form functionality has been superseded by Turbo. The rest lives in a weird inbetween land we need to address through other means. * Use new importmap location * Switch to using turbo-rails and stimulus-rails directly The hotwire-rails gem does not offer enough value for its indirection * Use latest Webpacker * Prevent version resolution requests from getting swallowed * Use ESM syntax for imports * Move management of yarn, package.json, etc to Webpacker 6 * Update for Webpacker 6 * Move bin/setup addition to Webpacker as well * Remove dead tests * Bump to Webpacker 6.0.0.rc.2 * No longer relevant given the new default is no webpacker * Rely on Webpacker 6 * No longer relevant * No longer relevant * Make cable channel generator work for both webpacker and importmap setups * Fix tests * For tests testing importmap way * Use Webpacker 6 dummy * RuboCopping * One more bump to fix webpack-dev-server * Another bump. Hopefully the last one! * Also enough to not want turbo tracking on * Fix tests * Latest * Fix tests * Fix more tests * Fix tests Co-authored-by: Alex Ghiculescu <alex@tanda.co> Co-authored-by: André Luis Leal Cardoso Junior <andrehjr@gmail.com> Co-authored-by: Abhay Nikam <nikam.abhay1@gmail.com> Co-authored-by: Guillermo Iguaran <guilleiguaran@gmail.com>
2021-08-26 04:39:36 -04:00
assert_match "Code LOC: 61 Test LOC: 3 Code to Test Ratio: 1:0.0",
rails("stats")
end
def test_logger_is_flushed_when_exiting_production_rake_tasks
add_to_config <<-RUBY
rake_tasks do
2012-10-14 06:03:39 -04:00
task log_something: :environment do
Rails.logger.error("Sample log message")
end
end
RUBY
rails "log_something", "RAILS_ENV=production"
assert_match "Sample log message", File.read("#{app_path}/log/production.log")
end
def test_loading_specific_fixtures
rails "generate", "model", "user", "username:string", "password:string"
rails "generate", "model", "product", "name:string"
rails "db:migrate"
require "#{rails_root}/config/environment"
# loading a specific fixture
rails "db:fixtures:load", "FIXTURES=products"
2019-02-11 15:44:25 -05:00
assert_equal 2, Product.count
assert_equal 0, User.count
end
def test_loading_only_yml_fixtures
rails "db:migrate"
app_file "test/fixtures/products.csv", ""
require "#{rails_root}/config/environment"
rails "db:fixtures:load"
end
def test_scaffold_tests_pass_by_default
rails "generate", "scaffold", "user", "username:string", "password:string"
with_rails_env("test") do
rails("db:migrate")
end
output = rails("test")
assert_match(/7 runs, 9 assertions, 0 failures, 0 errors/, output)
assert_no_match(/Errors running/, output)
end
def test_api_scaffold_tests_pass_by_default
add_to_config <<-RUBY
config.api_only = true
RUBY
app_file "app/controllers/application_controller.rb", <<-RUBY
class ApplicationController < ActionController::API
end
RUBY
rails "generate", "scaffold", "user", "username:string", "password:string"
with_rails_env("test") { rails("db:migrate") }
output = rails("test")
assert_match(/5 runs, 7 assertions, 0 failures, 0 errors/, output)
assert_no_match(/Errors running/, output)
end
def test_scaffold_with_references_columns_tests_pass_by_default
rails "generate", "model", "Product"
rails "generate", "model", "Cart"
rails "generate", "scaffold", "LineItems", "product:references", "cart:belongs_to"
with_rails_env("test") do
rails("db:migrate")
end
output = rails("test")
assert_match(/7 runs, 9 assertions, 0 failures, 0 errors/, output)
assert_no_match(/Errors running/, output)
end
def test_db_test_prepare_when_using_sql_format
add_to_config "config.active_record.schema_format = :sql"
rails "generate", "scaffold", "user", "username:string"
rails "db:migrate"
output = rails("db:test:prepare", "--trace")
assert_match(/Execute db:test:load_schema/, output)
end
def test_rake_dump_structure_should_respect_db_structure_env_variable
# ensure we have a schema_migrations table to dump
rails "db:migrate", "db:structure:dump", "SCHEMA=db/my_structure.sql"
assert File.exist?(File.join(app_path, "db", "my_structure.sql"))
end
def test_rake_dump_structure_should_be_called_twice_when_migrate_redo
add_to_config "config.active_record.schema_format = :sql"
rails "g", "model", "post", "title:string"
output = rails("db:migrate:redo", "--trace")
# expect only Invoke db:structure:dump (first_time)
assert_no_match(/^\*\* Invoke db:structure:dump\s+$/, output)
end
def test_rake_dump_schema_cache
rails "generate", "model", "post", "title:string"
rails "generate", "model", "product", "name:string"
rails "db:migrate", "db:schema:cache:dump"
2016-11-13 22:18:14 -05:00
assert File.exist?(File.join(app_path, "db", "schema_cache.yml"))
end
def test_rake_clear_schema_cache
rails "db:schema:cache:dump", "db:schema:cache:clear"
assert_not File.exist?(File.join(app_path, "db", "schema_cache.yml"))
end
2012-05-28 11:10:04 -04:00
2012-06-10 10:26:55 -04:00
def test_copy_templates
rails "app:templates:copy"
%w(controller mailer scaffold).each do |dir|
assert File.exist?(File.join(app_path, "lib", "templates", "erb", dir))
end
%w(controller helper scaffold_controller).each do |dir|
assert File.exist?(File.join(app_path, "lib", "templates", "rails", dir))
2012-06-10 10:26:55 -04:00
end
end
def test_template_load_initializers
app_file "config/initializers/dummy.rb", "puts 'Hello, World!'"
app_file "template.rb", ""
output = rails("app:template", "LOCATION=template.rb")
assert_match(/Hello, World!/, output)
end
end
end