Skip to content

Commit edd64db

Browse files
OsOperations::get_tempdir() is added (#254)
Signature: def get_tempdir(self) -> str
1 parent a0a8506 commit edd64db

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

testgres/operations/local_ops.py

+7
Original file line numberDiff line numberDiff line change
@@ -528,3 +528,10 @@ def is_port_free(self, number: int) -> bool:
528528
return True
529529
except OSError:
530530
return False
531+
532+
def get_tempdir(self) -> str:
533+
r = tempfile.gettempdir()
534+
assert r is not None
535+
assert type(r) == str # noqa: E721
536+
assert os.path.exists(r)
537+
return r

testgres/operations/os_ops.py

+3
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,6 @@ def get_process_children(self, pid):
130130
def is_port_free(self, number: int):
131131
assert type(number) == int # noqa: E721
132132
raise NotImplementedError()
133+
134+
def get_tempdir(self) -> str:
135+
raise NotImplementedError()

testgres/operations/remote_ops.py

+28
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,34 @@ def is_port_free(self, number: int) -> bool:
659659
out=output
660660
)
661661

662+
def get_tempdir(self) -> str:
663+
command = ["mktemp", "-u", "-d"]
664+
665+
exec_exitcode, exec_output, exec_error = self.exec_command(
666+
command,
667+
verbose=True,
668+
encoding=get_default_encoding(),
669+
ignore_errors=True
670+
)
671+
672+
assert type(exec_exitcode) == int # noqa: E721
673+
assert type(exec_output) == str # noqa: E721
674+
assert type(exec_error) == str # noqa: E721
675+
676+
if exec_exitcode != 0:
677+
RaiseError.CommandExecutionError(
678+
cmd=command,
679+
exit_code=exec_exitcode,
680+
message="Could not detect a temporary directory.",
681+
error=exec_error,
682+
out=exec_output)
683+
684+
temp_subdir = exec_output.strip()
685+
assert type(temp_subdir) == str # noqa: E721
686+
temp_dir = os.path.dirname(temp_subdir)
687+
assert type(temp_dir) == str # noqa: E721
688+
return temp_dir
689+
662690
@staticmethod
663691
def _is_port_free__process_0(error: str) -> bool:
664692
assert type(error) == str # noqa: E721

tests/test_os_ops_common.py

+33
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,39 @@ def LOCAL_server(s: socket.socket):
817817
if ok_count == 0:
818818
raise RuntimeError("No one free port was found.")
819819

820+
def test_get_tmpdir(self, os_ops: OsOperations):
821+
assert isinstance(os_ops, OsOperations)
822+
823+
dir = os_ops.get_tempdir()
824+
assert type(dir) == str # noqa: E721
825+
assert os_ops.path_exists(dir)
826+
assert os.path.exists(dir)
827+
828+
file_path = os.path.join(dir, "testgres--" + uuid.uuid4().hex + ".tmp")
829+
830+
os_ops.write(file_path, "1234", binary=False)
831+
832+
assert os_ops.path_exists(file_path)
833+
assert os.path.exists(file_path)
834+
835+
d = os_ops.read(file_path, binary=False)
836+
837+
assert d == "1234"
838+
839+
os_ops.remove_file(file_path)
840+
841+
assert not os_ops.path_exists(file_path)
842+
assert not os.path.exists(file_path)
843+
844+
def test_get_tmpdir__compare_with_py_info(self, os_ops: OsOperations):
845+
assert isinstance(os_ops, OsOperations)
846+
847+
actual_dir = os_ops.get_tempdir()
848+
assert actual_dir is not None
849+
assert type(actual_dir) == str # noqa: E721
850+
expected_dir = str(tempfile.tempdir)
851+
assert actual_dir == expected_dir
852+
820853
class tagData_OS_OPS__NUMS:
821854
os_ops_descr: OsOpsDescr
822855
nums: int

0 commit comments

Comments
 (0)