Skip to content

Commit

Permalink
fix(tests): fix test readme on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
ChieloNewctle committed Nov 7, 2023
1 parent fa163f4 commit 8ca1ea9
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,4 @@ docs/_build/
.python-version

# pytest-readme
test_readme.py
tests/test_readme.py
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ dynamic = ["version"]
features = ["pyo3/extension-module"]

[project.optional-dependencies]
test = ["pytest", "pytest-readme"]
test = ["pytest"]
54 changes: 52 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,53 @@
from pytest_readme import setup
def parse_from_readme():
"""
Adapted from https://github.com/boxed/pytest-readme
setup()
Copyright (c) 2020, Anders Hovmöller
Under the BSD 3-Clause "New" or "Revised" License.
"""
with open(
"tests/test_readme.py",
"w",
encoding="utf8",
) as out, open("README.md", encoding="utf8") as readme:
output, mode = [], None

for i, line in enumerate(readme.readlines()):
output.append("\n")

if mode is None and line.strip() == "```python":
mode = "first_line"
output[i] = "def test_line_%s():\n" % i
continue

if line.strip() == "```":
if mode == "doctest":
output[i] = ' """\n'
mode = None
continue

if mode == "first_line":
if line.strip() == "":
mode = None
output[i - 1] = "\n"
continue

if line.strip().startswith(">>>"):
mode = "doctest"
output[i - 2] = (
output[i - 1][:-1] + " " + output[i - 2]
) # move the def line one line up
output[i - 1] = ' """\n'
else:
mode = "test"

if mode in ("doctest", "test"):
output[i] = " " + line
else:
output[i] = "# %s" % line

out.writelines(output)


parse_from_readme()

0 comments on commit 8ca1ea9

Please sign in to comment.