Merge lp:~nataliabidart/ubuntuone-client/unify-os-helper-tests into lp:ubuntuone-client
- unify-os-helper-tests
- Merge into trunk
Proposed by
Natalia Bidart
Status: | Merged |
---|---|
Approved by: | Alejandro J. Cura |
Approved revision: | 1098 |
Merged at revision: | 1096 |
Proposed branch: | lp:~nataliabidart/ubuntuone-client/unify-os-helper-tests |
Merge into: | lp:ubuntuone-client |
Diff against target: |
479 lines (+43/-250) 4 files modified
contrib/testing/testcase.py (+28/-30) tests/platform/linux/test_os_helper.py (+2/-207) tests/platform/test_os_helper.py (+12/-12) ubuntuone/platform/linux/os_helper.py (+1/-1) |
To merge this branch: | bzr merge lp:~nataliabidart/ubuntuone-client/unify-os-helper-tests |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Alejandro J. Cura (community) | Approve | ||
Manuel de la Peña (community) | Approve | ||
Review via email:
|
Commit message
- Unify tests for os_helper in every platform (LP: #823223).
Description of the change
To post a comment you must log in.
- 1096. By Natalia Bidart
-
_testMethod is not defined in setUp.
- 1097. By Natalia Bidart
-
Merged trunk in.
- 1098. By Natalia Bidart
-
Clarification.
Revision history for this message
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
Alejandro J. Cura (alecu) wrote : | # |
Works as expected.
review:
Approve
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'contrib/testing/testcase.py' |
2 | --- contrib/testing/testcase.py 2011-08-04 19:17:11 +0000 |
3 | +++ contrib/testing/testcase.py 2011-08-09 16:37:00 +0000 |
4 | @@ -280,16 +280,16 @@ |
5 | def tmpdir(self): |
6 | """Default tmpdir: module/class/test_method.""" |
7 | # check if we already generated the root path |
8 | - root_dir = getattr(self, '__root', None) |
9 | - if root_dir: |
10 | - return root_dir |
11 | + if self.__root: |
12 | + return self.__root |
13 | base = os.path.join(self.__class__.__module__, |
14 | self.__class__.__name__, |
15 | self._testMethodName) |
16 | - # use _trial_temp dir, it should be os.gwtcwd() |
17 | + # use _trial_temp dir, it should be TRIAL_TEMP_DIR or os.getcwd() |
18 | # define the root temp dir of the testcase, pylint: disable=W0201 |
19 | root_tmp = os.environ.get('TRIAL_TEMP_DIR', os.getcwd()) |
20 | self.__root = os.path.join(root_tmp, '_trial_temp', base) |
21 | + self.addCleanup(self.rmtree, self.__root) |
22 | return self.__root |
23 | |
24 | def rmtree(self, path): |
25 | @@ -300,7 +300,7 @@ |
26 | if not path_exists(path): |
27 | return |
28 | # change perms to rw, so we can delete the temp dir |
29 | - if path != getattr(self, '__root', None): |
30 | + if path != self.__root: |
31 | set_dir_readwrite(os.path.dirname(path)) |
32 | if not can_write(path): |
33 | set_dir_readwrite(path) |
34 | @@ -318,8 +318,22 @@ |
35 | adir = adir.encode('utf8') |
36 | if not can_write(adir): |
37 | set_dir_readwrite(adir) |
38 | - # XXX: We should not be ignoring errors |
39 | - shutil.rmtree(path, ignore_errors=True) |
40 | + |
41 | + if sys.platform == 'win32': |
42 | + # in windows, we need to pass a unicode, literal path to |
43 | + # shutil.rmtree, otherwise we can't remove "deep and wide" paths |
44 | + path = u'\\\\?\\' + path.decode('utf8') |
45 | + |
46 | + # Instead of ignoring the errors when removing trees, we are temporarly |
47 | + # printing a message to stdout to caught everyone's attention. |
48 | + # Once the tests are fixed in this regard, we're removing the |
49 | + # try-except block and having shutil.rmtree failing if the path |
50 | + # can not be removed. |
51 | + try: |
52 | + shutil.rmtree(path) |
53 | + except Exception, e: |
54 | + print 'ERROR!! could not recursively remove %r ' \ |
55 | + '(error is %r).' % (path, e) |
56 | |
57 | def makedirs(self, path): |
58 | """Custom makedirs that handle ro parent.""" |
59 | @@ -331,12 +345,15 @@ |
60 | @defer.inlineCallbacks |
61 | def setUp(self): |
62 | yield super(BaseTwistedTestCase, self).setUp() |
63 | + self.__root = None |
64 | # use the config from the branch |
65 | - self.old_get_config_files = config.get_config_files |
66 | - config.get_config_files = lambda: [os.path.join(os.environ['ROOTDIR'], |
67 | + new_get_config_files = lambda: [os.path.join(os.environ['ROOTDIR'], |
68 | 'data', 'syncdaemon.conf')] |
69 | + self.patch(config, 'get_config_files', new_get_config_files) |
70 | + |
71 | # fake a very basic config file with sane defaults for the tests |
72 | - self.config_file = os.path.join(self.mktemp('config'), 'syncdaemon.conf') |
73 | + config_dir = self.mktemp('config') |
74 | + self.config_file = os.path.join(config_dir, 'syncdaemon.conf') |
75 | with open(self.config_file, 'w') as fp: |
76 | fp.write('[bandwidth_throttling]\n') |
77 | fp.write('on = False\n') |
78 | @@ -346,18 +363,6 @@ |
79 | config._user_config = None |
80 | config.get_user_config(config_file=self.config_file) |
81 | |
82 | - def tearDown(self): |
83 | - """ cleanup the temp dir. """ |
84 | - # invalidate the current config |
85 | - config._user_config = None |
86 | - # restore the old get_config_files |
87 | - config.get_config_files = self.old_get_config_files |
88 | - self.rmtree(os.path.dirname(self.config_file)) |
89 | - root_dir = getattr(self, '__root', None) |
90 | - if root_dir: |
91 | - self.rmtree(self.__root) |
92 | - return super(BaseTwistedTestCase, self).tearDown() |
93 | - |
94 | |
95 | class FakeMainTestCase(BaseTwistedTestCase): |
96 | """A testcase that starts up a Main instance.""" |
97 | @@ -376,20 +381,13 @@ |
98 | self.shares_dir = self.mktemp('shares_dir') |
99 | self.main = FakeMain(self.root_dir, self.shares_dir, |
100 | self.data_dir, self.partials_dir) |
101 | + self.addCleanup(self.main.shutdown) |
102 | self.vm = self.main.vm |
103 | self.fs = self.main.fs |
104 | self.event_q = self.main.event_q |
105 | self.action_q = self.main.action_q |
106 | self.event_q.push('SYS_INIT_DONE') |
107 | |
108 | - @defer.inlineCallbacks |
109 | - def tearDown(self): |
110 | - """Shutdown this testcase.""" |
111 | - yield super(FakeMainTestCase, self).tearDown() |
112 | - self.main.shutdown() |
113 | - self.log.info("finished test %s.%s", self.__class__.__name__, |
114 | - self._testMethodName) |
115 | - |
116 | |
117 | class FakeVolumeManager(object): |
118 | """ A volume manager that only knows one share, the root""" |
119 | |
120 | === modified file 'tests/platform/linux/test_os_helper.py' |
121 | --- tests/platform/linux/test_os_helper.py 2011-07-27 17:31:25 +0000 |
122 | +++ tests/platform/linux/test_os_helper.py 2011-08-09 16:37:00 +0000 |
123 | @@ -27,22 +27,9 @@ |
124 | from twisted.internet import defer |
125 | from ubuntuone.devtools.handlers import MementoHandler |
126 | |
127 | -from contrib.testing.testcase import BaseTwistedTestCase |
128 | +from tests.platform import test_os_helper |
129 | from ubuntuone.platform import ( |
130 | - access, |
131 | - allow_writes, |
132 | - listdir, |
133 | - make_dir, |
134 | move_to_trash, |
135 | - open_file, |
136 | - path_exists, |
137 | - remove_dir, |
138 | - remove_file, |
139 | - rename, |
140 | - set_dir_readonly, |
141 | - set_dir_readwrite, |
142 | - set_file_readonly, |
143 | - set_file_readwrite, |
144 | stat_path, |
145 | ) |
146 | |
147 | @@ -60,186 +47,19 @@ |
148 | return self._bad_trash_call |
149 | |
150 | |
151 | -class OSWrapperTests(BaseTwistedTestCase): |
152 | +class OSWrapperTests(test_os_helper.OSWrapperTests): |
153 | """Tests for os wrapper functions.""" |
154 | |
155 | @defer.inlineCallbacks |
156 | def setUp(self): |
157 | """Set up.""" |
158 | yield super(OSWrapperTests, self).setUp() |
159 | - self.basedir = self.mktemp('test_root') |
160 | - self.testfile = os.path.join(self.basedir, "test_file") |
161 | - open(self.testfile, 'w').close() |
162 | - |
163 | self.handler = MementoHandler() |
164 | self.handler.setLevel(logging.DEBUG) |
165 | self._logger = logging.getLogger('ubuntuone.SyncDaemon') |
166 | self._logger.addHandler(self.handler) |
167 | self.addCleanup(self._logger.removeHandler, self.handler) |
168 | |
169 | - def test_set_dir_readonly(self): |
170 | - """Test for set_dir_readonly.""" |
171 | - set_dir_readonly(self.basedir) |
172 | - self.assertRaises(OSError, os.mkdir, os.path.join(self.basedir, 'foo')) |
173 | - |
174 | - def test_set_dir_readwrite(self): |
175 | - """Test for set_dir_readwrite.""" |
176 | - set_dir_readonly(self.basedir) |
177 | - self.assertRaises(OSError, os.mkdir, os.path.join(self.basedir, 'foo')) |
178 | - set_dir_readwrite(self.basedir) |
179 | - os.mkdir(os.path.join(self.basedir, 'foo')) |
180 | - |
181 | - def test_allow_writes(self): |
182 | - """Test for allow_writes.""" |
183 | - set_dir_readonly(self.basedir) |
184 | - self.assertRaises(OSError, os.mkdir, os.path.join(self.basedir, 'foo')) |
185 | - with allow_writes(self.basedir): |
186 | - os.mkdir(os.path.join(self.basedir, 'foo')) |
187 | - |
188 | - def test_set_file_readonly(self): |
189 | - """Test for set_file_readonly.""" |
190 | - set_file_readonly(self.testfile) |
191 | - self.assertRaises(IOError, open, self.testfile, 'w') |
192 | - |
193 | - def test_set_file_readwrite(self): |
194 | - """Test for set_file_readwrite.""" |
195 | - set_file_readonly(self.testfile) |
196 | - self.assertRaises(IOError, open, self.testfile, 'w') |
197 | - set_file_readwrite(self.testfile) |
198 | - open(self.testfile, 'w') |
199 | - |
200 | - def test_path_file_exist_yes(self): |
201 | - """Test that the file exists.""" |
202 | - self.assertTrue(path_exists(self.testfile)) |
203 | - |
204 | - def test_path_file_exist_no(self): |
205 | - """Test that the file doesn't exist.""" |
206 | - os.remove(self.testfile) |
207 | - self.assertFalse(path_exists(self.testfile)) |
208 | - |
209 | - def test_path_dir_exist_yes(self): |
210 | - """Test that the dir exists.""" |
211 | - self.assertTrue(path_exists(self.basedir)) |
212 | - |
213 | - def test_path_dir_exist_no(self): |
214 | - """Test that the dir doesn't exist.""" |
215 | - os.remove(self.testfile) |
216 | - self.assertFalse(path_exists(os.path.join(self.basedir, 'nodir'))) |
217 | - |
218 | - def test_remove_file(self): |
219 | - """Test the remove file.""" |
220 | - remove_file(self.testfile) |
221 | - self.assertFalse(os.path.exists(self.testfile)) |
222 | - |
223 | - def test_remove_dir(self): |
224 | - """Test the remove dir.""" |
225 | - testdir = os.path.join(self.basedir, 'foodir') |
226 | - os.mkdir(testdir) |
227 | - assert os.path.exists(testdir) |
228 | - remove_dir(testdir) |
229 | - self.assertFalse(path_exists(testdir)) |
230 | - |
231 | - def test_make_dir_one(self): |
232 | - """Test the make dir with one dir.""" |
233 | - testdir = os.path.join(self.basedir, 'foodir') |
234 | - assert not os.path.exists(testdir) |
235 | - make_dir(testdir) |
236 | - self.assertTrue(os.path.exists(testdir)) |
237 | - |
238 | - def test_make_dir_already_there(self): |
239 | - """Test the make dir with one dir that exists.""" |
240 | - self.assertRaises(OSError, make_dir, self.basedir) |
241 | - |
242 | - def test_make_dir_recursive_no(self): |
243 | - """Test the make dir with some dirs, not recursive explicit.""" |
244 | - testdir = os.path.join(self.basedir, 'foo', 'bar') |
245 | - assert not os.path.exists(testdir) |
246 | - self.assertRaises(OSError, make_dir, testdir) |
247 | - |
248 | - def test_make_dir_recursive_yes(self): |
249 | - """Test the make dir with some dirs, recursive.""" |
250 | - testdir = os.path.join(self.basedir, 'foo', 'bar') |
251 | - assert not os.path.exists(testdir) |
252 | - make_dir(testdir, recursive=True) |
253 | - self.assertTrue(os.path.exists(testdir)) |
254 | - |
255 | - def test_open_file_not_there(self): |
256 | - """Open a file that does not exist.""" |
257 | - self.assertRaises(IOError, open_file, os.path.join(self.basedir, 'no')) |
258 | - |
259 | - def test_open_file_gets_a_fileobject(self): |
260 | - """Open a file, and get a file object.""" |
261 | - testfile = os.path.join(self.basedir, 'testfile') |
262 | - open(testfile, 'w').close() |
263 | - f = open_file(testfile) |
264 | - self.assertTrue(isinstance(f, file)) |
265 | - |
266 | - def test_open_file_read(self): |
267 | - """Open a file, and read.""" |
268 | - testfile = os.path.join(self.basedir, 'testfile') |
269 | - with open(testfile, 'w') as fh: |
270 | - fh.write("foo") |
271 | - f = open_file(testfile, 'r') |
272 | - self.assertTrue(f.read(), "foo") |
273 | - |
274 | - def test_open_file_write(self): |
275 | - """Open a file, and write.""" |
276 | - testfile = os.path.join(self.basedir, 'testfile') |
277 | - with open_file(testfile, 'w') as fh: |
278 | - fh.write("foo") |
279 | - f = open(testfile) |
280 | - self.assertTrue(f.read(), "foo") |
281 | - |
282 | - def test_rename_not_there(self): |
283 | - """Rename something that does not exist.""" |
284 | - self.assertRaises(OSError, rename, |
285 | - os.path.join(self.basedir, 'no'), 'foo') |
286 | - |
287 | - def test_rename_file(self): |
288 | - """Rename a file.""" |
289 | - testfile1 = os.path.join(self.basedir, 'testfile1') |
290 | - testfile2 = os.path.join(self.basedir, 'testfile2') |
291 | - open(testfile1, 'w').close() |
292 | - rename(testfile1, testfile2) |
293 | - self.assertFalse(os.path.exists(testfile1)) |
294 | - self.assertTrue(os.path.exists(testfile2)) |
295 | - |
296 | - def test_rename_dir(self): |
297 | - """Rename a dir.""" |
298 | - testdir1 = os.path.join(self.basedir, 'testdir1') |
299 | - testdir2 = os.path.join(self.basedir, 'testdir2') |
300 | - os.mkdir(testdir1) |
301 | - rename(testdir1, testdir2) |
302 | - self.assertFalse(os.path.exists(testdir1)) |
303 | - self.assertTrue(os.path.exists(testdir2)) |
304 | - |
305 | - def test_listdir(self): |
306 | - """Return a list of the files in a dir.""" |
307 | - open(os.path.join(self.basedir, 'foo'), 'w').close() |
308 | - open(os.path.join(self.basedir, 'bar'), 'w').close() |
309 | - l = listdir(self.basedir) |
310 | - self.assertEqual(sorted(l), ['bar', 'foo', 'test_file']) |
311 | - |
312 | - def test_access_rw(self): |
313 | - """Test access on a file with full permission.""" |
314 | - self.assertTrue(access(self.testfile)) |
315 | - |
316 | - def test_access_ro(self): |
317 | - """Test access on a file with read only permission.""" |
318 | - os.chmod(self.testfile, 0o444) |
319 | - self.assertTrue(access(self.testfile)) |
320 | - os.chmod(self.testfile, 0o664) |
321 | - |
322 | - def test_access_nothing(self): |
323 | - """Test access on a file with no permission at all.""" |
324 | - os.chmod(self.testfile, 0o000) |
325 | - self.assertFalse(access(self.testfile)) |
326 | - os.chmod(self.testfile, 0o664) |
327 | - |
328 | - def test_stat_normal(self): |
329 | - """Test on a normal file.""" |
330 | - self.assertEqual(os.stat(self.testfile), stat_path(self.testfile)) |
331 | - |
332 | def test_stat_symlink(self): |
333 | """Test that it doesn't follow symlinks. |
334 | |
335 | @@ -252,31 +72,6 @@ |
336 | self.assertNotEqual(os.stat(link).st_ino, stat_path(link).st_ino) |
337 | self.assertEqual(os.lstat(link).st_ino, stat_path(link).st_ino) |
338 | |
339 | - def test_stat_no_path(self): |
340 | - """Test that it raises proper error when no file is there.""" |
341 | - try: |
342 | - return stat_path(os.path.join(self.basedir, 'nofile')) |
343 | - except OSError, e: |
344 | - self.assertEqual(e.errno, errno.ENOENT) |
345 | - |
346 | - def test_path_exists_file_yes(self): |
347 | - """The file is there.""" |
348 | - self.assertTrue(path_exists(self.testfile)) |
349 | - |
350 | - def test_path_exists_file_no(self): |
351 | - """The file is not there.""" |
352 | - os.remove(self.testfile) |
353 | - self.assertFalse(path_exists(self.testfile)) |
354 | - |
355 | - def test_path_exists_dir_yes(self): |
356 | - """The dir is there.""" |
357 | - self.assertTrue(path_exists(self.basedir)) |
358 | - |
359 | - def test_path_exists_dir_no(self): |
360 | - """The dir is not there.""" |
361 | - self.rmtree(self.basedir) |
362 | - self.assertFalse(path_exists(self.basedir)) |
363 | - |
364 | def test_movetotrash_file_ok(self): |
365 | """Move a file to trash ok. |
366 | |
367 | |
368 | === modified file 'tests/platform/test_os_helper.py' |
369 | --- tests/platform/test_os_helper.py 2011-07-28 15:35:45 +0000 |
370 | +++ tests/platform/test_os_helper.py 2011-08-09 16:37:00 +0000 |
371 | @@ -66,7 +66,7 @@ |
372 | self.valid_path = valid_file_path_builder(self.testfile) |
373 | |
374 | # make sure the file exists |
375 | - open(self.valid_path, 'w').close() |
376 | + open_file(self.testfile, 'w').close() |
377 | |
378 | def test_set_dir_readonly(self): |
379 | """Test for set_dir_readonly.""" |
380 | @@ -98,7 +98,7 @@ |
381 | """Test for set_file_readonly.""" |
382 | set_file_readonly(self.testfile) |
383 | self.addCleanup(set_dir_readwrite, self.testfile) |
384 | - self.assertRaises(IOError, open, self.valid_path, 'w') |
385 | + self.assertRaises(IOError, open_file, self.testfile, 'w') |
386 | |
387 | def test_set_file_readwrite(self): |
388 | """Test for set_file_readwrite.""" |
389 | @@ -106,7 +106,7 @@ |
390 | self.addCleanup(set_dir_readwrite, self.testfile) |
391 | |
392 | set_file_readwrite(self.testfile) |
393 | - open(self.valid_path, 'w') |
394 | + open_file(self.testfile, 'w') |
395 | self.assertTrue(can_write(self.testfile)) |
396 | |
397 | def test_path_file_exist_yes(self): |
398 | @@ -115,7 +115,7 @@ |
399 | |
400 | def test_path_file_exist_no(self): |
401 | """Test that the file doesn't exist.""" |
402 | - os.remove(self.valid_path) |
403 | + remove_file(self.testfile) |
404 | self.assertFalse(path_exists(self.testfile)) |
405 | |
406 | def test_path_dir_exist_yes(self): |
407 | @@ -129,7 +129,7 @@ |
408 | def test_remove_file(self): |
409 | """Test the remove file.""" |
410 | remove_file(self.testfile) |
411 | - self.assertFalse(os.path.exists(self.valid_path)) |
412 | + self.assertFalse(path_exists(self.testfile)) |
413 | |
414 | def test_remove_dir(self): |
415 | """Test the remove dir.""" |
416 | @@ -174,7 +174,7 @@ |
417 | |
418 | def test_open_file_read(self): |
419 | """Open a file, and read.""" |
420 | - with open(self.valid_path, 'w') as fh: |
421 | + with open_file(self.testfile, 'w') as fh: |
422 | fh.write("foo") |
423 | f = open_file(self.testfile, 'r') |
424 | self.assertTrue(f.read(), "foo") |
425 | @@ -185,7 +185,7 @@ |
426 | fh.write("foo") |
427 | fh.close() |
428 | |
429 | - f = open(self.valid_path) |
430 | + f = open_file(self.testfile) |
431 | self.assertTrue(f.read(), "foo") |
432 | |
433 | def test_rename_not_there(self): |
434 | @@ -198,10 +198,10 @@ |
435 | if target is None: |
436 | target = os.path.join(self.basedir, 'target') |
437 | |
438 | - assert os.path.exists(self.valid_path) |
439 | + assert path_exists(self.testfile) |
440 | rename(self.testfile, target) |
441 | |
442 | - self.assertFalse(os.path.exists(self.valid_path), |
443 | + self.assertFalse(path_exists(self.testfile), |
444 | 'Path %r should not exist after rename.' % self.testfile) |
445 | self.assertTrue(path_exists(target), |
446 | 'Path %r should exist after rename.' % target) |
447 | @@ -242,8 +242,8 @@ |
448 | |
449 | def test_access_ro(self): |
450 | """Test access on a file with read only permission.""" |
451 | - os.chmod(self.valid_path, 0o444) |
452 | - self.addCleanup(os.chmod, self.valid_path, 0o664) |
453 | + set_file_readonly(self.testfile) |
454 | + self.addCleanup(set_file_readwrite, self.testfile) |
455 | self.assertTrue(access(self.testfile)) |
456 | |
457 | def test_access_nothing(self): |
458 | @@ -270,7 +270,7 @@ |
459 | |
460 | def test_path_exists_file_no(self): |
461 | """The file is not there.""" |
462 | - os.remove(self.valid_path) |
463 | + remove_file(self.testfile) |
464 | self.assertFalse(path_exists(self.testfile)) |
465 | |
466 | def test_path_exists_dir_yes(self): |
467 | |
468 | === modified file 'ubuntuone/platform/linux/os_helper.py' |
469 | --- ubuntuone/platform/linux/os_helper.py 2011-07-29 13:27:18 +0000 |
470 | +++ ubuntuone/platform/linux/os_helper.py 2011-08-09 16:37:00 +0000 |
471 | @@ -47,7 +47,7 @@ |
472 | |
473 | def set_file_readwrite(path): |
474 | """Change path permissions to readwrite in a file.""" |
475 | - os.chmod(path, 0774) |
476 | + os.chmod(path, 0664) |
477 | |
478 | def set_dir_readonly(path): |
479 | """Change path permissions to readonly in a dir.""" |
+1