1.

What are the steps to be undertaken to configure git repository so that it runs the code sanity checking tooks before any commits? How do you prevent it from happening again if the sanity testing fails?

Answer»

Sanity testing, also known as smoke testing, is a process used to determine if it’s reasonable to proceed to test.
Git REPOSITORY provides a hook called pre-commit which gets triggered RIGHT before a commit happens. A simple script by making use of this hook can be written to achieve the smoke test.

The script can be used to run other tools like linters and PERFORM sanity checks on the changes that would be committed into the repository.

The following snippet is an example of one such script:

#!/bin/shfiles=$(git diff –cached –name-only –diff-filter=ACM | grep ‘.py$’)if [ -z files ]; thenexit 0fiunfmtd=$(pyfmt -l $files)if [ -z unfmtd ]; thenexit 0fiecho “Some .py files are not properly fmt’d”EXIT 1

The above script checks if any .py files which are to be committed are properly formatted by making use of the python FORMATTING tool pyfmt. If the files are not properly formatted, then the script prevents the changes to be committed to the repository by exiting with status 1.
 



Discussion

No Comment Found