liveBook · Manning

Precommit hook

As explained in Build Python Web Apps with Streamlit

A pre-commit hook is an automated action triggered before a version-control commit is created.

A pre-commit hook runs an automated check before Git creates a commit. A common use is running tests so failed tests prevent the commit and problems are caught early. section 11.7

Set up a basic test-running hook in the repository:

  • Create a file named pre-commit inside .git/hooks/. section 11.7
  • Add a shell command that runs pytest and exits when tests fail. section 11.7
  • Make the hook executable with chmod +x .git/hooks/pre-commit. section 11.7

A test-gating hook

A hook containing `pytest -v || exit 1` runs the test suite before each commit. Passing tests allow the commit; a failure stops it. Run only the tests needed during development when slower integration or end-to-end tests use external services, delays, or costs. section 11.7

#!/bin/bash
pytest -v || exit 1

The listing shows a minimal shell hook that runs pytest verbosely and prevents continuation after failure. section 11.7

How Build Python Web Apps with Streamlit covers it

How deep it goes
Shows file creation, test commands, executable permissions, and failure prevention, but not advanced hook configuration.
Examples
Uses a .git/hooks/pre-commit script containing pytest -v || exit 1 and an executable-permission command.
What it assumes
Assumes a Git repository and tests that can run with pytest.
What you can do after
Operate/configure a Git hook that runs tests automatically and blocks commits when tests fail.
How it connects
Connects Git hooks with pytest, test filtering, pre-push hooks, continuous integration, and GitHub Actions.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Python Web Apps with Streamlit. The section links point to the parts of the book it draws on.