Skip to content

Commit

Permalink
Further Resolve Issue 653
Browse files Browse the repository at this point in the history
  • Loading branch information
derks committed Jan 2, 2024
1 parent 25d114b commit ea32057
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 19 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 12 additions & 3 deletions cement/core/foundation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 <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" %
Expand Down
51 changes: 37 additions & 14 deletions tests/core/test_foundation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down

0 comments on commit ea32057

Please sign in to comment.