summaryrefslogtreecommitdiff
path: root/tests/test_cmd2.py
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2017-06-30 16:31:40 -0400
committerGitHub <noreply@github.com>2017-06-30 16:31:40 -0400
commit309c3eceb46a841fec43bdd72d304288e0241656 (patch)
treeff8e7f63cafa4e78723e9f8d188a23bf8269239a /tests/test_cmd2.py
parent679401b5dd314843694e92ca5c7d68e4f0f63c4d (diff)
parentaa18449d189cd7ac3c1ce4f99abff7f767d1e63d (diff)
downloadcmd2-git-309c3eceb46a841fec43bdd72d304288e0241656.tar.gz
Merge pull request #153 from kmvanbrunt/text_file
Verifying a file to be loaded as a text script is either ASCII or UTF-8
Diffstat (limited to 'tests/test_cmd2.py')
-rw-r--r--tests/test_cmd2.py56
1 files changed, 52 insertions, 4 deletions
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py
index 91e0d08a..db7f8cf7 100644
--- a/tests/test_cmd2.py
+++ b/tests/test_cmd2.py
@@ -293,6 +293,54 @@ def test_base_load_with_empty_args(base_app, capsys):
assert normalize(str(err)) == expected
+def test_base_load_with_nonexistent_file(base_app, capsys):
+ # The way the load command works, we can't directly capture its stdout or stderr
+ run_cmd(base_app, 'load does_not_exist.txt')
+ out, err = capsys.readouterr()
+
+ # The load command requires a path to an existing file
+ assert str(err).startswith("ERROR")
+ assert "does not exist or is not a file" in str(err)
+
+
+def test_base_load_with_empty_file(base_app, capsys, request):
+ test_dir = os.path.dirname(request.module.__file__)
+ filename = os.path.join(test_dir, 'scripts', 'empty.txt')
+
+ # The way the load command works, we can't directly capture its stdout or stderr
+ run_cmd(base_app, 'load {}'.format(filename))
+ out, err = capsys.readouterr()
+
+ # The load command requires non-empty scripts files
+ assert str(err).startswith("ERROR")
+ assert "is empty" in str(err)
+
+
+def test_base_load_with_binary_file(base_app, capsys, request):
+ test_dir = os.path.dirname(request.module.__file__)
+ filename = os.path.join(test_dir, 'scripts', 'binary.bin')
+
+ # The way the load command works, we can't directly capture its stdout or stderr
+ run_cmd(base_app, 'load {}'.format(filename))
+ out, err = capsys.readouterr()
+
+ # The load command requires non-empty scripts files
+ assert str(err).startswith("ERROR")
+ assert "is not an ASCII or UTF-8 encoded text file" in str(err)
+
+
+def test_base_load_with_utf8_file(base_app, capsys, request):
+ test_dir = os.path.dirname(request.module.__file__)
+ filename = os.path.join(test_dir, 'scripts', 'utf8.txt')
+
+ # The way the load command works, we can't directly capture its stdout or stderr
+ run_cmd(base_app, 'load {}'.format(filename))
+ out, err = capsys.readouterr()
+
+ # TODO Make this test better once shell command is fixed to used cmd2's stdout
+ assert str(err) == ''
+
+
def test_base_relative_load(base_app, request):
test_dir = os.path.dirname(request.module.__file__)
filename = os.path.join(test_dir, 'script.txt')
@@ -458,10 +506,10 @@ now: True
def test_base_debug(base_app, capsys):
- # Try to load a non-existent file with debug set to False by default
- run_cmd(base_app, 'load does_not_exist.txt')
+ # Try to set a non-existent parameter with debug set to False by default
+ run_cmd(base_app, 'set does_not_exist 5')
out, err = capsys.readouterr()
- assert err.startswith('ERROR')
+ assert err.startswith('EXCEPTION')
# Set debug true
out = run_cmd(base_app, 'set debug True')
@@ -472,7 +520,7 @@ now: True
assert out == expected
# Verify that we now see the exception traceback
- run_cmd(base_app, 'load does_not_exist.txt')
+ run_cmd(base_app, 'set does_not_exist 5')
out, err = capsys.readouterr()
assert str(err).startswith('Traceback (most recent call last):')