1Contributions Guide
2===================
3
4We welcome contributions to the ``esptool.py`` project!
5
6How to Contribute
7-----------------
8
9Contributions to ``esptool.py`` - fixing bugs, adding features, adding documentation - are welcome. We accept contributions via `Github Pull Requests <https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/about-pull-requests>`_.
10
11.. _development-setup:
12
13Development Setup
14-----------------
15
16Development mode allows you to run the latest development version from the `esptool.py repository on GitHub <https://github.com/espressif/esptool>`_.
17
18.. code-block:: sh
19
20   $ git clone https://github.com/espressif/esptool.git
21   $ cd esptool
22   $ pip install --user -e .
23
24This will install ``esptool.py``’s dependencies and create some executable script wrappers in the user’s ``bin`` directory. The wrappers will run the scripts found in the git working directory directly, so any time the working directory contents change it will pick up the new versions.
25
26It’s also possible to run the scripts directly from the working directory with this Development Mode installation.
27
28To also install additional tools needed for actually developing and testing ``esptool.py``, run this command to install a development copy of ``esptool.py`` *plus* packages useful for development:
29
30::
31
32   $ pip install --user -e ".[dev]"
33
34(This command uses the “extras” feature of setuptools.)
35
36Reporting Issues
37----------------
38
39Please report bugs in ``esptool.py`` if you find them. However, before reporting a bug please check through the following:
40
41*  `Troubleshooting Guide <https://docs.espressif.com/projects/esptool/en/latest/troubleshooting.html>`_ - common problems and known issues.
42
43*  `Existing Open Issues <https://github.com/espressif/esptool/issues>`_ - someone might have already encountered this.
44
45If you don’t find anything, please `open a new issue <https://github.com/espressif/esptool/issues/new/choose>`_.
46
47Sending Feature Requests
48------------------------
49
50Feel free to post feature requests. It’s helpful if you can explain exactly why the feature would be useful.
51
52There are usually some outstanding feature requests in the `existing issues list <https://github.com/espressif/esptool/issues?q=is%3Aopen+is%3Aissue+label%3Aenhancement>`_, feel free to add comments to them.
53
54Before Contributing
55-------------------
56
57Before sending us a Pull Request, please consider this list of points:
58
59* Have you tried running ``esptool.py`` test suite locally?
60
61* Is the code adequately commented for people to understand how it is structured?
62
63* Is there documentation or examples that go with code contributions?
64
65* Are comments and documentation written in clear English, with no spelling or grammar errors?
66
67* If the contribution contains multiple commits, are they grouped together into logical changes (one major change per pull request)? Are any commits with names like "fixed typo" `squashed into previous commits <https://eli.thegreenplace.net/2014/02/19/squashing-github-pull-requests-into-a-single-commit/>`_?
68
69* If you're unsure about any of these points, please open the Pull Request anyhow and then ask us for feedback.
70
71Code Style & Static Analysis
72^^^^^^^^^^^^^^^^^^^^^^^^^^^^
73
74Please follow these coding standards when writing code for ``esptool.py``:
75
76Pre-commit checks
77"""""""""""""""""
78
79`pre-commit <https://pre-commit.com/>`_ is a framework for managing pre-commit hooks. These hooks help to identify simple issues before committing code for review.
80
81To use the tool, first install ``pre-commit``. Then enable the ``pre-commit`` and ``commit-msg`` git hooks:
82
83::
84
85   $ python -m pip install pre-commit
86   $ pre-commit install -t pre-commit -t commit-msg
87
88On the first commit ``pre-commit`` will install the hooks, subsequent checks will be significantly faster. If an error is found an appropriate error message will be displayed. Review the changes and re-stage for commit if you are happy with them.
89
90Conventional Commits
91""""""""""""""""""""
92
93``esptool.py`` complies with the `Conventional Commits standard <https://www.conventionalcommits.org/en/v1.0.0/#specification>`_. Every commit message is checked with `Conventional Precommit Linter <https://github.com/espressif/conventional-precommit-linter>`_, ensuring it adheres to the standard.
94
95
96Ruff
97""""
98
99``esptool.py`` is `PEP8 <https://peps.python.org/pep-0008/>`_ compliant and enforces this style guide. For compliance checking, we use `ruff <https://docs.astral.sh/ruff/>`_.
100``Ruff`` also auto-format files in the same style as previously used ``black``.
101
102
103``Ruff`` and ``Conventional Precommit Linter`` tools will be automatically run by ``pre-commit`` if that is configured. To check your code manually before submitting, run ``python -m ruff`` (this tool is installed as part of the development requirements shown at the beginning of this document).
104
105When you submit a Pull Request, the GitHub Actions automated build system will run automated checks using these tools.
106
107Shinx-lint
108""""""""""
109
110The documentation is checked for stylistic and formal issues by ``sphinx-lint``.
111
112
113Codespell check
114"""""""""""""""
115
116This repository utilizes an automatic `spell checker <https://github.com/codespell-project/codespell>`_ integrated into the pre-commit process. If any spelling issues are detected, the recommended corrections will be applied automatically to the file, ready for commit.
117In the event of false positives, you can adjust the configuration in the `.codespell.rc`. To exclude files from the spell check, utilize the `skip` keyword followed by comma-separated paths to the files (wildcards are supported). Additionally, to exclude specific words from the spell check, employ the `ignore-words-list` keyword followed by comma-separated words to be skipped.
118
119
120Automated Integration Tests
121^^^^^^^^^^^^^^^^^^^^^^^^^^^
122
123The test directory contains a `pytest <https://docs.pytest.org/>`_ integration suite with some integration tests for ``esptool.py``, ``espefuse.py``, and ``espsecure.py``.
124
125It is necessary to have ``esptool.py`` installed (see `Development Setup`_) in your environment in order to run these tests.
126
127The following tests run automatically by GitHub Actions for each Pull Request. You can run them locally to check for regressions in the respective functionality:
128
129*  ``test_imagegen.py`` tests the ``elf2image`` command
130*  ``test_image_info.py`` tests the ``image_info`` command
131*  ``test_mergebin.py`` tests the ``merge_bin`` command
132*  ``test_modules.py`` tests the modules used by ``esptool.py`` for regressions
133*  ``test_espsecure.py`` tests ``espsecure.py`` functionality
134*  ``test_espsecure_hsm.py`` tests support of external HSM signing in ``espsecure.py``. These tests require additional prerequisites, see ``SoftHSM2 setup`` in the `tests workflow definition <https://github.com/espressif/esptool/blob/master/.github/workflows/test_esptool.yml>`_ for more information.
135
136The following tests are not run automatically by GitHub Actions, because they need real connected hardware. Therefore, they need to be run locally in a command line:
137
138*  ``test_esptool.py`` contains integration tests for ``esptool.py`` and needs to be run against real Espressif hardware with the following format:
139
140   ``pytest test_esptool.py --port <serial port> --chip <name of chip> --baud <baud rate>``
141
142   For example, to run all tests on an ESP32 board connected to /dev/ttyUSB0, at 230400bps:
143
144   ``pytest test_esptool.py --port /dev/ttyUSB0 --chip esp32 --baud 230400``
145
146   Or to run the TestFlashing suite only (using the pytest ``-k`` option to select tests based on their name) on an ESP8266 board connected to /dev/ttyUSB2, at 460800bps:
147
148   ``pytest test_esptool.py --port /dev/ttyUSB2 --chip esp8266 --baud 460800 -k TestFlashing``
149
150   .. note::
151
152      Some tests might fail at higher baud rates on some hardware.
153
154*  ``test_esptool_sdm.py`` contains integration tests for ``esptool.py`` with chips in secure download mode. It needs to be run against real Espressif hardware (with active SDM). The command line format is the same as for ``test_esptool.py``.
155
156The following tests are not run automatically by GitHub Actions, but can be run locally in a command line:
157
158*  ``test_espefuse.py`` tests ``espefuse.py`` functionality. To run it:
159
160   ``pytest test_espefuse.py --chip <name of chip>``
161
162   These test use the ``--virt`` virtual mode of ``espefuse.py`` to safely test the functionality without a connection to a chip and without the possibility of affecting the actual eFuses in a real hardware.
163
164   .. warning::
165
166      Do not attempt to run these tests on real hardware! You risk damaging or destroying the ESP chip!
167
168The whole test suite (without the tests needing an actual hardware or installation of additional prerequisites) can be easily run with the following command in the esptool root folder: ``pytest -m host_test``
169
170
171Pull Request Process
172--------------------
173
174.. note::
175
176   If you are developing the stub flasher and plan to send a pull request, please use the latest toolchains available.
177
178After you open the Pull Request, there will probably be some discussion in the comments field of the request itself.
179
180Once the Pull Request is ready to merge, it will first be merged into our internal git system for in-house automated testing.
181
182If this process passes, it will be merged onto the public github repository, hooray!
183