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'
|
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
|
|
|
|
install_gems
|
|
|
|
remove_unwanted_gems
|
|
|
|
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
|
|
|
|
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
|
2015-02-08 16:38:55 -05:00
|
|
|
write_database_configuration
|
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
|
2015-10-25 23:14:07 -04:00
|
|
|
run_command! %W(rails new #{fs.project_directory} --skip-bundle --no-rc)
|
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
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
run_command! 'bundle exec rake db:drop db:create db:migrate'
|
|
|
|
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 install_gems
|
2014-10-26 20:53:11 -04:00
|
|
|
bundle.install_gems
|
2014-10-26 20:51:42 -04:00
|
|
|
end
|
2013-11-22 22:25:27 -05:00
|
|
|
|
2015-02-05 01:32:05 -05:00
|
|
|
def remove_unwanted_gems
|
|
|
|
bundle.updating do
|
|
|
|
bundle.remove_gem 'debugger'
|
|
|
|
bundle.remove_gem 'byebug'
|
|
|
|
bundle.remove_gem 'web-console'
|
|
|
|
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
|
|
|
|
end
|
|
|
|
end
|