forked from lra/mackup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_test.py
319 lines (254 loc) · 10.6 KB
/
utils_test.py
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import os
import tempfile
import unittest
import stat
# from unittest.mock import patch
from mackup import utils
def convert_to_octal(file_name):
"""
Using os.stat, returns file permissions (read, write, execute) as an octal.
"""
return oct(os.stat(file_name)[stat.ST_MODE])[-3:]
class TestMackup(unittest.TestCase):
def test_confirm_yes(self):
# Override the input used in utils
def custom_input(_):
return "Yes"
utils.input = custom_input
assert utils.confirm("Answer Yes to this question")
def test_confirm_no(self):
# Override the input used in utils
def custom_input(_):
return "No"
utils.input = custom_input
assert not utils.confirm("Answer No to this question")
def test_confirm_typo(self):
# Override the input used in utils
def custom_input(_):
return "No"
utils.input = custom_input
assert not utils.confirm("Answer garbage to this question")
def test_delete_file(self):
# Create a tmp file
tfile = tempfile.NamedTemporaryFile(delete=False)
tfpath = tfile.name
tfile.close()
# Make sure the created file exists
assert os.path.isfile(tfpath)
# Check if mackup can really delete it
utils.delete(tfpath)
assert not os.path.exists(tfpath)
def test_delete_folder_recursively(self):
# Create a tmp folder
tfpath = tempfile.mkdtemp()
# Let's put a file in it just for fun
tfile = tempfile.NamedTemporaryFile(dir=tfpath, delete=False)
filepath = tfile.name
tfile.close()
# Let's put another folder in it
subfolder_path = tempfile.mkdtemp(dir=tfpath)
# And a file in the subfolder
tfile = tempfile.NamedTemporaryFile(dir=subfolder_path, delete=False)
subfilepath = tfile.name
tfile.close()
# Make sure the created files and folders exists
assert os.path.isdir(tfpath)
assert os.path.isfile(filepath)
assert os.path.isdir(subfolder_path)
assert os.path.isfile(subfilepath)
# Check if mackup can really delete it
utils.delete(tfpath)
assert not os.path.exists(tfpath)
assert not os.path.exists(filepath)
assert not os.path.exists(subfolder_path)
assert not os.path.exists(subfilepath)
def test_copy_file(self):
# Create a tmp file
tfile = tempfile.NamedTemporaryFile(delete=False)
srcfile = tfile.name
tfile.close()
# Create a tmp folder
dstpath = tempfile.mkdtemp()
# Set the destination filename
dstfile = os.path.join(dstpath, "subfolder", os.path.basename(srcfile))
# Make sure the source file and destination folder exist and the
# destination file doesn't yet exist
assert os.path.isfile(srcfile)
assert os.path.isdir(dstpath)
assert not os.path.exists(dstfile)
# Check if mackup can copy it
utils.copy(srcfile, dstfile)
assert os.path.isfile(srcfile)
assert os.path.isdir(dstpath)
assert os.path.exists(dstfile)
# Let's clean up
utils.delete(dstpath)
def test_copy_fail(self):
# Create a tmp FIFO file
tfile = tempfile.NamedTemporaryFile()
srcfile = tfile.name
tfile.close()
os.mkfifo(srcfile)
# Create a tmp folder
dstpath = tempfile.mkdtemp()
# Set the destination filename
dstfile = os.path.join(dstpath, "subfolder", os.path.basename(srcfile))
# Make sure the source file and destination folder exist and the
# destination file doesn't yet exist
assert not os.path.isfile(srcfile)
assert stat.S_ISFIFO(os.stat(srcfile).st_mode)
assert os.path.isdir(dstpath)
assert not os.path.exists(dstfile)
# Check if mackup can copy it
self.assertRaises(ValueError, utils.copy, srcfile, dstfile)
assert not os.path.isfile(srcfile)
assert stat.S_ISFIFO(os.stat(srcfile).st_mode)
assert os.path.isdir(dstpath)
assert not os.path.exists(dstfile)
# Let's clean up
utils.delete(srcfile)
utils.delete(dstpath)
def test_copy_file_to_dir(self):
"""Copies a file to a destination folder that already exists."""
# Create a tmp folder
srcpath = tempfile.mkdtemp()
# Create a tmp file
tfile = tempfile.NamedTemporaryFile(delete=False, dir=srcpath)
srcfile = tfile.name
tfile.close()
# Create a tmp folder
dstpath = tempfile.mkdtemp()
# Set the destination filename
srcpath_basename = os.path.basename(srcpath)
dstfile = os.path.join(
dstpath, "subfolder", srcpath_basename, os.path.basename(srcfile)
)
# Make sure the source file and destination folder exist and the
# destination file doesn't yet exist
assert os.path.isdir(srcpath)
assert os.path.isfile(srcfile)
assert os.path.isdir(dstpath)
assert not os.path.exists(dstfile)
# Check if mackup can copy it
utils.copy(srcfile, dstfile)
assert os.path.isdir(srcpath)
assert os.path.isfile(srcfile)
assert os.path.isdir(dstpath)
assert os.path.exists(dstfile)
# Let's clean up
utils.delete(srcpath)
utils.delete(dstpath)
def test_copy_dir(self):
"""Copies a directory recursively to the destination path."""
# Create a tmp folder
srcpath = tempfile.mkdtemp()
# Create a tmp file
tfile = tempfile.NamedTemporaryFile(delete=False, dir=srcpath)
srcfile = tfile.name
tfile.close()
# Create a tmp folder
dstpath = tempfile.mkdtemp()
# Set the destination filename
srcpath_basename = os.path.basename(srcpath)
dstfile = os.path.join(dstpath, srcpath_basename, os.path.basename(srcfile))
# Make sure the source file and destination folder exist and the
# destination file doesn't yet exist
assert os.path.isdir(srcpath)
assert os.path.isfile(srcfile)
assert os.path.isdir(dstpath)
assert not os.path.exists(dstfile)
# Check if mackup can copy it
utils.copy(srcpath, dstfile)
assert os.path.isdir(srcpath)
assert os.path.isfile(srcfile)
assert os.path.isdir(dstpath)
assert os.path.exists(dstfile)
# Let's clean up
utils.delete(srcpath)
utils.delete(dstpath)
def test_link_file(self):
# Create a tmp file
tfile = tempfile.NamedTemporaryFile(delete=False)
srcfile = tfile.name
tfile.close()
# Create a tmp folder
dstpath = tempfile.mkdtemp()
# Set the destination filename
dstfile = os.path.join(dstpath, "subfolder", os.path.basename(srcfile))
# Make sure the source file and destination folder exist and the
# destination file doesn't yet exist
assert os.path.isfile(srcfile)
assert os.path.isdir(dstpath)
assert not os.path.exists(dstfile)
# Check if mackup can link it and the link points to the correct place
utils.link(srcfile, dstfile)
assert os.path.isfile(srcfile)
assert os.path.isdir(dstpath)
assert os.path.exists(dstfile)
assert os.readlink(dstfile) == srcfile
# Let's clean up
utils.delete(dstpath)
def test_chmod_file(self):
# Create a tmp file
tfile = tempfile.NamedTemporaryFile(delete=False)
file_name = tfile.name
# Create a tmp directory with a sub folder
dir_name = tempfile.mkdtemp()
nested_dir = tempfile.mkdtemp(dir=dir_name)
# # File Tests
# Change the tmp file stats to S_IWRITE (200), write access only
os.chmod(file_name, stat.S_IWRITE)
assert convert_to_octal(file_name) == "200"
# Check to make sure that utils.chmod changes the bits to 600,
# which is read and write access for the owner
utils.chmod(file_name)
assert convert_to_octal(file_name) == "600"
# # Directory Tests
# Change the tmp folder stats to S_IREAD (400), read access only
os.chmod(dir_name, stat.S_IREAD)
assert convert_to_octal(dir_name) == "400"
# Check to make sure that utils.chmod changes the bits of all
# directories to 700, which is read, write, and execute access for the
# owner
utils.chmod(dir_name)
assert convert_to_octal(dir_name) == "700"
assert convert_to_octal(nested_dir) == "700"
# Use an "unsupported file type". In this case, /dev/null
self.assertRaises(ValueError, utils.chmod, os.devnull)
def test_error(self):
test_string = "Hello World"
self.assertRaises(SystemExit, utils.error, test_string)
def test_failed_backup_location(self):
"""
Tests for the error that should occur if the backup folder cannot be
found for Dropbox and Google
"""
# Hack to make our home folder some temporary folder
temp_home = tempfile.mkdtemp()
utils.os.environ["HOME"] = temp_home
# Check for the missing Dropbox folder
assert not os.path.exists(os.path.join(temp_home, ".dropbox/host.db"))
self.assertRaises(SystemExit, utils.get_dropbox_folder_location)
# Check for the missing Google Drive folder
assert not os.path.exists(
os.path.join(
temp_home, "Library/Application Support/Google/Drive/sync_config.db"
)
)
self.assertRaises(SystemExit, utils.get_google_drive_folder_location)
def test_is_process_running(self):
# A pgrep that has one letter and a wildcard will always return id 1
assert utils.is_process_running("a*")
assert not utils.is_process_running("some imaginary process")
def test_can_file_be_synced_on_current_platform(self):
# Any file path will do, even if it doesn't exist
path = "some/file"
# Force the Mac OSX Test using lambda magic
utils.platform.system = lambda *args: utils.constants.PLATFORM_DARWIN
assert utils.can_file_be_synced_on_current_platform(path)
# Force the Linux Test using lambda magic
utils.platform.system = lambda *args: utils.constants.PLATFORM_LINUX
assert utils.can_file_be_synced_on_current_platform(path)
# Try to use the library path on Linux, which shouldn't work
path = os.path.join(os.environ["HOME"], "Library/")
assert not utils.can_file_be_synced_on_current_platform(path)