1111
1212import cmd2
1313from cmd2 .parsing import StatementParser
14- from cmd2 import utils
14+ from cmd2 import constants , utils
1515
1616@pytest .fixture
1717def parser ():
@@ -70,8 +70,8 @@ def test_parse_empty_string_default(default_parser):
7070
7171@pytest .mark .parametrize ('line,tokens' , [
7272 ('command' , ['command' ]),
73- ('command /* with some comment */ arg ' , ['command' , 'arg' ]),
74- ('command arg1 arg2 # comment at the end ' , ['command ' , 'arg1 ' , 'arg2 ' ]),
73+ (constants . COMMENT_CHAR + ' comment' , []),
74+ ('not ' + constants . COMMENT_CHAR + ' a comment ' , ['not ' , constants . COMMENT_CHAR , 'a ' , 'comment ' ]),
7575 ('termbare ; > /tmp/output' , ['termbare' , ';' , '>' , '/tmp/output' ]),
7676 ('termbare; > /tmp/output' , ['termbare' , ';' , '>' , '/tmp/output' ]),
7777 ('termbare & > /tmp/output' , ['termbare' , '&' , '>' , '/tmp/output' ]),
@@ -84,8 +84,8 @@ def test_tokenize_default(default_parser, line, tokens):
8484
8585@pytest .mark .parametrize ('line,tokens' , [
8686 ('command' , ['command' ]),
87- ('command /* with some comment */ arg ' , ['command' , 'arg' ]),
88- ('command arg1 arg2 # comment at the end ' , ['command ' , 'arg1 ' , 'arg2 ' ]),
87+ ('# comment' , []),
88+ ('not ' + constants . COMMENT_CHAR + ' a comment ' , ['not ' , constants . COMMENT_CHAR , 'a ' , 'comment ' ]),
8989 ('42 arg1 arg2' , ['theanswer' , 'arg1' , 'arg2' ]),
9090 ('l' , ['shell' , 'ls' , '-al' ]),
9191 ('termbare ; > /tmp/output' , ['termbare' , ';' , '>' , '/tmp/output' ]),
@@ -193,59 +193,23 @@ def test_parse_command_with_args_terminator_and_suffix(parser):
193193 assert statement .terminator == ';'
194194 assert statement .suffix == 'and suffix'
195195
196- def test_parse_hashcomment (parser ):
197- statement = parser .parse ('hi # this is all a comment' )
198- assert statement .command == 'hi'
199- assert statement == ''
200- assert statement .args == statement
201- assert statement .argv == ['hi' ]
202- assert not statement .arg_list
203-
204- def test_parse_c_comment (parser ):
205- statement = parser .parse ('hi /* this is | all a comment */' )
206- assert statement .command == 'hi'
207- assert statement == ''
208- assert statement .args == statement
209- assert statement .argv == ['hi' ]
210- assert not statement .arg_list
211- assert not statement .pipe_to
212-
213- def test_parse_c_comment_empty (parser ):
214- statement = parser .parse ('/* this is | all a comment */' )
196+ def test_parse_comment (parser ):
197+ statement = parser .parse (constants .COMMENT_CHAR + ' this is all a comment' )
215198 assert statement .command == ''
199+ assert statement == ''
216200 assert statement .args == statement
217- assert not statement .pipe_to
218201 assert not statement .argv
219202 assert not statement .arg_list
220- assert statement == ''
221203
222- def test_parse_c_comment_no_closing (parser ):
223- statement = parser .parse ('cat /tmp/*.txt' )
224- assert statement .command == 'cat'
225- assert statement == '/tmp/*.txt'
226- assert statement .args == statement
227- assert not statement .pipe_to
228- assert statement .argv == ['cat' , '/tmp/*.txt' ]
229- assert statement .arg_list == statement .argv [1 :]
230-
231- def test_parse_c_comment_multiple_opening (parser ):
232- statement = parser .parse ('cat /tmp/*.txt /tmp/*.cfg' )
233- assert statement .command == 'cat'
234- assert statement == '/tmp/*.txt /tmp/*.cfg'
204+ def test_parse_embedded_comment_char (parser ):
205+ command_str = 'hi ' + constants .COMMENT_CHAR + ' not a comment'
206+ statement = parser .parse (command_str )
207+ assert statement .command == 'hi'
208+ assert statement == constants .COMMENT_CHAR + ' not a comment'
235209 assert statement .args == statement
236- assert not statement .pipe_to
237- assert statement .argv == ['cat' , '/tmp/*.txt' , '/tmp/*.cfg' ]
210+ assert statement .argv == command_str .split ()
238211 assert statement .arg_list == statement .argv [1 :]
239212
240- def test_parse_what_if_quoted_strings_seem_to_start_comments (parser ):
241- statement = parser .parse ('what if "quoted strings /* seem to " start comments?' )
242- assert statement .command == 'what'
243- assert statement == 'if "quoted strings /* seem to " start comments?'
244- assert statement .args == statement
245- assert statement .argv == ['what' , 'if' , 'quoted strings /* seem to ' , 'start' , 'comments?' ]
246- assert statement .arg_list == ['if' , '"quoted strings /* seem to "' , 'start' , 'comments?' ]
247- assert not statement .pipe_to
248-
249213@pytest .mark .parametrize ('line' ,[
250214 'simple | piped' ,
251215 'simple|piped' ,
@@ -411,30 +375,6 @@ def test_parse_multiline_command_ignores_redirectors_within_it(parser, line, ter
411375 assert statement .arg_list == statement .argv [1 :]
412376 assert statement .terminator == terminator
413377
414- def test_parse_multiline_with_incomplete_comment (parser ):
415- """A terminator within a comment will be ignored and won't terminate a multiline command.
416- Un-closed comments effectively comment out everything after the start."""
417- line = 'multiline command /* with unclosed comment;'
418- statement = parser .parse (line )
419- assert statement .multiline_command == 'multiline'
420- assert statement .command == 'multiline'
421- assert statement == 'command /* with unclosed comment'
422- assert statement .args == statement
423- assert statement .argv == ['multiline' , 'command' , '/*' , 'with' , 'unclosed' , 'comment' ]
424- assert statement .arg_list == statement .argv [1 :]
425- assert statement .terminator == ';'
426-
427- def test_parse_multiline_with_complete_comment (parser ):
428- line = 'multiline command /* with comment complete */ is done;'
429- statement = parser .parse (line )
430- assert statement .multiline_command == 'multiline'
431- assert statement .command == 'multiline'
432- assert statement == 'command is done'
433- assert statement .args == statement
434- assert statement .argv == ['multiline' , 'command' , 'is' , 'done' ]
435- assert statement .arg_list == statement .argv [1 :]
436- assert statement .terminator == ';'
437-
438378def test_parse_multiline_terminated_by_empty_line (parser ):
439379 line = 'multiline command ends\n \n '
440380 statement = parser .parse (line )
@@ -464,7 +404,7 @@ def test_parse_multiline_with_embedded_newline(parser, line, terminator):
464404 assert statement .arg_list == ['command' , '"with\n embedded newline"' ]
465405 assert statement .terminator == terminator
466406
467- def test_parse_multiline_ignores_terminators_in_comments (parser ):
407+ def test_parse_multiline_ignores_terminators_in_quotes (parser ):
468408 line = 'multiline command "with term; ends" now\n \n '
469409 statement = parser .parse (line )
470410 assert statement .multiline_command == 'multiline'
@@ -762,6 +702,10 @@ def test_is_valid_command_invalid(parser):
762702 valid , errmsg = parser .is_valid_command ('' )
763703 assert not valid and 'cannot be an empty string' in errmsg
764704
705+ # Start with the comment character
706+ valid , errmsg = parser .is_valid_command (constants .COMMENT_CHAR )
707+ assert not valid and 'cannot start with the comment character' in errmsg
708+
765709 # Starts with shortcut
766710 valid , errmsg = parser .is_valid_command ('!ls' )
767711 assert not valid and 'cannot start with a shortcut' in errmsg
0 commit comments