11Developer's overview
22====================
33
4+ Tl;dr
5+ -----
6+
7+ Just run :code: `tox `. If everything passes, you're OK. If not, read on.
8+
49Code style
510----------
611
@@ -15,12 +20,20 @@ best-practice conventions (default settings unless otherwise noted):
1520 - bandit
1621 - dodgy
1722 - mccabe (max-complexity 5)
18- - pycodestyle (E203 disabled because it conflicts with black )
19- - pydocstyle
23+ - pycodestyle (E203 disabled [ #E203 ]_ )
24+ - pydocstyle (numpy style)
2025 - pyflakes
21- - pylint (W1203 disabled because f-strings are nice )
26+ - pylint (W1203 disabled [ #W1203 ]_ )
2227 - mypy (strict)
2328
29+ .. [#E203 ] pycodestyle E203 conflicts with black, see
30+ https://github.com/PyCQA/pycodestyle/issues/373.
31+ .. [#W1203 ] See https://github.com/pylint-dev/pylint/issues/2354. Tl;dr:
32+ F-strings in log messages negatively impact performance compared
33+ to %s-style formatting. However, the impact is very small. Because
34+ f-strings are more readable, mcbootflash accepts the performance
35+ hit.
36+
2437 In addition to these automatically enforced rules, the following conventions are
2538used:
2639
@@ -46,10 +59,10 @@ used:
4659
4760 from os import getcwd
4861 from os import path
49-
62+
5063 FUM = 0 # (8)
51-
52-
64+
65+
5366 class Foo : # (5)
5467 """ This is a doc string.""" # (4)
5568
6881 for bar in bars:
6982 if len (bar) == 1 ): # (3)
7083 fies.append(ham + 2 * bar)
71- # (1)
84+ # (1)
7285 # This is a comment. # (4)
7386 if fum in None :
7487 fum = 5
@@ -92,45 +105,20 @@ program's behavior changes, i.e. the data it writes to the serial bus in
92105response to any given command. If a test fails when you didn't intend to
93106change the program's behavior, you broke something.
94107
95- Tests should be representative of real-world scenarios. For this reason, tests
96- should mimic user actions to the greatest extent possible. Tests should
97- interface with the program like a user would interface with the program. Every
98- test should model an action the user might take, or a situation the user might
99- face. If you find it difficult to write tests that exercise the entire codebase
100- under these constraints, consider if the hard-to-hit lines are really necessary
101- for the program to do its job.
102-
103108Recording serial traffic
104109^^^^^^^^^^^^^^^^^^^^^^^^
105110
106111In order to test mcbootflash's behavior in isolation, i.e. without a connected
107112device running an appropriate bootloader, serial traffic is first recorded with
108113a real device. Then, during tests the recorded traffic is replayed using a
109- mocked serial port. A test passes if the bits written to the mocked port match
110- the recorded traffic exactly.
111-
112- Since the ability to record serial traffic is not part of mcbootflash's normal
113- functionality, it is maintained as a small patch in the `record ` branch. To
114- record serial traffic, do the following:
115-
116- 1. Rebase the `record ` branch on top of your working branch.
117- 2. Modify your test by running `.dump_log(<path_to_log.json>) ` on the
118- BootloaderConnection instance before finishing the test.
119- 3. Run the test.
120- 4. The recorded traffic can now be found in the `<path_to_log.json> ` file.
121- Place this file in the directory test/testcases/<your_test>/, where
122- <your_test> is either a new test if you're adding new functionality, or
123- an existing test if you are changing existing functionality.
124-
125- A note on test coverage
126- ^^^^^^^^^^^^^^^^^^^^^^^
127-
128- "If a line of code is important enough to write, it is important enough to test."
129-
130- While this rule is not applicable to every piece of software out there,
131- mcbootflash is small and simple enough that the rule can be applied. This
132- project therefore aims for 100% test coverage.
133-
134- 100% test coverage is not a goal unto itself. Tests should never be written for
135- the sole purpose of increasing coverage. 100% test coverage should happen as a
136- side-effect of good tests.
114+ mocked serial port. A test passes if the bits written to and read from the
115+ mocked port match the recorded traffic exactly.
116+
117+ A pytest plugin, pytest-reserial _, is used to record and replay serial traffic.
118+ To create a new test, or update an old one when intentionally changing
119+ mcbootflash's behavior, run :code: `pytest --record -k <test_name> ` with a device
120+ running the bootloader connected at /dev/ttyUSB0 with a baudrate of 460800
121+ (this port name and baudrate are currently hardcoded during testing; pull
122+ requests welcome if you need them to be dynamic).
123+
124+ .. _pytest-reserial : https://github.com/bessman/pytest-reserial
0 commit comments