Skip to content

Commit 5602437

Browse files
committed
.pfeedback() now honors feedback_to_output setting
If feedback_to_output is false, .pfeedback() now sends messages to stderr instead of stdout.
1 parent ebc75bb commit 5602437

2 files changed

Lines changed: 58 additions & 22 deletions

File tree

cmd2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ def pfeedback(self, msg):
627627
if self.feedback_to_output:
628628
self.poutput(msg)
629629
else:
630-
print(msg)
630+
sys.stderr.write("{}\n".format(msg))
631631

632632
def colorize(self, val, color):
633633
"""Given a string (``val``), returns that string wrapped in UNIX-style

tests/test_cmd2.py

Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"""
88
import os
99
import sys
10+
import tempfile
1011

1112
import mock
1213
import pytest
@@ -504,25 +505,60 @@ def test_save_invalid_path(base_app, capsys):
504505

505506

506507
def test_output_redirection(base_app):
507-
# TODO: Use a temporary directory/file for this file
508-
filename = 'out.txt'
509-
510-
# Verify that writing to a file works
511-
run_cmd(base_app, 'help > {}'.format(filename))
512-
expected = normalize(BASE_HELP)
513-
with open(filename) as f:
514-
content = normalize(f.read())
515-
assert content == expected
516-
517-
# Verify that appending to a file also works
518-
run_cmd(base_app, 'help history >> {}'.format(filename))
519-
expected = normalize(BASE_HELP + '\n' + HELP_HISTORY)
520-
with open(filename) as f:
521-
content = normalize(f.read())
522-
assert content == expected
523-
524-
# Delete file that was created
525-
os.remove(filename)
508+
fd, filename = tempfile.mkstemp(prefix='cmd2_test', suffix='.txt')
509+
510+
try:
511+
# Verify that writing to a file works
512+
run_cmd(base_app, 'help > {}'.format(filename))
513+
expected = normalize(BASE_HELP)
514+
with open(filename) as f:
515+
content = normalize(f.read())
516+
assert content == expected
517+
518+
# Verify that appending to a file also works
519+
run_cmd(base_app, 'help history >> {}'.format(filename))
520+
expected = normalize(BASE_HELP + '\n' + HELP_HISTORY)
521+
with open(filename) as f:
522+
content = normalize(f.read())
523+
assert content == expected
524+
except:
525+
raise
526+
finally:
527+
os.remove(filename)
528+
529+
530+
def test_feedback_to_output_true(base_app):
531+
base_app.feedback_to_output = True
532+
base_app.timing = True
533+
fd, filename = tempfile.mkstemp(prefix='cmd2_test', suffix='.txt')
534+
535+
try:
536+
run_cmd(base_app, 'help > {}'.format(filename))
537+
with open(filename) as f:
538+
content = f.readlines()
539+
assert content[-1].startswith('Elapsed: ')
540+
except:
541+
raise
542+
finally:
543+
os.remove(filename)
544+
545+
546+
def test_feedback_to_output_false(base_app, capsys):
547+
base_app.feedback_to_output = False
548+
base_app.timing = True
549+
fd, filename = tempfile.mkstemp(prefix='feedback_to_output', suffix='.txt')
550+
551+
try:
552+
run_cmd(base_app, 'help > {}'.format(filename))
553+
out, err = capsys.readouterr()
554+
with open(filename) as f:
555+
content = f.readlines()
556+
assert not content[-1].startswith('Elapsed: ')
557+
assert err.startswith('Elapsed')
558+
except:
559+
raise
560+
finally:
561+
os.remove(filename)
526562

527563

528564
def test_allow_redirection(base_app):
@@ -619,9 +655,9 @@ def test_base_timing(base_app, capsys):
619655
assert out == expected
620656
out, err = capsys.readouterr()
621657
if sys.platform == 'win32':
622-
assert out.startswith('Elapsed: 0:00:00')
658+
assert err.startswith('Elapsed: 0:00:00')
623659
else:
624-
assert out.startswith('Elapsed: 0:00:00.0')
660+
assert err.startswith('Elapsed: 0:00:00.0')
625661

626662

627663
def test_base_debug(base_app, capsys):

0 commit comments

Comments
 (0)