Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: Make sure we test some failure states #21

Merged
merged 4 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/pytest_markdown_docs/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ def runtest(self):
try:
tree = ast.parse(self.code)
except SyntaxError:
return
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦

raise

try:
# if we don't compile the code, it seems we get name lookup errors
# for functions etc. when doing cross-calls across inline functions
compiled = compile(tree, self.name, "exec", dont_inherit=True)
except SyntaxError:
return
raise

exec(compiled, all_globals)

Expand Down
37 changes: 20 additions & 17 deletions tests/plugin_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,29 @@ def pytest_markdown_docs_globals():
testdir.makefile(
".md",
"""
\"\"\"
```python
assert a + " world" == "hello world"
```
\"\"\"

```python
assert False
```

```python
**@ # this is a syntax error
```
""",
)

# run all tests with pytest
result = testdir.runpytest("--markdown-docs")
result.assert_outcomes(passed=1)
result.assert_outcomes(passed=1, failed=2)


def test_traceback(testdir):
testdir.makefile(
".md",
"""
\"\"\"
yada yada yada

```python
Expand All @@ -94,28 +99,28 @@ def bar():

foo()
```
\"\"\"
""",
)
result = testdir.runpytest("--markdown-docs")
result.assert_outcomes(passed=0, failed=1)

# we check the traceback vs a regex pattern since the file paths can change
expected_output_pattern = r"""
Error in code block:
```
5 def foo\(\):
6 raise Exception\("doh"\)
7
8 def bar\(\):
9 foo\(\)
10
11 foo\(\)
12
4 def foo\(\):
5 raise Exception\("doh"\)
6
7 def bar\(\):
8 foo\(\)
9
10 foo\(\)
11
```
Traceback \(most recent call last\):
File ".*/test_traceback.md", line 11, in <module>
File ".*/test_traceback.md", line 10, in <module>
foo\(\)
File ".*/test_traceback.md", line 6, in foo
File ".*/test_traceback.md", line 5, in foo
raise Exception\("doh"\)
Exception: doh
""".strip()
Expand All @@ -140,12 +145,10 @@ def initialize():
testdir.makefile(
".md",
"""
\"\"\"
```python
import pytest_markdown_docs
assert pytest_markdown_docs.bump == 1
```
\"\"\"
""",
)
result = testdir.runpytest("--markdown-docs")
Expand Down
Loading