From fa3908fd4bff3b3ca1f1cd779d29087fb1e845a7 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Sat, 9 Dec 2023 05:48:21 -0600 Subject: [PATCH 1/2] refactor!: Only return `proc` for `run()` if `G_IS_TEST` set. Or else running "g" would be command (as expected), but followed by output like this: --- conftest.py | 15 +++++++++++++++ src/g/__init__.py | 12 ++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 conftest.py diff --git a/conftest.py b/conftest.py new file mode 100644 index 0000000..e6eefd4 --- /dev/null +++ b/conftest.py @@ -0,0 +1,15 @@ +"""Setup pytest for g.""" +import pytest + + +@pytest.fixture(autouse=True) +def setup(monkeypatch: pytest.MonkeyPatch) -> None: + """Prepare pytest test environment. + + Environment variables + --------------------- + G_IS_TEST : + Control whether run() returns proc so function can be tested. If proc was always + returned, it would print ** after command. + """ + monkeypatch.setenv("G_IS_TEST", "1") diff --git a/src/g/__init__.py b/src/g/__init__.py index fdd902a..c21b280 100755 --- a/src/g/__init__.py +++ b/src/g/__init__.py @@ -1,5 +1,6 @@ #!/usr/bin/env python """Package for g.""" +import os import pathlib import subprocess import sys @@ -29,7 +30,14 @@ def run( *args: object, **kwargs: t.Any, ) -> t.Optional["subprocess.Popen[str]"]: - """CLI Entrypoint for g, overlay for current directory's VCS utility.""" + """CLI Entrypoint for g, overlay for current directory's VCS utility. + + Environment variables + --------------------- + G_IS_TEST : + Control whether run() returns proc so function can be tested. If proc was always + returned, it would print ** after command. + """ # Interpret default kwargs lazily for mockability of argv if cmd is DEFAULT: cmd = find_repo_type(pathlib.Path.cwd()) @@ -44,7 +52,7 @@ def run( proc.wait() else: proc.communicate() - if __name__ != "__main__": + if os.getenv("G_IS_TEST") and __name__ != "__main__": return proc return None From 138aff77bd4e7011c4f29158acf4edf52da8ebad Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Sat, 9 Dec 2023 05:51:51 -0600 Subject: [PATCH 2/2] docs(CHANGES): Note fix for running g --- CHANGES | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGES b/CHANGES index 1850de2..591cd74 100644 --- a/CHANGES +++ b/CHANGES @@ -85,6 +85,11 @@ $ pipx install --suffix=@next g --pip-args '\--pre' --force - Add docstrings to functions, methods, classes, and packages (#18) +### Bug fixes + +- Prevent outputting `` when running `g` + (#19) + ## g 0.0.2 (2022-09-11) **Maintenance only release, no bug fixes or features**