summaryrefslogtreecommitdiff
path: root/tests/test_cmd2.py
diff options
context:
space:
mode:
authorJared Crapo <jared@kotfu.net>2017-08-19 11:37:06 -0600
committerJared Crapo <jared@kotfu.net>2017-08-19 11:37:06 -0600
commit5602437e7a19b2be1133bd3290f823b222232ad3 (patch)
tree5c6bc233c28e9942ed9c8225d5694b41964e6658 /tests/test_cmd2.py
parentebc75bb1bf09f3ed0df85a5bcba587b732687a33 (diff)
downloadcmd2-git-5602437e7a19b2be1133bd3290f823b222232ad3.tar.gz
.pfeedback() now honors feedback_to_output setting
If feedback_to_output is false, .pfeedback() now sends messages to stderr instead of stdout.
Diffstat (limited to 'tests/test_cmd2.py')
-rw-r--r--tests/test_cmd2.py78
1 files changed, 57 insertions, 21 deletions
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py
index d95c19ae..4f8e1647 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,60 @@ 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')
+
+ 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
+ fd, filename = tempfile.mkstemp(prefix='cmd2_test', suffix='.txt')
+
+ 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
+ fd, filename = tempfile.mkstemp(prefix='feedback_to_output', suffix='.txt')
+
+ 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 +655,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):