From ea320570d7951483955637cfd8370bd7744f723e Mon Sep 17 00:00:00 2001 From: BJ Dierkes Date: Mon, 1 Jan 2024 20:30:55 -0600 Subject: [PATCH] Further Resolve Issue 653 --- Makefile | 4 +-- cement/core/foundation.py | 15 ++++++++--- tests/core/test_foundation.py | 51 +++++++++++++++++++++++++---------- 3 files changed, 51 insertions(+), 19 deletions(-) diff --git a/Makefile b/Makefile index 817ea375..6290bda0 100644 --- a/Makefile +++ b/Makefile @@ -7,10 +7,10 @@ dev: docker-compose exec cement /bin/bash test: comply - python -m pytest -v --cov=cement --cov-report=term --cov-report=html:coverage-report tests/ + python -m pytest -v --cov=cement --cov-report=term --cov-report=html:coverage-report --capture=sys tests/ test-core: comply - python -m pytest -v --cov=cement.core --cov-report=term --cov-report=html:coverage-report tests/core + python -m pytest -v --cov=cement.core --cov-report=term --cov-report=html:coverage-report --capture=sys tests/core virtualenv: virtualenv --prompt '|> cement <| ' env diff --git a/cement/core/foundation.py b/cement/core/foundation.py index b9f7e445..406b8fbd 100644 --- a/cement/core/foundation.py +++ b/cement/core/foundation.py @@ -1023,7 +1023,8 @@ def close(self, code=None): # reattach our stdout if in quiet mode to avoid lingering file handles # resolves: https://github.com/datafolklabs/cement/issues/653 - self._unsuppress_output() + if self._meta.quiet is True: + self._unsuppress_output() # in theory, this should happen last-last... but at that point `self` # would be kind of busted after _unlay_cement() is run. @@ -1145,11 +1146,19 @@ def _suppress_output(self): def _unsuppress_output(self): LOG.debug('unsuppressing all console output') - sys.stdout.close() - sys.stderr.close() + + # don't accidentally close the actual / + if hasattr(sys.stdout, 'name') and sys.stdout.name != '': + sys.stdout.close() + if hasattr(sys.stderr, 'name') and sys.stderr.name != '': + sys.stderr.close() sys.stdout = self.__saved_stdout__ sys.stderr = self.__saved_stderr__ + # have to resetup the log handler to unsuppress console output + if self.log is not None: + self._setup_log_handler() + def _lay_cement(self): """Initialize the framework.""" LOG.debug("laying cement for the '%s' application" % diff --git a/tests/core/test_foundation.py b/tests/core/test_foundation.py index 47266912..2dd8b5d7 100644 --- a/tests/core/test_foundation.py +++ b/tests/core/test_foundation.py @@ -641,23 +641,46 @@ def test_pre_render_hook(): def test_quiet(): stdout_ref = sys.stdout - with TestApp(argv=['--quiet']) as app: - app.run() - app.render({}) # coverage - assert app.quiet is True - assert stdout_ref is not sys.stdout + stderr_ref = sys.stderr + try: + with TestApp(argv=['--quiet']) as app: + app.run() + app.render({}) # coverage + assert app.quiet is True + assert stdout_ref is not sys.stdout - sys.stdout = stdout_ref - with TestApp(argv=['--quiet', '--debug']) as app: - app.run() - assert stdout_ref is sys.stdout + sys.stdout = stdout_ref + sys.stderr = stderr_ref + with TestApp(argv=['--quiet', '--debug']) as app: + app.run() + assert stdout_ref is sys.stdout - sys.stdout = stdout_ref - with TestApp(argv=['--quiet'], debug=True) as app: - app.run() - assert stdout_ref is sys.stdout + sys.stdout = stdout_ref + sys.stderr = stderr_ref + with TestApp(argv=['--quiet'], debug=True) as app: + app.run() + assert stdout_ref is sys.stdout - sys.stdout = stdout_ref + sys.stdout = stdout_ref + sys.stderr = stderr_ref + except Exception: + sys.stdout = stdout_ref + sys.stderr = stderr_ref + raise + + +def test_issue_653(): + with pytest.raises(SystemExit): + with App('myapp', argv=[], exit_on_close=True) as app: + app.run() + with pytest.raises(SystemExit): + with App('myapp', argv=["--quiet"], exit_on_close=True) as app: + app.run() + with App('myapp', argv=[]) as app: + print(app._meta.exit_on_close) + app.run() + with App('myapp', argv=["--quiet"]) as app: + app.run() def test_none_template_handler():