pytest-codeblock 0.1.5__tar.gz → 0.1.6__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,293 @@
1
+ Metadata-Version: 2.4
2
+ Name: pytest-codeblock
3
+ Version: 0.1.6
4
+ Summary: Pytest plugin to collect and test code blocks in reStructuredText and Markdown files.
5
+ Author-email: Artur Barseghyan <artur.barseghyan@gmail.com>
6
+ Maintainer-email: Artur Barseghyan <artur.barseghyan@gmail.com>
7
+ License-Expression: MIT
8
+ Project-URL: Homepage, https://github.com/barseghyanartur/pytest-codeblock/
9
+ Project-URL: Repository, https://github.com/barseghyanartur/pytest-codeblock/
10
+ Project-URL: Issues, https://github.com/barseghyanartur/pytest-codeblock/issues
11
+ Project-URL: Documentation, https://pytest-codeblock.readthedocs.io/
12
+ Project-URL: Changelog, https://pytest-codeblock.readthedocs.io/en/latest/changelog.html
13
+ Keywords: pytest,plugin,documentation,code blocks,markdown,rst
14
+ Classifier: Framework :: Pytest
15
+ Classifier: Development Status :: 4 - Beta
16
+ Classifier: Intended Audience :: Developers
17
+ Classifier: Operating System :: OS Independent
18
+ Classifier: Programming Language :: Python :: 3.9
19
+ Classifier: Programming Language :: Python :: 3.10
20
+ Classifier: Programming Language :: Python :: 3.11
21
+ Classifier: Programming Language :: Python :: 3.12
22
+ Classifier: Programming Language :: Python :: 3.13
23
+ Classifier: Programming Language :: Python
24
+ Classifier: Topic :: Software Development :: Testing
25
+ Classifier: Topic :: Software Development
26
+ Requires-Python: >=3.9
27
+ Description-Content-Type: text/x-rst
28
+ License-File: LICENSE
29
+ Requires-Dist: pytest
30
+ Provides-Extra: all
31
+ Requires-Dist: pytest-codeblock[build,dev,docs,test]; extra == "all"
32
+ Provides-Extra: dev
33
+ Requires-Dist: detect-secrets; extra == "dev"
34
+ Requires-Dist: doc8; extra == "dev"
35
+ Requires-Dist: ipython; extra == "dev"
36
+ Requires-Dist: mypy; extra == "dev"
37
+ Requires-Dist: pydoclint; extra == "dev"
38
+ Requires-Dist: ruff; extra == "dev"
39
+ Requires-Dist: twine; extra == "dev"
40
+ Requires-Dist: uv; extra == "dev"
41
+ Provides-Extra: test
42
+ Requires-Dist: django; extra == "test"
43
+ Requires-Dist: fake.py; extra == "test"
44
+ Requires-Dist: moto[s3]; extra == "test"
45
+ Requires-Dist: openai; extra == "test"
46
+ Requires-Dist: pytest; extra == "test"
47
+ Requires-Dist: pytest-cov; extra == "test"
48
+ Requires-Dist: pytest-django; extra == "test"
49
+ Provides-Extra: docs
50
+ Requires-Dist: sphinx<6.0; extra == "docs"
51
+ Requires-Dist: sphinx-autobuild; extra == "docs"
52
+ Requires-Dist: sphinx-rtd-theme>=1.3.0; extra == "docs"
53
+ Requires-Dist: sphinx-no-pragma; extra == "docs"
54
+ Provides-Extra: build
55
+ Requires-Dist: build; extra == "build"
56
+ Requires-Dist: twine; extra == "build"
57
+ Requires-Dist: wheel; extra == "build"
58
+ Dynamic: license-file
59
+
60
+ ================
61
+ pytest-codeblock
62
+ ================
63
+
64
+ .. External references
65
+ .. _reStructuredText: https://docutils.sourceforge.io/rst.html
66
+ .. _Markdown: https://daringfireball.net/projects/markdown/
67
+ .. _pytest: https://docs.pytest.org
68
+ .. _Django: https://www.djangoproject.com
69
+ .. _pip: https://pypi.org/project/pip/
70
+ .. _uv: https://pypi.org/project/uv/
71
+ .. _fake.py: https://github.com/barseghyanartur/fake.py
72
+ .. _boto3: https://github.com/boto/boto3
73
+ .. _moto: https://github.com/getmoto/moto
74
+ .. _openai: https://github.com/openai/openai-python
75
+ .. _Ollama: https://github.com/ollama/ollama
76
+
77
+ .. Internal references
78
+
79
+ .. _pytest-codeblock: https://github.com/barseghyanartur/pytest-codeblock/
80
+ .. _Read the Docs: http://pytest-codeblock.readthedocs.io/
81
+ .. _Examples: https://github.com/barseghyanartur/pytest-codeblock/tree/main/examples
82
+ .. _Contributor guidelines: https://pytest-codeblock.readthedocs.io/en/latest/contributor_guidelines.html
83
+ .. _reStructuredText docs: https://pytest-codeblock.readthedocs.io/en/latest/restructured_text.html
84
+ .. _Markdown docs: https://pytest-codeblock.readthedocs.io/en/latest/markdown.html
85
+ .. _llms.txt: https://barseghyanartur.github.io/pytest-codeblock/llms.txt
86
+
87
+ Test your documentation code blocks.
88
+
89
+ .. image:: https://img.shields.io/pypi/v/pytest-codeblock.svg
90
+ :target: https://pypi.python.org/pypi/pytest-codeblock
91
+ :alt: PyPI Version
92
+
93
+ .. image:: https://img.shields.io/pypi/pyversions/pytest-codeblock.svg
94
+ :target: https://pypi.python.org/pypi/pytest-codeblock/
95
+ :alt: Supported Python versions
96
+
97
+ .. image:: https://github.com/barseghyanartur/pytest-codeblock/actions/workflows/test.yml/badge.svg?branch=main
98
+ :target: https://github.com/barseghyanartur/pytest-codeblock/actions
99
+ :alt: Build Status
100
+
101
+ .. image:: https://readthedocs.org/projects/pytest-codeblock/badge/?version=latest
102
+ :target: http://pytest-codeblock.readthedocs.io
103
+ :alt: Documentation Status
104
+
105
+ .. image:: https://img.shields.io/badge/docs-llms.txt-blue
106
+ :target: https://barseghyanartur.github.io/pytest-codeblock/llms.txt
107
+ :alt: llms.txt - documentation for LLMs
108
+
109
+ .. image:: https://img.shields.io/badge/license-MIT-blue.svg
110
+ :target: https://github.com/barseghyanartur/pytest-codeblock/#License
111
+ :alt: MIT
112
+
113
+ .. image:: https://coveralls.io/repos/github/barseghyanartur/pytest-codeblock/badge.svg?branch=main&service=github
114
+ :target: https://coveralls.io/github/barseghyanartur/pytest-codeblock?branch=main
115
+ :alt: Coverage
116
+
117
+ `pytest-codeblock`_ is a `Pytest`_ plugin that discovers Python code examples
118
+ in your `reStructuredText`_ and `Markdown`_ documentation files and runs them
119
+ as part of your test suite. This ensures your docs stay correct and up-to-date.
120
+
121
+ Features
122
+ ========
123
+
124
+ - **reStructuredText and Markdown support**: Automatically find and test code
125
+ blocks in `reStructuredText`_ (``.rst``) and `Markdown`_ (``.md``) files.
126
+ The only requirement here is that your code blocks shall
127
+ have a name starting with ``test_``.
128
+ - **Grouping by name**: Split a single example across multiple code blocks;
129
+ the plugin concatenates them into one test.
130
+ - **Pytest markers support**: Add existing or custom `pytest`_ markers
131
+ to the code blocks and add hook into the tests life-cycle
132
+ using ``conftest.py``.
133
+
134
+ Prerequisites
135
+ =============
136
+ - Python 3.9+
137
+ - `pytest`_ is the only required dependency
138
+
139
+ Documentation
140
+ =============
141
+ - Documentation is available on `Read the Docs`_.
142
+ - For `reStructuredText`_, see a dedicated `reStructuredText docs`_.
143
+ - For `Markdown`_, see a dedicated `Markdown docs`_.
144
+ - Both `reStructuredText docs`_ and `Markdown docs`_ have extensive
145
+ documentation on `pytest`_ markers and corresponding ``conftest.py`` hooks.
146
+ - For guidelines on contributing check the `Contributor guidelines`_.
147
+
148
+ Installation
149
+ ============
150
+
151
+ Install with `pip`_:
152
+
153
+ .. code-block:: sh
154
+
155
+ pip install pytest-codeblock
156
+
157
+ Or install with `uv`_:
158
+
159
+ .. code-block:: sh
160
+
161
+ uv pip install pytest-codeblock
162
+
163
+ .. _configuration:
164
+
165
+ Configuration
166
+ =============
167
+ *Filename: pyproject.toml*
168
+
169
+ .. code-block:: text
170
+
171
+ [tool.pytest.ini_options]
172
+ testpaths = [
173
+ "**/*.rst",
174
+ "**/*.md",
175
+ ]
176
+
177
+ Usage
178
+ =====
179
+ reStructruredText usage
180
+ -----------------------
181
+ Any code directive, such as ``.. code-block:: python``, ``.. code:: python``,
182
+ or literal blocks with a preceding ``.. codeblock-name: <name>``, will be
183
+ collected and executed automatically, if your `pytest`_ `configuration`_
184
+ allows that.
185
+
186
+ ``code-block`` directive example
187
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
188
+
189
+ .. note:: Note that ``:name:`` value has a ``test_`` prefix.
190
+
191
+ *Filename: README.rst*
192
+
193
+ .. code-block:: rst
194
+
195
+ .. code-block:: python
196
+ :name: test_basic_example
197
+
198
+ import math
199
+
200
+ result = math.pow(3, 2)
201
+ assert result == 9
202
+
203
+
204
+ ``literalinclude`` directive example
205
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
206
+
207
+ .. note:: Note that ``:name:`` value has a ``test_`` prefix.
208
+
209
+ *Filename: README.rst*
210
+
211
+ .. code-block:: rst
212
+
213
+ .. literalinclude:: examples/python/basic_example.py
214
+ :name: test_li_basic_example
215
+
216
+ See a dedicated `reStructuredText docs`_ for more.
217
+
218
+ Markdown usage
219
+ --------------
220
+
221
+ Any fenced code block with a recognized Python language tag (e.g., ``python``,
222
+ ``py``) will be collected and executed automatically, if your `pytest`_
223
+ `configuration`_ allows that.
224
+
225
+ .. note:: Note that ``name`` value has a ``test_`` prefix.
226
+
227
+ *Filename: README.md*
228
+
229
+ .. code-block:: markdown
230
+
231
+ ```python name=test_basic_example
232
+ import math
233
+
234
+ result = math.pow(3, 2)
235
+ assert result == 9
236
+ ```
237
+
238
+ See a dedicated `Markdown docs`_ for more.
239
+
240
+ Tests
241
+ =====
242
+
243
+ Run the tests with `pytest`_:
244
+
245
+ .. code-block:: sh
246
+
247
+ pytest
248
+
249
+ Writing documentation
250
+ =====================
251
+
252
+ Keep the following hierarchy.
253
+
254
+ .. code-block:: text
255
+
256
+ =====
257
+ title
258
+ =====
259
+
260
+ header
261
+ ======
262
+
263
+ sub-header
264
+ ----------
265
+
266
+ sub-sub-header
267
+ ~~~~~~~~~~~~~~
268
+
269
+ sub-sub-sub-header
270
+ ^^^^^^^^^^^^^^^^^^
271
+
272
+ sub-sub-sub-sub-header
273
+ ++++++++++++++++++++++
274
+
275
+ sub-sub-sub-sub-sub-header
276
+ **************************
277
+
278
+ License
279
+ =======
280
+
281
+ MIT
282
+
283
+ Support
284
+ =======
285
+ For security issues contact me at the e-mail given in the `Author`_ section.
286
+
287
+ For overall issues, go
288
+ to `GitHub <https://github.com/barseghyanartur/pytest-codeblock/issues>`_.
289
+
290
+ Author
291
+ ======
292
+
293
+ Artur Barseghyan <artur.barseghyan@gmail.com>
@@ -0,0 +1,234 @@
1
+ ================
2
+ pytest-codeblock
3
+ ================
4
+
5
+ .. External references
6
+ .. _reStructuredText: https://docutils.sourceforge.io/rst.html
7
+ .. _Markdown: https://daringfireball.net/projects/markdown/
8
+ .. _pytest: https://docs.pytest.org
9
+ .. _Django: https://www.djangoproject.com
10
+ .. _pip: https://pypi.org/project/pip/
11
+ .. _uv: https://pypi.org/project/uv/
12
+ .. _fake.py: https://github.com/barseghyanartur/fake.py
13
+ .. _boto3: https://github.com/boto/boto3
14
+ .. _moto: https://github.com/getmoto/moto
15
+ .. _openai: https://github.com/openai/openai-python
16
+ .. _Ollama: https://github.com/ollama/ollama
17
+
18
+ .. Internal references
19
+
20
+ .. _pytest-codeblock: https://github.com/barseghyanartur/pytest-codeblock/
21
+ .. _Read the Docs: http://pytest-codeblock.readthedocs.io/
22
+ .. _Examples: https://github.com/barseghyanartur/pytest-codeblock/tree/main/examples
23
+ .. _Contributor guidelines: https://pytest-codeblock.readthedocs.io/en/latest/contributor_guidelines.html
24
+ .. _reStructuredText docs: https://pytest-codeblock.readthedocs.io/en/latest/restructured_text.html
25
+ .. _Markdown docs: https://pytest-codeblock.readthedocs.io/en/latest/markdown.html
26
+ .. _llms.txt: https://barseghyanartur.github.io/pytest-codeblock/llms.txt
27
+
28
+ Test your documentation code blocks.
29
+
30
+ .. image:: https://img.shields.io/pypi/v/pytest-codeblock.svg
31
+ :target: https://pypi.python.org/pypi/pytest-codeblock
32
+ :alt: PyPI Version
33
+
34
+ .. image:: https://img.shields.io/pypi/pyversions/pytest-codeblock.svg
35
+ :target: https://pypi.python.org/pypi/pytest-codeblock/
36
+ :alt: Supported Python versions
37
+
38
+ .. image:: https://github.com/barseghyanartur/pytest-codeblock/actions/workflows/test.yml/badge.svg?branch=main
39
+ :target: https://github.com/barseghyanartur/pytest-codeblock/actions
40
+ :alt: Build Status
41
+
42
+ .. image:: https://readthedocs.org/projects/pytest-codeblock/badge/?version=latest
43
+ :target: http://pytest-codeblock.readthedocs.io
44
+ :alt: Documentation Status
45
+
46
+ .. image:: https://img.shields.io/badge/docs-llms.txt-blue
47
+ :target: https://barseghyanartur.github.io/pytest-codeblock/llms.txt
48
+ :alt: llms.txt - documentation for LLMs
49
+
50
+ .. image:: https://img.shields.io/badge/license-MIT-blue.svg
51
+ :target: https://github.com/barseghyanartur/pytest-codeblock/#License
52
+ :alt: MIT
53
+
54
+ .. image:: https://coveralls.io/repos/github/barseghyanartur/pytest-codeblock/badge.svg?branch=main&service=github
55
+ :target: https://coveralls.io/github/barseghyanartur/pytest-codeblock?branch=main
56
+ :alt: Coverage
57
+
58
+ `pytest-codeblock`_ is a `Pytest`_ plugin that discovers Python code examples
59
+ in your `reStructuredText`_ and `Markdown`_ documentation files and runs them
60
+ as part of your test suite. This ensures your docs stay correct and up-to-date.
61
+
62
+ Features
63
+ ========
64
+
65
+ - **reStructuredText and Markdown support**: Automatically find and test code
66
+ blocks in `reStructuredText`_ (``.rst``) and `Markdown`_ (``.md``) files.
67
+ The only requirement here is that your code blocks shall
68
+ have a name starting with ``test_``.
69
+ - **Grouping by name**: Split a single example across multiple code blocks;
70
+ the plugin concatenates them into one test.
71
+ - **Pytest markers support**: Add existing or custom `pytest`_ markers
72
+ to the code blocks and add hook into the tests life-cycle
73
+ using ``conftest.py``.
74
+
75
+ Prerequisites
76
+ =============
77
+ - Python 3.9+
78
+ - `pytest`_ is the only required dependency
79
+
80
+ Documentation
81
+ =============
82
+ - Documentation is available on `Read the Docs`_.
83
+ - For `reStructuredText`_, see a dedicated `reStructuredText docs`_.
84
+ - For `Markdown`_, see a dedicated `Markdown docs`_.
85
+ - Both `reStructuredText docs`_ and `Markdown docs`_ have extensive
86
+ documentation on `pytest`_ markers and corresponding ``conftest.py`` hooks.
87
+ - For guidelines on contributing check the `Contributor guidelines`_.
88
+
89
+ Installation
90
+ ============
91
+
92
+ Install with `pip`_:
93
+
94
+ .. code-block:: sh
95
+
96
+ pip install pytest-codeblock
97
+
98
+ Or install with `uv`_:
99
+
100
+ .. code-block:: sh
101
+
102
+ uv pip install pytest-codeblock
103
+
104
+ .. _configuration:
105
+
106
+ Configuration
107
+ =============
108
+ *Filename: pyproject.toml*
109
+
110
+ .. code-block:: text
111
+
112
+ [tool.pytest.ini_options]
113
+ testpaths = [
114
+ "**/*.rst",
115
+ "**/*.md",
116
+ ]
117
+
118
+ Usage
119
+ =====
120
+ reStructruredText usage
121
+ -----------------------
122
+ Any code directive, such as ``.. code-block:: python``, ``.. code:: python``,
123
+ or literal blocks with a preceding ``.. codeblock-name: <name>``, will be
124
+ collected and executed automatically, if your `pytest`_ `configuration`_
125
+ allows that.
126
+
127
+ ``code-block`` directive example
128
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
129
+
130
+ .. note:: Note that ``:name:`` value has a ``test_`` prefix.
131
+
132
+ *Filename: README.rst*
133
+
134
+ .. code-block:: rst
135
+
136
+ .. code-block:: python
137
+ :name: test_basic_example
138
+
139
+ import math
140
+
141
+ result = math.pow(3, 2)
142
+ assert result == 9
143
+
144
+
145
+ ``literalinclude`` directive example
146
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
147
+
148
+ .. note:: Note that ``:name:`` value has a ``test_`` prefix.
149
+
150
+ *Filename: README.rst*
151
+
152
+ .. code-block:: rst
153
+
154
+ .. literalinclude:: examples/python/basic_example.py
155
+ :name: test_li_basic_example
156
+
157
+ See a dedicated `reStructuredText docs`_ for more.
158
+
159
+ Markdown usage
160
+ --------------
161
+
162
+ Any fenced code block with a recognized Python language tag (e.g., ``python``,
163
+ ``py``) will be collected and executed automatically, if your `pytest`_
164
+ `configuration`_ allows that.
165
+
166
+ .. note:: Note that ``name`` value has a ``test_`` prefix.
167
+
168
+ *Filename: README.md*
169
+
170
+ .. code-block:: markdown
171
+
172
+ ```python name=test_basic_example
173
+ import math
174
+
175
+ result = math.pow(3, 2)
176
+ assert result == 9
177
+ ```
178
+
179
+ See a dedicated `Markdown docs`_ for more.
180
+
181
+ Tests
182
+ =====
183
+
184
+ Run the tests with `pytest`_:
185
+
186
+ .. code-block:: sh
187
+
188
+ pytest
189
+
190
+ Writing documentation
191
+ =====================
192
+
193
+ Keep the following hierarchy.
194
+
195
+ .. code-block:: text
196
+
197
+ =====
198
+ title
199
+ =====
200
+
201
+ header
202
+ ======
203
+
204
+ sub-header
205
+ ----------
206
+
207
+ sub-sub-header
208
+ ~~~~~~~~~~~~~~
209
+
210
+ sub-sub-sub-header
211
+ ^^^^^^^^^^^^^^^^^^
212
+
213
+ sub-sub-sub-sub-header
214
+ ++++++++++++++++++++++
215
+
216
+ sub-sub-sub-sub-sub-header
217
+ **************************
218
+
219
+ License
220
+ =======
221
+
222
+ MIT
223
+
224
+ Support
225
+ =======
226
+ For security issues contact me at the e-mail given in the `Author`_ section.
227
+
228
+ For overall issues, go
229
+ to `GitHub <https://github.com/barseghyanartur/pytest-codeblock/issues>`_.
230
+
231
+ Author
232
+ ======
233
+
234
+ Artur Barseghyan <artur.barseghyan@gmail.com>
@@ -2,7 +2,7 @@
2
2
  name = "pytest-codeblock"
