-
Notifications
You must be signed in to change notification settings - Fork 82
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
Improve utils.execute_command
, add a Pytest selftest for it, and run it in GHA
#1243
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# executable scripts in the scripts directory all manipulate `sys.path` | ||
import ods_ci.utils.scripts.ocm.ocm as _ # imported for side-effects |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import contextlib | ||
import doctest | ||
import functools | ||
import io | ||
import logging | ||
import subprocess | ||
import sys | ||
import unittest | ||
import unittest.mock | ||
|
||
import pytest | ||
|
||
from ods_ci.utils.scripts import util | ||
|
||
|
||
@contextlib.contextmanager | ||
def capture_logging(level: int | None = None, formatter: logging.Formatter | None = None): | ||
"""Temporarily replaces all logging handlers with a StringIO logging handler. | ||
|
||
Note: Pytest has the `caplog` fixture (`LogCaptureFixture` class) which functions similarly. | ||
|
||
Usage example: | ||
|
||
>>> with capture_logging(logging.DEBUG, formatter=logging.Formatter("%(message)s")) as string_io: | ||
... logging.debug('debug message') | ||
>>> print(string_io.getvalue(), end="") | ||
debug message | ||
""" | ||
string_io = io.StringIO() | ||
ch = logging.StreamHandler(string_io) | ||
if level: | ||
ch.setLevel(level) | ||
|
||
if formatter: | ||
ch.setFormatter(formatter) | ||
|
||
handlers = logging.root.handlers | ||
logging.root.handlers = [] | ||
logging.root.addHandler(ch) | ||
try: | ||
yield string_io | ||
finally: | ||
logging.root.removeHandler(ch) | ||
logging.root.handlers = handlers | ||
|
||
|
||
# https://stackoverflow.com/questions/5681330/using-doctests-from-within-unittests | ||
def load_tests(loader: unittest.TestLoader, tests: unittest.TestSuite, pattern: str) -> unittest.TestSuite: | ||
tests.addTest(doctest.DocTestSuite()) | ||
return tests | ||
|
||
|
||
class TestExecuteCommand(unittest.TestCase): | ||
def test_failed_to_run(self): | ||
# without mocking, the test would be sensitive to `/bin/sh --version` | ||
with unittest.mock.patch.object( | ||
subprocess.Popen, "__init__", new=functools.partialmethod(subprocess.Popen.__init__, executable="/bin/bash") | ||
): | ||
assert "No such file or directory" in util.execute_command("/this-file-does-not-exist", print_stdout=False) | ||
|
||
def test_success(self): | ||
assert util.execute_command("/bin/true") == "" | ||
|
||
def test_fail(self): | ||
assert util.execute_command("/bin/false") == "" | ||
|
||
def test_stdout(self): | ||
assert util.execute_command("echo stdout") == "stdout\n" | ||
|
||
def test_stderr(self): | ||
assert util.execute_command("echo stderr >&2") == "stderr\n" | ||
|
||
def test_string_cmd(self): | ||
assert util.execute_command("echo hello world", print_stdout=False) == "hello world\n" | ||
|
||
def test_list_cmd_single_list_item(self): | ||
assert util.execute_command(["echo hello world"], print_stdout=False) == "hello world\n" | ||
|
||
def test_list_cmd_multiple_list_items(self): | ||
# this is surprising, but it's what subprocess.Popen does | ||
assert util.execute_command(["echo", "hello", "world"], print_stdout=False) == "\n" | ||
|
||
def test_multiple_output_lines(self): | ||
python = sys.executable | ||
assert util.execute_command(f"""{python} -c 'print("a\\n"*13, end="")'""", print_stdout=False) == "a\n" * 13 | ||
|
||
@pytest.mark.slow | ||
def test_many_long_output_lines(self): | ||
python = sys.executable | ||
assert ( | ||
util.execute_command(f"""{python} -c 'print(("a" * 40 + "\\n")*1_000_000, end="")'""", print_stdout=False) | ||
== ("a" * 40 + "\n") * 1_000_000 | ||
) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is to have an option to quiet down the output in selftests