Skip to content

Commit 14bc693

Browse files
committed
tweak find_examples()
1 parent 23dad46 commit 14bc693

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

pytest_examples/find_examples.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import annotations as _annotations
22

33
import re
4-
import sys
54
from dataclasses import dataclass
65
from pathlib import Path
76
from textwrap import dedent
@@ -99,29 +98,30 @@ def __str__(self):
9998
return f'{path}:{self.start_line}-{self.end_line}'
10099

101100

102-
def find_examples(*directories: str, run_on_windows: bool = False) -> Iterable[CodeExample]:
101+
def find_examples(*paths: str, skip: bool = False) -> Iterable[CodeExample]:
103102
"""
104103
Find Python code examples in markdown files and python file docstrings.
105104
106-
Yields `CodeExample` objects wrapped in a `pytest.param` object.
105+
:param paths: Directories or files to search for examples in.
106+
:param skip: Whether to exit early and not search for examples, useful when running on windows where search fails.
107+
:return: A generator of `CodeExample` objects.
107108
"""
108-
if not run_on_windows and sys.platform == 'win32':
109-
# this avoids errors reading files on windows as pytest parametrize is eager
109+
if skip:
110110
return
111111

112-
for d in directories:
113-
dir_path = Path(d)
114-
if dir_path.is_file():
115-
paths = [dir_path]
116-
elif dir_path.is_dir():
117-
paths = dir_path.glob('**/*')
112+
for s in paths:
113+
path = Path(s)
114+
if path.is_file():
115+
sub_paths = [path]
116+
elif path.is_dir():
117+
sub_paths = path.glob('**/*')
118118
else:
119-
raise ValueError(f'Not a file or directory: {d!r}')
119+
raise ValueError(f'Not a file or directory: {s!r}')
120120

121-
for path in paths:
121+
for path in sub_paths:
122122
group = uuid4()
123123
if path.suffix == '.py':
124-
code = path.read_text()
124+
code = path.read_text('utf-8')
125125
for m_docstring in re.finditer(r'(^ *)(r?"""\n)(.+?)\1"""', code, flags=re.M | re.S):
126126
start_line = code[: m_docstring.start()].count('\n') + 1
127127
docstring = m_docstring.group(3)

0 commit comments

Comments
 (0)