forked from urnest/urnest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileReader.py.test
122 lines (107 loc) · 3.84 KB
/
FileReader.py.test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python3
# Copyright (c) 2022 Trevor Taylor
# coding: utf-8
#
# Permission to use, copy, modify, and/or distribute this software for
# any purpose with or without fee is hereby granted, provided that all
# copyright notices and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
from sys import path
from os.path import dirname
if path[0]==dirname(__file__): path.pop(0)
from xju.cmc.io import FileReader, FilePosition
from pathlib import Path
from xju.assert_ import Assert
from xju.xn import readable_repr
from xju.misc import ByteCount
from xju.patch import PatchAttr
import os
from io import TextIOWrapper,FileIO
f:FileReader
try:
with FileReader(Path("xxx.txt")) as f:
pass
except Exception as e:
Assert(readable_repr(e))=="Failed to open xxx.txt reader with close-on-exec True because\n[Errno 2] No such file or directory: 'xxx.txt'."
else:
assert False, f'should not be here with {f}'
pass
f2:TextIOWrapper
with open('xxx.txt','w') as f2:
f2.write('fredward')
pass
with FileReader(Path("xxx.txt")) as f:
Assert(f.input.readall().decode('utf-8'))=="fredward"
Assert(f.size())==ByteCount(8)
Assert(f.seek_by(ByteCount(-2)).read(ByteCount(2)))==b'rd'
Assert(f.position())==FilePosition(8)
Assert(f.seek_to(FilePosition(2)).read(ByteCount(3)))==b'edw'
# seeking past end is not an error
Assert(f.seek_to(FilePosition(9)).position())==FilePosition(9)
try:
f.seek_to(FilePosition(-1))
except Exception as e:
Assert(readable_repr(e).replace('\n',' ')).matches(
'Failed to position so next read occurs -1 bytes from start of file because.* Invalid argument.')
else:
assert False, 'should not b here'
pass
try:
f.seek_by(ByteCount(-11))
except Exception as e:
Assert(readable_repr(e))=='Failed to position xxx.txt reader with close-on-exec True so next read occurs -11 bytes from current position because\n[Errno 22] Invalid argument.'
else:
assert False, 'should not b here'
pass
assert not hasattr(f,'input')
def raise_some_error(*args,**kwargs):
raise Exception('some error')
try:
with PatchAttr(os,'close',raise_some_error):
with FileReader(Path("xxx.txt")) as f:
pass
pass
pass
except Exception as e:
Assert(readable_repr(e))=="Failed to close xxx.txt reader with close-on-exec True because\nsome error."
pass
with FileReader(Path("xxx.txt")) as f:
try:
with PatchAttr(os,'fstat',raise_some_error):
f.size()
except Exception as e:
Assert(readable_repr(e))=="Failed to return size of xxx.txt reader with close-on-exec True's file because\nsome error."
else:
assert False
pass
pass
with FileReader(Path("xxx.txt")) as f:
try:
with PatchAttr(FilePosition,'__init__',raise_some_error):
f.position()
except Exception as e:
Assert(readable_repr(e))=="Failed to get current position of xxx.txt reader with close-on-exec True because\nsome error."
else:
assert False
pass
pass
with FileReader(Path("xxx.txt")) as f:
c=ByteCount(1)
with PatchAttr(ByteCount,'value',raise_some_error):
try:
f.read(c)
except Exception as e:
Assert('some error').isIn(readable_repr(e))
else:
assert False
pass
pass
pass