DEV Community

David Carr
David Carr

Posted on • Originally published at dcblog.dev on

Run tests on commit

Go into git hooks folder and copy pre-commit.sample to pre-commit


cp .git/hooks/pre-commit.sample .git/hooks/pre-commit
Enter fullscreen mode Exit fullscreen mode

then edit it with vim:


vi .git/hooks/pre-commit
Enter fullscreen mode Exit fullscreen mode

clear the contents by pressing dG to clear the contents.

Next to into insert mode by pressing i

now paste in:


#!/usr/bin/env php
<?php
echo "Running tests.. ";
exec('vendor/bin/phpunit', $output, $returnCode);
if ($returnCode !== 0) {
    // Show full output
    echo PHP_EOL . implode($output, PHP_EOL) . PHP_EOL;
    echo "Aborting commit.." . PHP_EOL;
    exit(1);
}
// Show summary (last line)
echo array_pop($output) . PHP_EOL;
exit(0);
Enter fullscreen mode Exit fullscreen mode

Now save and exit by pressing ESC then :wq to write and quit

now every commit will run the tests suite.

So you only want to do this where you have tests passing or you can't move forward until you fix the tests.

Top comments (0)