2015-02-08 16:38:55 -05:00
|
|
|
require_relative '../tests/bundle'
|
2014-10-26 20:53:11 -04:00
|
|
|
require_relative '../tests/command_runner'
|
2015-02-08 16:38:55 -05:00
|
|
|
require_relative '../tests/database'
|
2014-10-26 20:53:11 -04:00
|
|
|
require_relative '../tests/filesystem'
|
2017-07-24 23:24:12 -04:00
|
|
|
require_relative 'helpers/rails_versions'
|
2015-02-08 16:38:55 -05:00
|
|
|
|
|
|
|
require 'yaml'
|
2013-11-22 22:25:27 -05:00
|
|
|
|
2014-10-26 20:51:42 -04:00
|
|
|
module UnitTests
|
2014-10-26 20:53:11 -04:00
|
|
|
class RailsApplication
|
|
|
|
def initialize
|
|
|
|
@fs = Tests::Filesystem.new
|
|
|
|
@bundle = Tests::Bundle.new
|
2015-02-08 16:38:55 -05:00
|
|
|
@database = Tests::Database.instance
|
2014-10-26 20:53:11 -04:00
|
|
|
end
|
2014-10-26 20:51:42 -04:00
|
|
|
|
|
|
|
def create
|
2014-10-26 20:53:11 -04:00
|
|
|
fs.clean
|
2014-10-26 20:51:42 -04:00
|
|
|
generate
|
2015-02-08 16:38:55 -05:00
|
|
|
|
2015-02-05 01:32:05 -05:00
|
|
|
fs.within_project do
|
2019-03-21 01:24:02 -04:00
|
|
|
update_gems
|
2015-02-05 01:32:05 -05:00
|
|
|
end
|
2014-10-26 20:51:42 -04:00
|
|
|
end
|
2013-11-22 22:25:27 -05:00
|
|
|
|
2014-10-26 20:51:42 -04:00
|
|
|
def load
|
|
|
|
load_environment
|
2020-01-13 17:51:43 -05:00
|
|
|
|
|
|
|
if rails_version > 5 && bundle.includes?('actiontext')
|
|
|
|
add_action_text_migration
|
|
|
|
end
|
|
|
|
|
2014-10-26 20:51:42 -04:00
|
|
|
run_migrations
|
|
|
|
end
|
2013-11-22 22:25:27 -05:00
|
|
|
|
2014-10-26 20:51:42 -04:00
|
|
|
def gemfile_path
|
2014-10-26 20:53:11 -04:00
|
|
|
fs.find('Gemfile')
|
2014-10-26 20:51:42 -04:00
|
|
|
end
|
2013-11-22 22:25:27 -05:00
|
|
|
|
2014-10-26 20:51:42 -04:00
|
|
|
def environment_file_path
|
2014-10-26 20:53:11 -04:00
|
|
|
fs.find_in_project('config/environment')
|
2014-10-26 20:51:42 -04:00
|
|
|
end
|
2013-11-22 22:25:27 -05:00
|
|
|
|
2014-10-26 20:53:11 -04:00
|
|
|
def temp_views_directory
|
|
|
|
fs.find_in_project('tmp/views')
|
2014-10-26 20:51:42 -04:00
|
|
|
end
|
2013-11-22 22:25:27 -05:00
|
|
|
|
2014-10-26 20:51:42 -04:00
|
|
|
def create_temp_view(path, contents)
|
|
|
|
full_path = temp_view_path_for(path)
|
2014-10-26 20:53:11 -04:00
|
|
|
full_path.dirname.mkpath
|
|
|
|
full_path.open('w') { |f| f.write(contents) }
|
2014-10-26 20:51:42 -04:00
|
|
|
end
|
2013-11-22 22:25:27 -05:00
|
|
|
|
2014-10-26 20:51:42 -04:00
|
|
|
def delete_temp_views
|
2014-10-26 20:53:11 -04:00
|
|
|
if temp_views_directory.exist?
|
|
|
|
temp_views_directory.rmtree
|
|
|
|
end
|
2014-10-26 20:51:42 -04:00
|
|
|
end
|
2013-11-22 22:25:27 -05:00
|
|
|
|
2014-10-26 20:51:42 -04:00
|
|
|
def draw_routes(&block)
|
|
|
|
Rails.application.routes.draw(&block)
|
|
|
|
Rails.application.routes
|
|
|
|
end
|
2013-11-22 22:25:27 -05:00
|
|
|
|
2014-10-26 20:53:11 -04:00
|
|
|
protected
|
|
|
|
|
2015-02-08 16:38:55 -05:00
|
|
|
attr_reader :fs, :shell, :bundle, :database
|
2014-10-26 20:53:11 -04:00
|
|
|
|
2014-10-26 20:51:42 -04:00
|
|
|
private
|
2013-11-22 22:25:27 -05:00
|
|
|
|
2014-10-26 20:53:11 -04:00
|
|
|
def migrations_directory
|
|
|
|
fs.find_in_project('db/migrate')
|
2014-10-26 20:51:42 -04:00
|
|
|
end
|
2013-11-22 22:25:27 -05:00
|
|
|
|
2014-10-26 20:51:42 -04:00
|
|
|
def temp_view_path_for(path)
|
2014-10-26 20:53:11 -04:00
|
|
|
temp_views_directory.join(path)
|
2014-10-26 20:51:42 -04:00
|
|
|
end
|
2013-11-22 22:25:27 -05:00
|
|
|
|
2014-10-26 20:51:42 -04:00
|
|
|
def generate
|
|
|
|
rails_new
|
|
|
|
fix_available_locales_warning
|
2018-09-10 01:04:10 -04:00
|
|
|
remove_bootsnap
|
2015-02-08 16:38:55 -05:00
|
|
|
write_database_configuration
|
2019-04-13 07:57:25 -04:00
|
|
|
write_activerecord_model_with_default_connection
|
|
|
|
write_activerecord_model_with_different_connection
|
2017-07-24 23:24:12 -04:00
|
|
|
|
2019-03-21 01:24:02 -04:00
|
|
|
if rails_version >= 5
|
2017-07-24 23:24:12 -04:00
|
|
|
add_initializer_for_time_zone_aware_types
|
|
|
|
end
|
2014-10-26 20:51:42 -04:00
|
|
|
end
|
2014-01-21 13:34:45 -05:00
|
|
|
|
2014-10-26 20:51:42 -04:00
|
|
|
def rails_new
|
2019-03-21 01:24:02 -04:00
|
|
|
run_command!(*rails_new_command)
|
|
|
|
end
|
|
|
|
|
|
|
|
def rails_new_command
|
|
|
|
if rails_version > 5
|
|
|
|
[
|
|
|
|
'rails',
|
|
|
|
'new',
|
|
|
|
fs.project_directory.to_s,
|
|
|
|
'--skip-bundle',
|
|
|
|
'--no-rc',
|
|
|
|
'--skip-webpack-install',
|
|
|
|
]
|
|
|
|
else
|
|
|
|
[
|
|
|
|
'rails',
|
|
|
|
'new',
|
|
|
|
fs.project_directory.to_s,
|
|
|
|
'--skip-bundle',
|
|
|
|
'--no-rc',
|
|
|
|
]
|
|
|
|
end
|
2014-10-26 20:51:42 -04:00
|
|
|
end
|
2013-11-22 22:25:27 -05:00
|
|
|
|
2014-10-26 20:51:42 -04:00
|
|
|
def fix_available_locales_warning
|
|
|
|
# See here for more on this:
|
|
|
|
# http://stackoverflow.com/questions/20361428/rails-i18n-validation-deprecation-warning
|
2015-02-05 01:32:05 -05:00
|
|
|
fs.transform('config/application.rb') do |lines|
|
|
|
|
lines.insert(-3, <<-EOT)
|
2014-01-21 13:34:45 -05:00
|
|
|
if I18n.respond_to?(:enforce_available_locales=)
|
|
|
|
I18n.enforce_available_locales = false
|
|
|
|
end
|
2015-02-05 01:32:05 -05:00
|
|
|
EOT
|
|
|
|
end
|
2014-01-21 13:34:45 -05:00
|
|
|
end
|
|
|
|
|
2018-09-10 01:04:10 -04:00
|
|
|
def remove_bootsnap
|
|
|
|
# Rails 5.2 introduced bootsnap, which is helpful when you're developing
|
|
|
|
# or deploying an app, but we don't really need it (and it messes with
|
|
|
|
# Zeus anyhow)
|
|
|
|
fs.comment_lines_matching(
|
|
|
|
'config/boot.rb',
|
|
|
|
%r{\Arequire 'bootsnap/setup'},
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2015-02-08 16:38:55 -05:00
|
|
|
def write_database_configuration
|
|
|
|
YAML.dump(database.config.to_hash, fs.open('config/database.yml', 'w'))
|
|
|
|
end
|
|
|
|
|
2019-04-13 07:57:25 -04:00
|
|
|
def write_activerecord_model_with_different_connection
|
|
|
|
# To simulate multi-db connections, we create a new "base model" which
|
|
|
|
# connects to a different database (in this case -
|
|
|
|
# shoulda-matchers-test_production).
|
|
|
|
# Any models which inherit from this class, or uses this model's
|
|
|
|
# connection will be routed to this database.
|
|
|
|
path = 'app/models/production_record.rb'
|
|
|
|
fs.write(path, <<-TEXT)
|
|
|
|
class ProductionRecord < ActiveRecord::Base
|
|
|
|
self.abstract_class = true
|
|
|
|
establish_connection :production
|
|
|
|
end
|
|
|
|
TEXT
|
|
|
|
end
|
|
|
|
|
|
|
|
def write_activerecord_model_with_default_connection
|
|
|
|
# Alongside ProductionRecord created above, we also create a dummy
|
|
|
|
# DevelopmentRecord which connects to the default database (in this case -
|
|
|
|
# shoulda-matchers-test_development, for symmetry's sake. This allows us
|
|
|
|
# to be a little more explicit when writing tests, for example:
|
|
|
|
# expect(with_index_on(:age1, parent_class: DevelopmentRecord)).to have_db_index(:age1)
|
|
|
|
# expect(with_index_on(:age2, parent_class: ProductionRecord)).to have_db_index(:age2)
|
|
|
|
path = 'app/models/development_record.rb'
|
|
|
|
fs.write(path, <<-TEXT)
|
|
|
|
class DevelopmentRecord < ActiveRecord::Base
|
|
|
|
self.abstract_class = true
|
|
|
|
end
|
|
|
|
TEXT
|
|
|
|
end
|
|
|
|
|
2020-01-13 17:51:43 -05:00
|
|
|
def add_action_text_migration
|
|
|
|
fs.within_project do
|
|
|
|
run_command! 'bundle exec rake action_text:install:migrations'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-24 23:24:12 -04:00
|
|
|
def add_initializer_for_time_zone_aware_types
|
|
|
|
path = 'config/initializers/configure_time_zone_aware_types.rb'
|
2017-07-25 01:04:38 -04:00
|
|
|
fs.write(path, <<-TEXT)
|
|
|
|
Rails.application.configure do
|
|
|
|
config.active_record.time_zone_aware_types = [:datetime, :time]
|
|
|
|
end
|
2017-07-24 23:24:12 -04:00
|
|
|
TEXT
|
|
|
|
end
|
|
|
|
|
2014-10-26 20:51:42 -04:00
|
|
|
def load_environment
|
|
|
|
require environment_file_path
|
|
|
|
end
|
2013-11-22 22:25:27 -05:00
|
|
|
|
2014-10-26 20:51:42 -04:00
|
|
|
def run_migrations
|
2015-02-08 16:38:55 -05:00
|
|
|
fs.within_project do
|
2019-04-13 07:57:25 -04:00
|
|
|
run_command! 'bundle exec rake db:drop:all db:create:all db:migrate'
|
2015-02-08 16:38:55 -05:00
|
|
|
end
|
2014-10-26 20:51:42 -04:00
|
|
|
end
|
2013-11-22 22:25:27 -05:00
|
|
|
|
2019-03-21 01:24:02 -04:00
|
|
|
def update_gems
|
2015-02-05 01:32:05 -05:00
|
|
|
bundle.updating do
|
2019-03-21 01:24:02 -04:00
|
|
|
bundle.remove_gem 'turn'
|
|
|
|
bundle.remove_gem 'coffee-rails'
|
|
|
|
bundle.remove_gem 'uglifier'
|
2015-02-05 01:32:05 -05:00
|
|
|
bundle.remove_gem 'debugger'
|
|
|
|
bundle.remove_gem 'byebug'
|
|
|
|
bundle.remove_gem 'web-console'
|
2019-03-21 01:24:02 -04:00
|
|
|
bundle.add_gem 'pg'
|
|
|
|
bundle.remove_gem 'sqlite3'
|
|
|
|
bundle.add_gem 'sqlite3', '~> 1.3.6'
|
2015-02-05 01:32:05 -05:00
|
|
|
end
|
|
|
|
end
|
2014-10-26 20:51:42 -04:00
|
|
|
|
2014-10-26 20:53:11 -04:00
|
|
|
def run_command!(*args)
|
|
|
|
Tests::CommandRunner.run!(*args)
|
2013-11-22 22:25:27 -05:00
|
|
|
end
|
2019-03-21 01:24:02 -04:00
|
|
|
|
|
|
|
def rails_version
|
|
|
|
bundle.version_of('rails')
|
|
|
|
end
|
2013-11-22 22:25:27 -05:00
|
|
|
end
|
|
|
|
end
|