Skip to content

Commit edbdb9d

Browse files
Merge pull request #173 from dmitry-lipetsk/D20241225_004--read_binary-offset
OsOps::read_binary is updated (offset)
2 parents 989e209 + 2c1dd97 commit edbdb9d

File tree

5 files changed

+35
-11
lines changed

5 files changed

+35
-11
lines changed

Diff for: testgres/operations/local_ops.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -331,13 +331,15 @@ def readlines(self, filename, num_lines=0, binary=False, encoding=None):
331331
buffers * max(2, int(num_lines / max(cur_lines, 1)))
332332
) # Adjust buffer size
333333

334-
def read_binary(self, filename, start_pos):
334+
def read_binary(self, filename, offset):
335335
assert type(filename) == str # noqa: E721
336-
assert type(start_pos) == int # noqa: E721
337-
assert start_pos >= 0
336+
assert type(offset) == int # noqa: E721
337+
338+
if offset < 0:
339+
raise ValueError("Negative 'offset' is not supported.")
338340

339341
with open(filename, 'rb') as file: # open in a binary mode
340-
file.seek(start_pos, os.SEEK_SET)
342+
file.seek(offset, os.SEEK_SET)
341343
r = file.read()
342344
assert type(r) == bytes # noqa: E721
343345
return r

Diff for: testgres/operations/os_ops.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,10 @@ def read(self, filename, encoding, binary):
9898
def readlines(self, filename):
9999
raise NotImplementedError()
100100

101-
def read_binary(self, filename, start_pos):
101+
def read_binary(self, filename, offset):
102102
assert type(filename) == str # noqa: E721
103-
assert type(start_pos) == int # noqa: E721
104-
assert start_pos >= 0
103+
assert type(offset) == int # noqa: E721
104+
assert offset >= 0
105105
raise NotImplementedError()
106106

107107
def isfile(self, remote_file):

Diff for: testgres/operations/remote_ops.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -370,12 +370,14 @@ def readlines(self, filename, num_lines=0, binary=False, encoding=None):
370370

371371
return lines
372372

373-
def read_binary(self, filename, start_pos):
373+
def read_binary(self, filename, offset):
374374
assert type(filename) == str # noqa: E721
375-
assert type(start_pos) == int # noqa: E721
376-
assert start_pos >= 0
375+
assert type(offset) == int # noqa: E721
377376

378-
cmd = ["tail", "-c", "+{}".format(start_pos + 1), filename]
377+
if offset < 0:
378+
raise ValueError("Negative 'offset' is not supported.")
379+
380+
cmd = ["tail", "-c", "+{}".format(offset + 1), filename]
379381
r = self.exec_command(cmd)
380382
assert type(r) == bytes # noqa: E721
381383
return r

Diff for: tests/test_local.py

+10
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,16 @@ def test_read_binary__spec__unk_file(self):
162162
match=re.escape("[Errno 2] No such file or directory: '/dummy'")):
163163
self.operations.read_binary("/dummy", 0)
164164

165+
def test_read_binary__spec__negative_offset(self):
166+
"""
167+
Test LocalOperations::read_binary with negative offset.
168+
"""
169+
170+
with pytest.raises(
171+
ValueError,
172+
match=re.escape("Negative 'offset' is not supported.")):
173+
self.operations.read_binary(__file__, -1)
174+
165175
def test_get_file_size(self):
166176
"""
167177
Test LocalOperations::get_file_size.

Diff for: tests/test_remote.py

+10
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,16 @@ def test_read_binary__spec__unk_file(self):
288288
with pytest.raises(ExecUtilException, match=re.escape("tail: cannot open '/dummy' for reading: No such file or directory")):
289289
self.operations.read_binary("/dummy", 0)
290290

291+
def test_read_binary__spec__negative_offset(self):
292+
"""
293+
Test RemoteOperations::read_binary with negative offset.
294+
"""
295+
296+
with pytest.raises(
297+
ValueError,
298+
match=re.escape("Negative 'offset' is not supported.")):
299+
self.operations.read_binary(__file__, -1)
300+
291301
def test_get_file_size(self):
292302
"""
293303
Test RemoteOperations::get_file_size.

0 commit comments

Comments
 (0)