diff options
author | kotfu <kotfu@kotfu.net> | 2018-06-19 10:06:20 -0600 |
---|---|---|
committer | kotfu <kotfu@kotfu.net> | 2018-06-19 10:06:20 -0600 |
commit | 101395a437ef66846e207c039a12ee946128fab9 (patch) | |
tree | 4e024a7899396f4e111bda10f2cd8c053db12a5b /tests/test_cmd2.py | |
parent | b0a0251c77e73a3f3c0a755f3fabb9fdf136ccfa (diff) | |
parent | b5def934f4d368a7e1a1fe67a98b3cdcc14cd2d9 (diff) | |
download | cmd2-git-101395a437ef66846e207c039a12ee946128fab9.tar.gz |
Merge branch 'master' into plugin_functions
Diffstat (limited to 'tests/test_cmd2.py')
-rw-r--r-- | tests/test_cmd2.py | 42 |
1 files changed, 40 insertions, 2 deletions
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index 24a14ea2..f167793e 100644 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -230,7 +230,7 @@ def test_pyscript_with_nonexist_file(base_app, capsys): python_script = 'does_not_exist.py' run_cmd(base_app, "pyscript {}".format(python_script)) out, err = capsys.readouterr() - assert err.startswith('ERROR: [Errno 2] No such file or directory:') + assert err.startswith("EXCEPTION of type 'FileNotFoundError' occurred with message:") def test_pyscript_with_exception(base_app, capsys, request): test_dir = os.path.dirname(request.module.__file__) @@ -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 @@ -1271,7 +1309,7 @@ def test_select_invalid_option(select_app): expected = normalize(""" 1. sweet 2. salty -3 isn't a valid choice. Pick a number between 1 and 2: +'3' isn't a valid choice. Pick a number between 1 and 2: {} with sweet sauce, yum! """.format(food)) |