Skip to content

OsOperations::get_tempdir() is added #254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions testgres/operations/local_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,3 +528,10 @@ def is_port_free(self, number: int) -> bool:
return True
except OSError:
return False

def get_tempdir(self) -> str:
r = tempfile.gettempdir()
assert r is not None
assert type(r) == str # noqa: E721
assert os.path.exists(r)
return r
3 changes: 3 additions & 0 deletions testgres/operations/os_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,6 @@ def get_process_children(self, pid):
def is_port_free(self, number: int):
assert type(number) == int # noqa: E721
raise NotImplementedError()

def get_tempdir(self) -> str:
raise NotImplementedError()
28 changes: 28 additions & 0 deletions testgres/operations/remote_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,34 @@ def is_port_free(self, number: int) -> bool:
out=output
)

def get_tempdir(self) -> str:
command = ["mktemp", "-u", "-d"]

exec_exitcode, exec_output, exec_error = self.exec_command(
command,
verbose=True,
encoding=get_default_encoding(),
ignore_errors=True
)

assert type(exec_exitcode) == int # noqa: E721
assert type(exec_output) == str # noqa: E721
assert type(exec_error) == str # noqa: E721

if exec_exitcode != 0:
RaiseError.CommandExecutionError(
cmd=command,
exit_code=exec_exitcode,
message="Could not detect a temporary directory.",
error=exec_error,
out=exec_output)

temp_subdir = exec_output.strip()
assert type(temp_subdir) == str # noqa: E721
temp_dir = os.path.dirname(temp_subdir)
assert type(temp_dir) == str # noqa: E721
return temp_dir

@staticmethod
def _is_port_free__process_0(error: str) -> bool:
assert type(error) == str # noqa: E721
Expand Down
33 changes: 33 additions & 0 deletions tests/test_os_ops_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,39 @@ def LOCAL_server(s: socket.socket):
if ok_count == 0:
raise RuntimeError("No one free port was found.")

def test_get_tmpdir(self, os_ops: OsOperations):
assert isinstance(os_ops, OsOperations)

dir = os_ops.get_tempdir()
assert type(dir) == str # noqa: E721
assert os_ops.path_exists(dir)
assert os.path.exists(dir)

file_path = os.path.join(dir, "testgres--" + uuid.uuid4().hex + ".tmp")

os_ops.write(file_path, "1234", binary=False)

assert os_ops.path_exists(file_path)
assert os.path.exists(file_path)

d = os_ops.read(file_path, binary=False)

assert d == "1234"

os_ops.remove_file(file_path)

assert not os_ops.path_exists(file_path)
assert not os.path.exists(file_path)

def test_get_tmpdir__compare_with_py_info(self, os_ops: OsOperations):
assert isinstance(os_ops, OsOperations)

actual_dir = os_ops.get_tempdir()
assert actual_dir is not None
assert type(actual_dir) == str # noqa: E721
expected_dir = str(tempfile.tempdir)
assert actual_dir == expected_dir

class tagData_OS_OPS__NUMS:
os_ops_descr: OsOpsDescr
nums: int
Expand Down