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