Never break remote master (again)

Run extensive testing from clean environment before pushing code to remote repo.

I have previously described using pre-commit nodejs module to keep me from accidentally committing bad changes to the local git repository.

This has been a very useful feature, that once setup, freed my mind to concentrate on more important things:

  • good commit message
  • how this commit affects other features
  • deployment schedule
  • user information

Notice that previously, remembering to run the tests / sanity checks before committing pushed one of these things out of my mental slots.

My second observation is that the pre-commit checks must be fast. I usually commit multiple small changes, and if I had to sit through 60 seconds of unit testing every time, I would be sad. So, I usually setup just basic checks: jshint and a few small unit tests.

Remote repository

While the pre-commit check is fast, I would really like to prevent from pushing the local commits to the remote (central) git server, and this check might take longer, since this is an infrequent event.

I took the pre-commit module and forked it into my own repo. This pre-commit module on its installation places both pre-commit and pre-push hooks into the local repository. You can configure a list of commands separately to run for each pre- operation. For example:

// package.json
"scripts": {
    "test": "node-qunit *.js"
},
"pre-commit": [
    "grunt jshint"
],
"pre-push": [
    "grunt clean",
    "grunt build",
    "grunt test"
],
"devDependencies": {
    "pre-commit": "https://github.com/bahmutov/pre-commit#0.0.7"
}

In this example, the pre-commit command runs style and basic syntax checks using grunt jshint task, while pre-push includes cleaning the folder, running the build and unit tests.

Of course, no local testing is fullprooth. There might be situations, where the environment differences between your machine and the remote ones guarantees a failed build. Still, I feel you should sleep a little sounder at night knowing you will NEVER forget to run the unit tests from clean build before sending your code out into the world.