forked from fixin.me/fixin.me
Compare commits
1 Commits
feature/ex
...
feature/mu
| Author | SHA1 | Date | |
|---|---|---|---|
| 24539f236c |
@@ -21,7 +21,7 @@ Bundler.require(*Rails.groups)
|
|||||||
module FixinMe
|
module FixinMe
|
||||||
class Application < Rails::Application
|
class Application < Rails::Application
|
||||||
# Allow RAILS_DATABASE_YML to override the database config file path.
|
# Allow RAILS_DATABASE_YML to override the database config file path.
|
||||||
# Used by the multi-database test runner (lib/tasks/test_databases.rake).
|
# Used by `rails test:all_databases` to test against multiple DB adapters.
|
||||||
config.paths['config/database'] = [ENV['RAILS_DATABASE_YML']] if ENV['RAILS_DATABASE_YML']
|
config.paths['config/database'] = [ENV['RAILS_DATABASE_YML']] if ENV['RAILS_DATABASE_YML']
|
||||||
|
|
||||||
# Initialize configuration defaults for originally generated Rails version.
|
# Initialize configuration defaults for originally generated Rails version.
|
||||||
|
|||||||
@@ -49,20 +49,22 @@ production:
|
|||||||
# <<: *default
|
# <<: *default
|
||||||
# database: fixinme_test
|
# database: fixinme_test
|
||||||
|
|
||||||
# Multi-database testing
|
# Multi-database testing — `bundle exec rails test:all_databases`
|
||||||
# ----------------------
|
# ---------------------------------------------------------------
|
||||||
# Any key starting with "test" is treated as a test database.
|
# Add any number of `test_<name>:` blocks to run the full test suite
|
||||||
# When more than one is present, EVERY test task (rails test, rails test:models,
|
# against additional database adapters in a single command.
|
||||||
# rails test:system, …) automatically runs against all of them.
|
# Each adapter's gem must be available in the bundle:
|
||||||
|
# bundle config --local with "mysql:sqlite" # mysql + sqlite
|
||||||
|
# bundle config --local with "mysql:pg" # mysql + postgresql
|
||||||
#
|
#
|
||||||
# The adapter gem must be available:
|
# Example — run tests against MySQL and SQLite:
|
||||||
# bundle config --local with "mysql sqlite" # mysql + sqlite
|
|
||||||
# bundle config --local with "mysql pg" # mysql + postgresql
|
|
||||||
#
|
#
|
||||||
#test_sqlite:
|
#test_sqlite:
|
||||||
# adapter: sqlite3
|
# adapter: sqlite3
|
||||||
# database: db/fixinme_test.sqlite3
|
# database: db/fixinme_test.sqlite3
|
||||||
#
|
#
|
||||||
|
# Example — run tests against MySQL and PostgreSQL:
|
||||||
|
#
|
||||||
#test_pg:
|
#test_pg:
|
||||||
# adapter: postgresql
|
# adapter: postgresql
|
||||||
# database: fixinme_test
|
# database: fixinme_test
|
||||||
|
|||||||
@@ -2,157 +2,117 @@ require 'yaml'
|
|||||||
require 'erb'
|
require 'erb'
|
||||||
require 'tmpdir'
|
require 'tmpdir'
|
||||||
|
|
||||||
# Multi-database test runner
|
namespace :test do
|
||||||
# ==========================
|
desc <<~DESC
|
||||||
# When database.yml contains more than one `test*` configuration, every
|
Run the full test suite against every test database configured in database.yml.
|
||||||
# standard test task (test, test:models, test:system, …) is automatically
|
|
||||||
# rewritten to run the full suite against EACH configured database in turn.
|
|
||||||
#
|
|
||||||
# Convention — any top-level key that starts with "test" and holds a Hash:
|
|
||||||
#
|
|
||||||
# test: ← required primary database
|
|
||||||
# adapter: mysql2
|
|
||||||
# database: fixinme_test
|
|
||||||
# ...
|
|
||||||
#
|
|
||||||
# test_sqlite: ← optional additional databases
|
|
||||||
# adapter: sqlite3
|
|
||||||
# database: db/fixinme_test.sqlite3
|
|
||||||
#
|
|
||||||
# test_pg:
|
|
||||||
# adapter: postgresql
|
|
||||||
# ...
|
|
||||||
#
|
|
||||||
# A single-database setup is unchanged: every task behaves exactly as before.
|
|
||||||
#
|
|
||||||
# The mechanism uses RAILS_DATABASE_YML — an env var read by
|
|
||||||
# config/application.rb(.dist) to override Rails' database config path before
|
|
||||||
# initialisation, giving each subprocess a clean, isolated database config.
|
|
||||||
|
|
||||||
module MultiDbTests
|
Any top-level key that starts with "test" and contains a Hash is treated as a
|
||||||
ADAPTER_GEMS = {
|
test database configuration:
|
||||||
'mysql2' => 'mysql2',
|
|
||||||
'sqlite3' => 'sqlite3',
|
|
||||||
'postgresql' => 'pg',
|
|
||||||
'pg' => 'pg',
|
|
||||||
}.freeze
|
|
||||||
|
|
||||||
ADAPTER_BUNDLE_GROUPS = {
|
test: # always required — the primary test database
|
||||||
'mysql2' => 'mysql',
|
adapter: mysql2
|
||||||
'sqlite3' => 'sqlite',
|
database: fixinme_test
|
||||||
'postgresql' => 'postgresql',
|
...
|
||||||
'pg' => 'postgresql',
|
|
||||||
}.freeze
|
|
||||||
|
|
||||||
# Rake task names generated by railties/lib/rails/test_unit/testing.rake
|
test_sqlite: # optional additional databases
|
||||||
# that use run_from_rake — these are the ones we rewrite.
|
adapter: sqlite3
|
||||||
WRAPPED_TASKS = (
|
database: db/fixinme_test.sqlite3
|
||||||
['test'] +
|
|
||||||
Rails::TestUnit::Runner::TEST_FOLDERS.map { |f| "test:#{f}" } +
|
|
||||||
%w[test:all test:system test:generators test:units test:functionals]
|
|
||||||
).freeze
|
|
||||||
|
|
||||||
class << self
|
test_pg:
|
||||||
# Returns {name => config_hash} for every key starting with "test".
|
adapter: postgresql
|
||||||
def test_configs
|
database: fixinme_test
|
||||||
@test_configs ||= begin
|
...
|
||||||
db_file = Rails.root.join('config', 'database.yml')
|
|
||||||
all = YAML.safe_load(ERB.new(db_file.read).result, aliases: true) || {}
|
For each database the task will:
|
||||||
all.select { |k, v| k.to_s.start_with?('test') && v.is_a?(Hash) }
|
1. Check that the required adapter gem is available (skip with warning if not).
|
||||||
|
2. Run `rails db:test:prepare` to create/migrate the database.
|
||||||
|
3. Run `rails test` and record pass/fail.
|
||||||
|
|
||||||
|
A summary is printed at the end. The task exits non-zero if any database fails.
|
||||||
|
DESC
|
||||||
|
task :all_databases do
|
||||||
|
db_file = Rails.root.join('config', 'database.yml')
|
||||||
|
all = YAML.safe_load(ERB.new(db_file.read).result, aliases: true) || {}
|
||||||
|
test_cfgs = all.select { |k, v| k.to_s.start_with?('test') && v.is_a?(Hash) }
|
||||||
|
non_test = all.reject { |k, _| k.to_s.start_with?('test') }
|
||||||
|
|
||||||
|
abort "No test database configurations found in #{db_file}." if test_cfgs.empty?
|
||||||
|
|
||||||
|
results = {}
|
||||||
|
|
||||||
|
test_cfgs.each do |name, config|
|
||||||
|
adapter = config['adapter'].to_s
|
||||||
|
|
||||||
|
puts "\n#{'─' * 64}"
|
||||||
|
puts " #{name} (adapter: #{adapter})"
|
||||||
|
puts '─' * 64
|
||||||
|
|
||||||
|
unless adapter_available?(adapter)
|
||||||
|
puts " SKIPPED — gem for '#{adapter}' adapter not available in bundle."
|
||||||
|
puts " Add it to Gemfile (e.g. `bundle config --local with #{adapter_group(adapter)}`)"
|
||||||
|
results[name] = :skipped
|
||||||
|
next
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
def non_test_configs
|
Dir.mktmpdir('rails_test_db_') do |tmpdir|
|
||||||
@non_test_configs ||= begin
|
tmp_yml = File.join(tmpdir, 'database.yml')
|
||||||
db_file = Rails.root.join('config', 'database.yml')
|
# Write a standalone database.yml with just this config as `test:`
|
||||||
all = YAML.safe_load(ERB.new(db_file.read).result, aliases: true) || {}
|
# (non-test configs are preserved so boot doesn't fail on `production:` lookup)
|
||||||
all.reject { |k, _| k.to_s.start_with?('test') }
|
File.write(tmp_yml, non_test.merge('test' => config).to_yaml)
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Run rails +task_name+ for every configured test database.
|
env = { 'RAILS_DATABASE_YML' => tmp_yml }
|
||||||
# Called from the rewritten rake task actions.
|
|
||||||
def run(task_name)
|
|
||||||
cfgs = test_configs
|
|
||||||
results = {}
|
|
||||||
|
|
||||||
cfgs.each do |db_name, config|
|
if system(env, 'bundle exec rails db:test:prepare')
|
||||||
adapter = config['adapter'].to_s
|
results[name] = system(env, 'bundle exec rails test') ? :pass : :fail
|
||||||
puts "\n#{'─' * 64}"
|
else
|
||||||
puts " #{task_name} · #{db_name} (#{adapter})"
|
results[name] = :prepare_failed
|
||||||
puts '─' * 64
|
|
||||||
|
|
||||||
unless adapter_available?(adapter)
|
|
||||||
warn " SKIPPED — '#{adapter}' gem not in bundle.\n" \
|
|
||||||
" Run: bundle config --local with \"#{current_with} #{adapter_group(adapter)}\""
|
|
||||||
results[db_name] = :skipped
|
|
||||||
next
|
|
||||||
end
|
|
||||||
|
|
||||||
Dir.mktmpdir('rails_test_') do |tmpdir|
|
|
||||||
tmp_yml = File.join(tmpdir, 'database.yml')
|
|
||||||
File.write(tmp_yml, non_test_configs.merge('test' => config).to_yaml)
|
|
||||||
env = { 'RAILS_DATABASE_YML' => tmp_yml }
|
|
||||||
|
|
||||||
if system(env, 'bundle exec rails db:test:prepare')
|
|
||||||
results[db_name] = system(env, 'rails', task_name) ? :pass : :fail
|
|
||||||
else
|
|
||||||
results[db_name] = :prepare_failed
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
print_summary(cfgs, results)
|
|
||||||
|
|
||||||
failed = results.count { |_, s| [:fail, :prepare_failed].include?(s) }
|
|
||||||
exit(false) if failed > 0
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
puts "\n#{'═' * 64}"
|
||||||
|
puts " SUMMARY"
|
||||||
def adapter_available?(adapter)
|
puts '═' * 64
|
||||||
require ADAPTER_GEMS.fetch(adapter, adapter)
|
results.each do |name, status|
|
||||||
true
|
adapter = test_cfgs[name]['adapter']
|
||||||
rescue LoadError
|
icon = { pass: '✓', fail: '✗', prepare_failed: '✗', skipped: '–' }[status]
|
||||||
false
|
label = { pass: 'PASS', fail: 'FAIL',
|
||||||
|
prepare_failed: 'PREPARE FAILED', skipped: 'SKIPPED' }[status]
|
||||||
|
puts " #{icon} #{name.ljust(26)} (#{adapter.ljust(12)}) #{label}"
|
||||||
end
|
end
|
||||||
|
puts '═' * 64
|
||||||
|
|
||||||
def adapter_group(adapter)
|
failed = results.count { |_, s| s == :fail || s == :prepare_failed }
|
||||||
ADAPTER_BUNDLE_GROUPS.fetch(adapter, adapter)
|
abort "\n #{failed} database(s) failed." if failed > 0
|
||||||
end
|
|
||||||
|
|
||||||
def current_with
|
|
||||||
(Bundler.settings[:with] || '').split(':').join(' ')
|
|
||||||
end
|
|
||||||
|
|
||||||
def print_summary(cfgs, results)
|
|
||||||
return if results.size <= 1 # no summary for single-DB runs
|
|
||||||
|
|
||||||
puts "\n#{'═' * 64}"
|
|
||||||
puts ' SUMMARY'
|
|
||||||
puts '═' * 64
|
|
||||||
results.each do |db_name, status|
|
|
||||||
adapter = cfgs.dig(db_name, 'adapter') || '?'
|
|
||||||
icon = status == :pass ? '✓' : (status == :skipped ? '–' : '✗')
|
|
||||||
label = { pass: 'PASS', fail: 'FAIL',
|
|
||||||
prepare_failed: 'PREPARE FAILED', skipped: 'SKIPPED' }[status]
|
|
||||||
puts " #{icon} #{db_name.ljust(26)} (#{adapter.ljust(12)}) #{label}"
|
|
||||||
end
|
|
||||||
puts '═' * 64
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Rewrite every standard test task to run against all configured databases.
|
private
|
||||||
# This file loads after railties/testing.rake, so all tasks already exist.
|
|
||||||
# Single-database setups are completely unaffected.
|
|
||||||
if MultiDbTests.test_configs.size > 1
|
|
||||||
MultiDbTests::WRAPPED_TASKS.each do |task_name|
|
|
||||||
next unless Rake::Task.task_defined?(task_name)
|
|
||||||
|
|
||||||
Rake::Task[task_name].clear_actions
|
# Returns true if the gem required for this adapter is loadable.
|
||||||
Rake::Task[task_name].enhance do
|
ADAPTER_GEMS = {
|
||||||
MultiDbTests.run(task_name)
|
'mysql2' => 'mysql2',
|
||||||
end
|
'sqlite3' => 'sqlite3',
|
||||||
end
|
'postgresql' => 'pg',
|
||||||
|
'pg' => 'pg',
|
||||||
|
}.freeze
|
||||||
|
|
||||||
|
def adapter_available?(adapter)
|
||||||
|
gem_name = ADAPTER_GEMS.fetch(adapter, adapter)
|
||||||
|
require gem_name
|
||||||
|
true
|
||||||
|
rescue LoadError
|
||||||
|
false
|
||||||
|
end
|
||||||
|
|
||||||
|
# Returns the Bundler group name that installs the adapter gem.
|
||||||
|
ADAPTER_GROUPS = {
|
||||||
|
'mysql2' => 'mysql',
|
||||||
|
'sqlite3' => 'sqlite',
|
||||||
|
'postgresql' => 'postgresql',
|
||||||
|
'pg' => 'postgresql',
|
||||||
|
}.freeze
|
||||||
|
|
||||||
|
def adapter_group(adapter)
|
||||||
|
ADAPTER_GROUPS.fetch(adapter, adapter)
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user