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