Created
April 4, 2017 10:47
-
-
Save andyjeffries/dcb7a32ea4d0fe6ccbb8eeefd56e9b9d to your computer and use it in GitHub Desktop.
My Rails template to setup RSpec and Guard and Dokku integration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Add to the given file the given lines, after the line with the given text, or replace the content | |
def add_to_file(path, text, after=nil, replace=false) | |
lines = [] | |
if replace | |
lines = text | |
else | |
File.readlines(path).each do |line| | |
if after != nil and line.include?(after) | |
lines << line | |
lines << text | |
else | |
lines << line | |
end | |
end | |
lines << text if after == nil | |
end | |
File.open(path, 'w') do |file| | |
file.puts lines | |
end | |
end | |
# Find app name | |
app_name = open('config/application.rb').grep(/module\ /)[0].sub('module ', '').sub("\n",'') | |
# Add default gems | |
gem 'mysql2' | |
gem 'execjs' | |
gem 'therubyracer' | |
gem_group :test, :development do | |
gem 'database_cleaner' | |
gem 'factory_girl_rails' | |
gem 'rspec-rails' | |
gem 'capybara' | |
gem "capybara-webkit" | |
gem 'guard-rspec', require: false | |
end | |
run "bundle install" | |
puts "If you get an error like - Command 'qmake ' not available then run the following:" | |
puts "brew install [email protected]" | |
puts "brew link --force qt55" | |
# Customize generators | |
generators = " config.generators do |g| | |
g.orm :active_record | |
g.template_engine :erb | |
g.test_framework :rspec, fixture: true, views: false | |
g.fixture_replacement :factory_girl, :dir => \"spec/factories\" | |
g.stylesheets false | |
end" | |
add_to_file('config/application.rb', generators, "Rails::Application") | |
run "rails generate rspec:install" | |
# Change rspec setup | |
# https://www.devmynd.com/blog/setting-up-rspec-and-capybara-in-rails-5-for-testing/ | |
rspecy = %Q[RSpec.configure do |config| | |
config.expect_with :rspec do |expectations| | |
expectations.include_chain_clauses_in_custom_matcher_descriptions = true | |
end | |
config.mock_with :rspec do |mocks| | |
mocks.verify_partial_doubles = true | |
end | |
config.shared_context_metadata_behavior = :apply_to_host_groups | |
config.filter_run_when_matching :focus | |
config.example_status_persistence_file_path = "spec/examples.txt" | |
config.disable_monkey_patching! | |
config.default_formatter = 'doc' if config.files_to_run.one? | |
config.order = :random | |
Kernel.srand config.seed | |
# config.profile_examples = 10 | |
end] | |
add_to_file('spec/spec_helper.rb', rspecy, nil, true) | |
railsrspecy = %Q[ENV["RAILS_ENV"] ||= "test" | |
require File.expand_path("../../config/environment", __FILE__) | |
abort("The Rails environment is running in production mode!") if Rails.env.production? | |
require "spec_helper" | |
require "rspec/rails" | |
# Add additional requires below this line. Rails is not loaded until this point! | |
require "capybara/rspec" | |
ActiveRecord::Migration.maintain_test_schema! | |
Capybara.javascript_driver = :webkit | |
RSpec.configure do |config| | |
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures | |
config.fixture_path = "\#{::Rails.root}/spec/fixtures" | |
config.use_transactional_fixtures = false | |
config.infer_spec_type_from_file_location! | |
config.filter_rails_from_backtrace! | |
config.before(:suite) do | |
DatabaseCleaner.clean_with(:truncation) | |
end | |
config.before(:each) do | |
DatabaseCleaner.strategy = :transaction | |
end | |
config.before(:each, js: true) do | |
DatabaseCleaner.strategy = :truncation | |
end | |
# This block must be here, do not combine with the other `before(:each)` block. | |
# This makes it so Capybara can see the database. | |
config.before(:each) do | |
DatabaseCleaner.start | |
end | |
config.after(:each) do | |
DatabaseCleaner.clean | |
end | |
end] | |
add_to_file('spec/rails_helper.rb', railsrspecy, nil, true) | |
# Create database setup | |
dbconf = %Q{development: | |
adapter: mysql2 | |
encoding: utf8 | |
reconnect: false | |
database: #{app_name.downcase}_development | |
pool: 5 | |
username: root | |
password: | |
socket: /tmp/mysql.sock | |
# Warning: The database defined as \"test\" will be erased and | |
# re-generated from your development database when you run \"rake\". | |
# Do not set this db to the same as development or production. | |
test: | |
adapter: mysql2 | |
encoding: utf8 | |
reconnect: false | |
database: #{app_name.downcase}_test | |
pool: 5 | |
username: root | |
password: | |
socket: /tmp/mysql.sock | |
production: | |
adapter: mysql2 | |
encoding: utf8 | |
reconnect: true | |
database: <%= ENV["DB_DATABASE"] %> | |
pool: 25 | |
username: <%= ENV["DB_USERNAME"] %> | |
password: <%= ENV["DB_PASSWORD"] %> | |
<% if ENV["DB_HOSTNAME"].present? %> | |
host: <%= ENV["DB_HOSTNAME"] %> | |
port: 3306 | |
<% else %> | |
socket: /var/run/mysqld/mysqld.sock | |
<% end %>} | |
add_to_file("config/database.yml", dbconf, nil, true) | |
# Preparing for Dokku-isation | |
checks = "WAIT=3 # Wait 3 seconds between each attempt | |
ATTEMPTS=10 # Try 10 times total before abandoning | |
/ Welcome to #{app_name}" | |
add_to_file("CHECKS", checks, nil, true) | |
app_json = %Q[ | |
{ | |
"name": "#{app_name}", | |
"description": "The official website for #{app_name}", | |
"scripts": { | |
"dokku": { | |
"predeploy": "bundle exec rake db:migrate" | |
} | |
} | |
} | |
] | |
add_to_file("app.json", app_json, nil, true) | |
run "rake db:create" | |
run "bundle exec guard init rspec" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment