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 | |
| 14 | Compatibility |
| 15 | ============= |
| 16 | Python 3 |
| 17 | |
| 18 | pw_presubmit Python package |
| 19 | =========================== |
| 20 | |
| 21 | Presubmit tools |
| 22 | --------------- |
| 23 | A presubmit check is defined as a function or other callable. The function may |
| 24 | take either no arguments or a list of the paths on which to run. Presubmit |
| 25 | checks communicate failure by raising any exception. |
| 26 | |
| 27 | For example, either of these functions may be used as presubmit checks: |
| 28 | |
| 29 | .. code-block:: python |
| 30 | |
| 31 | @pw_presubmit.filter_paths(endswith='.py') |
| 32 | def file_contains_ni(paths): |
| 33 | for path in paths: |
| 34 | with open(path) as file: |
| 35 | contents = file.read() |
| 36 | if 'ni' not in contents and 'nee' not in contents: |
| 37 | raise PresumitFailure('Files must say "ni"!', path=path) |
| 38 | |
| 39 | def run_the_build(): |
| 40 | subprocess.run(['make', 'release'], check=True) |
| 41 | |
| 42 | Presubmit checks are provided to the ``parse_args_and_run_presubmit`` or |
| 43 | ``run_presubmit`` function as a list. For example, |
| 44 | |
| 45 | .. code-block:: python |
| 46 | |
| 47 | PRESUBMIT_CHECKS = [file_contains_ni, run_the_build] |
| 48 | sys.exit(0 if parse_args_and_run_presubmit(PRESUBMIT_CHECKS) else 1) |
| 49 | |
| 50 | Presubmit checks that accept a list of paths may use the ``filter_paths`` |
| 51 | decorator to automatically filter the paths list for file types they care about. |
| 52 | |
| 53 | Members |
| 54 | ^^^^^^^ |
| 55 | .. autofunction:: pw_presubmit.run_presubmit |
| 56 | |
| 57 | .. autofunction:: pw_presubmit.parse_args_and_run_presubmit |
| 58 | |
| 59 | .. autodecorator:: pw_presubmit.filter_paths |
| 60 | |
| 61 | .. autofunction:: pw_presubmit.call |
| 62 | |
| 63 | .. autoexception:: pw_presubmit.PresubmitFailure |
| 64 | |
| 65 | Presubmit checks |
| 66 | ---------------- |
| 67 | The pw_presubmit package includes presubmit checks that can be used with any |
| 68 | project. These checks include: |
| 69 | |
| 70 | * Check code format of several languages including C, C++, and Python |
| 71 | * Initialize a Python environment |
| 72 | * Run all Python tests |
| 73 | * Run pylint |
| 74 | * Ensure source files are included in the GN and Bazel builds |
| 75 | * Build and run all tests with GN |
| 76 | * Build and run all tests with Bazel |
| 77 | * Ensure all header files contain ``#pragma once`` |
| 78 | |
| 79 | pw_presubmit.format_code |
| 80 | ------------------------ |
| 81 | The ``format_code`` submodule formats supported source files using external code |
| 82 | format tools. The file ``format_code.py`` can be invoked directly from the |
| 83 | command line or from ``pw`` as ``pw format``. |