Learn more about Velocity Partners offshore software development company
Software development processes have undergone significant changes over the last decade, with new methodologies becoming increasingly more prevalent. One of the most well-known is Agile, which is a complement to technical practices like test-driven development (TDD) and behavior-driven development (BDD). Combined, they help establish good automation processes and support continuous integration (CI).
As the first step in any CI process, a developer combines his or her code with that of other developers by pushing code to a shared repository. In this post, we’ll offer up a tip to help developers avoid introducing bugs or defects into the main repository by using hooks available in the version control system. We’ll use Git as an example.
Initial step: Good TDD/BDD scripts
As testing methodologies often used by used Agile teams, TDD and BDD allow developers to test code across various levels. In this phase, developers write acceptance and unit tests to validate new code. These tests are usually run from the command line, which needs to return the status, where 0 is satisfactory and other numbers represent an unsuccessful execution. To validate the test, the developer can run echo $?
immediately after the test is run in the bash terminal – this command shows the test script result.
Second step: GIT pre-push hook
Git has scripts hooks that help developers automate certain requirements in specific phases of the version control process. The pre-push
hook runs during Git push, serving as the tangent point between the developer and the unified repository, and this is where we can prevent some bugs from being integrated into the application.
The hooks are all stored in the hooks subdirectory of the Git directory – in this case, the script can be found at PROJECT_GIT_ROOT\.git\hooks\pre-push
Each time a developer starts a Git repository, an example script called pre-push.sample
is created as a template.
For an example, a developer could use the script as follows to help avoid inserting bugs into a Ruby project:
#!/bin/sh
# Go to the project root path
cd $(git rev-parse --show-toplevel)
# Here is where the test script must be executed
bundle exec rake test
# Return the final status of the test script
exit $?
Final steps
- Check the script with
git push
command. This can be done easily by mirroring 0 and 1 from the pre-push script. - The developer can then adjust the script accordingly by running build tools and tests.