pytest-codeblock 0.1.5__tar.gz → 0.1.7__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,292 @@
1
+ Metadata-Version: 2.4
2
+ Name: pytest-codeblock
3
+ Version: 0.1.7
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 hook into the tests life-cycle using ``conftest.py``.
132
+
133
+ Prerequisites
134
+ =============
135
+ - Python 3.9+
136
+ - `pytest`_ is the only required dependency
137
+
138
+ Documentation
139
+ =============
140
+ - Documentation is available on `Read the Docs`_.
141
+ - For `reStructuredText`_, see a dedicated `reStructuredText docs`_.
142
+ - For `Markdown`_, see a dedicated `Markdown docs`_.
143
+ - Both `reStructuredText docs`_ and `Markdown docs`_ have extensive
144
+ documentation on `pytest`_ markers and corresponding ``conftest.py`` hooks.
145
+ - For guidelines on contributing check the `Contributor guidelines`_.
146
+
147
+ Installation
148
+ ============
149
+
150
+ Install with `pip`_:
151
+
152
+ .. code-block:: sh
153
+
154
+ pip install pytest-codeblock
155
+
156
+ Or install with `uv`_:
157
+
158
+ .. code-block:: sh
159
+
160
+ uv pip install pytest-codeblock
161
+
162
+ .. _configuration:
163
+
164
+ Configuration
165
+ =============
166
+ *Filename: pyproject.toml*
167
+
168
+ .. code-block:: text
169
+
170
+ [tool.pytest.ini_options]
171
+ testpaths = [
172
+ "**/*.rst",
173
+ "**/*.md",
174
+ ]
175
+
176
+ Usage
177
+ =====
178
+ reStructruredText usage
179
+ -----------------------
180
+ Any code directive, such as ``.. code-block:: python``, ``.. code:: python``,
181
+ or literal blocks with a preceding ``.. codeblock-name: <name>``, will be
182
+ collected and executed automatically, if your `pytest`_ `configuration`_
183
+ allows that.
184
+
185
+ ``code-block`` directive example
186
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
187
+
188
+ .. note:: Note that ``:name:`` value has a ``test_`` prefix.
189
+
190
+ *Filename: README.rst*
191
+
192
+ .. code-block:: rst
193
+
194
+ .. code-block:: python
195
+ :name: test_basic_example
196
+
197
+ import math
198
+
199
+ result = math.pow(3, 2)
200
+ assert result == 9
201
+
202
+
203
+ ``literalinclude`` directive example
204
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
205
+
206
+ .. note:: Note that ``:name:`` value has a ``test_`` prefix.
207
+
208
+ *Filename: README.rst*
209
+
210
+ .. code-block:: rst
211
+
212
+ .. literalinclude:: examples/python/basic_example.py
213
+ :name: test_li_basic_example
214
+
215
+ See a dedicated `reStructuredText docs`_ for more.
216
+
217
+ Markdown usage
218
+ --------------
219
+
220
+ Any fenced code block with a recognized Python language tag (e.g., ``python``,
221
+ ``py``) will be collected and executed automatically, if your `pytest`_
222
+ `configuration`_ allows that.
223
+
224
+ .. note:: Note that ``name`` value has a ``test_`` prefix.
225
+
226
+ *Filename: README.md*
227
+
228
+ .. code-block:: markdown
229
+
230
+ ```python name=test_basic_example
231
+ import math
232
+
233
+ result = math.pow(3, 2)
234
+ assert result == 9
235
+ ```
236
+
237
+ See a dedicated `Markdown docs`_ for more.
238
+
239
+ Tests
240
+ =====
241
+
242
+ Run the tests with `pytest`_:
243
+
244
+ .. code-block:: sh
245
+
246
+ pytest
247
+
248
+ Writing documentation
249
+ =====================
250
+
251
+ Keep the following hierarchy.
252
+
253
+ .. code-block:: text
254
+
255
+ =====
256
+ title
257
+ =====
258
+
259
+ header
260
+ ======
261
+
262
+ sub-header
263
+ ----------
264
+
265
+ sub-sub-header
266
+ ~~~~~~~~~~~~~~
267
+
268
+ sub-sub-sub-header
269
+ ^^^^^^^^^^^^^^^^^^
270
+
271
+ sub-sub-sub-sub-header
272
+ ++++++++++++++++++++++
273
+
274
+ sub-sub-sub-sub-sub-header
275
+ **************************
276
+
277
+ License
278
+ =======
279
+
280
+ MIT
281
+
282
+ Support
283
+ =======
284
+ For security issues contact me at the e-mail given in the `Author`_ section.
285
+
286
+ For overall issues, go
287
+ to `GitHub <https://github.com/barseghyanartur/pytest-codeblock/issues>`_.
288
+
289
+ Author
290
+ ======
291
+
292
+ Artur Barseghyan <artur.barseghyan@gmail.com>
@@ -0,0 +1,233 @@
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 hook into the tests life-cycle using ``conftest.py``.
73
+
74
+ Prerequisites
75
+ =============
76
+ - Python 3.9+
77
+ - `pytest`_ is the only required dependency
78
+
79
+ Documentation
80
+ =============
81
+ - Documentation is available on `Read the Docs`_.
82
+ - For `reStructuredText`_, see a dedicated `reStructuredText docs`_.
83
+ - For `Markdown`_, see a dedicated `Markdown docs`_.
84
+ - Both `reStructuredText docs`_ and `Markdown docs`_ have extensive
85
+ documentation on `pytest`_ markers and corresponding ``conftest.py`` hooks.
86
+ - For guidelines on contributing check the `Contributor guidelines`_.
87
+
88
+ Installation
89
+ ============
90
+
91
+ Install with `pip`_:
92
+
93
+ .. code-block:: sh
94
+
95
+ pip install pytest-codeblock
96
+
97
+ Or install with `uv`_:
98
+
99
+ .. code-block:: sh
100
+
101
+ uv pip install pytest-codeblock
102
+
103
+ .. _configuration:
104
+
105
+ Configuration
106
+ =============
107
+ *Filename: pyproject.toml*
108
+
109
+ .. code-block:: text
110
+
111
+ [tool.pytest.ini_options]
112
+ testpaths = [
113
+ "**/*.rst",
114
+ "**/*.md",
115
+ ]
116
+
117
+ Usage
118
+ =====
119
+ reStructruredText usage
120
+ -----------------------
121
+ Any code directive, such as ``.. code-block:: python``, ``.. code:: python``,
122
+ or literal blocks with a preceding ``.. codeblock-name: <name>``, will be
123
+ collected and executed automatically, if your `pytest`_ `configuration`_
124
+ allows that.
125
+
126
+ ``code-block`` directive example
127
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
128
+
129
+ .. note:: Note that ``:name:`` value has a ``test_`` prefix.
130
+
131
+ *Filename: README.rst*
132
+
133
+ .. code-block:: rst
134
+
135
+ .. code-block:: python
136
+ :name: test_basic_example
137
+
138
+ import math
139
+
140
+ result = math.pow(3, 2)
141
+ assert result == 9
142
+
143
+
144
+ ``literalinclude`` directive example
145
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
146
+
147
+ .. note:: Note that ``:name:`` value has a ``test_`` prefix.
148
+
149
+ *Filename: README.rst*
150
+
151
+ .. code-block:: rst
152
+
153
+ .. literalinclude:: examples/python/basic_example.py
154
+ :name: test_li_basic_example
155
+
156
+ See a dedicated `reStructuredText docs`_ for more.
157
+
158
+ Markdown usage
159
+ --------------
160
+
161
+ Any fenced code block with a recognized Python language tag (e.g., ``python``,
162
+ ``py``) will be collected and executed automatically, if your `pytest`_
163
+ `configuration`_ allows that.
164
+
165
+ .. note:: Note that ``name`` value has a ``test_`` prefix.
166
+
167
+ *Filename: README.md*
168
+
169
+ .. code-block:: markdown
170
+
171
+ ```python name=test_basic_example
172
+ import math
173
+
174
+ result = math.pow(3, 2)
175
+ assert result == 9
176
+ ```
177
+
178
+ See a dedicated `Markdown docs`_ for more.
179
+
180
+ Tests
181
+ =====
182
+
183
+ Run the tests with `pytest`_:
184
+
185
+ .. code-block:: sh
186
+
187
+ pytest
188
+
189
+ Writing documentation
190
+ =====================
191
+
192
+ Keep the following hierarchy.
193
+
194
+ .. code-block:: text
195
+
196
+ =====
197
+ title
198
+ =====
199
+
200
+ header
201
+ ======
202
+
203
+ sub-header
204
+ ----------
205
+
206
+ sub-sub-header
207
+ ~~~~~~~~~~~~~~
208
+
209
+ sub-sub-sub-header
210
+ ^^^^^^^^^^^^^^^^^^
211
+
212
+ sub-sub-sub-sub-header
213
+ ++++++++++++++++++++++
214
+
215
+ sub-sub-sub-sub-sub-header
216
+ **************************
217
+
218
+ License
219
+ =======
220
+
221
+ MIT
222
+
223
+ Support
224
+ =======
225
+ For security issues contact me at the e-mail given in the `Author`_ section.
226
+
227
+ For overall issues, go
228
+ to `GitHub <https://github.com/barseghyanartur/pytest-codeblock/issues>`_.
229
+
230
+ Author
231
+ ======
232
+
233
+ 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.7"
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.7"
6
6
  __author__ = "Artur Barseghyan <artur.barseghyan@gmail.com>"
