blob: be568c89df350e7c8db0c76dba2b98be18572e1a [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
Keir Mierle086ef1c2020-03-19 02:03:51 -070014Presubmit checks are essential tools, but they take work to set up, and
15projects dont always get around to it. The ``pw_presubmit`` module provides
16tools for setting up high quality presubmit checks for any project. We use this
17framework to run Pigweeds presubmit on our workstations and in our automated
18building tools.
19
20The ``pw_presubmit`` module also includes ``pw format``, a tool that provides a
21unified interface for automatically formatting code in a variety of languages.
22With ``pw format``, you can format C, C++, Python, GN, and Go code according to
23configurations defined by your project. ``pw format`` leverages existing tools
24like ``clang-format``, and its 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 Hepleree3e02f2019-12-05 10:52:31 -080030Compatibility
31=============
32Python 3
33
34pw_presubmit Python package
35===========================
36
37Presubmit tools
38---------------
39A presubmit check is defined as a function or other callable. The function may
40take either no arguments or a list of the paths on which to run. Presubmit
41checks communicate failure by raising any exception.
42
43For 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
58Presubmit 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
66Presubmit checks that accept a list of paths may use the ``filter_paths``
67decorator to automatically filter the paths list for file types they care about.
68
69Members
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
81Presubmit checks
82----------------
Armando Montanez0054a9b2020-03-13 13:06:24 -070083The ``pw_presubmit`` package includes presubmit checks that can be used with any
Wyatt Hepleree3e02f2019-12-05 10:52:31 -080084project. 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
95pw_presubmit.format_code
96------------------------
97The ``format_code`` submodule formats supported source files using external code
98format tools. The file ``format_code.py`` can be invoked directly from the
99command line or from ``pw`` as ``pw format``.