class RootPath(unittest.TestCase): def test_root_path(self): self.p = Path("/") self.assertEqual(self.p.fslash(), "/") self.assertEqual(self.p.bslash(), "\\") def test_drive_letter_root_path(self): self.p = Path("C:\\") self.assertEqual(self.p.fslash(), "C:/") self.assertEqual(self.p.bslash(), "C:\\")
class SpecifyDriveLetterUse(unittest.TestCase): def test_remove_from_path(self): self.p = Path("C:\\a\\b\\c") self.assertEqual(self.p.fslash(with_drive=False), "/a/b/c") self.assertEqual(self.p.bslash(with_drive=False), "\\a\\b\\c") def test_remove_from_root_path(self): self.p = Path("C:\\") self.assertEqual(self.p.fslash(with_drive=False), "/") self.assertEqual(self.p.bslash(with_drive=False), "\\")
class WindowsMixedPathTest(unittest.TestCase): def test_abs_in_fslash_out(self): self.p = Path("\\a\\b\\c/d/e") self.assertEqual(self.p.fslash(), "/a/b/c/d/e") def test_abs_in_bslash_out(self): self.p = Path("\\a\\b\\c/d/e") self.assertEqual(self.p.bslash(), "\\a\\b\\c\\d\\e") def test_letter_abs_in_fslash_out(self): self.p = Path("C:\\a\\b\\c/d/e") self.assertEqual(self.p.fslash(), "C:/a/b/c/d/e") def test_letter_abs_in_bslash_out(self): self.p = Path("C:\\a\\b\\c/d/e") self.assertEqual(self.p.bslash(), "C:\\a\\b\\c\\d\\e")
class AbsPosixPathTest(unittest.TestCase): def setUp(self): self.p = Path("/a/b/c") def test_fslash_out(self): self.assertEqual(self.p.fslash(), "/a/b/c") def test_win_path_out(self): self.assertEqual(self.p.bslash(), "\\a\\b\\c")
class PathExpansionTest(unittest.TestCase): def setUp(self): self.env = { "HOME": "/users/joebloggs", "SHOT": "/metropolis/shot01", "DEPT": "texturing", } def test_posix_tilde_input(self): with mock.patch.dict("os.environ", self.env): self.p = Path("~/a/b/c") self.assertEqual(self.p.fslash(), "/users/joebloggs/a/b/c") def test_posix_var_input(self): with mock.patch.dict("os.environ", self.env): self.p = Path("$SHOT/a/b/c") self.assertEqual(self.p.fslash(), "/metropolis/shot01/a/b/c") def test_posix_two_var_input(self): with mock.patch.dict("os.environ", self.env): self.p = Path("$SHOT/a/b/$DEPT/c") self.assertEqual(self.p.fslash(), "/metropolis/shot01/a/b/texturing/c") def test_windows_var_input(self): with mock.patch.dict("os.environ", self.env): self.p = Path("$HOME\\a\\b\\c") self.assertEqual(self.p.bslash(), "\\users\\joebloggs\\a\\b\\c") self.assertEqual(self.p.fslash(), "/users/joebloggs/a/b/c") def test_tilde_no_expand(self): with mock.patch.dict("os.environ", self.env): self.p = Path("~/a/b/c", no_expand=True) self.assertEqual(self.p.fslash(), "~/a/b/c") def test_posix_var_no_expand(self): with mock.patch.dict("os.environ", self.env): self.p = Path("$SHOT/a/b/c", no_expand=True) self.assertEqual(self.p.fslash(), "$SHOT/a/b/c") def no_expand_variable_considered_relative(self): with mock.patch.dict("os.environ", self.env): self.p = Path("$SHOT/a/b/c", no_expand=True) self.assertTrue(self.p.relative) self.assertFalse(self.p.absolute) def expanded_variable_considered_absolute(self): with mock.patch.dict("os.environ", self.env): self.p = Path("$SHOT/a/b/c", no_expand=False) self.assertFalse(self.p.relative) self.assertTrue(self.p.absolute)
class AbsWindowsPathTest(unittest.TestCase): def setUp(self): self.p = Path("C:\\a\\b\\c") def test_fslash_out(self): self.assertEqual(self.p.fslash(), "C:/a/b/c") def test_win_path_out(self): self.assertEqual(self.p.bslash(), "C:\\a\\b\\c") # consider just testing on both platforms def test_os_path_out(self): with mock.patch("os.name", "posix"): self.assertEqual(self.p.os_path(), "C:/a/b/c") with mock.patch("os.name", "nt"): self.assertEqual(self.p.os_path(), "C:\\a\\b\\c")
def test_initialize_with_relative_components(self): p = Path(["a", "b", "c"]) self.assertEqual(p.bslash(with_drive=True), "a\\b\\c")
def test_up_down_sibling_bslash(self): p = Path("../a") self.assertEqual(p.bslash(), "..\\a")
class MiscPathTest(unittest.TestCase): def test_many_to_single_backslashes_bslash_out(self): self.p = Path("C:\\\\a\\b///c") self.assertEqual(self.p.bslash(), "C:\\a\\b\\c")