summaryrefslogtreecommitdiff
path: root/tests/test_cmd2.py
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2018-06-07 00:26:35 -0400
committerTodd Leonhardt <todd.leonhardt@gmail.com>2018-06-07 00:26:35 -0400
commit91a69cb3a6c4882aa6c2f953582444e5918aca07 (patch)
treec2354f1cbf98be74c8e86aaa062ef443cec4ea76 /tests/test_cmd2.py
parent05e15985e4a593c8b14151c705315e8985cff00e (diff)
downloadcmd2-git-91a69cb3a6c4882aa6c2f953582444e5918aca07.tar.gz
Backported file open crash fixes for Issue #430 to python2 branch
Also: - Bumped version on python2 branch to 0.8.8 - Updated CHANGELOG
Diffstat (limited to 'tests/test_cmd2.py')
-rw-r--r--tests/test_cmd2.py40
1 files changed, 39 insertions, 1 deletions
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py
index 66e4d601..17577e2b 100644
--- a/tests/test_cmd2.py
+++ b/tests/test_cmd2.py
@@ -26,7 +26,7 @@ from conftest import run_cmd, normalize, BASE_HELP, BASE_HELP_VERBOSE, \
def test_ver():
- assert cmd2.__version__ == '0.8.7'
+ assert cmd2.__version__ == '0.8.8'
def test_empty_statement(base_app):
@@ -553,6 +553,44 @@ def test_output_redirection(base_app):
finally:
os.remove(filename)
+def test_output_redirection_to_nonexistent_directory(base_app):
+ filename = '~/fakedir/this_does_not_exist.txt'
+
+ # Verify that writing to a file in a non-existent directory doesn't work
+ run_cmd(base_app, 'help > {}'.format(filename))
+ expected = normalize(BASE_HELP)
+ with pytest.raises(cmd2.FILE_NOT_FOUND_ERROR):
+ 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 pytest.raises(cmd2.FILE_NOT_FOUND_ERROR):
+ with open(filename) as f:
+ content = normalize(f.read())
+ assert content == expected
+
+def test_output_redirection_to_too_long_filename(base_app):
+ filename = '~/sdkfhksdjfhkjdshfkjsdhfkjsdhfkjdshfkjdshfkjshdfkhdsfkjhewfuihewiufhweiufhiweufhiuewhiuewhfiuwehfiuewhfiuewhfiuewhfiuewhiuewhfiuewhfiuewfhiuwehewiufhewiuhfiweuhfiuwehfiuewfhiuwehiuewfhiuewhiewuhfiuewhfiuwefhewiuhewiufhewiufhewiufhewiufhewiufhewiufhewiufhewiuhewiufhewiufhewiuheiufhiuewheiwufhewiufheiufheiufhieuwhfewiuhfeiufhiuewfhiuewheiwuhfiuewhfiuewhfeiuwfhewiufhiuewhiuewhfeiuwhfiuwehfuiwehfiuehiuewhfieuwfhieufhiuewhfeiuwfhiuefhueiwhfw'
+
+ # Verify that writing to a file in a non-existent directory doesn't work
+ run_cmd(base_app, 'help > {}'.format(filename))
+ expected = normalize(BASE_HELP)
+ with pytest.raises(IOError):
+ 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 pytest.raises(IOError):
+ with open(filename) as f:
+ content = normalize(f.read())
+ assert content == expected
+
def test_feedback_to_output_true(base_app):
base_app.feedback_to_output = True