Skip to content

Commit fd18368

Browse files
authored
Merge pull request #68 from nschloe/skipif
add skipif
2 parents 07ded63 + 9af71af commit fd18368

7 files changed

Lines changed: 50 additions & 20 deletions

File tree

.flake8

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[flake8]
2-
ignore = E203, E266, E501, W503
2+
ignore = E203, E266, E501, W503, F401, C901
33
max-line-length = 80
44
max-complexity = 18
55
select = B,C,E,F,W,T4,B9

.github/workflows/ci.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ jobs:
2626
python-version: ["3.7", "3.8", "3.9", "3.10"]
2727
include:
2828
- platform: macos-latest
29-
python-version: "3.9"
29+
python-version: "3.10"
3030
- platform: windows-latest
31-
python-version: "3.9"
31+
python-version: "3.10"
3232

3333
runs-on: ${{ matrix.platform }}
3434

@@ -40,6 +40,6 @@ jobs:
4040
- name: Test with tox
4141
run: |
4242
pip install tox
43-
tox -- --codeblocks --cov pytest_codeblocks --cov-report xml --cov-report term
43+
tox -- --cov pytest_codeblocks --cov-report xml --cov-report term
4444
- uses: codecov/codecov-action@v1
45-
if: ${{ matrix.python-version == '3.9' }}
45+
if: ${{ matrix.python-version == '3.10' && matrix.os == 'ubuntu-latest' }}

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
[![Anaconda Cloud](https://anaconda.org/conda-forge/pytest-codeblocks/badges/version.svg?=style=flat-square)](https://anaconda.org/conda-forge/pytest-codeblocks/)
88
[![PyPI pyversions](https://img.shields.io/pypi/pyversions/pytest-codeblocks.svg?style=flat-square)](https://pypi.org/project/pytest-codeblocks/)
99
[![GitHub stars](https://img.shields.io/github/stars/nschloe/pytest-codeblocks.svg?style=flat-square&logo=github&label=Stars&logoColor=white)](https://github.com/nschloe/pytest-codeblocks)
10-
[![PyPi downloads](https://img.shields.io/pypi/dm/pytest-codeblocks.svg?style=flat-square)](https://pypistats.org/packages/pytest-codeblocks)
10+
[![Downloads](https://pepy.tech/badge/pytest-codeblocks/month?style=flat-square)](https://pepy.tech/project/pytest-codeblocks)
11+
<!--[![PyPi downloads](https://img.shields.io/pypi/dm/pytest-codeblocks.svg?style=flat-square)](https://pypistats.org/packages/pytest-codeblocks)-->
1112

1213
[![gh-actions](https://img.shields.io/github/workflow/status/nschloe/pytest-codeblocks/ci?style=flat-square)](https://github.com/nschloe/pytest-codeblocks/actions?query=workflow%3Aci)
1314
[![codecov](https://img.shields.io/codecov/c/github/nschloe/pytest-codeblocks.svg?style=flat-square)](https://app.codecov.io/gh/nschloe/pytest-codeblocks)
@@ -19,17 +20,17 @@ blocks from README files. It supports Python and shell code.
1920

2021
Install with
2122

22-
```
23+
```sh
2324
pip install pytest-codeblocks
2425
```
2526

2627
and run pytest with
2728

28-
```
29+
```sh
2930
pytest --codeblocks
3031
```
3132

32-
```
33+
```sh
3334
================================= test session starts =================================
3435
platform linux -- Python 3.9.4, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
3536
rootdir: /path/to/directory
@@ -61,6 +62,11 @@ foo + bar # not working
6162
dolor sit amet.
6263
````
6364

65+
Conditionally skipping code blocks works with `skipif`, e.g.,
66+
```
67+
<!--pytest-codeblocks:skipif(sys.version_info <= (3, 7))-->
68+
```
69+
6470
#### Merging code blocks
6571

6672
Broken-up code blocks can be merged into one with the `pytest-codeblocks:cont` prefix

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = pytest-codeblocks
3-
version = 0.11.4
3+
version = 0.12.0
44
author = Nico Schlömer
55
author_email = nico.schloemer@gmail.com
66
description = Test code blocks in your READMEs

src/pytest_codeblocks/main.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class CodeBlock:
1818
syntax: str | None = None
1919
expected_output: str | None = None
2020
expect_exception: bool = False
21+
skip: bool = False
22+
skipif: str | None = None
2123

2224

2325
def extract_from_file(
@@ -27,7 +29,7 @@ def extract_from_file(
2729
return extract_from_buffer(handle, *args, **kwargs)
2830

2931

30-
def extract_from_buffer(f, max_num_lines: int = 10000):
32+
def extract_from_buffer(f, max_num_lines: int = 10000) -> list[CodeBlock]:
3133
out = []
3234
previous_nonempty_line = None
3335
k = 1
@@ -81,9 +83,7 @@ def extract_from_buffer(f, max_num_lines: int = 10000):
8183
keyword = m.group(1).strip("- ")
8284

8385
# handle special tags
84-
if keyword == "skip":
85-
continue
86-
elif keyword == "expected-output":
86+
if keyword == "expected-output":
8787
if len(out) == 0:
8888
raise RuntimeError(
8989
"Found <!--pytest-codeblocks-expected-output--> "
@@ -110,14 +110,25 @@ def extract_from_buffer(f, max_num_lines: int = 10000):
110110
out[-1].expected_output,
111111
out[-1].expect_exception,
112112
)
113+
elif keyword == "skip":
114+
out.append(CodeBlock("".join(code_block), lineno, syntax, skip=True))
115+
elif keyword.startswith("skipif"):
116+
m = re.match(r"skipif\((.*)\)", keyword)
117+
if m is None:
118+
raise RuntimeError(
119+
"pytest-codeblocks: Expected skipif(some-condition)"
120+
)
121+
out.append(
122+
CodeBlock("".join(code_block), lineno, syntax, skipif=m.group(1))
123+
)
113124
elif keyword in ["expect-exception", "expect-error"]:
114125
out.append(
115126
CodeBlock(
116127
"".join(code_block), lineno, syntax, expect_exception=True
117128
)
118129
)
119130
else:
120-
raise RuntimeError('Unknown pytest-codeblocks keyword "{keyword}."')
131+
raise RuntimeError(f'Unknown pytest-codeblocks keyword "{keyword}."')
121132

122133
previous_nonempty_line = line
123134

src/pytest_codeblocks/plugin.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,32 @@ def collect(self):
3131
if block.syntax not in ["python", "sh", "bash"]:
3232
continue
3333
# https://docs.pytest.org/en/stable/deprecations.html#node-construction-changed-to-node-from-parent
34-
out = Codeblock.from_parent(parent=self, name=self.name)
34+
out = TestBlock.from_parent(parent=self, name=self.name)
3535
out.obj = block
3636
yield out
3737

3838

39-
class Codeblock(pytest.Item):
39+
class TestBlock(pytest.Item):
4040
def __init__(self, name, parent, obj=None):
4141
super().__init__(name, parent=parent)
4242
self.obj = obj
4343

4444
# TODO for python 3.7+, stdout=subprocess.PIPE can be replaced by
4545
# capture_output=True
4646
def runtest(self):
47+
assert self.obj is not None
4748
output = None
49+
50+
if self.obj.skip:
51+
pytest.skip()
52+
53+
if self.obj.skipif is not None:
54+
# needed for sys.version_info in skipif eval():
55+
import sys
56+
57+
if eval(self.obj.skipif):
58+
pytest.skip()
59+
4860
if self.obj.syntax == "python":
4961
if self.obj.expect_exception:
5062
with pytest.raises(Exception):

tests/test_general.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def test_skip(testdir):
3535
"""
3636
testdir.makefile(".md", string1)
3737
result = testdir.runpytest("--codeblocks")
38-
result.assert_outcomes(passed=0)
38+
result.assert_outcomes(skipped=2)
3939

4040

4141
# example.md against reference strings test against
@@ -50,6 +50,7 @@ def test_reference():
5050
),
5151
pytest_codeblocks.CodeBlock("foobar\n", 16, "ruby"),
5252
pytest_codeblocks.CodeBlock("echo abc\n", 20, "sh"),
53+
pytest_codeblocks.CodeBlock("bar\n", 25, "python", skip=True),
5354
pytest_codeblocks.CodeBlock("# ```import math```\n", 30, "python"),
5455
pytest_codeblocks.CodeBlock("1 + 1 == 2\n", 35, "python"),
5556
pytest_codeblocks.CodeBlock("1 + 1 == 2\n", 40, "python"),
@@ -58,6 +59,6 @@ def test_reference():
5859
lst = pytest_codeblocks.extract_from_file(this_dir / "example.md")
5960
print(lst)
6061
for r, obj in zip(ref, lst):
61-
print("r ", r)
62-
print("obj", obj)
62+
print("\n\nr ", r)
63+
print("\nobj", obj)
6364
assert r == obj

0 commit comments

Comments
 (0)