1
- import pytest
2
1
import io
3
2
from pathlib import Path
4
3
5
- from path_or_file_like import accepts_path_or_file_like_read , accepts_file_like_write
4
+ import pytest
5
+ from path_or_file_like import (
6
+ accepts_file_like_write ,
7
+ accepts_path_or_file_like_read ,
8
+ name_of_file_like ,
9
+ )
6
10
7
11
8
12
@pytest .fixture
9
13
def small_sample () -> str :
10
14
p = Path (__file__ ).parent .parent
11
- return str (p .joinpath (' sample.txt' ))
15
+ return str (p .joinpath (" sample.txt" ))
12
16
13
17
14
18
def test_it_reads_from_io_object (small_sample ):
@@ -17,12 +21,14 @@ def test_it_reads_from_io_object(small_sample):
17
21
18
22
assert accepts_path_or_file_like_read (io .BytesIO (r )) == "Hello World!"
19
23
24
+
20
25
def test_it_reads_from_textio_object (small_sample ):
21
26
with open (small_sample , "rt" ) as o :
22
27
r = o .read ()
23
28
24
29
assert accepts_path_or_file_like_read (io .StringIO (r )) == "Hello World!"
25
30
31
+
26
32
def test_it_reads_from_file_backed_object (small_sample ):
27
33
with open (small_sample , "rb" ) as o :
28
34
assert accepts_path_or_file_like_read (o ) == "Hello World!"
@@ -35,11 +41,12 @@ def test_it_fails_on_non_file_object():
35
41
36
42
def test_it_fails_when_write_returns_none ():
37
43
class FileLike :
38
-
39
44
def write (self , _data ):
40
45
return None
41
46
42
- with pytest .raises (OSError , match = r'write\(\) returned None, expected number of bytes written' ):
47
+ with pytest .raises (
48
+ OSError , match = r"write\(\) returned None, expected number of bytes written"
49
+ ):
43
50
accepts_file_like_write (FileLike ())
44
51
45
52
@@ -53,3 +60,13 @@ def test_it_writes_to_textio_object():
53
60
f = io .StringIO ()
54
61
accepts_file_like_write (f )
55
62
assert f .getvalue () == "Hello, world!"
63
+
64
+
65
+ def test_file_name_from_path (small_sample : str ):
66
+ with open (small_sample , "rb" ) as o :
67
+ assert name_of_file_like (o ) == small_sample
68
+
69
+
70
+ def test_file_name_from_textio ():
71
+ f = io .StringIO ()
72
+ assert name_of_file_like (f ) is None
0 commit comments