示例#1
0
 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)
示例#2
0
 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")
示例#3
0
 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)
示例#4
0
 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)
示例#5
0
 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")
示例#6
0
 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)
示例#7
0
 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)
示例#8
0
 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---")
示例#9
0
 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)
示例#10
0
 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)
示例#11
0
 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)
示例#12
0
 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, "")
示例#13
0
 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)
示例#14
0
 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)
示例#15
0
 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)
示例#16
0
 def test_size(self):
     # Make file
     with open("test.txt", "wt") as fp:
         fp.write("test")
     self.assertEqual(FilesystemObject("test.txt").size, 4)