Skip to content

Commit a772fc7

Browse files
committed
NO-JIRA: chore(python): write a doctest test and enable doctest tests execution in pytest
1 parent 0f37c5b commit a772fc7

File tree

3 files changed

+55
-6
lines changed

3 files changed

+55
-6
lines changed

ntb/strings.py

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,51 @@
1313
from pyfakefs.fake_filesystem import FakeFilesystem
1414

1515

16-
def indent(template: templatelib.Template) -> str:
17-
"""Template-rendering function to render a t-string while preserving indentation,
18-
processing all the accustomed f-string formatters.
19-
References: https://www.pythonmorsels.com/t-strings-in-python/
20-
https://github.com/t-strings/pep750-examples/blob/main/pep/fstring.py"""
16+
def process_template_with_indents(template: templatelib.Template) -> str:
17+
"""Process a template string while preserving indentation.
18+
19+
The standard f-string formatters in the template are processed.
20+
21+
This is useful for embedding multi-line code blocks inside other code blocks,
22+
for example, in Dockerfiles.
23+
24+
>>> from string import templatelib as t
25+
>>> import textwrap
26+
>>>
27+
>>> cleanup_fragment = '''
28+
... dnf clean all
29+
... rm -rf /var/lib/dnf
30+
... '''.strip()
31+
>>>
32+
>>> # Instead of this, which is hard to read:
33+
>>> script1 = f'''RUN /bin/bash <<'EOF'
34+
... if [ -z "$RUN_CLEANUP" ]; then
35+
... {textwrap.indent(cleanup_fragment, prefix=" ")}
36+
... EOF
37+
... '''
38+
>>>
39+
>>> # You can do this, which is more straightforward to read:
40+
>>> script2 = process_template_with_indents(t'''RUN /bin/bash <<'EOF'
41+
... if [ -z "$RUN_CLEANUP" ]; then
42+
... {cleanup_fragment}
43+
... EOF
44+
... ''')
45+
>>>
46+
>>> print(script2)
47+
RUN /bin/bash <<'EOF'
48+
if [ -z "$RUN_CLEANUP" ]; then
49+
dnf clean all
50+
rm -rf /var/lib/dnf
51+
EOF
52+
<BLANKLINE>
53+
>>> # The results are the same
54+
>>> script1 == script2
55+
True
56+
57+
References:
58+
- https://www.pythonmorsels.com/t-strings-in-python/
59+
- https://github.com/t-strings/pep750-examples/blob/main/pep/fstring.py
60+
"""
2161
parts = []
2262
indent = 0
2363
for item in template:

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ dev = [
2222
"pyfakefs",
2323
"pydantic",
2424
"requests",
25+
2526
"pyyaml",
2627

2728
"testcontainers",

pytest.ini

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
# https://docs.pytest.org/en/7.1.x/reference/reference.html#configuration-options
22

33
[pytest]
4-
addopts = --strict-markers --capture=no --tb=short
4+
# https://docs.pytest.org/en/stable/how-to/doctest.html
5+
# https://docs.pytest.org/en/stable/example/pythoncollection.html#ignore-paths-during-test-collection
6+
addopts =
7+
--strict-markers --capture=no --tb=short
8+
--ignore=ci --ignore=codeserver --ignore=jupyter --ignore=rstudio
9+
--doctest-modules
10+
11+
# https://docs.pytest.org/en/stable/example/pythoncollection.html#changing-directory-recursion
12+
norecursedirs = [".git"]
513

614
# pytest-subtest plugin is built-in since pytest 9.0: https://docs.pytest.org/en/stable/how-to/subtests.html
715
required_plugins =

0 commit comments

Comments
 (0)