Last active
August 29, 2015 14:22
-
-
Save KenshoFujisaki/0606fcdb859be88167ee to your computer and use it in GitHub Desktop.
gitコミット前後のRSpecの実行結果で、Failureケースの差分を出力するシェル
This file contains 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
#!/bin/bash | |
# constants | |
seed_number=1234 | |
# check args | |
if [ $# -ne 2 ]; then | |
echo "usage: $0 [before_revision_hash] [after_revision_hash]" | |
echo "e.g.: $0 212dfa0 0bc3dc7" | |
echo "e.g.: $0 0bc3dc7^ 0bc3dc7" | |
exit 1 | |
fi | |
before_revision_hash="$1" | |
after_revision_hash="$2" | |
# memo original branch name | |
original_branch=`git branch | awk '/^\*/{print $2}'` | |
# trap error | |
trap "echo -e '\033[40m\033[1;31mCanceled!! rollback to original git branch.\033[0m'; "\ | |
"git stash; git checkout $original_branch; git stash pop; exit 1" 1 2 3 15 | |
# get target files | |
git stash -q | |
git checkout -q "$after_revision_hash" | |
git stash pop -q | |
target_files=$(git diff --name-only ${before_revision_hash}..${after_revision_hash} \ | |
| sed -e 's/app\(.*_\(controller\|model\)\)\.rb/spec\1_spec\.rb/' \ | |
| grep -E '_spec\.rb$' \ | |
| sort -u \ | |
| xargs -I{} bash -c 'if [ -e {} ]; then echo {}; fi') | |
if [ "$target_files" = "" ]; then | |
echo -e "\033[40m\033[1;33mRspec target files: noting.\033[0m" | |
echo -e "\n\033[40m\033[32m[SUCCESS] New red case is not increased. Your commit is safe :)\033[0m" | |
exit 0 | |
else | |
echo -e "\033[40m\033[1;33mRspec target files:\n${target_files}\033[0m" | |
fi | |
# obtain the failures of before revision | |
echo -e "\n\033[40m\033[1;33mRspec execution at before revision hash($before_revision_hash):\033[0m" | |
git stash -q | |
git checkout -q "$before_revision_hash" | |
git stash pop -q | |
echo -e "$target_files" \ | |
| xargs -I{} spring rspec --seed $seed_number -fd -c {} \ | |
| tee spec_before.log | |
# obtain the failures of after revision | |
echo -e "\n\033[40m\033[1;33mRspec execution at after revision hash($after_revision_hash):\033[0m" | |
git stash -q | |
git checkout -q "$after_revision_hash" | |
git stash pop -q | |
echo -e "$target_files" \ | |
| xargs -I{} spring rspec --seed $seed_number -fd -c {} \ | |
| tee spec_after.log | |
# obtain the diff of failures | |
echo -e "\n\033[40m\033[1;33mDiff rspec execution:\033[0m" | |
echo "--------------------------------------------------------------------------------------" | |
echo -e "[-:before($before_revision_hash),+:after($after_revision_hash)]" | |
diff_result=$(diff -U0 \ | |
<(cat spec_before.log | sed -ne '/Failed examples:/,${/^rspec/p}' |\ | |
ruby -nle 'a=$_.split(" ");a[1]=a[1].match(/(.*)\:\d+$/)[1];print a.join(" ")' | sort) \ | |
<(cat spec_after.log | sed -ne '/Failed examples:/,${/^rspec/p}' |\ | |
ruby -nle 'a=$_.split(" ");a[1]=a[1].match(/(.*)\:\d+$/)[1];print a.join(" ")' | sort )) | |
echo -e "$diff_result" | |
echo "--------------------------------------------------------------------------------------" | |
# restore original branch | |
echo -e "\n\033[40m\033[1;33mBack to original git branch:\033[0m" | |
git stash -q | |
git checkout $original_branch | |
git stash pop | |
# result | |
if [ "$(echo -e $diff_result | grep -E '\+rspec')" != "" ]; then | |
echo -e "\n\033[40m\033[1;31m[ERROR] New red case is increased !! Your commit contains bugs. Please fix it :<\033[0m" | |
echo "--------------------------------------------------------------------------------------" | |
echo -e "$diff_result" | grep -E '^\+rspec' | |
echo "--------------------------------------------------------------------------------------" | |
exit 1 | |
else | |
echo -e "\n\033[40m\033[32m[SUCCESS] New red case is not increased. Your commit is safe :)\033[0m" | |
exit 0 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment