Further Resolve Issue 653

This commit is contained in:
BJ Dierkes 2024-01-01 20:30:55 -06:00
parent 25d114bbb2
commit ea320570d7
3 changed files with 51 additions and 19 deletions

View File

@ -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

View File

@ -1023,7 +1023,8 @@ class App(meta.MetaMixin):
# 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 @@ class App(meta.MetaMixin):
def _unsuppress_output(self):
LOG.debug('unsuppressing all console output')
sys.stdout.close()
sys.stderr.close()
# don't accidentally close the actual <stdout>/<stderr>
if hasattr(sys.stdout, 'name') and sys.stdout.name != '<stdout>':
sys.stdout.close()
if hasattr(sys.stderr, 'name') and sys.stderr.name != '<stderr>':
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" %

View File

@ -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():