Wyatt Hepler | ee3e02f | 2019-12-05 10:52:31 -0800 | [diff] [blame] | 1 | .. _chapter-pw-presubmit: |
| 2 | |
| 3 | .. default-domain:: cpp |
| 4 | |
| 5 | .. highlight:: sh |
| 6 | |
| 7 | ------------ |
| 8 | pw_presubmit |
| 9 | ------------ |
| 10 | The presubmit module provides Python tools for running presubmit checks and |
| 11 | checking and fixing code format. It also includes the presubmit check script for |
| 12 | the Pigweed repository, ``pigweed_presubmit.py``. |
| 13 | |
Keir Mierle | 086ef1c | 2020-03-19 02:03:51 -0700 | [diff] [blame^] | 14 | Presubmit checks are essential tools, but they take work to set up, and |
| 15 | projects don’t always get around to it. The ``pw_presubmit`` module provides |
| 16 | tools for setting up high quality presubmit checks for any project. We use this |
| 17 | framework to run Pigweed’s presubmit on our workstations and in our automated |
| 18 | building tools. |
| 19 | |
| 20 | The ``pw_presubmit`` module also includes ``pw format``, a tool that provides a |
| 21 | unified interface for automatically formatting code in a variety of languages. |
| 22 | With ``pw format``, you can format C, C++, Python, GN, and Go code according to |
| 23 | configurations defined by your project. ``pw format`` leverages existing tools |
| 24 | like ``clang-format``, and it’s simple to add support for new languages. |
| 25 | |
| 26 | .. image:: ../docs/images/pw_presubmit_demo.gif |
| 27 | :alt: ``pw format`` demo |
| 28 | :align: left |
| 29 | |
Wyatt Hepler | ee3e02f | 2019-12-05 10:52:31 -0800 | [diff] [blame] | 30 | Compatibility |
| 31 | ============= |
| 32 | Python 3 |
| 33 | |
| 34 | pw_presubmit Python package |
| 35 | =========================== |
| 36 | |
| 37 | Presubmit tools |
| 38 | --------------- |
| 39 | A presubmit check is defined as a function or other callable. The function may |
| 40 | take either no arguments or a list of the paths on which to run. Presubmit |
| 41 | checks communicate failure by raising any exception. |
| 42 | |
| 43 | For example, either of these functions may be used as presubmit checks: |
| 44 | |
| 45 | .. code-block:: python |
| 46 | |
| 47 | @pw_presubmit.filter_paths(endswith='.py') |
| 48 | def file_contains_ni(paths): |
| 49 | for path in paths: |
| 50 | with open(path) as file: |
| 51 | contents = file.read() |
| 52 | if 'ni' not in contents and 'nee' not in contents: |
| 53 | raise PresumitFailure('Files must say "ni"!', path=path) |
| 54 | |
| 55 | def run_the_build(): |
| 56 | subprocess.run(['make', 'release'], check=True) |
| 57 | |
| 58 | Presubmit checks are provided to the ``parse_args_and_run_presubmit`` or |
| 59 | ``run_presubmit`` function as a list. For example, |
| 60 | |
| 61 | .. code-block:: python |
| 62 | |
| 63 | PRESUBMIT_CHECKS = [file_contains_ni, run_the_build] |
| 64 | sys.exit(0 if parse_args_and_run_presubmit(PRESUBMIT_CHECKS) else 1) |
| 65 | |
| 66 | Presubmit checks that accept a list of paths may use the ``filter_paths`` |
| 67 | decorator to automatically filter the paths list for file types they care about. |
| 68 | |
| 69 | Members |
| 70 | ^^^^^^^ |
| 71 | .. autofunction:: pw_presubmit.run_presubmit |
| 72 | |
| 73 | .. autofunction:: pw_presubmit.parse_args_and_run_presubmit |
| 74 | |
| 75 | .. autodecorator:: pw_presubmit.filter_paths |
| 76 | |
| 77 | .. autofunction:: pw_presubmit.call |
| 78 | |
| 79 | .. autoexception:: pw_presubmit.PresubmitFailure |
| 80 | |
| 81 | Presubmit checks |
| 82 | ---------------- |
Armando Montanez | 0054a9b | 2020-03-13 13:06:24 -0700 | [diff] [blame] | 83 | The ``pw_presubmit`` package includes presubmit checks that can be used with any |
Wyatt Hepler | ee3e02f | 2019-12-05 10:52:31 -0800 | [diff] [blame] | 84 | project. These checks include: |
| 85 | |
| 86 | * Check code format of several languages including C, C++, and Python |
| 87 | * Initialize a Python environment |
| 88 | * Run all Python tests |
| 89 | * Run pylint |
| 90 | * Ensure source files are included in the GN and Bazel builds |
| 91 | * Build and run all tests with GN |
| 92 | * Build and run all tests with Bazel |
| 93 | * Ensure all header files contain ``#pragma once`` |
| 94 | |
| 95 | pw_presubmit.format_code |
| 96 | ------------------------ |
| 97 | The ``format_code`` submodule formats supported source files using external code |
| 98 | format tools. The file ``format_code.py`` can be invoked directly from the |
| 99 | command line or from ``pw`` as ``pw format``. |