class LocalPathTest(unittest.TestCase): def setUp(self): self.longpath = LocalPath("/some/long/path/to/file.txt") def test_name(self): name = self.longpath.name self.assertTrue(isinstance(name, six.string_types)) self.assertEqual("file.txt", str(name)) def test_dirname(self): name = self.longpath.dirname self.assertTrue(isinstance(name, LocalPath)) self.assertEqual("/some/long/path/to", str(name).replace("\\", "/")) def test_indexing(self): p = LocalPath("/some/long/path/to/dir") self.assertEqual(p[:-1], LocalPath("/some/long/path/to")) self.assertEqual(p[1:-1], RelativePath("long/path/to".split("/"))) self.assertEqual(p[1], "long") self.assertEqual(p[::2], "some path dir".split()) self.assertEqual(p['file.txt'], LocalPath("/some/long/path/to/dir/file.txt")) self.assertEqual(p['subdir/file.txt'], LocalPath("/some/long/path/to/dir/subdir/file.txt")) self.assertEqual(p['/root/file.txt'], LocalPath("/root/file.txt")) def test_uri(self): self.assertEqual("file:///some/long/path/to/file.txt", self.longpath.as_uri()) @skip_without_chown def test_chown(self): with local.tempdir() as dir: p = dir / "foo.txt" p.write(six.b("hello")) self.assertEqual(p.uid, os.getuid()) self.assertEqual(p.gid, os.getgid()) p.chown(p.uid.name) self.assertEqual(p.uid, os.getuid()) def test_split(self): p = local.path("/var/log/messages") self.assertEqual(p.split(), ["var", "log", "messages"]) def test_suffix(self): p1 = self.longpath p2 = local.path("file.tar.gz") self.assertEqual(p1.suffix, ".txt") self.assertEqual(p1.suffixes, [".txt"]) self.assertEqual(p2.suffix, ".gz") self.assertEqual(p2.suffixes, [".tar",".gz"]) self.assertEqual(p1.with_suffix(".tar.gz"), local.path("/some/long/path/to/file.tar.gz")) self.assertEqual(p2.with_suffix(".other"), local.path("file.tar.other")) self.assertEqual(p2.with_suffix(".other", 2), local.path("file.other")) self.assertEqual(p2.with_suffix(".other", 0), local.path("file.tar.gz.other")) self.assertEqual(p2.with_suffix(".other", None), local.path("file.other")) def test_newname(self): p1 = self.longpath p2 = local.path("file.tar.gz") self.assertEqual(p1.with_name("something.tar"), local.path("/some/long/path/to/something.tar")) self.assertEqual(p2.with_name("something.tar"), local.path("something.tar")) def test_relative_to(self): p = local.path("/var/log/messages") self.assertEqual(p.relative_to("/var/log/messages"), RelativePath([])) self.assertEqual(p.relative_to("/var/"), RelativePath(["log", "messages"])) self.assertEqual(p.relative_to("/"), RelativePath(["var", "log", "messages"])) self.assertEqual(p.relative_to("/var/tmp"), RelativePath(["..", "log", "messages"])) self.assertEqual(p.relative_to("/opt"), RelativePath(["..", "var", "log", "messages"])) self.assertEqual(p.relative_to("/opt/lib"), RelativePath(["..", "..", "var", "log", "messages"])) for src in [local.path("/var/log/messages"), local.path("/var"), local.path("/opt/lib")]: delta = p.relative_to(src) self.assertEqual(src + delta, p) def test_read_write(self): with local.tempdir() as dir: f = dir / "test.txt" text = six.b('hello world\xd7\xa9\xd7\x9c\xd7\x95\xd7\x9d').decode("utf8") f.write(text, "utf8") text2 = f.read("utf8") self.assertEqual(text, text2) def test_parts(self): parts = self.longpath.parts self.assertEqual(parts, ('/', 'some', 'long', 'path', 'to', 'file.txt')) def test_stem(self): self.assertEqual(self.longpath.stem, "file") p = local.path("/some/directory") self.assertEqual(p.stem, "directory") @skipIf(pathlib is None, "This test requires pathlib") def test_root_drive(self): pl_path = pathlib.Path("/some/long/path/to/file.txt").absolute() self.assertEqual(self.longpath.root, pl_path.root) self.assertEqual(self.longpath.drive, pl_path.drive) p_path = local.cwd / "somefile.txt" pl_path = pathlib.Path("somefile.txt").absolute() self.assertEqual(p_path.root, pl_path.root) self.assertEqual(p_path.drive, pl_path.drive) @skipIf(pathlib is None, "This test requires pathlib") def test_compare_pathlib(self): def filename_compare(name): p = local.path(str(name)) pl = pathlib.Path(str(name)).absolute() self.assertEqual(str(p), str(pl)) self.assertEqual(p.parts, pl.parts) self.assertEqual(p.exists(), pl.exists()) self.assertEqual(p.as_uri(), pl.as_uri()) self.assertEqual(str(p.with_suffix('.this')), str(pl.with_suffix('.this'))) self.assertEqual(p.name, pl.name) filename_compare("/some/long/path/to/file.txt") filename_compare(local.cwd / "somefile.txt") filename_compare("/some/long/path/") filename_compare("/some/long/path") filename_compare(__file__) def test_suffix_expected(self): self.assertEqual(self.longpath.preferred_suffix('.tar'), self.longpath) self.assertEqual((local.cwd / 'this').preferred_suffix('.txt'), local.cwd / 'this.txt')