@@ -142,6 +142,34 @@ def test_base_run_pyscript(base_app, capsys, request):
142142 out , err = capsys .readouterr ()
143143 assert out == expected
144144
145+ def test_recursive_pyscript_not_allowed (base_app , capsys , request ):
146+ test_dir = os .path .dirname (request .module .__file__ )
147+ python_script = os .path .join (test_dir , 'scripts' , 'recursive.py' )
148+ expected = 'ERROR: Recursively entering interactive Python consoles is not allowed.\n '
149+
150+ run_cmd (base_app , "pyscript {}" .format (python_script ))
151+ out , err = capsys .readouterr ()
152+ assert err == expected
153+
154+ def test_pyscript_with_nonexist_file (base_app , capsys ):
155+ python_script = 'does_not_exist.py'
156+ run_cmd (base_app , "pyscript {}" .format (python_script ))
157+ out , err = capsys .readouterr ()
158+ assert err .startswith ('ERROR: [Errno 2] No such file or directory:' )
159+
160+ def test_pyscript_with_exception (base_app , capsys , request ):
161+ test_dir = os .path .dirname (request .module .__file__ )
162+ python_script = os .path .join (test_dir , 'scripts' , 'raises_exception.py' )
163+ run_cmd (base_app , "pyscript {}" .format (python_script ))
164+ out , err = capsys .readouterr ()
165+ assert err .startswith ('Traceback' )
166+ assert err .endswith ("TypeError: unsupported operand type(s) for +: 'int' and 'str'\n " )
167+
168+ def test_pyscript_requires_an_argument (base_app , capsys ):
169+ run_cmd (base_app , "pyscript" )
170+ out , err = capsys .readouterr ()
171+ assert err .startswith ('ERROR: pyscript command requires at least 1 argument ...' )
172+
145173
146174def test_base_error (base_app ):
147175 out = run_cmd (base_app , 'meow' )
@@ -957,3 +985,21 @@ def test_select_uneven_list_of_tuples(select_app):
957985
958986 # And verify the expected output to stdout
959987 assert out == expected
988+
989+ @pytest .fixture
990+ def noarglist_app ():
991+ cmd2 .set_use_arg_list (False )
992+ app = cmd2 .Cmd ()
993+ app .stdout = StdOut ()
994+ return app
995+
996+ def test_pyscript_with_noarglist (noarglist_app , capsys , request ):
997+ test_dir = os .path .dirname (request .module .__file__ )
998+ python_script = os .path .join (test_dir , '..' , 'examples' , 'scripts' , 'arg_printer.py' )
999+ expected = """Running Python script 'arg_printer.py' which was called with 2 arguments
1000+ arg 1: 'foo'
1001+ arg 2: 'bar'
1002+ """
1003+ run_cmd (noarglist_app , 'pyscript {} foo bar' .format (python_script ))
1004+ out , err = capsys .readouterr ()
1005+ assert out == expected
0 commit comments