diff --git a/testgres/operations/local_ops.py b/testgres/operations/local_ops.py index 82d1711d..c88c16ca 100644 --- a/testgres/operations/local_ops.py +++ b/testgres/operations/local_ops.py @@ -10,6 +10,7 @@ import psutil from ..exceptions import ExecUtilException +from ..exceptions import InvalidOperationException from .os_ops import ConnectionParams, OsOperations, pglib, get_default_encoding from .raise_error import RaiseError from .helpers import Helpers @@ -266,13 +267,35 @@ def touch(self, filename): os.utime(filename, None) def read(self, filename, encoding=None, binary=False): - mode = "rb" if binary else "r" - with open(filename, mode) as file: + assert type(filename) == str # noqa: E721 + assert encoding is None or type(encoding) == str # noqa: E721 + assert type(binary) == bool # noqa: E721 + + if binary: + if encoding is not None: + raise InvalidOperationException("Enconding is not allowed for read binary operation") + + return self._read__binary(filename) + + # python behavior + assert (None or "abc") == "abc" + assert ("" or "abc") == "abc" + + return self._read__text_with_encoding(filename, encoding or get_default_encoding()) + + def _read__text_with_encoding(self, filename, encoding): + assert type(filename) == str # noqa: E721 + assert type(encoding) == str # noqa: E721 + with open(filename, mode='r', encoding=encoding) as file: # open in a text mode + content = file.read() + assert type(content) == str # noqa: E721 + return content + + def _read__binary(self, filename): + assert type(filename) == str # noqa: E721 + with open(filename, 'rb') as file: # open in a binary mode content = file.read() - if binary: - return content - if isinstance(content, bytes): - return content.decode(encoding or get_default_encoding()) + assert type(content) == bytes # noqa: E721 return content def readlines(self, filename, num_lines=0, binary=False, encoding=None): diff --git a/testgres/operations/remote_ops.py b/testgres/operations/remote_ops.py index 128a2a21..c0307195 100644 --- a/testgres/operations/remote_ops.py +++ b/testgres/operations/remote_ops.py @@ -3,6 +3,7 @@ import platform import subprocess import tempfile +import io # we support both pg8000 and psycopg2 try: @@ -14,6 +15,7 @@ raise ImportError("You must have psycopg2 or pg8000 modules installed") from ..exceptions import ExecUtilException +from ..exceptions import InvalidOperationException from .os_ops import OsOperations, ConnectionParams, get_default_encoding from .raise_error import RaiseError from .helpers import Helpers @@ -319,13 +321,39 @@ def touch(self, filename): self.exec_command("touch {}".format(filename)) def read(self, filename, binary=False, encoding=None): - cmd = "cat {}".format(filename) - result = self.exec_command(cmd, encoding=encoding) + assert type(filename) == str # noqa: E721 + assert encoding is None or type(encoding) == str # noqa: E721 + assert type(binary) == bool # noqa: E721 - if not binary and result: - result = result.decode(encoding or get_default_encoding()) + if binary: + if encoding is not None: + raise InvalidOperationException("Enconding is not allowed for read binary operation") - return result + return self._read__binary(filename) + + # python behavior + assert (None or "abc") == "abc" + assert ("" or "abc") == "abc" + + return self._read__text_with_encoding(filename, encoding or get_default_encoding()) + + def _read__text_with_encoding(self, filename, encoding): + assert type(filename) == str # noqa: E721 + assert type(encoding) == str # noqa: E721 + content = self._read__binary(filename) + assert type(content) == bytes # noqa: E721 + buf0 = io.BytesIO(content) + buf1 = io.TextIOWrapper(buf0, encoding=encoding) + content_s = buf1.read() + assert type(content_s) == str # noqa: E721 + return content_s + + def _read__binary(self, filename): + assert type(filename) == str # noqa: E721 + cmd = ["cat", filename] + content = self.exec_command(cmd) + assert type(content) == bytes # noqa: E721 + return content def readlines(self, filename, num_lines=0, binary=False, encoding=None): if num_lines > 0: diff --git a/tests/test_local.py b/tests/test_local.py index e223b090..47a63994 100644 --- a/tests/test_local.py +++ b/tests/test_local.py @@ -4,6 +4,7 @@ import re from testgres import ExecUtilException +from testgres import InvalidOperationException from testgres import LocalOperations from .helpers.run_conditions import RunConditions @@ -56,6 +57,67 @@ def test_exec_command_failure__expect_error(self): assert exit_status == 127 assert result == b'' + def test_read__text(self): + """ + Test LocalOperations::read for text data. + """ + filename = __file__ # current file + + with open(filename, 'r') as file: # open in a text mode + response0 = file.read() + + assert type(response0) == str # noqa: E721 + + response1 = self.operations.read(filename) + assert type(response1) == str # noqa: E721 + assert response1 == response0 + + response2 = self.operations.read(filename, encoding=None, binary=False) + assert type(response2) == str # noqa: E721 + assert response2 == response0 + + response3 = self.operations.read(filename, encoding="") + assert type(response3) == str # noqa: E721 + assert response3 == response0 + + response4 = self.operations.read(filename, encoding="UTF-8") + assert type(response4) == str # noqa: E721 + assert response4 == response0 + + def test_read__binary(self): + """ + Test LocalOperations::read for binary data. + """ + filename = __file__ # current file + + with open(filename, 'rb') as file: # open in a binary mode + response0 = file.read() + + assert type(response0) == bytes # noqa: E721 + + response1 = self.operations.read(filename, binary=True) + assert type(response1) == bytes # noqa: E721 + assert response1 == response0 + + def test_read__binary_and_encoding(self): + """ + Test LocalOperations::read for binary data and encoding. + """ + filename = __file__ # current file + + with pytest.raises( + InvalidOperationException, + match=re.escape("Enconding is not allowed for read binary operation")): + self.operations.read(filename, encoding="", binary=True) + + def test_read__unknown_file(self): + """ + Test LocalOperations::read with unknown file. + """ + + with pytest.raises(FileNotFoundError, match=re.escape("[Errno 2] No such file or directory: '/dummy'")): + self.operations.read("/dummy") + def test_read_binary__spec(self): """ Test LocalOperations::read_binary. @@ -95,7 +157,9 @@ def test_read_binary__spec__unk_file(self): Test LocalOperations::read_binary with unknown file. """ - with pytest.raises(FileNotFoundError, match=re.escape("[Errno 2] No such file or directory: '/dummy'")): + with pytest.raises( + FileNotFoundError, + match=re.escape("[Errno 2] No such file or directory: '/dummy'")): self.operations.read_binary("/dummy", 0) def test_get_file_size(self): diff --git a/tests/test_remote.py b/tests/test_remote.py index 780ad46e..7421ca3a 100755 --- a/tests/test_remote.py +++ b/tests/test_remote.py @@ -4,6 +4,7 @@ import re from testgres import ExecUtilException +from testgres import InvalidOperationException from testgres import RemoteOperations from testgres import ConnectionParams @@ -182,6 +183,69 @@ def test_read_binary_file(self): assert isinstance(response, bytes) + def test_read__text(self): + """ + Test RemoteOperations::read for text data. + """ + filename = __file__ # current file + + with open(filename, 'r') as file: # open in a text mode + response0 = file.read() + + assert type(response0) == str # noqa: E721 + + response1 = self.operations.read(filename) + assert type(response1) == str # noqa: E721 + assert response1 == response0 + + response2 = self.operations.read(filename, encoding=None, binary=False) + assert type(response2) == str # noqa: E721 + assert response2 == response0 + + response3 = self.operations.read(filename, encoding="") + assert type(response3) == str # noqa: E721 + assert response3 == response0 + + response4 = self.operations.read(filename, encoding="UTF-8") + assert type(response4) == str # noqa: E721 + assert response4 == response0 + + def test_read__binary(self): + """ + Test RemoteOperations::read for binary data. + """ + filename = __file__ # current file + + with open(filename, 'rb') as file: # open in a binary mode + response0 = file.read() + + assert type(response0) == bytes # noqa: E721 + + response1 = self.operations.read(filename, binary=True) + assert type(response1) == bytes # noqa: E721 + assert response1 == response0 + + def test_read__binary_and_encoding(self): + """ + Test RemoteOperations::read for binary data and encoding. + """ + filename = __file__ # current file + + with pytest.raises( + InvalidOperationException, + match=re.escape("Enconding is not allowed for read binary operation")): + self.operations.read(filename, encoding="", binary=True) + + def test_read__unknown_file(self): + """ + Test RemoteOperations::read with unknown file. + """ + + with pytest.raises( + ExecUtilException, + match=re.escape("cat: /dummy: No such file or directory")): + self.operations.read("/dummy") + def test_read_binary__spec(self): """ Test RemoteOperations::read_binary. pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy