The Gaudi Framework  master (37c0b60a)
FixtureResult.py
Go to the documentation of this file.
1 
11 import datetime
12 import subprocess
13 from typing import List, Union
14 
15 
16 class ProcessTimeoutError(Exception):
17  def __init__(self, message, stack_trace):
18  super().__init__(message)
19  self.stack_trace = stack_trace
20 
21 
22 class ExceededStreamError(Exception):
23  def __init__(self, message, exceeded_stream):
24  super().__init__(message)
25  self.exceeded_stream = exceeded_stream
26 
27 
29  """
30  A class to encapsulate the results of a subprocess execution in a test fixture.
31  """
32 
33  def __init__(
34  self,
35  completed_process: subprocess.CompletedProcess,
36  start_time: datetime,
37  end_time: datetime,
38  run_exception: Union[ProcessTimeoutError, ExceededStreamError, None],
39  command: List[str],
40  expanded_command: List[str],
41  env: dict,
42  cwd: str,
43  ):
44  self.completed_process = completed_process
45  self.start_time = start_time
46  self.end_time = end_time
47  self.run_exception = run_exception
48  self.command = command
49  self.expanded_command = expanded_command
50  self.runtime_environment = env
51  self.cwd = cwd
52 
53  def elapsed_time(self):
54  return self.end_time - self.start_time
55 
56  @staticmethod
57  def format_value(value):
58  """
59  Format a given value to a string representation.
60  """
61  if isinstance(value, list):
62  return repr(value)
63  elif isinstance(value, dict):
64  return "\n".join(f"{k}={v}" for k, v in value.items())
65  return str(value or "")
66 
67  def to_dict(self) -> dict:
68  """
69  Convert the attributes of the FixtureResult into a dictionary.
70  """
71  properties = {attr: getattr(self, attr) for attr in vars(self)}
72  properties.update(
73  {
74  "stdout": self.completed_process.stdout.decode(
75  "utf-8", errors="backslashreplace"
76  )
77  if self.completed_process.stdout
78  else None,
79  "stderr": self.completed_process.stderr.decode(
80  "utf-8", errors="backslashreplace"
81  )
82  if self.completed_process.stderr
83  else None,
84  "return_code": self.completed_process.returncode
85  if self.completed_process.returncode
86  else None,
87  "elapsed_time": self.elapsed_time(),
88  "stack_trace": self.run_exception.stack_trace
89  if hasattr(self.run_exception, "stack_trace")
90  else None,
91  "stream_exceeded": self.run_exception.exceeded_stream
92  if hasattr(self.run_exception, "exceeded_stream")
93  else None,
94  }
95  )
96  formatted_properties = {
97  key: self.format_value(value) for key, value in properties.items()
98  }
99  return formatted_properties
GaudiTesting.FixtureResult.ProcessTimeoutError
Definition: FixtureResult.py:16
GaudiTesting.FixtureResult.FixtureResult.to_dict
dict to_dict(self)
Definition: FixtureResult.py:67
GaudiTesting.FixtureResult.FixtureResult.elapsed_time
def elapsed_time(self)
Definition: FixtureResult.py:53
GaudiTesting.FixtureResult.ExceededStreamError.__init__
def __init__(self, message, exceeded_stream)
Definition: FixtureResult.py:23
GaudiTesting.FixtureResult.FixtureResult.__init__
def __init__(self, subprocess.CompletedProcess completed_process, datetime start_time, datetime end_time, Union[ProcessTimeoutError, ExceededStreamError, None] run_exception, List[str] command, List[str] expanded_command, dict env, str cwd)
Definition: FixtureResult.py:33
GaudiTesting.FixtureResult.FixtureResult.completed_process
completed_process
Definition: FixtureResult.py:34
GaudiTesting.FixtureResult.FixtureResult.expanded_command
expanded_command
Definition: FixtureResult.py:39
GaudiTesting.FixtureResult.ExceededStreamError
Definition: FixtureResult.py:22
GaudiTesting.FixtureResult.ProcessTimeoutError.__init__
def __init__(self, message, stack_trace)
Definition: FixtureResult.py:17
GaudiTesting.FixtureResult.FixtureResult.runtime_environment
runtime_environment
Definition: FixtureResult.py:40
GaudiTesting.FixtureResult.FixtureResult
Definition: FixtureResult.py:28
GaudiTesting.FixtureResult.FixtureResult.cwd
cwd
Definition: FixtureResult.py:41
GaudiTesting.FixtureResult.ProcessTimeoutError.stack_trace
stack_trace
Definition: FixtureResult.py:19
GaudiTesting.FixtureResult.ExceededStreamError.exceeded_stream
exceeded_stream
Definition: FixtureResult.py:25
GaudiTesting.FixtureResult.FixtureResult.format_value
def format_value(value)
Definition: FixtureResult.py:57
GaudiTesting.FixtureResult.FixtureResult.end_time
end_time
Definition: FixtureResult.py:36
GaudiTesting.FixtureResult.FixtureResult.command
command
Definition: FixtureResult.py:38
GaudiTesting.FixtureResult.FixtureResult.start_time
start_time
Definition: FixtureResult.py:35
GaudiTesting.FixtureResult.FixtureResult.run_exception
run_exception
Definition: FixtureResult.py:37