Use Git Hooks to run Rubocop, Brakeman and Rspec

Tram Ho

In this article, I will show you how to install Git Hooks for your Rails application to ensure the quality of everyone’s code. If people have been doing weird Rails will no longer make do with concepts like Rubocop , Breakman and Rspec anymore. Before push code on Git, people used to run commands like rubocop or rspec . It seems a bit of “typing” when you have to run 3 rubocop , rspec commands and then git push ... I will guide you to run the 3 commands in turn only in 1 “keystroke”.

First create a folder called scripts in your project.

After that, we will create 3 more bash files into the scripts directory for each command we want to run. In this case three files used to run bash Rubocop , Breakman and Rspec .

1. scripts / run-rubocop.bash

2. scripts / run-brakeman.bash

3. scripts / run-tests.bash

We can also create other bash files to run the commands we want, depending on the needs.

Next, we will create two more bash files, pre-commit and pre-push

1. scripts / pre-commit.bash

2. scripts / pre-push.bash

Next, we will create a bash file to link the newly created bash file.

scripts / install-hooks.bash

All necessary files have been created, before running scripts/install-hooks.bash we need to grant access to it by running the command:

Finally, we run the install-hooks.bash file

Terminal screen will display

Thus, now every time you execute a commit code, the rubocop statement will automatically run and when you execute the push code, the two breakeman and rspec will also be automatically run. This will save some time between your commit and your push code. Good luck.

Share the news now

Source : Viblo