summaryrefslogtreecommitdiff
path: root/tests/test_parsing.py
diff options
context:
space:
mode:
authorkotfu <kotfu@kotfu.net>2018-05-07 21:01:56 -0600
committerkotfu <kotfu@kotfu.net>2018-05-07 21:01:56 -0600
commitf5f0c90aa44ec658b33da422c4f0dc1cea2e6b98 (patch)
treed9971edb68c475428d306ecb276e58dc15fca5d2 /tests/test_parsing.py
parentd6d92949784b223ad03f70999bc55518c92d9fc9 (diff)
downloadcmd2-git-f5f0c90aa44ec658b33da422c4f0dc1cea2e6b98.tar.gz
Make alias checking and command parsing use the same regex
Provide a new is_valid_command() method on StatementParser to determine whether a string of characters could be a valid command. That means it can’t include any redirection, quote chars, whitespace, or terminator characters. This method is used when someone tries to create an alias, to ensure when we try and parse the alias that it will actually parse. This nicely encapsulates and standardizes all the logic for parsing and expansion into the StatementParser class. Also fix a bug in the regex to match valid command names, and add a bunch of new unit tests to ensure the bug stays fixed.
Diffstat (limited to 'tests/test_parsing.py')
-rw-r--r--tests/test_parsing.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/tests/test_parsing.py b/tests/test_parsing.py
index ad4d31cd..bfb55b23 100644
--- a/tests/test_parsing.py
+++ b/tests/test_parsing.py
@@ -439,3 +439,27 @@ def test_parse_command_only_quoted_args(parser):
assert statement.command == 'shell'
assert statement.args == 'ls -al "/tmp/directory with spaces/doit.sh"'
assert statement.command_and_args == line.replace('l', 'shell ls -al')
+
+@pytest.mark.parametrize('line', [
+ 'helpalias > out.txt',
+ 'helpalias>out.txt',
+ 'helpalias >> out.txt',
+ 'helpalias>>out.txt',
+ 'help|less',
+ 'helpalias;',
+])
+def test_parse_command_only_specialchars(parser, line):
+ statement = parser.parse_command_only(line)
+ assert statement.command == 'help'
+
+@pytest.mark.parametrize('line', [
+ ';',
+ '>',
+ "'",
+ '"',
+ '|',
+])
+def test_parse_command_only_none(parser, line):
+ statement = parser.parse_command_only(line)
+ assert statement.command == None
+ assert statement.args == None