def test_less_than(self): a = DirectoryEntry("a", "/b/baz", RegularFile, inode=7) dot = DirectoryEntry(".", "/bar/baz", RegularFile, inode=7L) self.assertLess(dot, a) self.assertGreater(a, dot) self.assertLessEqual(a, a) self.assertGreaterEqual(a, a)
def test_equal_dirents(self): first = DirectoryEntry("foo", "/bar/baz", RegularFile, inode=7) second = DirectoryEntry("foo", "/bar/baz", RegularFile, inode=7) self.assertEquals(first, second) self.assertEquals(second, first)
def test_is_not_hashable_because_it_is_mutable(self): first = DirectoryEntry("foo", "/bar/baz", RegularFile, inode=7) import collections self.assertTrue(isinstance(first, collections.Hashable))
def test_disequal_inodes_different_attrs(self): first = DirectoryEntry("foo", "/bar/baz", RegularFile, inode=7) second = DirectoryEntry("foo", "/bar/baz", RegularFile, inode=8) self.assertNotEqual(first, second) self.assertNotEqual(second, first)
def set_up_workspace(): def ino(f): return os.lstat(f).st_ino test_tree = {} test_tree["contents"] = set() test_tree["contents: extra recursive"] = set() test_tree["contents: normal-?-file"] = set() test_tree["contents: normal*"] = set() root = tempfile.mkdtemp(prefix="pyreaddir.unittests.") test_tree["root dir"] = root # Create a normal file normal = os.path.join(root, "normal-file") f = open(normal, "w") f.close() test_tree["normal"] = DirectoryEntry("normal-file", root, RegularFile, inode=ino(normal)) test_tree["contents"].add(test_tree["normal"]) test_tree["contents: normal*"].add(test_tree["normal"]) # Make more more files, used for glob tests normal = os.path.join(root, "normal-1-file") f = open(normal, "w") f.close() test_tree["normal-1"] = DirectoryEntry("normal-1-file", root, RegularFile, inode=ino(normal)) test_tree["contents"].add(test_tree["normal-1"]) test_tree["contents: normal-?-file"].add(test_tree["normal-1"]) test_tree["contents: normal*"].add(test_tree["normal-1"]) normal = os.path.join(root, "normal-2-file") f = open(normal, "w") f.close() test_tree["normal-2"] = DirectoryEntry("normal-2-file", root, RegularFile, inode=ino(normal)) test_tree["contents"].add(test_tree["normal-2"]) test_tree["contents: normal-?-file"].add(test_tree["normal-2"]) test_tree["contents: normal*"].add(test_tree["normal-2"]) normal = os.path.join(root, ".hidden-file") f = open(normal, "w") f.close() test_tree["hidden"] = DirectoryEntry(".hidden-file", root, RegularFile, inode=ino(normal)) test_tree["contents"].add(test_tree["hidden"]) # Create a directory test_dir = os.path.join(root, "directory") os.mkdir(test_dir) test_tree["directory"] = DirectoryEntry("directory", root, Directory, inode=ino(test_dir)) test_tree["contents"].add(test_tree["directory"]) test_dir = os.path.join(root, "normal-3-file") # It lies os.mkdir(test_dir) test_tree["directory-3"] = DirectoryEntry("normal-3-file", root, Directory, inode=ino(test_dir)) test_tree["contents"].add(test_tree["directory-3"]) test_tree["contents: normal-?-file"].add(test_tree["directory-3"]) test_tree["contents: normal*"].add(test_tree["directory-3"]) if True: # (Just indent to show structure) # Create a file *in* that directory: inner = os.path.join(test_dir, "inner-normal-file") f = open(inner, "w") f.close() test_tree["file in directory"] = DirectoryEntry("inner-normal-file", test_dir, RegularFile, inode=ino(inner)) test_tree["contents: extra recursive"].add( test_tree["file in directory"]) # Create a named pipe fifo = os.path.join(root, "fifo") os.mkfifo(fifo) test_tree["named pipe"] = DirectoryEntry("fifo", root, NamedPipe, inode=ino(fifo)) test_tree["contents"].add(test_tree["named pipe"]) # Create devices?! # Create symlink to directory link_dir = os.path.join(root, "symlink-to-dir") os.symlink(test_dir, link_dir) test_tree["directory symlink"] = DirectoryEntry("symlink-to-dir", root, SymbolicLink, inode=ino(link_dir)) test_tree["contents"].add(test_tree["directory symlink"]) # Create symlink to file link_file = os.path.join(root, "symlink-to-file") os.symlink(normal, link_file) test_tree["file symlink"] = DirectoryEntry("symlink-to-file", root, SymbolicLink, inode=ino(link_file)) test_tree["contents"].add(test_tree["file symlink"]) # Create socket?! # Create whiteout?! # Add . and .. test_tree["dot"] = DirectoryEntry(".", root, Directory, inode=ino(root)) test_tree["contents"].add(test_tree["dot"]) test_tree["dotdot"] = DirectoryEntry("..", root, Directory, inode=ino(os.path.join(root, ".."))) test_tree["contents"].add(test_tree["dotdot"]) return test_tree
def test_different_name_has_unequal_hash(self): other = DirectoryEntry("sporz", "/bar/baz", RegularFile, inode=7L) self.assertNotEqual(hash(self.e), hash(other))
def test_different_inode_is_unequal(self): other = DirectoryEntry("foo", "/bar/baz", RegularFile, inode=8L) self.assertNotEqual(self.e, other)
def test_no_inode_is_unequal(self): other = DirectoryEntry("foo", "/bar/baz", RegularFile) self.assertNotEqual(self.e, other)
def test_different_type_is_unequal(self): other = DirectoryEntry("foo", "/bar/baz", Directory, inode=7L) self.assertNotEqual(self.e, other)
def setUp(self): self.e = DirectoryEntry("foo", "/bar/baz", RegularFile, inode=7L)
def test_almost_exact_dupe_has_same_hash(self): exact_dupe = DirectoryEntry("foo", "/bar/baz", RegularFile, inode=7) self.assertEqual(hash(self.e), hash(exact_dupe))
def test_equal_but_different_kind_of_inode_integer(self): equal = DirectoryEntry("foo", "/bar/baz", RegularFile, inode=7) self.assertEqual(self.e, equal)
def test_exact_dupe_is_equal(self): exact_dupe = DirectoryEntry("foo", "/bar/baz", RegularFile, inode=7L) self.assertEqual(self.e, exact_dupe)