7
7
  __copyright__ = "2025 Artur Barseghyan"
8
8
  __license__ = "MIT"
@@ -2,7 +2,7 @@ import re
2
2
  import textwrap
3
3
  import traceback
4
4
  from pathlib import Path
5
- from typing import Optional
5
+ from typing import Optional, Union
6
6
 
7
7
  import pytest
8
8
 
@@ -15,11 +15,13 @@ __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
- base_dir: Path,
24
+ base_dir: Union[str, Path],
23
25
  include_path: str,
24
26
  ) -> Optional[str]:
25
27
  """
@@ -27,12 +29,18 @@ def resolve_literalinclude_path(
27
29
  Returns None if the file doesn't exist.
28
30
  """
29
31
  _include_path = Path(include_path)
32
+
33
+ # If `include_path` is already absolute or relative and exists, done
30
34
  if _include_path.exists():
31
35
  return str(_include_path.resolve())
32
36
 
33
- _base_dir = Path(base_dir.dirname) if base_dir.isfile() else base_dir
37
+ # If base_path is a file, switch to its parent directory
38
+ _base_path = Path(base_dir)
39
+ if _base_path.is_file():
40
+ _base_path = _base_path.parent
41
+
34
42
  try:
35
- full_path = _base_dir / include_path
43
+ full_path = _base_path / include_path
36
44
  if full_path.exists():
37
45
  return str(full_path.resolve())
38
46
  except Exception: