def test_path(self): rel_path = "test" abs_path = os.path.abspath(rel_path) # Supply relative path self.assertEqual(FilesystemObject(rel_path).path, abs_path) # Supply absolute path self.assertEqual(FilesystemObject(rel_path).path, abs_path)
def test_raw_symlink_target(self): # Make file and symlink with open("test.txt", "wt") as fp: fp.write("test") os.symlink("test.txt", "test.lnk") self.assertEqual( FilesystemObject("test.lnk").raw_symlink_target, "test.txt") # Broken symlink os.symlink("missing", "missing.lnk") self.assertEqual( FilesystemObject("missing.lnk").raw_symlink_target, "missing")
def test_iscompressed(self): # Uncompressed file with open("test.txt", "wt") as fp: fp.write("uncompressed") self.assertFalse(FilesystemObject("test.txt").iscompressed) # Gzipped file with gzip.open("test.txt.gz", "wt") as fp: fp.write("gzipped") self.assertTrue(FilesystemObject("test.txt.gz").iscompressed) # Bzipped2 file with bz2.open("test.txt.bz2", "wt") as fp: fp.write("bzipped2") self.assertTrue(FilesystemObject("test.txt.bz2").iscompressed)
def test_ishidden(self): # Make test filesystem objects with open("test.txt", "wt") as fp: fp.write("test") self.assertFalse(FilesystemObject("test.txt").ishidden) with open(".hidden.txt", "wt") as fp: fp.write("test") self.assertTrue(FilesystemObject(".hidden.txt").ishidden) # Directory os.mkdir("test.dir") self.assertFalse(FilesystemObject("test.dir").ishidden) os.mkdir(".hidden.dir") self.assertTrue(FilesystemObject(".hidden.dir").ishidden) # Files in subdirs with open("test.dir/test.txt", "wt") as fp: fp.write("test") self.assertFalse(FilesystemObject("test.dir/test.txt").ishidden) with open("test.dir/.hidden.txt", "wt") as fp: fp.write("test") self.assertTrue(FilesystemObject("test.dir/.hidden.txt").ishidden) with open(".hidden.dir/test.txt", "wt") as fp: fp.write("test") self.assertTrue(FilesystemObject(".hidden.dir/test.txt").ishidden) with open(".hidden.dir/.hidden.txt", "wt") as fp: fp.write("test") self.assertTrue(FilesystemObject(".hidden.dir/.hidden.txt").ishidden)
def test_md5sum(self): # Make test filesystem objects with open("test.txt", "wt") as fp: fp.write("test") self.assertEqual( FilesystemObject("test.txt").md5sum, "098f6bcd4621d373cade4e832627b4f6")
def test_isdir(self): # Doesn't exist self.assertFalse(FilesystemObject("test").isdir) # File that exists with open("test.txt", "wt") as fp: fp.write("test") self.assertFalse(FilesystemObject("test.txt").isdir) # Directory os.mkdir("test.dir") self.assertTrue(FilesystemObject("test.dir").isdir) # Symlink os.symlink("test.txt", "test.lnk") self.assertFalse(FilesystemObject("test.lnk").isdir) # Broken symlink os.symlink("missing", "missing.lnk") self.assertFalse(FilesystemObject("missing.lnk").isdir)
def test_gid(self): # Get GID for current user current_gid = os.getgid() # Make file with open("test.txt", "wt") as fp: fp.write("test") self.assertEqual(FilesystemObject("test.txt").gid, current_gid)
def test_linux_permissions(self): # Make test filesystem object with open("test.txt", "wt") as fp: fp.write("test") # Set permissions and test outputs os.chmod("test.txt", 0o444) self.assertEqual( FilesystemObject("test.txt").linux_permissions, "r--r--r--") os.chmod("test.txt", 0o666) self.assertEqual( FilesystemObject("test.txt").linux_permissions, "rw-rw-rw-") os.chmod("test.txt", 0o750) self.assertEqual( FilesystemObject("test.txt").linux_permissions, "rwxr-x---") os.chmod("test.txt", 0o070) self.assertEqual( FilesystemObject("test.txt").linux_permissions, "---rwx---")
def test_groupname(self): # Get group name for current user current_groupname = grp.getgrgid(os.getgid()).gr_name # Make file with open("test.txt", "wt") as fp: fp.write("test") self.assertEqual( FilesystemObject("test.txt").groupname, current_groupname)
def test_username(self): # Get username for current user current_username = getpass.getuser() # Make file with open("test.txt", "wt") as fp: fp.write("test") self.assertEqual( FilesystemObject("test.txt").username, current_username)
def test_timestamp(self): # Get current time in seconds timestamp = int(time.time()) # Make file with known timestamp with open("test.txt", "wt") as fp: fp.write("test") os.utime("test.txt", (timestamp, timestamp)) self.assertEqual(FilesystemObject("test.txt").timestamp, timestamp)
def test_type_extension(self): # Make test filesystem objects with open("test.txt", "wt") as fp: fp.write("test") with open("test.txt.gz", "wt") as fp: fp.write("test") with open("test.txt.filtered.gz.1", "wt") as fp: fp.write("test") with open("test.gz", "wt") as fp: fp.write("test") with open("test", "wt") as fp: fp.write("test") # Check type extensions self.assertEqual(FilesystemObject("test.txt").type_extension, "txt") self.assertEqual(FilesystemObject("test.txt.gz").type_extension, "txt") self.assertEqual( FilesystemObject("test.txt.filtered.gz.1").type_extension, "1") self.assertEqual(FilesystemObject("test.gz").type_extension, "") self.assertEqual(FilesystemObject("test").type_extension, "")
def test_type(self): # Doesn't exist self.assertEqual( FilesystemObject("test").type, FilesystemObjectType.MISSING) # File that exists with open("test.txt", "wt") as fp: fp.write("test") self.assertEqual( FilesystemObject("test.txt").type, FilesystemObjectType.FILE) # Directory os.mkdir("test.dir") self.assertEqual( FilesystemObject("test.dir").type, FilesystemObjectType.DIRECTORY) # Symlink os.symlink("test.txt", "test.lnk") self.assertEqual( FilesystemObject("test.lnk").type, FilesystemObjectType.SYMLINK) # Broken symlink os.symlink("missing", "missing.lnk") self.assertEqual( FilesystemObject("missing.lnk").type, FilesystemObjectType.SYMLINK)
def test_isaccessible(self): # Make test filesystem objects with open("test.txt", "wt") as fp: fp.write("test") self.assertTrue(FilesystemObject("test.txt").isaccessible) # Directory os.mkdir("test.dir") self.assertTrue(FilesystemObject("test.dir").isaccessible) # Symlink os.symlink("test.txt", "test.lnk") self.assertTrue(FilesystemObject("test.lnk").isaccessible) # Broken symlink os.symlink("missing", "missing.lnk") self.assertTrue(FilesystemObject("missing.lnk").isaccessible) # Set permissions to make objects unreadable os.chmod("test.txt", 0o044) self.assertFalse(FilesystemObject("test.txt").isaccessible) self.assertTrue(FilesystemObject("test.lnk").isaccessible) os.chmod("test.dir", 0o055) self.assertFalse(FilesystemObject("test.dir").isaccessible)
def test_handle_non_existent_object(self): self.assertFalse(FilesystemObject("missing").exists) self.assertEqual(FilesystemObject("missing").timestamp, None) self.assertEqual(FilesystemObject("missing").size, None) self.assertEqual(FilesystemObject("missing").uid, None) self.assertEqual(FilesystemObject("missing").username, None) self.assertEqual(FilesystemObject("missing").gid, None) self.assertEqual(FilesystemObject("missing").groupname, None) self.assertFalse(FilesystemObject("missing").islink) self.assertFalse(FilesystemObject("missing").isfile) self.assertFalse(FilesystemObject("missing").isdir) self.assertFalse(FilesystemObject("missing").iscompressed) self.assertEqual( FilesystemObject("missing").type, FilesystemObjectType.MISSING) self.assertEqual(FilesystemObject("missing").raw_symlink_target, None) self.assertFalse(FilesystemObject("missing").ishidden) self.assertFalse(FilesystemObject("missing").isaccessible) self.assertEqual(FilesystemObject("missing").md5sum, None) self.assertEqual(FilesystemObject("missing").linux_permissions, None)
def test_size(self): # Make file with open("test.txt", "wt") as fp: fp.write("test") self.assertEqual(FilesystemObject("test.txt").size, 4)