Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit88371d1

Browse files
OsOperations::read_binary(self, filename, start_pos) is added
It is a specialized function to read binary data from files.
1 parent85d2aa3 commit88371d1

File tree

5 files changed

+124
-0
lines changed

5 files changed

+124
-0
lines changed

‎testgres/operations/local_ops.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,17 @@ def readlines(self, filename, num_lines=0, binary=False, encoding=None):
308308
buffers*max(2,int(num_lines/max(cur_lines,1)))
309309
)# Adjust buffer size
310310

311+
defread_binary(self,filename,start_pos):
312+
asserttype(filename)==str# noqa: E721
313+
asserttype(start_pos)==int# noqa: E721
314+
assertstart_pos>=0
315+
316+
withopen(filename,'rb')asfile:# open in a binary mode
317+
file.seek(start_pos,os.SEEK_SET)
318+
r=file.read()
319+
asserttype(r)==bytes# noqa: E721
320+
returnr
321+
311322
defisfile(self,remote_file):
312323
returnos.path.isfile(remote_file)
313324

‎testgres/operations/os_ops.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ def read(self, filename, encoding, binary):
9898
defreadlines(self,filename):
9999
raiseNotImplementedError()
100100

101+
defread_binary(self,filename,start_pos):
102+
asserttype(filename)==str# noqa: E721
103+
asserttype(start_pos)==int# noqa: E721
104+
assertstart_pos>=0
105+
raiseNotImplementedError()
106+
101107
defisfile(self,remote_file):
102108
raiseNotImplementedError()
103109

‎testgres/operations/remote_ops.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,16 @@ def readlines(self, filename, num_lines=0, binary=False, encoding=None):
340340

341341
returnlines
342342

343+
defread_binary(self,filename,start_pos):
344+
asserttype(filename)==str# noqa: E721
345+
asserttype(start_pos)==int# noqa: E721
346+
assertstart_pos>=0
347+
348+
cmd="tail -c +{} {}".format(start_pos+1,__class__._escape_path(filename))
349+
r=self.exec_command(cmd)
350+
asserttype(r)==bytes# noqa: E721
351+
returnr
352+
343353
defisfile(self,remote_file):
344354
stdout=self.exec_command("test -f {}; echo $?".format(remote_file))
345355
result=int(stdout.strip())
@@ -386,6 +396,15 @@ def db_connect(self, dbname, user, password=None, host="localhost", port=5432):
386396
)
387397
returnconn
388398

399+
def_escape_path(path):
400+
asserttype(path)==str# noqa: E721
401+
assertpath!=""# Ok?
402+
403+
r="'"
404+
r+=path
405+
r+="'"
406+
returnr
407+
389408

390409
defnormalize_error(error):
391410
ifisinstance(error,bytes):

‎tests/test_local.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
importos
2+
13
importpytest
4+
importre
25

36
fromtestgresimportExecUtilException
47
fromtestgresimportLocalOperations
@@ -52,3 +55,45 @@ def test_exec_command_failure__expect_error(self):
5255
asserterror==b'/bin/sh: 1: nonexistent_command: not found\n'
5356
assertexit_status==127
5457
assertresult==b''
58+
59+
deftest_read_binary__spec(self):
60+
"""
61+
Test LocalOperations::read_binary.
62+
"""
63+
filename=__file__# current file
64+
65+
withopen(filename,'rb')asfile:# open in a binary mode
66+
response0=file.read()
67+
68+
asserttype(response0)==bytes# noqa: E721
69+
70+
response1=self.operations.read_binary(filename,0)
71+
asserttype(response1)==bytes# noqa: E721
72+
assertresponse1==response0
73+
74+
response2=self.operations.read_binary(filename,1)
75+
asserttype(response2)==bytes# noqa: E721
76+
assertlen(response2)<len(response1)
77+
assertlen(response2)+1==len(response1)
78+
assertresponse2==response1[1:]
79+
80+
response3=self.operations.read_binary(filename,len(response1))
81+
asserttype(response3)==bytes# noqa: E721
82+
assertlen(response3)==0
83+
84+
response4=self.operations.read_binary(filename,len(response2))
85+
asserttype(response4)==bytes# noqa: E721
86+
assertlen(response4)==1
87+
assertresponse4[0]==response1[len(response1)-1]
88+
89+
response5=self.operations.read_binary(filename,len(response1)+1)
90+
asserttype(response5)==bytes# noqa: E721
91+
assertlen(response5)==0
92+
93+
deftest_read_binary__spec__unk_file(self):
94+
"""
95+
Test LocalOperations::read_binary with unknown file.
96+
"""
97+
98+
withpytest.raises(FileNotFoundError,match=re.escape("[Errno 2] No such file or directory: '/dummy'")):
99+
self.operations.read_binary("/dummy",0)

‎tests/test_remote.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
importos
22

33
importpytest
4+
importre
45

56
fromtestgresimportExecUtilException
67
fromtestgresimportRemoteOperations
@@ -181,6 +182,48 @@ def test_read_binary_file(self):
181182

182183
assertisinstance(response,bytes)
183184

185+
deftest_read_binary__spec(self):
186+
"""
187+
Test RemoteOperations::read_binary.
188+
"""
189+
filename=__file__# currnt file
190+
191+
withopen(filename,'rb')asfile:# open in a binary mode
192+
response0=file.read()
193+
194+
asserttype(response0)==bytes# noqa: E721
195+
196+
response1=self.operations.read_binary(filename,0)
197+
asserttype(response1)==bytes# noqa: E721
198+
assertresponse1==response0
199+
200+
response2=self.operations.read_binary(filename,1)
201+
asserttype(response2)==bytes# noqa: E721
202+
assertlen(response2)<len(response1)
203+
assertlen(response2)+1==len(response1)
204+
assertresponse2==response1[1:]
205+
206+
response3=self.operations.read_binary(filename,len(response1))
207+
asserttype(response3)==bytes# noqa: E721
208+
assertlen(response3)==0
209+
210+
response4=self.operations.read_binary(filename,len(response2))
211+
asserttype(response4)==bytes# noqa: E721
212+
assertlen(response4)==1
213+
assertresponse4[0]==response1[len(response1)-1]
214+
215+
response5=self.operations.read_binary(filename,len(response1)+1)
216+
asserttype(response5)==bytes# noqa: E721
217+
assertlen(response5)==0
218+
219+
deftest_read_binary__spec__unk_file(self):
220+
"""
221+
Test RemoteOperations::read_binary with unknown file.
222+
"""
223+
224+
withpytest.raises(ExecUtilException,match=re.escape("tail: cannot open '/dummy' for reading: No such file or directory")):
225+
self.operations.read_binary("/dummy",0)
226+
184227
deftest_touch(self):
185228
"""
186229
Test touch for creating a new file or updating access and modification times of an existing file.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp