Skip to content

Commit 53cded0

Browse files
committed
Refactor out shared script checker util
Needed by system_tests, so need to be in src/
1 parent 4f9e236 commit 53cded0

File tree

3 files changed

+111
-74
lines changed

3 files changed

+111
-74
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"""
2+
This private module exists to allow testing utility code to be shared between genie_python's
3+
unit tests, and the external system tests.
4+
"""
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import os
2+
import tempfile
3+
from types import TracebackType
4+
5+
from genie_python.genie_script_checker import ScriptChecker
6+
7+
8+
def write_to_temp_file(
9+
message: str, suffix: str = "", dir: str = ""
10+
) -> tempfile.NamedTemporaryFile:
11+
"""
12+
write to temporary file for test check_script
13+
14+
Args:
15+
message: message to write to file
16+
suffix: filename suffix
17+
dir: directory to write into
18+
19+
Returns:
20+
temporary file
21+
"""
22+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=suffix, dir=dir)
23+
for line in message:
24+
temp_file.write(line.encode("utf-8"))
25+
temp_file.close()
26+
return temp_file
27+
28+
29+
class CreateTempScriptAndReturnErrors:
30+
def __init__(
31+
self,
32+
script_checker: ScriptChecker,
33+
machine: str,
34+
script: str,
35+
warnings_as_error: bool = True,
36+
no_pyright: bool = False,
37+
no_pylint: bool = False,
38+
dir: str = "",
39+
) -> None:
40+
self.script = script
41+
self.machine = machine
42+
self.dir = dir
43+
self.warnings_as_error = warnings_as_error
44+
self.no_pyright = no_pyright
45+
self.no_pylint = no_pylint
46+
self.script_checker = script_checker
47+
48+
def __enter__(self) -> list[str]:
49+
self.temp_script_file = write_to_temp_file(self.script, dir=self.dir)
50+
return self.script_checker.check_script(
51+
self.temp_script_file.name,
52+
self.machine,
53+
warnings_as_error=self.warnings_as_error,
54+
no_pyright=self.no_pyright,
55+
no_pylint=self.no_pylint,
56+
)
57+
58+
def __exit__(
59+
self,
60+
exc_type: type[BaseException] | None,
61+
exc_value: BaseException | None,
62+
exc_traceback: TracebackType,
63+
) -> None:
64+
os.unlink(self.temp_script_file.name)

0 commit comments

Comments
 (0)