Skip to content

Commit

Permalink
Merge pull request #20 from nschloe/pytests
Browse files Browse the repository at this point in the history
pytests
  • Loading branch information
nschloe authored Feb 16, 2021
2 parents d264734 + 7e1b7af commit 384d6f4
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 44 deletions.
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2020 Nico Schlömer
Copyright (c) 2020-2021 Nico Schlömer

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
33 changes: 6 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
[![LGTM](https://img.shields.io/lgtm/grade/python/github/nschloe/exdown.svg?style=flat-square)](https://lgtm.com/projects/g/nschloe/exdown)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg?style=flat-square)](https://github.com/psf/black)

This is exdown, a tool for extracting code blocks from Markdown files. This can be used
for testing code in your README files.
This is exdown, a tool for extracting code blocks from Markdown files and to create
tests from them.

Install with
```
Expand All @@ -23,34 +23,20 @@ pip install exdown
and use as
```python
import exdown
import pytest


@pytest.mark.parametrize("string, lineno", exdown.extract("README.md"))
def test_readme(string, lineno):
exec(string)
blocks = exdown.extract("README.md")
```
or more fancy as
or, to create tests for [pytest](https://docs.pytest.org/en/stable/)
```python
import exdown
import pytest


@pytest.mark.parametrize("string, lineno", exdown.extract("README.md"))
def test_readme(string, lineno):
try:
# https://stackoverflow.com/a/62851176/353337
exec(string, {"__MODULE__": "__main__"})
except Exception:
print(f"README.md (line {lineno}):\n```\n{string}```")
raise
test_readme = exdown.pytests("README.md")
```
to get better error messages.

If you don't want all code blocks to be extracted, you can filter by syntax
<!--exdown-skip-->
```python
exdown.extract("README.md", syntax_filter="python")
exdown.pytests("README.md", syntax_filter="python")
```
or prefix your code block in the Markdown file with an `exdown-skip` comment
````markdown
Expand All @@ -62,12 +48,5 @@ foo + bar # not working
dolor sit amet.
````

### Testing

To run the unit tests, check out this repository and type
```
pytest
```

### License
exdown is published under the [MIT license](https://en.wikipedia.org/wiki/MIT_License).
4 changes: 2 additions & 2 deletions exdown/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .__about__ import __version__
from .main import extract, from_buffer
from .main import extract, from_buffer, pytests

__all__ = ["extract", "from_buffer", "__version__"]
__all__ = ["extract", "pytests", "from_buffer", "__version__"]
17 changes: 17 additions & 0 deletions exdown/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,20 @@ def from_buffer(f, max_num_lines=10000, syntax_filter=None):
previous_line = line

return out


def pytests(filename, syntax_filter=None):
import pytest

@pytest.mark.parametrize(
"string, lineno", extract(filename, syntax_filter=syntax_filter)
)
def exec_raise(string, lineno):
try:
# https://stackoverflow.com/a/62851176/353337
exec(string, {"__MODULE__": "__main__"})
except Exception:
print(f"{filename} (line {lineno}):\n```\n{string}```")
raise

return exec_raise
7 changes: 6 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = exdown
version = 0.7.1
version = 0.8.0
author = Nico Schlömer
author_email = nico.schloemer@gmail.com
description = Extract code blocks from markdown
Expand All @@ -22,10 +22,15 @@ classifiers =
Programming Language :: Python :: 3.6
Programming Language :: Python :: 3.7
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9
Topic :: Text Processing

[options]
packages = find:
install_requires =
importlib_metadata;python_version<"3.8"
python_requires = >=3.6

[options.extras_require]
all =
pytest
14 changes: 1 addition & 13 deletions test/test_readme.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
import pytest

import exdown


@pytest.mark.parametrize(
"string, lineno", exdown.extract("README.md", syntax_filter="python")
)
def test_readme(string, lineno):
try:
# https://stackoverflow.com/a/62851176/353337
exec(string, {"__MODULE__": "__main__"})
except Exception:
print(f"README.md (line {lineno}):\n```\n{string}```")
raise
test_readme = exdown.pytests("README.md", syntax_filter="python")

0 comments on commit 384d6f4

Please sign in to comment.