From 89fea305ee36a973f452027feb7b02a64b18dba0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nico=20Schl=C3=B6mer?= Date: Tue, 25 May 2021 10:09:49 +0200 Subject: [PATCH 1/4] simplify plugin a bit --- src/pytest_codeblocks/plugin.py | 24 +++++++++--------------- tests/test_shell.py | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/pytest_codeblocks/plugin.py b/src/pytest_codeblocks/plugin.py index 1b05e2c..71b0773 100644 --- a/src/pytest_codeblocks/plugin.py +++ b/src/pytest_codeblocks/plugin.py @@ -42,6 +42,7 @@ def __init__(self, name, parent, obj=None): self.obj = obj def runtest(self): + output = None if self.obj.syntax == "python": if self.obj.expect_exception: with pytest.raises(Exception): @@ -58,15 +59,7 @@ 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"] if self.obj.expect_exception: @@ -79,13 +72,14 @@ def runtest(self): self.obj.code, shell=True, check=True, stdout=subprocess.PIPE ) 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.""" diff --git a/tests/test_shell.py b/tests/test_shell.py index e33f9e2..605bda5 100644 --- a/tests/test_shell.py +++ b/tests/test_shell.py @@ -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 +# ``` +# +# ```sh +# abc +# ``` +# """ +# testdir.makefile(".md", string) +# result = testdir.runpytest("--codeblocks") +# result.assert_outcomes(passed=1) From 60d7eb283bf274355d63d8f82200fc4e2a751b9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nico=20Schl=C3=B6mer?= Date: Tue, 25 May 2021 10:13:18 +0200 Subject: [PATCH 2/4] fix for bash --- src/pytest_codeblocks/plugin.py | 23 +++++++++++++++++++---- tests/test_shell.py | 32 ++++++++++++++++---------------- 2 files changed, 35 insertions(+), 20 deletions(-) diff --git a/src/pytest_codeblocks/plugin.py b/src/pytest_codeblocks/plugin.py index 71b0773..7a03d80 100644 --- a/src/pytest_codeblocks/plugin.py +++ b/src/pytest_codeblocks/plugin.py @@ -41,6 +41,8 @@ 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": @@ -60,18 +62,31 @@ def runtest(self): + f"{e}" ) output = s.getvalue() - else: - assert self.obj.syntax in ["sh", "bash"] + elif self.obj.syntax == "sh": if self.obj.expect_exception: with pytest.raises(Exception): subprocess.run(self.obj.code, shell=True, check=True) 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 ) output = ret.stdout.decode() + else: + assert self.obj.syntax == "bash" + if self.obj.expect_exception: + with pytest.raises(Exception): + subprocess.run( + self.obj.code, shell=True, check=True, executable="/bin/bash" + ) + else: + ret = subprocess.run( + self.obj.code, + shell=True, + check=True, + stdout=subprocess.PIPE, + executable="/bin/bash", + ) + output = ret.stdout.decode() if output is not None and self.obj.expected_output is not None: if self.obj.expected_output != output: diff --git a/tests/test_shell.py b/tests/test_shell.py index 605bda5..1706f09 100644 --- a/tests/test_shell.py +++ b/tests/test_shell.py @@ -79,19 +79,19 @@ def test_shell_expect_output_fail(testdir): result.assert_outcomes(failed=1) -# def test_bash(testdir): -# string = """ -# ```bash -# foo=1 -# if [[ $foo == 1 ]]; then -# echo abc -# fi -# ``` -# -# ```sh -# abc -# ``` -# """ -# testdir.makefile(".md", string) -# result = testdir.runpytest("--codeblocks") -# result.assert_outcomes(passed=1) +def test_bash(testdir): + string = """ + ```bash + foo=1 + if [[ $foo == 1 ]]; then + echo abc + fi + ``` + + ```sh + abc + ``` + """ + testdir.makefile(".md", string) + result = testdir.runpytest("--codeblocks") + result.assert_outcomes(passed=1) From c662cbbf576255f33340a87335474fe75710290d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nico=20Schl=C3=B6mer?= Date: Tue, 25 May 2021 10:19:10 +0200 Subject: [PATCH 3/4] less code duplication --- README.md | 4 ++-- src/pytest_codeblocks/plugin.py | 20 ++++++++------------ 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 7b14372..370b58d 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/pytest_codeblocks/plugin.py b/src/pytest_codeblocks/plugin.py index 7a03d80..fe3768f 100644 --- a/src/pytest_codeblocks/plugin.py +++ b/src/pytest_codeblocks/plugin.py @@ -62,21 +62,17 @@ def runtest(self): + f"{e}" ) output = s.getvalue() - elif self.obj.syntax == "sh": - if self.obj.expect_exception: - with pytest.raises(Exception): - subprocess.run(self.obj.code, shell=True, check=True) - else: - ret = subprocess.run( - self.obj.code, shell=True, check=True, stdout=subprocess.PIPE - ) - output = ret.stdout.decode() else: - assert self.obj.syntax == "bash" + 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, executable="/bin/bash" + self.obj.code, shell=True, check=True, executable=executable ) else: ret = subprocess.run( @@ -84,7 +80,7 @@ def runtest(self): shell=True, check=True, stdout=subprocess.PIPE, - executable="/bin/bash", + executable=executable, ) output = ret.stdout.decode() From 622ecbfff8aeafd9e13c73fc778f6e2916241759 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nico=20Schl=C3=B6mer?= Date: Tue, 25 May 2021 10:19:24 +0200 Subject: [PATCH 4/4] version bump --- setup.cfg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.cfg b/setup.cfg index cadb541..83ad763 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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