Skip to content

Commit

Permalink
remove print_stderr patching
Browse files Browse the repository at this point in the history
- refactor: remove print_stderr patching. Apply sane defaults
  • Loading branch information
msftcangoblowm committed Feb 21, 2025
1 parent f54a260 commit 61519e6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 51 deletions.
65 changes: 17 additions & 48 deletions src/sphobjinv/cli/core_textconv.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,42 +68,12 @@
import io
import os
import sys
from unittest.mock import patch

from sphobjinv import Inventory
from sphobjinv.cli.convert import do_convert
from sphobjinv.cli.load import inv_local, inv_stdin, inv_url
from sphobjinv.cli.parser import getparser_textconv, PrsConst


def print_stderr_2(thing, params_b, *, end=os.linesep):
r"""Bypass parser dependent, print_strerr.
Use along with :func:`unittest.mock.patch` whenever calling
sphobjinv.cli internals.
Parameters
----------
thing
*any* -- Object to be printed
params
|dict| or |None| -- User input parameters/values mapping
end
|str| -- String to append to printed content (default: ``\n``\ )
"""
kwargs = {"file": sys.stderr, "end": end}
if params_b is None:
args = (thing,)
else:
args = (thing, params_b)

print(*args, **kwargs)
from sphobjinv.cli.ui import print_stderr


def _update_with_hardcoded(params):
Expand Down Expand Up @@ -168,12 +138,11 @@ def _wrap_inv_stdin(params):
"""
f = io.StringIO()
with patch("sphobjinv.cli.load.print_stderr", wraps=print_stderr_2):
with contextlib.redirect_stderr(f):
with contextlib.suppress(SystemExit):
inv = inv_stdin(params)
msg_err = f.getvalue().strip()
f.close()
with contextlib.redirect_stderr(f):
with contextlib.suppress(SystemExit):
inv = inv_stdin(params)
msg_err = f.getvalue().strip()
f.close()

is_inv = "inv" in locals() and inv is not None and issubclass(type(inv), Inventory)

Expand Down Expand Up @@ -221,7 +190,8 @@ def main():

# Regardless of mode, insert extra blank line
# for cosmetics
print_stderr_2(os.linesep, params)
d_empty = {}
print_stderr(os.linesep, d_empty, end=None)

# Generate the input Inventory based on --url or stdio or file.
# These inventory-load functions should call
Expand All @@ -233,15 +203,14 @@ def main():
# Bypass problematic sphobjinv.cli.ui:print_stderr
# sphobjinv-textconv --url 'file:///tests/resource/objects_cclib.inv'
f = io.StringIO()
with patch("sphobjinv.cli.load.print_stderr", wraps=print_stderr_2):
with contextlib.redirect_stderr(f):
with contextlib.suppress(SystemExit):
inv, in_path = inv_url(params)
msg_err = f.getvalue().strip()
f.close()
with contextlib.redirect_stderr(f):
with contextlib.suppress(SystemExit):
inv, in_path = inv_url(params)
msg_err = f.getvalue().strip()
f.close()

if len(msg_err) != 0 and msg_err.startswith("Error: URL mode"):
print_stderr_2(msg_err, None)
print_stderr(msg_err, d_empty)
sys.exit(1)
elif params[PrsConst.INFILE] == "-":
"""
Expand Down Expand Up @@ -274,12 +243,12 @@ def main():
"""
msg_err = "Invalid plaintext or JSON inventory format."
print_stderr_2(msg_err, None)
print_stderr(msg_err, d_empty)
sys.exit(1)
else:
if is_inv:
# Cosmetic final blank line
print_stderr_2(os.linesep, params)
print_stderr(os.linesep, d_empty, end=None)
sys.exit(0)
else: # pragma: no cover
# No inventory
Expand All @@ -295,7 +264,7 @@ def main():
do_convert(inv, in_path, params)

# Cosmetic final blank line
print_stderr_2(os.linesep, params)
print_stderr(os.linesep, d_empty)
else: # pragma: no cover
# No inventory
pass
Expand Down
15 changes: 13 additions & 2 deletions src/sphobjinv/cli/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,20 @@
"""

from __future__ import annotations

import os
import sys

from sphobjinv.cli.parser import PrsConst


def print_stderr(thing, params, *, end="\n"):
def print_stderr(
thing,
params,
*,
end: str | None = os.linesep,
) -> None:
r"""Print `thing` to stderr if not in quiet mode.
Quiet mode is indicated by the value at the |cli:QUIET| key
Expand All @@ -58,7 +66,10 @@ def print_stderr(thing, params, *, end="\n"):
|str| -- String to append to printed content (default: ``\n``\ )
"""
if params[PrsConst.SUBPARSER_NAME][:2] == "su" or not params[PrsConst.QUIET]:
is_quiet = params.get(PrsConst.QUIET, False)
subparser_name = params.get(PrsConst.SUBPARSER_NAME, None)

if not is_quiet or (subparser_name is None or subparser_name[:2] == "su"):
print(thing, file=sys.stderr, end=end)


Expand Down
6 changes: 5 additions & 1 deletion tests/test_cli_textconv_nonlocal.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@

CLI_TEST_TIMEOUT = 5

pytestmark = [pytest.mark.cli, pytest.mark.nonloc]
pytestmark = [
pytest.mark.cli,
pytest.mark.nonloc,
pytest.mark.flaky(retries=2, delay=5),
]


class TestTextconvOnlineBad:
Expand Down

0 comments on commit 61519e6

Please sign in to comment.