-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathtest_cloudpath_file_io.py
More file actions
163 lines (119 loc) · 4.01 KB
/
test_cloudpath_file_io.py
File metadata and controls
163 lines (119 loc) · 4.01 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from datetime import datetime
import os
from pathlib import PurePosixPath
from time import sleep
import pytest
from cloudpathlib.exceptions import (
BuiltInOpenWriteError,
CloudPathIsADirectoryError,
DirectoryNotEmptyError,
)
def test_file_discovery(rig):
p = rig.create_cloud_path("dir_0/file0_0.txt")
assert p.exists()
p2 = rig.create_cloud_path("dir_0/not_a_file")
assert not p2.exists()
p2.touch()
assert p2.exists()
p2.unlink()
p3 = rig.create_cloud_path("dir_0/")
assert p3.exists()
assert len(list(p3.iterdir())) == 3
assert len(list(p3.glob("**/*"))) == 3
with pytest.raises(CloudPathIsADirectoryError):
p3.unlink()
with pytest.raises(DirectoryNotEmptyError):
p3.rmdir()
p3.rmtree()
assert not p3.exists()
p4 = rig.create_cloud_path("")
assert p4.exists()
assert len(list(p4.iterdir())) == 1 # only bucket/dir_1/ should still exist
assert len(list(p4.glob("**/*"))) == 4
assert list(p4.glob("**/*")) == list(p4.rglob("*"))
def test_file_read_writes(rig, tmp_path):
p = rig.create_cloud_path("dir_0/file0_0.txt")
p2 = rig.create_cloud_path("dir_0/not_a_file")
p3 = rig.create_cloud_path("")
text = "lalala" * 10_000
p.write_text(text)
assert p.read_text() == text
p2.write_text(text)
# sleep between writes to p to ensure different
# modified times
sleep(1)
p.write_bytes(p2.read_bytes())
assert p.read_text() == p2.read_text()
before_touch = datetime.now()
sleep(1)
p.touch()
assert datetime.fromtimestamp(p.stat().st_mtime) > before_touch
# no-op
p.mkdir()
assert p.etag is not None
dest = rig.create_cloud_path("dir2/new_file0_0.txt")
assert not dest.exists()
p.rename(dest)
assert dest.exists()
assert not p.exists()
p.touch()
dest.replace(p)
assert p.exists()
dl_file = tmp_path / "file"
p.download_to(dl_file)
assert dl_file.exists()
assert p.read_text() == dl_file.read_text()
dl_dir = tmp_path / "directory"
dl_dir.mkdir(parents=True, exist_ok=True)
p3.download_to(dl_dir)
cloud_rel_paths = sorted(
# CloudPath("prefix://drive/dir/file.txt")._no_prefix_no_drive = "/dir/file.txt"
[p._no_prefix_no_drive[len(rig.test_dir) + 2 :] for p in p3.glob("**/*")]
)
dled_rel_paths = sorted(
[str(PurePosixPath(p.relative_to(dl_dir))) for p in dl_dir.glob("**/*")]
)
assert cloud_rel_paths == dled_rel_paths
def test_cloud_path_download_to(rig, tmp_path):
p = rig.create_cloud_path("dir_0/file0_0.txt")
dl_dir = tmp_path
assert not (dl_dir / p.name).exists()
p.download_to(dl_dir)
assert (dl_dir / p.name).is_file()
def test_fspath(rig):
p = rig.create_cloud_path("dir_0")
assert os.fspath(p) == p.fspath
def test_os_open_read(rig):
p = rig.create_cloud_path("dir_0/file0_0.txt")
with open(p, "r") as f:
assert f.readable()
# entire function is passed as source, so check separately
# that all of the built in open write modes fail
def test_os_open_write0(rig):
p = rig.create_cloud_path("dir_0/file0_0.txt")
with pytest.raises(BuiltInOpenWriteError):
with open(p, "w") as f:
assert f.readable()
def test_os_open_write1(rig):
p = rig.create_cloud_path("dir_0/file0_0.txt")
with pytest.raises(BuiltInOpenWriteError):
with open(p, "wb") as f:
assert f.readable()
def test_os_open_write2(rig):
p = rig.create_cloud_path("dir_0/file0_0.txt")
with pytest.raises(BuiltInOpenWriteError):
with open(p, "a") as f:
assert f.readable()
def test_os_open_write3(rig):
p = rig.create_cloud_path("dir_0/file0_0.txt")
with pytest.raises(BuiltInOpenWriteError):
with open(p, "r+") as f:
assert f.readable()
def test_os_open_write4(rig):
p = rig.create_cloud_path("dir_0/file0_0.txt")
with pytest.raises(BuiltInOpenWriteError):
with open(
p,
"w",
) as f:
assert f.readable()