diff options
-rw-r--r-- | ufo/test/unit/common/test_diskfile.py | 36 |
1 files changed, 34 insertions, 2 deletions
diff --git a/ufo/test/unit/common/test_diskfile.py b/ufo/test/unit/common/test_diskfile.py index fb0dc3e16d5..85d539a29f0 100644 --- a/ufo/test/unit/common/test_diskfile.py +++ b/ufo/test/unit/common/test_diskfile.py @@ -66,6 +66,21 @@ def _mock_do_unlink(f): raise ose +def _mock_do_unlink_eacces_err(f): + ose = OSError() + ose.errno = errno.EACCES + raise ose + +def _mock_getsize_eaccess_err(f): + ose = OSError() + ose.errno = errno.EACCES + raise ose + +def _mock_do_rmdir_eacces_err(f): + ose = OSError() + ose.errno = errno.EACCES + raise ose + class MockRenamerCalled(Exception): pass @@ -654,13 +669,16 @@ class TestDiskFile(unittest.TestCase): os.chmod(the_path, stats.st_mode & (~stat.S_IWUSR)) # Handle the case do_unlink() raises an OSError + __os_unlink = os.unlink + os.unlink = _mock_do_unlink_eacces_err try: gdf.unlinkold(normalize_timestamp(later)) except OSError as e: - assert e.errno != errno.ENOENT + assert e.errno == errno.EACCES else: self.fail("Excepted an OSError when unlinking file") finally: + os.unlink = __os_unlink os.chmod(the_path, stats.st_mode) assert os.path.isdir(gdf.datadir) @@ -699,11 +717,14 @@ class TestDiskFile(unittest.TestCase): stats = os.stat(gdf.datadir) os.chmod(gdf.datadir, 0) + __os_rmdir = os.rmdir + os.rmdir = _mock_do_rmdir_eacces_err try: later = float(gdf.metadata['X-Timestamp']) + 1 gdf.unlinkold(normalize_timestamp(later)) finally: os.chmod(gdf.datadir, stats.st_mode) + os.rmdir = __os_rmdir assert os.path.isdir(gdf.datadir) assert os.path.isdir(gdf.data_file) finally: @@ -795,13 +816,16 @@ class TestDiskFile(unittest.TestCase): assert not gdf._is_dir stats = os.stat(the_path) os.chmod(the_path, 0) + __os_path_getsize = os.path.getsize + os.path.getsize = _mock_getsize_eaccess_err try: s = gdf.get_data_file_size() except OSError as err: - assert err.errno != errno.ENOENT + assert err.errno == errno.EACCES else: self.fail("Expected OSError exception") finally: + os.path.getsize = __os_path_getsize os.chmod(the_path, stats.st_mode) finally: shutil.rmtree(td) @@ -873,7 +897,15 @@ class TestDiskFile(unittest.TestCase): assert os.path.basename(saved_tmppath)[:3] == '.z.' assert os.path.exists(saved_tmppath) os.write(fd, "123") + # At the end of previous with block a close on fd is called. + # Calling os.close on the same fd will raise an OSError + # exception and we must catch it. + try: os.close(fd) + except OSError as err: + pass + else: + self.fail("Exception expected") assert not os.path.exists(saved_tmppath) finally: shutil.rmtree(td) |