summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2018-06-06 23:35:28 -0400
committerTodd Leonhardt <todd.leonhardt@gmail.com>2018-06-06 23:35:28 -0400
commite7367b692bad892f27c0dd7ad04e6aa3c9f56230 (patch)
tree8e318c476b33257671e2053c9f4684cfe8a70fe8 /tests
parent794414745f7c28c4c96300117ce9a57c38d4d5b5 (diff)
downloadcmd2-git-e7367b692bad892f27c0dd7ad04e6aa3c9f56230.tar.gz
Fixed a couple potential crashes on opening files
Fixed crashes that occur when attempting to open a file in a non-existent directory or a when the filename is too long. Specifically fixed this when redirecting output to a file and when saving a transcript based on the history. Also added a couple unit tests related to the fixes.
Diffstat (limited to 'tests')
-rw-r--r--tests/test_cmd2.py38
-rw-r--r--tests/test_transcript.py26
2 files changed, 64 insertions, 0 deletions
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py
index 24a14ea2..37592da1 100644
--- a/tests/test_cmd2.py
+++ b/tests/test_cmd2.py
@@ -615,6 +615,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(FileNotFoundError):
+ 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(FileNotFoundError):
+ 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(OSError):
+ 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(OSError):
+ 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
diff --git a/tests/test_transcript.py b/tests/test_transcript.py
index 302d80c8..3caf6a37 100644
--- a/tests/test_transcript.py
+++ b/tests/test_transcript.py
@@ -154,6 +154,32 @@ this is a \/multiline\/ command
assert transcript == expected
+def test_history_transcript_bad_filename(request, capsys):
+ app = CmdLineApp()
+ app.stdout = StdOut()
+ run_cmd(app, 'orate this is\na /multiline/\ncommand;\n')
+ run_cmd(app, 'speak /tmp/file.txt is not a regex')
+
+ expected = r"""(Cmd) orate this is
+> a /multiline/
+> command;
+this is a \/multiline\/ command
+(Cmd) speak /tmp/file.txt is not a regex
+\/tmp\/file.txt is not a regex
+"""
+
+ # make a tmp file
+ history_fname = '~/fakedir/this_does_not_exist.txt'
+
+ # tell the history command to create a transcript
+ run_cmd(app, 'history -t "{}"'.format(history_fname))
+
+ # read in the transcript created by the history command
+ with pytest.raises(FileNotFoundError):
+ with open(history_fname) as f:
+ transcript = f.read()
+ assert transcript == expected
+
@pytest.mark.parametrize('expected, transformed', [
# strings with zero or one slash or with escaped slashes means no regular
# expression present, so the result should just be what re.escape returns.