Make 'args' optional to /run/code

This commit is contained in:
Jim Paris 2013-07-21 19:49:30 -04:00
parent 40fd377a38
commit d7551bde0b
2 changed files with 16 additions and 2 deletions

View File

@ -138,12 +138,14 @@ class AppRun(object):
@cherrypy.tools.json_out()
@exception_to_httperror(nilmrun.processmanager.ProcessError)
@cherrypy.tools.CORS_allow(methods = ["POST"])
def code(self, code, args):
def code(self, code, args = None):
"""Execute arbitrary Python code. 'code' is a formatted string.
It will be run as if it were written into a Python file and
executed. 'args' is a list of strings, and they are passed
on the command line as additional arguments (i.e., they end up
in sys.argv[1:])"""
if args is None:
args = []
if not isinstance(args, list):
raise cherrypy.HTTPError("400 Bad Request",
"args must be a list of strings")

View File

@ -256,7 +256,10 @@ class TestClient(object):
eq_(client.get("process/list"), [])
def do(code, args, kill):
pid = client.post("run/code", { "code": code, "args": args } )
if args is not None:
pid = client.post("run/code", { "code": code, "args": args } )
else:
pid = client.post("run/code", { "code": code } )
eq_(client.get("process/list"), [pid])
if kill:
return self.wait_kill(client, pid)
@ -321,6 +324,15 @@ class TestClient(object):
eq_(status["log"], "hello\n")
ne_(status["exitcode"], 0)
# default arguments are empty
code = textwrap.dedent("""
import sys
print 'args:', len(sys.argv[1:])
""")
status = do(code, None, False)
eq_(status["log"], "args: 0\n")
eq_(status["exitcode"], 0)
def test_client_08_bad_types(self):
client = HTTPClient(baseurl = testurl, post_json = True)