blob: 01389b3824f43fa548f1c0e79423f288b5005267 [file] [log] [blame]
Wyatt Hepleree3e02f2019-12-05 10:52:31 -08001.. _chapter-pw-presubmit:
2
3.. default-domain:: cpp
4
5.. highlight:: sh
6
7------------
8pw_presubmit
9------------
10The presubmit module provides Python tools for running presubmit checks and
11checking and fixing code format. It also includes the presubmit check script for
12the Pigweed repository, ``pigweed_presubmit.py``.
13
14Compatibility
15=============
16Python 3
17
18pw_presubmit Python package
19===========================
20
21Presubmit tools
22---------------
23A presubmit check is defined as a function or other callable. The function may
24take either no arguments or a list of the paths on which to run. Presubmit
25checks communicate failure by raising any exception.
26
27For 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
42Presubmit 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
50Presubmit checks that accept a list of paths may use the ``filter_paths``
51decorator to automatically filter the paths list for file types they care about.
52
53Members
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
65Presubmit checks
66----------------
67The pw_presubmit package includes presubmit checks that can be used with any
68project. 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
79pw_presubmit.format_code
80------------------------
81The ``format_code`` submodule formats supported source files using external code
82format tools. The file ``format_code.py`` can be invoked directly from the
83command line or from ``pw`` as ``pw format``.