def test_ctime(self): import time p = Path(self.tpath, 'ctime.f') p.open('w') now = int(time.time()) os.utime(str(p), (now, now)) self.assertEqual(p.ctime, now)
def test_chmod(self): f = Path(self.tpath, 'chmod.f') self.assertFalse(f.exists) self.assertIsNone(f.chmod(0777)) # when file doesn't exist f.open() self.assertTrue(f.exists) self.assertIsNone(f.chmod(0777)) self.assertEqual(f.mode, 0777)
def test_mtime(self): import time now = int(time.time()) p = Path(self.tpath, 'mtime.f') self.assertIsNone(p.mtime) p.open('w') os.utime(str(p), (now, now)) self.assertEqual(p.mtime, now)
def test_readlink(self): n = Path(self.tpath, 'readlink.n') # does not exist l = Path(self.tpath, 'readlink.l') # existing link f = Path(self.tpath, 'readlink.f') # existing file f.open() os.symlink(f.value, l.value) self.assertEqual(l.readlink(), f) self.assertRaises(TypeError, n.readlink) self.assertRaises(TypeError, f.readlink)
def test_utime(self): import time f = Path(self.tpath, 'utime.f') f.open() when = time.time() - 3600 self.assertTrue(f.isfile) f.utime(when, when - 3600) self.assertEqual(int(os.path.getatime(f.value)), int(when)) self.assertEqual(int(os.path.getmtime(f.value)), int(when) - 3600)
def test__len_(self): f = Path(self.tpath, 'len.f') d = Path(self.tpath, 'len.d') f.open('w').write('=' * 256) self.assertEqual(len(f), 256) d.mkdir() for i in range(2): Path(d, str(i)).open('w') self.assertEqual(len(d), 2)
def test_copy(self): data = '=' * 256 n = Path(self.tpath, 'copy.0') f = Path(self.tpath, 'copy.1') o = Path(self.tpath, 'copy.2') f.open().write(data) self.assertEqual(len(f), 256) f.copy(o) self.assertTrue(o.isfile) self.assertEqual(len(o), 256) self.assertEqual(o.open().read(), data) self.assertRaises(IOError, n.copy, o)
def test_remove(self): f = Path(self.tpath, 'remove.f') d = Path(self.tpath, 'remove.d') n = Path(self.tpath, 'remove.n') f.open() d.mkdir() self.assertIsNone(f.remove()) self.assertFalse(f.exists) self.assertIsNone(d.remove()) self.assertFalse(d.exists) self.assertIsNone(n.remove()) self.assertFalse(n.exists)
def test_open(self): f1 = Path(self.tpath, 'open.f') self.assertRaises(TypeError, f1.open, 'r') f = f1.open() self.assertIsInstance(f, file) self.assertTrue(f1.isfile) self.assertEqual(f.mode, 'w') f.close() f = f1.open() self.assertIsInstance(f, file) self.assertEqual(f.mode, 'r') f.close() os.remove(f1.value) os.mkdir(f1.value) self.assertRaises(TypeError, f1.open, 'w')
def test_rename(self): n = Path(self.tpath, 'rename.n') f = Path(self.tpath, 'rename.f') o = Path(self.tpath, 'rename.o') so = os.path.join(self.tdir, 'rename.so') # string based self.assertRaises(TypeError, n.rename, 'rename.other') f.open() self.assertTrue(f.exists) other = f.rename(o) self.assertFalse(f.exists) self.assertTrue(other.exists) self.assertIs(o, other) other = o.rename(so) self.assertIsInstance(other, Path) self.assertFalse(o.exists) self.assertTrue(other.exists) self.assertEqual(other, so)
def test_makepipe(self): mask = os.umask(0) try: n = Path(self.tpath, 'makepipe.n') # does not exist f = Path(self.tpath, 'makepipe.f') # existing file readonly = int('0666', 8) readwrite = int('0777', 8) f.open() self.assertRaises(TypeError, f.makepipe) self.assertIsNone(n.makepipe()) self.assertEqual(n.type, Path.TYPE.PIPE) self.assertEqual(n.mode, readonly) n.remove() self.assertIsNone(n.makepipe(readwrite)) self.assertEqual(n.type, Path.TYPE.PIPE) self.assertEqual(n.mode, readwrite) finally: os.umask(mask)
def test_mkdir(self): n = Path(self.tpath, 'mkdir.n') # does not exist f = Path(self.tpath, 'mkdir.f') # existing file d = Path(self.tpath, 'mkdir.d') # existing directory d1 = d + 'subdir' d2 = d1 + 'subsubdir' f.open() os.mkdir(d.value) # do it the old fashioned way for now self.assertTrue(f.isfile) self.assertRaises(TypeError, f.mkdir) self.assertIsNone(n.mkdir()) self.assertTrue(n.isdir) self.assertTrue(d.isdir) self.assertIsNone(d.mkdir()) self.assertFalse(d2.exists) self.assertIsNone(d2.mkdir()) self.assertTrue(d2.isdir) self.assertTrue(d1.isdir)
def test_makelink(self): n = Path(self.tpath, 'makelink.n') # does not exist l = Path(self.tpath, 'makelink.l') # existing link f = Path(self.tpath, 'makelink.f') # existing file d = Path(self.tpath, 'makelink.d') # existing directory ptr = 'foobar' pptr = Path(ptr) self.assertIsNone(n.makelink(ptr)) # passing str value self.assertTrue(n.islink) self.assertEqual(os.readlink(n.value), ptr) n.remove() self.assertIsNone(n.makelink(pptr)) # passing Path instance self.assertTrue(n.islink) self.assertEqual(os.readlink(n.value), ptr) f.open() d.mkdir() os.symlink('xyzzy', l.value) self.assertRaises(TypeError, f.makelink, ptr) self.assertRaises(TypeError, d.makelink, ptr) self.assertIsNone(l.makelink(ptr)) self.assertTrue(l.islink) self.assertEqual(os.readlink(l.value), ptr)
def test_real(self): f = Path(self.tpath, 'real.f') l = Path(self.tpath, 'real.l') f.open('w') l.makelink('real.f') self.assertEqual(l.real, f.real)