How to run Pint and Pest on Git hooks with Laravel Sail

Vladimir Nikolic
April 25th, 2023

Running Pint and Pest on Git hooks with Laravel Sail
Developers use Git hooks to run scripts automatically when a specific Git event occurs, such as committing code changes. Pint and Pest are popular testing frameworks for PHP applications. Pint is a PHP code style checker, and Pest is a PHP testing framework. This tutorial shows you how to set up Git hooks for Pint and Pest while using Laravel Sail.
Laravel Sail is a lightweight command-line interface (CLI) for running Laravel applications in a Docker container. It simplifies the development environment setup process by providing preconfigured Docker images that work out of the box.
Assuming you already have Pint and Pest installed, follow these steps to set up Git hooks with Laravel Sail:
- Create a Git hook file
Navigate to your Laravel application's root directory and create a new file called
pre-commit
in the.git/hooks/
directory:
touch .git/hooks/pre-commit
- Make the Git hook file executable
Make the
pre-commit
file executable by running the following command:
chmod +x .git/hooks/pre-commit
- Open the Git hook file and add the following code
Open the
pre-commit
file in a text editor and add the following code:
#!/bin/sh
echo "Running Pint ... "
files=$(git diff --cached --name-only --diff-filter=ACM -- '*.php');
./vendor/bin/pint $files
git add $files
echo "Running Pest ..."
[ -f sail ] && bash sail pest || bash vendor/bin/sail pest
This code runs Pint and Pest before a Git commit is executed. It first runs Pint and then runs Pest using Laravel Sail if the sail
script exists; otherwise, it runs Pest using the local vendor/bin/sail
script.
- Test the Git hook
Save the changes to the
pre-commit
file and make a change to your code. Attempt to commit your changes, and you should see output similar to the following:
Running Pint ...
[OK] No syntax error found
Running Pest ...
PASS Tests: 2, Assertions: 2, Passed: 2, Skipped: 0, Incomplete: 0, Risky: 0, Error: 0, Failed: 0
If you encounter any issues, check that Pint and Pest are installed and that the paths in the pre-commit
file are correct.
In conclusion, setting up Git hooks for Pint and Pest with Laravel Sail is a straightforward process that can save you time by automatically running tests before committing code changes. By following these steps, you can ensure that your code adheres to the coding standards and that your tests pass before pushing code changes to your repository.