Skip to content

Instantly share code, notes, and snippets.

@markburns
Created July 24, 2017 15:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markburns/3caa2c9c80069efa0e0824e0f8f7adba to your computer and use it in GitHub Desktop.
Save markburns/3caa2c9c80069efa0e0824e0f8f7adba to your computer and use it in GitHub Desktop.
run lint and db:seed in specs only if changed
# spec/lint_spec.rb
require "rails_helper"
RSpec.describe "Lint" do
it "FactoryGirl" do
should_run = directory_changed?("spec/factories") || directory_changed?("db")
next unless should_run
FactoryGirl.lint
end
context "db seeds" do
let(:command) { "bundle exec rake db:seed --trace" }
it "runs rake db:seed succesfully" do
next unless directory_changed?("db")
result, success = invoke_rake_command!
failure!(result) unless success
cleanup_db!
end
def invoke_rake_command!
puts "Running #{command}"
result = `#{command} 2>&1`
[result, $?]
end
def cleanup_db!
puts "Command completed - cleaning db"
DatabaseCleaner.clean_with :truncation
DatabaseCleaner.clean
puts "DB cleaned"
end
SeedExecutionFailure = Class.new StandardError
def failure!
message = ["-" * 80, "Failure executing command", command, result, "-" * 80].join("\n")
raise SeedExecutionFailure.new message
end
end
def directory_changed?(dir)
return false
result = `git diff master -- #{dir} | wc -c`
result.chomp.strip.to_i > 0
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment