Back to index

moin  1.9.0~rc2
test_filesys.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 """
00003     MoinMoin - MoinMoin.util.filesys Tests
00004 
00005     @copyright: 2008 MoinMoin:ThomasWaldmann
00006     @license: GNU GPL, see COPYING for details.
00007 """
00008 import sys, os, time
00009 import shutil, tempfile
00010 
00011 import py.test
00012 
00013 from MoinMoin.util import filesys
00014 
00015 class TestFuid:
00016     """ test filesys.fuid (a better mtime() alternative for up-to-date checking) """
00017 
00018     def setup_method(self, method):
00019         self.test_dir = tempfile.mkdtemp('', 'fuid_')
00020         self.fname = os.path.join(self.test_dir, "fuid-test")
00021         self.tmpname = os.path.join(self.test_dir, "fuid-temp")
00022 
00023     def teardown_method(self, method):
00024         shutil.rmtree(self.test_dir)
00025 
00026     def testNoFile(self):
00027         # no file created
00028         uid = filesys.fuid(self.fname)
00029 
00030         assert uid is None  # there is no file yet, fuid will fail internally and return None
00031 
00032     def makefile(self, fname, content):
00033         f = open(fname, "w")
00034         f.write(content)
00035         f.close()
00036 
00037     def testNewFile(self):
00038         # freshly created file
00039         self.makefile(self.fname, "foo")
00040         uid1 = filesys.fuid(self.fname)
00041 
00042         assert uid1 is not None  # None would mean some failure in fuid()
00043 
00044     def testUpdateFileInPlace(self):
00045         # update file in place, changing size and maybe mtime
00046         self.makefile(self.fname, "foo")
00047         uid1 = filesys.fuid(self.fname)
00048 
00049         self.makefile(self.fname, "foofoo")
00050         uid2 = filesys.fuid(self.fname)
00051 
00052         assert uid2 != uid1 # we changed size and maybe mtime
00053 
00054     def testUpdateFileMovingFromTemp(self):
00055         # update file by moving another file over it (see caching.update)
00056         # changing inode, maybe mtime, but not size
00057         if sys.platform == 'win32':
00058             py.test.skip("Inode change detection not supported on win32")
00059 
00060         self.makefile(self.fname, "foo")
00061         uid1 = filesys.fuid(self.fname)
00062 
00063         self.makefile(self.tmpname, "bar")
00064         os.rename(self.tmpname, self.fname)
00065         uid2 = filesys.fuid(self.fname)
00066 
00067         assert uid2 != uid1 # we didn't change size, but inode and maybe mtime
00068 
00069     def testStale(self):
00070         # is a file with mtime older than max_staleness considered stale?
00071         if sys.platform != 'win32':
00072             py.test.skip("max_staleness check only done on win32 because it doesn't support inode change detection")
00073 
00074         self.makefile(self.fname, "foo")
00075         uid1 = filesys.fuid(self.fname)
00076 
00077         time.sleep(2) # thanks for waiting :)
00078         uid2 = filesys.fuid(self.fname, max_staleness=1)
00079         assert uid2 != uid1  # should be considered stale if platform has no inode support
00080 
00081 
00082 class TestRename:
00083     """ test filesys.rename* """
00084 
00085     def setup_method(self, method):
00086         self.test_dir = tempfile.mkdtemp('', 'rename_')
00087         self.src = os.path.join(self.test_dir, "rename-src")
00088         self.dst = os.path.join(self.test_dir, "rename-dst")
00089 
00090     def teardown_method(self, method):
00091         shutil.rmtree(self.test_dir)
00092 
00093     def makefile(self, fname, content):
00094         f = open(fname, "w")
00095         f.write(content)
00096         f.close()
00097 
00098     def test_posix_rename_exists(self):
00099         self.makefile(self.src, "src")
00100         self.makefile(self.dst, "dst")
00101         # posix rename overwrites an existing destination
00102         # (on win32, we emulate this behaviour)
00103         filesys.rename(self.src, self.dst)
00104         dst_contents = open(self.dst).read()
00105         assert dst_contents == "src"
00106 
00107     def test_win32_rename_exists(self):
00108         self.makefile(self.src, "src")
00109         self.makefile(self.dst, "dst")
00110         # win32-like rename does not overwrite an existing destination
00111         # (on posix, we emulate this behaviour)
00112         py.test.raises(OSError, filesys.rename_no_overwrite, self.src, self.dst)
00113 
00114     def test_special_rename_exists(self):
00115         self.makefile(self.src, "src")
00116         self.makefile(self.dst, "dst")
00117         py.test.raises(OSError, filesys.rename_no_overwrite, self.src, self.dst, delete_old=True)
00118         assert not os.path.exists(self.src)
00119 
00120     def test_posix_rename_notexists(self):
00121         self.makefile(self.src, "src")
00122         filesys.rename(self.src, self.dst)
00123         dst_contents = open(self.dst).read()
00124         assert dst_contents == "src"
00125 
00126     def test_win32_rename_notexists(self):
00127         self.makefile(self.src, "src")
00128         filesys.rename_no_overwrite(self.src, self.dst)
00129         dst_contents = open(self.dst).read()
00130         assert dst_contents == "src"
00131 
00132     def test_special_rename_notexists(self):
00133         self.makefile(self.src, "src")
00134         filesys.rename_no_overwrite(self.src, self.dst, delete_old=True)
00135         assert not os.path.exists(self.src)
00136 
00137 
00138 coverage_modules = ['MoinMoin.util.filesys']