def test_to_dict(self): cdir = CDir('') self.assertEqual( { 'names': tuple(), 'path': '', 'type': 'DIR' }, cdir.to_dict() )
def should_include(self, cpath: CPath): assert cpath.is_rel assert cpath.has_parent() # does the parent dir matcher group exist parent_cdir = CDir(cpath.get_cpath_info().get_parent()) parent_matcher_group: Union[FsMatcherRootGroup, None] = self.__matcher_groups.get(parent_cdir.names, None) if parent_matcher_group is not None: # if parent says to exclude then look nothing beyond if parent_matcher_group.should_exclude(cpath): return False else: # look if any ancestor say that you should not match ancestor_cdir: CDir = parent_cdir while True: ancestor_cdir = ancestor_cdir.get_parent() ancestor_matcher_group: Union[FsMatcherGroup, None] = self.__matcher_groups.get(ancestor_cdir.names, None) if ancestor_matcher_group is not None: if ancestor_matcher_group.should_exclude(cpath): return False if ancestor_cdir.is_root(): break # No group asked to exclude, then include it return True
def test_is_ancestor(self): # root dir root cannot play ancestor self.assertFalse(CPathUtils.is_ancestor(CDir(""), CDir(""))) # ancestor must be dir self.assertRaises(CFSException, lambda: CPathUtils.is_ancestor( CPath("a"), CPath("a/b/c/d"))) # TODO: create proper exception self.assertRaises( CFSException, lambda: CPathUtils.is_ancestor(CFile("a", 1, 2), CPath("a/b/c/d"))) # a dir is ancestor self.assertTrue(CPathUtils.is_ancestor(CPath("a/"), CPath("a/b/c/d"))) # parent is also an ancestor self.assertTrue( CPathUtils.is_ancestor(CPath("a/b/c/"), CPath("a/b/c/d"))) # not ancestor self.assertFalse( CPathUtils.is_ancestor(CDir("a/b"), CFile("x/y", 1, 1)))
def test_is_parent(self): # root dir root cannot play parent child self.assertFalse(CPathUtils.is_parent(CDir(""), CDir(""))) # parent must be dir self.assertRaises( CFSException, lambda: CPathUtils.is_parent(CPath("a"), CPath("a/b"))) self.assertRaises( CFSException, lambda: CPathUtils.is_parent(CFile("a", 1, 2), CPath("a/b/"))) # a dir is parent self.assertTrue(CPathUtils.is_parent(CPath("a/"), CPath("a/b"))) self.assertTrue(CPathUtils.is_parent(CPath("a/b/"), CPath("a/b/c.txt"))) # not parent self.assertFalse( CPathUtils.is_ancestor(CDir("a/b"), CFile("x/y", 1, 1)))
def setUp(self) -> None: root_tree1 = CDirTree() root_tree1.add(CFile("d1/a.txt", 1, 2)) root_tree1.add(CDir("d2/")) root_tree1.add(CDir("d3/p/q/r")) root_tree1.add(CFile("d3/p/x.txt", 1, 5)) root_tree2 = CDirTree() root_tree2.add(CFile("d1/a.txt", 1, 2)) # root_tree2.add(CDir("dir2/")) - deleted root_tree2.add(CDir("d3/p/q/r")) root_tree2.add(CFile("d3/p/x.txt", 1, 5)) root_tree2.add(CFile("d3/p/y.txt", 1, 5)) # new # TODO: test in real file system too, with hash self.root_tree1 = root_tree1 self.root_tree2 = root_tree2 self.diff = self.root_tree1.diff(root_tree2)
def test_exists__path_type_independence(self): cpath1 = CPath(f"{self.this_test_dir_name}/subdir/afile") cfile1 = CFile(f"{self.this_test_dir_name}/subdir/afile", 1, 1) cpath2 = CPath(f"{self.this_test_dir_name}/subdir") cdir2 = CDir(f"{self.this_test_dir_name}/subdir") self.assertTrue(self.real_fs.exists(cpath1)) self.assertTrue(self.real_fs.exists(cfile1)) self.assertTrue(self.real_fs.exists(cpath2)) self.assertTrue(self.real_fs.exists(cdir2))
def test_without_dev_matcher(self): matcher_group = FsMatcherGroup(CDir("")) self.assertFalse(matcher_group.should_exclude(CPath(".git/"))) self.assertTrue(matcher_group.should_include(CPath(".git/")))
def with_dev_matcher(self): return self.add(dev_matcher.DevFsMatcher(), CDir(""))
def test_init_by_cdir(self): cdir = CDir("a") self.assertRaises(CFSExceptionInvalidPathName, lambda: CFile(cdir, 1, 1))
def test_is_file(self): self.assertFalse(CDir('').is_file())
def test_equals_abs_rel(self): cdir1 = CDir('a/b') cdir2 = CDir('/a/b') self.assertFalse(cdir1.equals(cdir2))
def test_equals_by_path(self): cdir1 = CDir('a/b') cdir2 = CDir('a/b/') self.assertTrue(cdir1.equals_by_path_only(cdir2))
def test_equals(self): cdir1 = CDir('a/b') cdir2 = CDir('a/b/') self.assertTrue(cdir1.equals(cdir2))
def test_get_type(self): cpath = CPath("a/") cdir = CDir("a") self.assertEqual(cpath.get_type(), cdir.get_type())
def test_is_dir(self): self.assertTrue(CDir('').is_dir())
def get_parent(self) -> Union['CDir', None]: cpath_info = self.get_cpath_info() if not cpath_info.has_parent(): return None return CDir(cpath_info.get_parent())
def test_init_by_cfile(self): cfile = CFile("a", 1, 2) self.assertRaises(CFSExceptionInvalidPathName, lambda: CDir(cfile))