summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2019-07-24 12:09:24 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2019-07-24 12:09:24 -0400
commit35e084f64f715f90e2bfba232a299301b9c5d850 (patch)
tree68d40fccd78b2aa63d4aed60ef124ba43a90b45d /tests
parentf3325cb39e9e20aa24d07107ebf048029059b44f (diff)
downloadcmd2-git-35e084f64f715f90e2bfba232a299301b9c5d850.tar.gz
Added unit tests
Diffstat (limited to 'tests')
-rw-r--r--tests/test_cmd2.py34
-rw-r--r--tests/test_run_pyscript.py25
-rw-r--r--tests/test_utils.py10
3 files changed, 65 insertions, 4 deletions
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py
index 1bd81c50..d9c971b5 100644
--- a/tests/test_cmd2.py
+++ b/tests/test_cmd2.py
@@ -656,6 +656,36 @@ def test_edit_file(base_app, request, monkeypatch):
# We think we have an editor, so should expect a Popen call
m.assert_called_once()
+def test_edit_file_with_odd_file_names(base_app, monkeypatch):
+ """Test editor and file names with various patterns"""
+ # Mock out the do_shell call to see what args are passed
+ shell_mock = mock.MagicMock(name='do_shell')
+ monkeypatch.setattr("cmd2.Cmd.do_shell", shell_mock)
+
+ base_app.editor = 'fooedit'
+ python_script = utils.quote_string('nothingweird.py')
+ out, err = run_cmd(base_app, "edit {}".format(python_script))
+ shell_mock.assert_called_once_with('"fooedit" "nothingweird.py"')
+ shell_mock.reset_mock()
+
+ base_app.editor = 'foo edit'
+ python_script = utils.quote_string('has spaces.py')
+ out, err = run_cmd(base_app, "edit {}".format(python_script))
+ shell_mock.assert_called_once_with('"foo edit" "has spaces.py"')
+ shell_mock.reset_mock()
+
+ base_app.editor = '"fooedit"'
+ python_script = utils.quote_string('"is_double_quoted.py"')
+ out, err = run_cmd(base_app, "edit {}".format(python_script))
+ shell_mock.assert_called_once_with('\'"fooedit"\' \'"is_double_quoted.py"\'')
+ shell_mock.reset_mock()
+
+ base_app.editor = "'fooedit'"
+ python_script = utils.quote_string("'is_single_quoted.py'")
+ out, err = run_cmd(base_app, "edit {}".format(python_script))
+ shell_mock.assert_called_once_with('"\'fooedit\'" "\'is_single_quoted.py\'"')
+ shell_mock.reset_mock()
+
def test_edit_file_with_spaces(base_app, request, monkeypatch):
# Set a fake editor just to make sure we have one. We aren't really going to call it due to the mock
base_app.editor = 'fooedit'
@@ -986,7 +1016,7 @@ class SelectApp(cmd2.Cmd):
def do_procrastinate(self, arg):
"""Waste time in your manner of choice."""
# Pass in a list of tuples for selections
- leisure_activity = self.select([('Netflix and chill', 'Netflix'), ('Porn', 'WebSurfing')],
+ leisure_activity = self.select([('Netflix and chill', 'Netflix'), ('YouTube', 'WebSurfing')],
'How would you like to procrastinate? ')
result = 'Have fun procrasinating with {}!\n'.format(leisure_activity)
self.stdout.write(result)
@@ -1098,7 +1128,7 @@ def test_select_list_of_tuples(select_app):
1. Netflix
2. WebSurfing
Have fun procrasinating with {}!
-""".format('Porn'))
+""".format('YouTube'))
# Make sure our mock was called with the expected arguments
m.assert_called_once_with('How would you like to procrastinate? ')
diff --git a/tests/test_run_pyscript.py b/tests/test_run_pyscript.py
index 15cdd7be..60a6798e 100644
--- a/tests/test_run_pyscript.py
+++ b/tests/test_run_pyscript.py
@@ -6,7 +6,7 @@ Unit/functional testing for run_pytest in cmd2
import builtins
import os
-from cmd2 import plugin
+from cmd2 import plugin, utils
from .conftest import run_cmd
# Python 3.5 had some regressions in the unitest.mock module, so use 3rd party mock if available
@@ -52,6 +52,27 @@ def test_run_pyscript_with_non_python_file(base_app, request):
out, err = run_cmd(base_app, 'run_pyscript {}'.format(filename))
assert "does not have a .py extension" in err[0]
+def test_run_pyscript_with_odd_file_names(base_app):
+ """
+ Pass in file names with various patterns. Since these files don't exist, we will rely
+ on the error text to make sure the file names were processed correctly.
+ """
+ python_script = utils.quote_string('nothingweird.py')
+ out, err = run_cmd(base_app, "run_pyscript {}".format(python_script))
+ assert "'nothingweird.py'" in err[0]
+
+ python_script = utils.quote_string('has spaces.py')
+ out, err = run_cmd(base_app, "run_pyscript {}".format(python_script))
+ assert "'has spaces.py'" in err[0]
+
+ python_script = utils.quote_string('"is_double_quoted.py"')
+ out, err = run_cmd(base_app, "run_pyscript {}".format(python_script))
+ assert "'\"is_double_quoted.py\"'" in err[0]
+
+ python_script = utils.quote_string("'is_single_quoted.py'")
+ out, err = run_cmd(base_app, "run_pyscript {}".format(python_script))
+ assert "''is_single_quoted.py''" in err[0]
+
def test_run_pyscript_with_exception(base_app, request):
test_dir = os.path.dirname(request.module.__file__)
python_script = os.path.join(test_dir, 'pyscript', 'raises_exception.py')
@@ -107,4 +128,4 @@ def test_run_pyscript_run(base_app, request):
expected = 'I have been run'
out, err = run_cmd(base_app, "run_pyscript {}".format(python_script))
- assert expected in out \ No newline at end of file
+ assert expected in out
diff --git a/tests/test_utils.py b/tests/test_utils.py
index c32d6870..1890a753 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -93,6 +93,16 @@ def test_is_quoted_no():
simple_str = "hello world"
assert not cu.is_quoted(simple_str)
+def test_quote_string():
+ my_str = "Hello World"
+ assert cu.quote_string(my_str) == '"' + my_str + '"'
+
+ my_str = "'Hello World'"
+ assert cu.quote_string(my_str) == '"' + my_str + '"'
+
+ my_str = '"Hello World"'
+ assert cu.quote_string(my_str) == "'" + my_str + "'"
+
def test_quote_string_if_needed_yes():
my_str = "Hello World"
assert cu.quote_string_if_needed(my_str) == '"' + my_str + '"'