3
3
  description = "Pytest plugin to collect and test code blocks in reStructuredText and Markdown files."
4
4
  readme = "README.rst"
5
- version = "0.1.5"
5
+ version = "0.1.6"
6
6
  requires-python = ">=3.9"
7
7
  dependencies = [
8
8
  "pytest",
@@ -167,7 +167,6 @@ addopts = [
167
167
  "--ignore=.tox",
168
168
  "--ignore=requirements",
169
169
  "--ignore=release",
170
- "--ignore=examples",
171
170
  "--ignore=tmp",
172
171
  "--cov-report=html",
173
172
  "--cov-report=term",
@@ -187,7 +186,7 @@ pythonpath = [
187
186
  "examples/md_example",
188
187
  "examples/rst_example",
189
188
  ]
190
- norecursedirs = [".git", "examples"]
189
+ norecursedirs = [".git"]
191
190
  DJANGO_SETTINGS_MODULE = "django_settings"
192
191
 
193
192
  markers = [
@@ -2,7 +2,7 @@ from .md import MarkdownFile
2
2
  from .rst import RSTFile
3
3
 
4
4
  __title__ = "pytest-codeblock"
5
- __version__ = "0.1.5"
5
+ __version__ = "0.1.6"
6
6
  __author__ = "Artur Barseghyan <artur.barseghyan@gmail.com>"
7
7
  __copyright__ = "2025 Artur Barseghyan"
8
8
  __license__ = "MIT"
@@ -15,9 +15,11 @@ __license__ = "MIT"
15
15
  __all__ = (
16
16
  "RSTFile",
17
17
  "parse_rst",
18
+ "resolve_literalinclude_path",
19
+ "get_literalinclude_content",
18
20
  )
19
21
 
20
- # Highlight: Added helper function for literalinclude path resolution
22
+
21
23
  def resolve_literalinclude_path(
22
24
  base_dir: Path,
23
25
  include_path: str,
@@ -30,7 +32,7 @@ def resolve_literalinclude_path(
30
32
  if _include_path.exists():
31
33
  return str(_include_path.resolve())
32
34
 
33
- _base_dir = Path(base_dir.dirname) if base_dir.isfile() else base_dir
35
+ _base_dir = Path(base_dir.dirname) if base_dir.is_file() else base_dir
34
36
  try:
35
37
  full_path = _base_dir / include_path
36
38
  if full_path.exists():