Created
July 24, 2017 15:44
-
-
Save markburns/3caa2c9c80069efa0e0824e0f8f7adba to your computer and use it in GitHub Desktop.
run lint and db:seed in specs only if changed
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
# 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