Commit 82012dd284257ce785b1e3996a9a2519e8a4b303

Authored by Simon Glass
1 parent 48ba5856eb

patman: Provide a way to intercept commands for testing

Add a test point for the command module. This allows tests to emulate
the execution of commands. This provides more control (since we can make
the fake 'commands' do whatever we like), makes it faster to write tests
since we don't need to set up as much environment, and speeds up test
execution.

Signed-off-by: Simon Glass <sjg@chromium.org>

Showing 1 changed file with 20 additions and 0 deletions Side-by-side Diff

tools/patman/command.py
... ... @@ -20,10 +20,26 @@
20 20 def __init__(self):
21 21 self.stdout = None
22 22 self.stderr = None
  23 + self.combined = None
23 24 self.return_code = None
24 25 self.exception = None
25 26  
  27 + def __init__(self, stdout='', stderr='', combined='', return_code=0,
  28 + exception=None):
  29 + self.stdout = stdout
  30 + self.stderr = stderr
  31 + self.combined = combined
  32 + self.return_code = return_code
  33 + self.exception = exception
26 34  
  35 +
  36 +# This permits interception of RunPipe for test purposes. If it is set to
  37 +# a function, then that function is called with the pipe list being
  38 +# executed. Otherwise, it is assumed to be a CommandResult object, and is
  39 +# returned as the result for every RunPipe() call.
  40 +# When this value is None, commands are executed as normal.
  41 +test_result = None
  42 +
27 43 def RunPipe(pipe_list, infile=None, outfile=None,
28 44 capture=False, capture_stderr=False, oneline=False,
29 45 raise_on_error=True, cwd=None, **kwargs):
... ... @@ -44,6 +60,10 @@
44 60 Returns:
45 61 CommandResult object
46 62 """
  63 + if test_result:
  64 + if hasattr(test_result, '__call__'):
  65 + return test_result(pipe_list=pipe_list)
  66 + return test_result
47 67 result = CommandResult()
48 68 last_pipe = None
49 69 pipeline = list(pipe_list)