Skip to content

Instantly share code, notes, and snippets.

@marcelorxaviers
Last active March 4, 2020 11:35
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 marcelorxaviers/7d60258ee79e93c31bcfbf6c406f77db to your computer and use it in GitHub Desktop.
Save marcelorxaviers/7d60258ee79e93c31bcfbf6c406f77db to your computer and use it in GitHub Desktop.
#!/bin/bash
chruby 2.7.0
export RUBYOPT=-W:no-deprecated
# Install all gem dependencies
bundle install
# Just to prevent showing many "error" messages
dirty_bit=0
# Run rubocop
rubocop -a
last_command_ret_code=$?
if [ $last_command_ret_code -eq 0 ]; then
# Run unit tests
bundle exec rails test
last_command_ret_code=$?
else
echo "RUBOCOP OFFENSES PREVENTED ME FROM COMMITTING..."
dirty_bit=1
fi
if [ $last_command_ret_code -eq 0 ]; then
# Prepare to run system tests
yarn build-for-tests
# Run system tests
bundle exec rails test:system
last_command_ret_code=$?
elif [ $dirty_bit -eq 0 ]; then
echo "UNIT TESTS FAILURES/ERRORS PREVENTED ME FROM COMMITTING..."
dirty_bit=1
fi
if [ $last_command_ret_code -eq 0 ]; then
echo "ALL GOOD. COMMITTING..."
elif [ $dirty_bit -eq 0 ]; then
echo "SYSTEM TESTS FAILURES/ERRORS PREVENTED ME FROM COMMITTING..."
fi
# Return the last "error" code if any
exit $last_command_ret_code
@xa-bi
Copy link

xa-bi commented Jan 30, 2020

My suggestion:

- rubocop -a
- NUM_OFFENSES=`rubocop -a | grep "offenses detected" | awk {'print $4'}`
+ NUM_OFFENSES=`rubocop -a | egrep 'offenses? detected' | perl -e '($d,undef,$c)=<STDIN>=~m/\s(\S*)\soffenses?\sdetected(,\s(\S*)\soffenses?\scorrected)?/;print"$d"eq"no"||"$d"eq"$c"?0:$d-$c;'`

just one pass of rubocop

The idea is no offenses => 0 N offenses detected, K offenses corrected => N-K

@xa-bi
Copy link

xa-bi commented Jan 30, 2020

Also be careful with this condition: if [ $NUM_FAILS -ne 0 ] cause when its true the script exits without doing a git stash pop -q

@beagleknight
Copy link

beagleknight commented Jan 31, 2020

At Codegram we are using https://github.com/typicode/husky to manage git hooks. You can include the hooks in the package json like this

{
  "scripts": {
    "pre-commit": "bundle exec rubocop -a"
  },
  "lint-staged": {
    "*.rb": [
      "bundle exec rubocop -a"
    ]
  }
}

The lint-staged package is optional but it is very useful to run the command just for the files that have been changed.

@marcelorxaviers
Copy link
Author

Nice, I'll check Husky out. 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment