Skip to content
This repository has been archived by the owner on Aug 20, 2018. It is now read-only.

Commit

Permalink
Bug 917550 - [mozprocess] ProcessHandler commandline attr broken;r=wlach
Browse files Browse the repository at this point in the history
Consistently store `cmd` as a string and `args` as a list. Throw an
error when `args` are over-specified. Document the complete usage of the
`cmd` and `args` options.
  • Loading branch information
jugglinmike authored and wlach committed Nov 7, 2013
1 parent 0d7441b commit a03f6f2
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
17 changes: 9 additions & 8 deletions mozprocess/mozprocess/processhandler.py
Expand Up @@ -31,8 +31,8 @@ class ProcessHandlerMixin(object):
"""
A class for launching and manipulating local processes.
:param cmd: command to run.
:param args: is a list of arguments to pass to the command (defaults to None).
:param cmd: command to run. May be a string or a list. If specified as a list, the first element will be interpreted as the command, and all additional elements will be interpreted as arguments to that command.
:param args: list of arguments to pass to the command (defaults to None). Must not be set when `cmd` is specified as a list.
:param cwd: working directory for command (defaults to None).
:param env: is the environment to use for the process (defaults to os.environ).
:param ignore_children: causes system to ignore child processes when True, defaults to False (which tracks child processes).
Expand Down Expand Up @@ -582,11 +582,12 @@ def __init__(self,

# It is common for people to pass in the entire array with the cmd and
# the args together since this is how Popen uses it. Allow for that.
if not isinstance(self.cmd, list):
self.cmd = [self.cmd]

if self.args:
self.cmd = self.cmd + self.args
if isinstance(self.cmd, list):
if self.args != None:
raise TypeError("cmd and args must not both be lists")
(self.cmd, self.args) = (self.cmd[0], self.cmd[1:])
elif self.args is None:
self.args = []

@property
def timedOut(self):
Expand Down Expand Up @@ -624,7 +625,7 @@ def run(self, timeout=None, outputTimeout=None):
args.update(self.keywordargs)

# launch the process
self.proc = self.Process(self.cmd, **args)
self.proc = self.Process([self.cmd] + self.args, **args)

self.processOutput(timeout=timeout, outputTimeout=outputTimeout)

Expand Down
42 changes: 42 additions & 0 deletions mozprocess/tests/test_mozprocess.py
Expand Up @@ -136,6 +136,48 @@ def test_process_normal_finish(self):
p.proc.returncode,
p.didTimeout)

def test_commandline_no_args(self):
"""Command line is reported correctly when no arguments are specified"""
p = processhandler.ProcessHandler(self.proclaunch, cwd=here)
self.assertEqual(p.commandline, self.proclaunch)

def test_commandline_overspecified(self):
"""Command line raises an exception when the arguments are specified ambiguously"""
err = None
try:
p = processhandler.ProcessHandler([self.proclaunch, "process_normal_finish.ini"],
args=["1", "2", "3"],
cwd=here)
except TypeError, e:
err = e

self.assertTrue(err)

def test_commandline_from_list(self):
"""Command line is reported correctly when command and arguments are specified in a list"""
p = processhandler.ProcessHandler([self.proclaunch, "process_normal_finish.ini"],
cwd=here)
self.assertEqual(p.commandline, self.proclaunch + ' process_normal_finish.ini')

def test_commandline_over_specified(self):
"""Command line raises an exception when the arguments are specified ambiguously"""
err = None
try:
p = processhandler.ProcessHandler([self.proclaunch, "process_normal_finish.ini"],
args=["1", "2", "3"],
cwd=here)
except TypeError, e:
err = e

self.assertTrue(err)

def test_commandline_from_args(self):
"""Command line is reported correctly when arguments are specified in a dedicated list"""
p = processhandler.ProcessHandler(self.proclaunch,
args=["1", "2", "3"],
cwd=here)
self.assertEqual(p.commandline, self.proclaunch + ' 1 2 3')

def test_process_wait(self):
"""Process is started runs to completion while we wait indefinitely"""

Expand Down

0 comments on commit a03f6f2

Please sign in to comment.