Skip to content

Commit f49d88a

Browse files
authored
Merge pull request #211 from kotfu/fix/feedback_to_output
.pfeedback() now honors feedback_to_output setting
2 parents ebc75bb + a0c895b commit f49d88a

2 files changed

Lines changed: 61 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: 60 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,63 @@ 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+
os.close(fd)
510+
511+
try:
512+
# Verify that writing to a file works
513+
run_cmd(base_app, 'help > {}'.format(filename))
514+
expected = normalize(BASE_HELP)
515+
with open(filename) as f:
516+
content = normalize(f.read())
517+
assert content == expected
518+
519+
# Verify that appending to a file also works
520+
run_cmd(base_app, 'help history >> {}'.format(filename))
521+
expected = normalize(BASE_HELP + '\n' + HELP_HISTORY)
522+
with open(filename) as f:
523+
content = normalize(f.read())
524+
assert content == expected
525+
except:
526+
raise
527+
finally:
528+
os.remove(filename)
529+
530+
531+
def test_feedback_to_output_true(base_app):
532+
base_app.feedback_to_output = True
533+
base_app.timing = True
534+
f, filename = tempfile.mkstemp(prefix='cmd2_test', suffix='.txt')
535+
os.close(f)
536+
537+
try:
538+
run_cmd(base_app, 'help > {}'.format(filename))
539+
with open(filename) as f:
540+
content = f.readlines()
541+
assert content[-1].startswith('Elapsed: ')
542+
except:
543+
raise
544+
finally:
545+
os.remove(filename)
546+
547+
548+
def test_feedback_to_output_false(base_app, capsys):
549+
base_app.feedback_to_output = False
550+
base_app.timing = True
551+
f, filename = tempfile.mkstemp(prefix='feedback_to_output', suffix='.txt')
552+
os.close(f)
553+
554+
try:
555+
run_cmd(base_app, 'help > {}'.format(filename))
556+
out, err = capsys.readouterr()
557+
with open(filename) as f:
558+
content = f.readlines()
559+
assert not content[-1].startswith('Elapsed: ')
560+
assert err.startswith('Elapsed')
561+
except:
562+
raise
563+
finally:
564+
os.remove(filename)
526565

527566

528567
def test_allow_redirection(base_app):
@@ -619,9 +658,9 @@ def test_base_timing(base_app, capsys):
619658
assert out == expected
620659
out, err = capsys.readouterr()
621660
if sys.platform == 'win32':
622-
assert out.startswith('Elapsed: 0:00:00')
661+
assert err.startswith('Elapsed: 0:00:00')
623662
else:
624-
assert out.startswith('Elapsed: 0:00:00.0')
663+
assert err.startswith('Elapsed: 0:00:00.0')
625664

626665

627666
def test_base_debug(base_app, capsys):

0 commit comments

Comments
 (0)