From 02381f7f8717d4f11b8a11bfab4e805be4e26d99 Mon Sep 17 00:00:00 2001 From: "d.kovalenko" Date: Mon, 3 Mar 2025 19:28:28 +0300 Subject: [PATCH] RemoteOperations::path_exists is updated - command is passed through list - we process all the result codes of test --- testgres/operations/remote_ops.py | 26 ++++++++++++++++++++++++-- tests/test_remote.py | 26 ++++++++++++++++---------- 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/testgres/operations/remote_ops.py b/testgres/operations/remote_ops.py index dc392bee..60d5265c 100644 --- a/testgres/operations/remote_ops.py +++ b/testgres/operations/remote_ops.py @@ -247,8 +247,30 @@ def listdir(self, path): return result.splitlines() def path_exists(self, path): - result = self.exec_command("test -e {}; echo $?".format(path), encoding=get_default_encoding()) - return int(result.strip()) == 0 + command = ["test", "-e", path] + + exit_status, output, error = self.exec_command(cmd=command, encoding=get_default_encoding(), ignore_errors=True, verbose=True) + + assert type(output) == str # noqa: E721 + assert type(error) == str # noqa: E721 + + if exit_status == 0: + return True + + if exit_status == 1: + return False + + errMsg = "Test operation returns an unknown result code: {0}. Path is [{1}].".format( + exit_status, + path) + + RaiseError.CommandExecutionError( + cmd=command, + exit_code=exit_status, + msg_arg=errMsg, + error=error, + out=output + ) @property def pathsep(self): diff --git a/tests/test_remote.py b/tests/test_remote.py index 6114e29e..85e65c24 100755 --- a/tests/test_remote.py +++ b/tests/test_remote.py @@ -130,23 +130,29 @@ def test_listdir(self): assert isinstance(files, list) - def test_path_exists_true(self): + def test_path_exists_true__directory(self): """ - Test path_exists for an existing path. + Test path_exists for an existing directory. """ - path = "/etc" - response = self.operations.path_exists(path) + assert self.operations.path_exists("/etc") is True - assert response is True + def test_path_exists_true__file(self): + """ + Test path_exists for an existing file. + """ + assert self.operations.path_exists(__file__) is True - def test_path_exists_false(self): + def test_path_exists_false__directory(self): """ - Test path_exists for a non-existing path. + Test path_exists for a non-existing directory. """ - path = "/nonexistent_path" - response = self.operations.path_exists(path) + assert self.operations.path_exists("/nonexistent_path") is False - assert response is False + def test_path_exists_false__file(self): + """ + Test path_exists for a non-existing file. + """ + assert self.operations.path_exists("/etc/nonexistent_path.txt") is False def test_write_text_file(self): """