diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2017-08-19 13:38:12 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-19 13:38:12 -0700 |
commit | f49d88a5bc5d981c78c2242a53ac34f8576b89ee (patch) | |
tree | a9d672bf19d64dc87c0f89eb827d009e00fce1d5 | |
parent | ebc75bb1bf09f3ed0df85a5bcba587b732687a33 (diff) | |
parent | a0c895b3b773dfaca3667da0db794457d0b93b03 (diff) | |
download | cmd2-git-f49d88a5bc5d981c78c2242a53ac34f8576b89ee.tar.gz |
Merge pull request #211 from kotfu/fix/feedback_to_output
.pfeedback() now honors feedback_to_output setting
-rwxr-xr-x | cmd2.py | 2 | ||||
-rw-r--r-- | tests/test_cmd2.py | 81 |
2 files changed, 61 insertions, 22 deletions
@@ -627,7 +627,7 @@ class Cmd(cmd.Cmd): if self.feedback_to_output: self.poutput(msg) else: - print(msg) + sys.stderr.write("{}\n".format(msg)) def colorize(self, val, color): """Given a string (``val``), returns that string wrapped in UNIX-style diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index d95c19ae..67dfacf4 100644 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -7,6 +7,7 @@ Released under MIT license, see LICENSE file """ import os import sys +import tempfile import mock import pytest @@ -504,25 +505,63 @@ def test_save_invalid_path(base_app, capsys): def test_output_redirection(base_app): - # TODO: Use a temporary directory/file for this file - filename = 'out.txt' - - # Verify that writing to a file works - run_cmd(base_app, 'help > {}'.format(filename)) - expected = normalize(BASE_HELP) - with open(filename) as f: - content = normalize(f.read()) - assert content == expected - - # Verify that appending to a file also works - run_cmd(base_app, 'help history >> {}'.format(filename)) - expected = normalize(BASE_HELP + '\n' + HELP_HISTORY) - with open(filename) as f: - content = normalize(f.read()) - assert content == expected - - # Delete file that was created - os.remove(filename) + fd, filename = tempfile.mkstemp(prefix='cmd2_test', suffix='.txt') + os.close(fd) + + try: + # Verify that writing to a file works + run_cmd(base_app, 'help > {}'.format(filename)) + expected = normalize(BASE_HELP) + with open(filename) as f: + content = normalize(f.read()) + assert content == expected + + # Verify that appending to a file also works + run_cmd(base_app, 'help history >> {}'.format(filename)) + expected = normalize(BASE_HELP + '\n' + HELP_HISTORY) + with open(filename) as f: + content = normalize(f.read()) + assert content == expected + except: + raise + finally: + os.remove(filename) + + +def test_feedback_to_output_true(base_app): + base_app.feedback_to_output = True + base_app.timing = True + f, filename = tempfile.mkstemp(prefix='cmd2_test', suffix='.txt') + os.close(f) + + try: + run_cmd(base_app, 'help > {}'.format(filename)) + with open(filename) as f: + content = f.readlines() + assert content[-1].startswith('Elapsed: ') + except: + raise + finally: + os.remove(filename) + + +def test_feedback_to_output_false(base_app, capsys): + base_app.feedback_to_output = False + base_app.timing = True + f, filename = tempfile.mkstemp(prefix='feedback_to_output', suffix='.txt') + os.close(f) + + try: + run_cmd(base_app, 'help > {}'.format(filename)) + out, err = capsys.readouterr() + with open(filename) as f: + content = f.readlines() + assert not content[-1].startswith('Elapsed: ') + assert err.startswith('Elapsed') + except: + raise + finally: + os.remove(filename) def test_allow_redirection(base_app): @@ -619,9 +658,9 @@ now: True assert out == expected out, err = capsys.readouterr() if sys.platform == 'win32': - assert out.startswith('Elapsed: 0:00:00') + assert err.startswith('Elapsed: 0:00:00') else: - assert out.startswith('Elapsed: 0:00:00.0') + assert err.startswith('Elapsed: 0:00:00.0') def test_base_debug(base_app, capsys): |