-
Notifications
You must be signed in to change notification settings - Fork 254
Expand file tree
/
Copy pathtest_fileutils.py
More file actions
286 lines (236 loc) · 10 KB
/
test_fileutils.py
File metadata and controls
286 lines (236 loc) · 10 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import doctest
import os
import sys
import time
import urllib.request
from collections import namedtuple
from io import BytesIO, UnsupportedOperation
from pathlib import Path
from tempfile import TemporaryDirectory
from unittest import mock
import pytest
import responses
import cumulusci
from cumulusci.utils import fileutils, temporary_dir, update_tree
from cumulusci.utils.fileutils import (
FSResource,
load_from_source,
open_fs_resource,
view_file,
)
class TestFileutils:
@responses.activate
@mock.patch("urllib.request.urlopen")
def test_docstrings(self, urlopen):
pretend_html = b"<!DOCTYPE HTML ...blah blah blah"
responses.add("GET", "http://www.salesforce.com/", body=pretend_html)
fake_http_stream = BytesIO(pretend_html)
fake_http_stream.url = "https://www.salesforce.com/"
urlopen.return_value = fake_http_stream
try:
doctest.testmod(fileutils, raise_on_error=True, verbose=True)
except doctest.DocTestFailure as e:
print("Got")
print(str(e.got))
raise
def test_binary_becomes_string(self):
with load_from_source(BytesIO(b"foo")) as (file, path):
data = file.read()
assert isinstance(data, str)
assert data == "foo"
def test_writable_file_throws(self):
with temporary_dir():
with open("writable", "wt") as t:
with pytest.raises(UnsupportedOperation):
with load_from_source(t) as (data, filename):
pass
@responses.activate
def test_load_from_url(self):
html = "<!DOCTYPE HTML ..."
responses.add("GET", "http://www.salesforce.com", body=html)
with load_from_source("http://www.salesforce.com") as (data, filename):
assert data.read() == html
def test_load_from_Path(self):
p = Path(cumulusci.__file__).parent / "cumulusci.yml"
with load_from_source(p) as (data, filename):
assert "tasks:" in data.read()
def test_load_from_path_string(self):
p = Path(cumulusci.__file__).parent / "cumulusci.yml"
with load_from_source(str(p)) as (data, filename):
assert "tasks:" in data.read()
def test_load_from_open_file(self):
p = Path(cumulusci.__file__).parent / "cumulusci.yml"
with open(p) as f:
with load_from_source(f) as (data, filename):
assert "tasks:" in data.read()
assert str(p) == filename
def test_load_from_fs_resource(self):
p = Path(cumulusci.__file__).parent / "cumulusci.yml"
with open_fs_resource(p) as p2:
with load_from_source(p2) as (data, filename):
assert "tasks:" in data.read()
def test_view_file_str_path(self):
"""Verify view_file works when given a path as a string"""
with mock.patch("webbrowser.open") as webbrowser_open:
path = "robot/results/index.html"
view_file(path)
url = f"file://{urllib.request.pathname2url(str(Path(path).resolve()))}"
webbrowser_open.assert_called_once_with(url)
def test_view_file_Path(self):
"""Verify view_file works when given a path as a Path object"""
with mock.patch("webbrowser.open") as webbrowser_open:
path = Path("robot/results/index.html")
view_file(path)
url = f"file://{urllib.request.pathname2url(str(path.resolve()))}"
webbrowser_open.assert_called_once_with(url)
class _TestFSResourceShared:
file = Path(__file__)
def test_resource_exists_abspath(self):
abspath = os.path.abspath(self.file)
with open_fs_resource(abspath) as resource:
assert resource.exists()
assert str(resource.getsyspath()) == abspath
def test_resource_does_not_exist(self):
abspath = os.path.abspath(self.file)
with open_fs_resource(abspath + "xyzzy") as resource:
assert not resource.exists()
assert abspath + "xyzzy" in resource
def test_resource_exists_pathlib_abspath(self):
abspath = os.path.abspath(self.file)
with open_fs_resource(Path(abspath)) as resource:
assert resource.exists()
assert abspath in resource
def test_resource_doesnt_exist_pathlib_abspath(self):
abspath = os.path.abspath(self.file)
with open_fs_resource(Path(abspath + "xyzzy")) as resource:
assert not resource.exists()
assert abspath + "xyzzy" in resource
def test_resource_exists_url_abspath(self):
abspath = os.path.abspath(self.file)
url = f"file://{abspath}"
with open_fs_resource(url) as resource:
assert resource.exists()
assert abspath in resource
def test_resource_as_str(self):
abspath = os.path.abspath(self.file)
with open_fs_resource(Path(abspath)) as resource:
assert "file://" in repr(resource)
def test_join_paths(self):
parent = Path(self.file).parent
with open_fs_resource(parent) as resource:
this_file = resource / self.file.name
assert this_file.exists()
def test_clone_fsresource(self):
abspath = os.path.abspath(self.file)
with open_fs_resource(Path(abspath)) as resource:
with open_fs_resource(resource) as resource2:
assert abspath in str(resource2)
def test_load_from_file_system(self):
abspath = os.path.abspath(self.file)
# Backwards compatibility: pass a dummy filesystem and ensure it is ignored
DummyFS = namedtuple("DummyFS", [])
fs = DummyFS()
with open_fs_resource(abspath, fs) as f:
assert abspath in str(f)
def test_windows_path(self):
abspath = "c:\\foo\\bar"
with open_fs_resource(abspath) as f:
if sys.platform == "win32":
assert str(f.getsyspath()), str(f.getsyspath()) == r"c:\foo\bar"
class TestFSResource(_TestFSResourceShared):
def test_resource_exists_relpath(self):
relpath = os.path.relpath(self.file)
with open_fs_resource(relpath) as resource:
assert resource.exists()
assert (
str(Path(resource.getsyspath()).relative_to(Path(".").absolute()))
== relpath
)
def test_resource_doesnt_exist_relpath(self):
relpath = os.path.relpath(self.file)
with open_fs_resource(relpath + "xyzzy") as resource:
assert not resource.exists()
assert relpath + "xyzzy" in resource
def test_resource_exists_pathlib_relpath(self):
relpath = os.path.relpath(self.file)
with open_fs_resource(Path(relpath)) as resource:
assert resource.exists()
assert relpath in resource
def test_resource_exists_url_relpath(self):
relpath = os.path.relpath(self.file)
url = f"file://{relpath}"
with open_fs_resource(url) as resource:
assert resource.exists()
assert relpath in resource
def test_resource_test_resource_doesnt_exist_pathlib_relpath(self):
relpath = os.path.relpath(self.file)
with open_fs_resource(Path(relpath + "xyzzy")) as resource:
assert not resource.exists()
assert relpath + "xyzzy" in resource
class TestFSResourceTempdir(_TestFSResourceShared):
def setup_method(self):
self.tempdir = TemporaryDirectory()
self.file = Path(self.tempdir.name) / "testfile.txt"
self.file.touch()
def teardown(self):
self.tempdir.cleanup()
def test_copy_to(self):
abspath = os.path.abspath(self.file)
with open_fs_resource(abspath) as f:
f.copy_to(abspath + ".bak")
assert Path(abspath + ".bak").exists()
f.copy_to(Path(abspath + ".bak2"))
assert Path(abspath + ".bak2").exists()
f.copy_to(FSResource.new(abspath + ".bak3"))
assert Path(abspath + ".bak3").exists()
def test_mkdir_rmdir(self):
abspath = Path(self.tempdir.name) / "doesnotexist"
assert not abspath.exists()
with open_fs_resource(abspath) as f:
f.mkdir()
assert abspath.exists()
f.mkdir(exist_ok=True)
f.rmdir()
abspath = abspath / "foo" / "bar" / "baz"
assert not abspath.exists()
with open_fs_resource(abspath) as f:
f.mkdir(parents=True)
f.mkdir(parents=True, exist_ok=True)
f.mkdir(parents=False, exist_ok=True)
assert abspath.exists()
with pytest.raises(FileExistsError):
f.mkdir(parents=False, exist_ok=False)
f.rmdir()
def test_open_for_write(self):
abspath = Path(self.tempdir.name) / "blah"
with open_fs_resource(abspath) as fs:
fs.mkdir()
newfile = fs / "newfile"
with newfile.open("w") as f:
f.write("xyzzy")
with newfile.open("r") as f:
assert f.read() == "xyzzy"
class TestFSResourceError:
def test_fs_resource_init_error(self):
with pytest.raises(NotImplementedError):
FSResource()
def test_update_tree(tmpdir):
source_dir = Path(tmpdir.mkdir("source"))
source_file = source_dir / "testfile.txt"
source_file.write_text("original content")
dest_dir = Path(tmpdir.mkdir("dest"))
dest_file = dest_dir / "testfile.txt"
dest_file.write_text("modified content")
# Ensure the source file has an older timestamp
past_time = time.time() - 100
os.utime(str(source_file), (past_time, past_time))
update_tree(source_dir, dest_dir)
assert dest_file.read_text() == "modified content"
# Add a new file to source and run update_tree again
new_source_file = source_dir / "newfile.txt"
new_source_file.write_text("new file content")
update_tree(source_dir, dest_dir)
# Verify that the new file is copied to destination
new_dest_file = dest_dir / "newfile.txt"
assert new_dest_file.exists()
assert new_dest_file.read_text() == "new file content"