Skip to content

Commit

Permalink
Merge pull request #50 from nschloe/bash2
Browse files Browse the repository at this point in the history
bash
  • Loading branch information
nschloe authored May 25, 2021
2 parents 408c8f2 + 622ecbf commit 6b52722
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 22 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ README.md .......................
================================= 56 passed in 0.08s ==================================
```
pytest-codeblocks will only pick up code blocks with `python` and `sh`/`bash` syntax
highlighting.
pytest-codeblocks will only pick up code blocks with `python` and `sh`/`bash`/`zsh`
syntax highlighting.


#### Skipping code blocks
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = pytest-codeblocks
version = 0.11.0
version = 0.11.1
author = Nico Schlömer
author_email = nico.schloemer@gmail.com
description = Test code blocks in your READMEs
Expand Down
43 changes: 24 additions & 19 deletions src/pytest_codeblocks/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ def __init__(self, name, parent, obj=None):
super().__init__(name, parent=parent)
self.obj = obj

# TODO for python 3.7+, stdout=subprocess.PIPE can be replaced by
# capture_output=True
def runtest(self):
output = None
if self.obj.syntax == "python":
if self.obj.expect_exception:
with pytest.raises(Exception):
Expand All @@ -58,34 +61,36 @@ def runtest(self):
+ "```\n\n"
+ f"{e}"
)

output = s.getvalue()
if self.obj.expected_output is not None:
if self.obj.expected_output != output:
raise RuntimeError(
f"{self.name}, line {self.obj.lineno}:\n```\n"
+ f"Expected output\n```\n{self.obj.expected_output}```\n"
+ f"but got\n```\n{output}```"
)
else:
assert self.obj.syntax in ["sh", "bash"]
executable = {
"sh": None,
"bash": "/bin/bash",
"zsh": "/bin/zsh",
}[self.obj.syntax]
if self.obj.expect_exception:
with pytest.raises(Exception):
subprocess.run(self.obj.code, shell=True, check=True)
subprocess.run(
self.obj.code, shell=True, check=True, executable=executable
)
else:
# TODO for python 3.7+, stdout=subprocess.PIPE can be replaced by
# capture_output=True
ret = subprocess.run(
self.obj.code, shell=True, check=True, stdout=subprocess.PIPE
self.obj.code,
shell=True,
check=True,
stdout=subprocess.PIPE,
executable=executable,
)
output = ret.stdout.decode()
if self.obj.expected_output is not None:
if self.obj.expected_output != output:
raise RuntimeError(
f"{self.name}, line {self.obj.lineno}:\n```\n"
+ f"Expected output\n```\n{self.obj.expected_output}```\n"
+ f"but got\n```\n{output}```"
)

if output is not None and self.obj.expected_output is not None:
if self.obj.expected_output != output:
raise RuntimeError(
f"{self.name}, line {self.obj.lineno}:\n```\n"
+ f"Expected output\n```\n{self.obj.expected_output}```\n"
+ f"but got\n```\n{output}```"
)

def repr_failure(self, excinfo):
"""Called when self.runtest() raises an exception."""
Expand Down
18 changes: 18 additions & 0 deletions tests/test_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,21 @@ def test_shell_expect_output_fail(testdir):
testdir.makefile(".md", string)
result = testdir.runpytest("--codeblocks")
result.assert_outcomes(failed=1)


def test_bash(testdir):
string = """
```bash
foo=1
if [[ $foo == 1 ]]; then
echo abc
fi
```
<!--pytest-codeblocks:expected-output-->
```sh
abc
```
"""
testdir.makefile(".md", string)
result = testdir.runpytest("--codeblocks")
result.assert_outcomes(passed=1)

0 comments on commit 6b52722

Please sign in to comment.