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/isolation/abstract_unit.rb

473 lines
12 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
# Note:
# It is important to keep this file as light as possible
# the goal for tests that require this is to test booting up
2016-07-12 02:51:36 -04:00
# Rails from an empty state, so anything added here could
# hide potential failures
#
# It is also good to know what is the bare minimum to get
# Rails booted up.
require "fileutils"
2009-10-07 20:02:36 -04:00
require "bundler/setup" unless defined?(Bundler)
require "active_support"
require "active_support/testing/autorun"
require "active_support/testing/stream"
require "active_support/test_case"
RAILS_FRAMEWORK_ROOT = File.expand_path("../../..", __dir__)
# These files do not require any others and are needed
# to run the tests
require "active_support/core_ext/object/blank"
2012-05-11 12:56:05 -04:00
require "active_support/testing/isolation"
require "active_support/core_ext/kernel/reporting"
require "tmpdir"
require "rails/secrets"
module TestHelpers
module Paths
2012-06-19 17:27:52 -04:00
def app_template_path
File.join Dir.tmpdir, "app_template"
2012-06-19 17:27:52 -04:00
end
def tmp_path(*args)
@tmp_path ||= File.realpath(Dir.mktmpdir)
2012-06-19 19:27:54 -04:00
File.join(@tmp_path, *args)
end
def app_path(*args)
path = tmp_path(*%w[app] + args)
if block_given?
yield path
else
path
end
end
2009-10-05 10:41:08 -04:00
def framework_path
RAILS_FRAMEWORK_ROOT
end
2009-10-05 10:41:08 -04:00
def rails_root
app_path
end
end
module Rack
def app(env = "production")
old_env = ENV["RAILS_ENV"]
@app ||= begin
ENV["RAILS_ENV"] = env
require "#{app_path}/config/environment"
Rails.application
end
ensure
ENV["RAILS_ENV"] = old_env
end
def extract_body(response)
"".dup.tap do |body|
response[2].each { |chunk| body << chunk }
end
end
def get(path)
@app.call(::Rack::MockRequest.env_for(path))
end
def assert_welcome(resp)
resp = Array(resp)
assert_equal 200, resp[0]
assert_match "text/html", resp[1]["Content-Type"]
assert_match "charset=utf-8", resp[1]["Content-Type"]
assert extract_body(resp).match(/Yay! You.*re on Rails!/)
end
end
module Generation
2011-04-15 12:14:12 -04:00
# Build an application by invoking the generator and going through the whole stack.
def build_app(options = {})
@prev_rails_env = ENV["RAILS_ENV"]
ENV["RAILS_ENV"] = "development"
FileUtils.rm_rf(app_path)
2012-06-19 17:27:52 -04:00
FileUtils.cp_r(app_template_path, app_path)
# Delete the initializers unless requested
unless options[:initializers]
Dir["#{app_path}/config/initializers/**/*.rb"].each do |initializer|
File.delete(initializer)
end
end
routes = File.read("#{app_path}/config/routes.rb")
if routes =~ /(\n\s*end\s*)\z/
File.open("#{app_path}/config/routes.rb", "w") do |f|
f.puts $` + "\nActiveSupport::Deprecation.silence { match ':controller(/:action(/:id))(.:format)', via: :all }\n" + $1
end
end
if options[:multi_db]
File.open("#{app_path}/config/database.yml", "w") do |f|
f.puts <<-YAML
default: &default
adapter: sqlite3
pool: 5
timeout: 5000
development:
primary:
<<: *default
database: db/development.sqlite3
animals:
<<: *default
database: db/development_animals.sqlite3
migrations_paths: db/animals_migrate
test:
primary:
<<: *default
database: db/test.sqlite3
animals:
<<: *default
database: db/test_animals.sqlite3
migrations_paths: db/animals_migrate
production:
primary:
<<: *default
database: db/production.sqlite3
animals:
<<: *default
database: db/production_animals.sqlite3
migrations_paths: db/animals_migrate
YAML
end
else
File.open("#{app_path}/config/database.yml", "w") do |f|
f.puts <<-YAML
default: &default
adapter: sqlite3
pool: 5
timeout: 5000
development:
<<: *default
database: db/development.sqlite3
test:
<<: *default
database: db/test.sqlite3
production:
<<: *default
database: db/production.sqlite3
YAML
end
end
add_to_config <<-RUBY
config.eager_load = false
2012-10-14 06:03:39 -04:00
config.session_store :cookie_store, key: "_myapp_session"
config.active_support.deprecation = :log
config.active_support.test_order = :random
config.action_controller.allow_forgery_protection = false
config.log_level = :info
RUBY
end
def teardown_app
ENV["RAILS_ENV"] = @prev_rails_env if @prev_rails_env
`rails new` runs `rails active_storage:install` Omit `rails activestorage:install` for jdbcmysql, jdbc and shebang tests AppGeneratorTest#test_config_jdbcmysql_database rails aborted! LoadError: Could not load 'active_record/connection_adapters/mysql_adapter'. Make sure that the adapter in config/database.yml is valid. If you use an adapter other than 'mysql2', 'postgresql' or 'sqlite3' add the necessary adapter gem to the Gemfile. (compressed) bin/rails:4:in `<main>' Tasks: TOP => activestorage:install => environment (See full trace by running task with --trace) AppGeneratorTest#test_config_jdbc_database rails aborted! LoadError: Could not load 'active_record/connection_adapters/jdbc_adapter'. Make sure that the adapter in config/database.yml is valid. If you use an adapter other than 'mysql2', 'postgresql' or 'sqlite3' add the necessary adapter gem to the Gemfile. (compressed) bin/rails:4:in `<main>' Tasks: TOP => activestorage:install => environment (See full trace by running task with --trace) AppGeneratorTest#test_shebang_is_added_to_rails_file /home/ubuntu/.rbenv/versions/2.4.1/bin/ruby: no Ruby script found in input (LoadError) Prevent PendingMigrationError in tests * Run `bin/rails db:migrate RAILS_ENV=test` in test_cases before start tests to prevent PendingMigrationError * FileUtils.rm_r("db/migrate") * --skip-active-storage Fix failed tests in `railties/test/railties/engine_test.rb` Related to #30111 Imporve `SharedGeneratorTests#test_default_frameworks_are_required_when_others_are_removed` - Explicitly skip active_storage - Ensure that skipped frameworks are commented - Ensure that default frameworks are not commented Fix error `Errno::ENOSPC: No space left on device - sendfile` Since `rails new` runs `rails active_storage:install` that boots an app. Since adding Bootsnap 0312a5c67e35b960e33677b5358c539f1047e4e1 during booting an app, it creates the cache: 264K tmp/cache/bootsnap-load-path-cache 27M tmp/cache/bootsnap-compile-cache * teardown_app must remove app
2017-08-07 00:31:14 -04:00
FileUtils.rm_rf(tmp_path)
end
2011-04-15 12:14:12 -04:00
# Make a very basic app, without creating the whole directory structure.
# This is faster and simpler than the method above.
def make_basic_app
require "rails"
require "action_controller/railtie"
require "action_view/railtie"
Add credentials using a generic EncryptedConfiguration class (#30067) * WIP: Add credentials using a generic EncryptedConfiguration class This is sketch code so far. * Flesh out EncryptedConfiguration and test it * Better name * Add command and generator for credentials * Use the Pathnames * Extract EncryptedFile from EncryptedConfiguration and add serializers * Test EncryptedFile * Extract serializer validation * Stress the point about losing comments * Allow encrypted configuration to be read without parsing for display * Use credentials by default and base them on the master key * Derive secret_key_base in test/dev, source it from credentials in other envs And document the usage. * Document the new credentials setup * Stop generating the secrets.yml file now that we have credentials * Document what we should have instead Still need to make it happen, tho. * [ci skip] Keep wording to `key base`; prefer defaults. Usually we say we change defaults, not "spec" out a release. Can't use backticks in our sdoc generated documentation either. * Abstract away OpenSSL; prefer MessageEncryptor. * Spare needless new when raising. * Encrypted file test shouldn't depend on subclass. * [ci skip] Some woordings. * Ditch serializer future coding. * I said flip it. Flip it good. * [ci skip] Move require_master_key to the real production.rb. * Add require_master_key to abort the boot process. In case the master key is required in a certain environment we should inspect that the key is there and abort if it isn't. * Print missing key message and exit immediately. Spares us a lengthy backtrace and prevents further execution. I've verified the behavior in a test app, but couldn't figure the test out as loading the app just exits immediately with: ``` /Users/kasperhansen/Documents/code/rails/activesupport/lib/active_support/testing/isolation.rb:23:in `load': marshal data too short (ArgumentError) from /Users/kasperhansen/Documents/code/rails/activesupport/lib/active_support/testing/isolation.rb:23:in `run' from /Users/kasperhansen/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/minitest-5.10.2/lib/minitest.rb:830:in `run_one_method' from /Users/kasperhansen/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/minitest-5.10.2/lib/minitest/parallel.rb:32:in `block (2 levels) in start' ``` It's likely we need to capture and prevent the exit somehow. Kernel.stub(:exit) didn't work. Leaving it for tomorrow. * Fix require_master_key config test. Loading the app would trigger the `exit 1` per require_master_key's semantics, which then aborted the test. Fork and wait for the child process to finish, then inspect the exit status. Also check we aborted because of a missing master key, so something else didn't just abort the boot. Much <3 to @tenderlove for the tip. * Support reading/writing configs via methods. * Skip needless deep symbolizing. * Remove save; test config reader elsewhere. * Move secret_key_base check to when we're reading it. Otherwise we'll abort too soon since we don't assign the secret_key_base to secrets anymore. * Add missing string literal comments; require unneeded yaml require. * ya ya ya, rubocop. * Add master_key/credentials after bundle. Then we can reuse the existing message on `rails new bc4`. It'll look like: ``` Using web-console 3.5.1 from https://github.com/rails/web-console.git (at master@ce985eb) Using rails 5.2.0.alpha from source at `/Users/kasperhansen/Documents/code/rails` Using sass-rails 5.0.6 Bundle complete! 16 Gemfile dependencies, 72 gems now installed. Use `bundle info [gemname]` to see where a bundled gem is installed. Adding config/master.key to store the master encryption key: 97070158c44b4675b876373a6bc9d5a0 Save this in a password manager your team can access. If you lose the key, no one, including you, can access anything encrypted with it. create config/master.key ``` And that'll be executed even if `--skip-bundle` was passed. * Ensure test app has secret_key_base. * Assign secret_key_base to app or omit. * Merge noise * Split options for dynamic delegation into its own method and use deep symbols to make it work * Update error to point to credentials instead * Appease Rubocop * Validate secret_key_base when reading it. Instead of relying on the validation in key_generator move that into secret_key_base itself. * Fix generator and secrets test. Manually add config.read_encrypted_secrets since it's not there by default anymore. Move mentions of config/secrets.yml to config/credentials.yml.enc. * Remove files I have no idea how they got here. * [ci skip] swap secrets for credentials. * [ci skip] And now, changelogs are coming.
2017-09-11 14:21:20 -04:00
@app = Class.new(Rails::Application) do
def self.name; "RailtiesTestApp"; end
end
@app.config.eager_load = false
@app.config.session_store :cookie_store, key: "_myapp_session"
@app.config.active_support.deprecation = :log
@app.config.active_support.test_order = :random
@app.config.log_level = :info
yield @app if block_given?
@app.initialize!
@app.routes.draw do
get "/" => "omg#index"
end
require "rack/test"
extend ::Rack::Test::Methods
end
def simple_controller
controller :foo, <<-RUBY
class FooController < ApplicationController
def index
render plain: "foo"
end
end
RUBY
app_file "config/routes.rb", <<-RUBY
Rails.application.routes.draw do
get ':controller(/:action)'
end
RUBY
end
2009-11-06 20:21:39 -05:00
class Bukkit
2010-01-26 08:58:00 -05:00
attr_reader :path
2009-11-06 20:21:39 -05:00
def initialize(path)
@path = path
end
def write(file, string)
path = "#{@path}/#{file}"
FileUtils.mkdir_p(File.dirname(path))
File.open(path, "w") { |f| f.puts string }
2009-11-06 20:21:39 -05:00
end
def delete(file)
File.delete("#{@path}/#{file}")
end
end
2010-01-26 08:58:00 -05:00
def engine(name)
dir = "#{app_path}/random/#{name}"
FileUtils.mkdir_p(dir)
app = File.readlines("#{app_path}/config/application.rb")
app.insert(4, "$:.unshift(\"#{dir}/lib\")")
app.insert(5, "require #{name.inspect}")
2010-01-26 08:58:00 -05:00
File.open("#{app_path}/config/application.rb", "r+") do |f|
2010-01-26 08:58:00 -05:00
f.puts app
end
2009-11-06 20:21:39 -05:00
Bukkit.new(dir).tap do |bukkit|
yield bukkit if block_given?
end
end
# Invoke a bin/rails command inside the app
#
# allow_failure:: true to return normally if the command exits with
# a non-zero status. By default, this method will raise.
# stderr:: true to pass STDERR output straight to the "real" STDERR.
# By default, the STDERR and STDOUT of the process will be
# combined in the returned string.
2017-09-07 17:56:20 -04:00
def rails(*args, allow_failure: false, stderr: false)
args = args.flatten
2017-09-07 17:56:20 -04:00
fork = true
command = "bin/rails #{Shellwords.join args}#{' 2>&1' unless stderr}"
# Don't fork if the environment has disabled it
fork = false if ENV["NO_FORK"]
# Don't fork if the runtime isn't able to
fork = false if !Process.respond_to?(:fork)
# Don't fork if we're re-invoking minitest
fork = false if args.first == "t" || args.grep(/\Atest(:|\z)/).any?
if fork
out_read, out_write = IO.pipe
if stderr
err_read, err_write = IO.pipe
else
err_write = out_write
end
pid = fork do
out_read.close
err_read.close if err_read
$stdin.reopen(File::NULL, "r")
$stdout.reopen(out_write)
$stderr.reopen(err_write)
at_exit do
case $!
when SystemExit
exit! $!.status
when nil
exit! 0
else
err_write.puts "#{$!.class}: #{$!}"
exit! 1
end
end
Rails.instance_variable_set :@_env, nil
$-v = $-w = false
Dir.chdir app_path unless Dir.pwd == app_path
ARGV.replace(args)
load "./bin/rails"
exit! 0
end
out_write.close
if err_read
err_write.close
$stderr.write err_read.read
end
output = out_read.read
Process.waitpid pid
else
output = `cd #{app_path}; #{command}`
end
raise "rails command failed (#{$?.exitstatus}): #{command}\n#{output}" unless allow_failure || $?.success?
output
end
2014-11-04 17:14:03 -05:00
def add_to_top_of_config(str)
environment = File.read("#{app_path}/config/application.rb")
if environment =~ /(Rails::Application\s*)/
File.open("#{app_path}/config/application.rb", "w") do |f|
2014-11-04 17:14:03 -05:00
f.puts $` + $1 + "\n#{str}\n" + $'
end
end
end
def add_to_config(str)
environment = File.read("#{app_path}/config/application.rb")
if environment =~ /(\n\s*end\s*end\s*)\z/
File.open("#{app_path}/config/application.rb", "w") do |f|
f.puts $` + "\n#{str}\n" + $1
end
end
end
def add_to_env_config(env, str)
environment = File.read("#{app_path}/config/environments/#{env}.rb")
if environment =~ /(\n\s*end\s*)\z/
File.open("#{app_path}/config/environments/#{env}.rb", "w") do |f|
f.puts $` + "\n#{str}\n" + $1
end
end
end
def remove_from_config(str)
remove_from_file("#{app_path}/config/application.rb", str)
end
def remove_from_env_config(env, str)
remove_from_file("#{app_path}/config/environments/#{env}.rb", str)
end
def remove_from_file(file, str)
2010-11-18 15:10:37 -05:00
contents = File.read(file)
contents.sub!(/#{str}/, "")
File.write(file, contents)
end
def app_file(path, contents, mode = "w")
file_name = "#{app_path}/#{path}"
FileUtils.mkdir_p File.dirname(file_name)
File.open(file_name, mode) do |f|
f.puts contents
end
file_name
end
def remove_file(path)
FileUtils.rm_rf "#{app_path}/#{path}"
end
def controller(name, contents)
app_file("app/controllers/#{name}_controller.rb", contents)
end
def use_frameworks(arr)
to_remove = [:actionmailer, :activerecord, :activestorage, :activejob] - arr
if to_remove.include?(:activerecord)
remove_from_config "config.active_record.*"
end
$:.reject! { |path| path =~ %r'/(#{to_remove.join('|')})/' }
end
def use_postgresql
File.open("#{app_path}/config/database.yml", "w") do |f|
f.puts <<-YAML
default: &default
adapter: postgresql
pool: 5
database: railties_test
development:
<<: *default
test:
<<: *default
YAML
end
end
end
end
2012-01-05 20:30:17 -05:00
class ActiveSupport::TestCase
include TestHelpers::Paths
include TestHelpers::Rack
include TestHelpers::Generation
include ActiveSupport::Testing::Stream
def frozen_error_class
Object.const_defined?(:FrozenError) ? FrozenError : RuntimeError
end
end
# Create a scope and build a fixture rails app
Module.new do
extend TestHelpers::Paths
# Build a rails app
FileUtils.rm_rf(app_template_path)
2012-06-19 17:27:52 -04:00
FileUtils.mkdir(app_template_path)
2016-02-27 13:02:02 -05:00
`#{Gem.ruby} #{RAILS_FRAMEWORK_ROOT}/railties/exe/rails new #{app_template_path} --skip-gemfile --skip-listen --no-rc`
File.open("#{app_template_path}/config/boot.rb", "w") do |f|
f.puts "require 'rails/all'"
end
# Fake 'Bundler.require' -- we run using the repo's Gemfile, not an
# app-specific one: we don't want to require every gem that lists.
contents = File.read("#{app_template_path}/config/application.rb")
contents.sub!(/^Bundler\.require.*/, "%w(turbolinks).each { |r| require r }")
File.write("#{app_template_path}/config/application.rb", contents)
require "rails"
require "active_model"
require "active_job"
require "active_record"
require "action_controller"
require "action_mailer"
require "action_view"
require "active_storage"
require "action_cable"
require "sprockets"
2017-09-03 03:40:14 -04:00
require "action_view/helpers"
require "action_dispatch/routing/route_set"
end unless defined?(RAILS_ISOLATED_ENGINE)