def test_gitignore_parser__root_relative(self): """Tests whether a rule that is root relative come out as root relative path matcher""" path_matcher1 = gitignore_parser("/a/c/b")[0] self.assertTrue(path_matcher1.is_root_relative) path_matcher2 = gitignore_parser("a**b")[0] self.assertFalse(path_matcher2.is_root_relative)
def test_gitignore_parser__directories_only(self): """Tests whether a pattern that is only for directories come out as directories only path matcher""" path_matcher1 = gitignore_parser("a/")[0] self.assertTrue(path_matcher1.directories_only) path_matcher2 = gitignore_parser("!/z/a/")[0] self.assertTrue(path_matcher2.directories_only)
def test_gitignore_parser__negative(self): """Tests whether a negative rule passed come out as a negative path matcher""" path_matcher1 = gitignore_parser("!a")[0] self.assertTrue(path_matcher1.is_negative) path_matcher2 = gitignore_parser("!a/b/c")[0] self.assertTrue(path_matcher2.is_negative)
def test_gitignore_parser__matchers_double_asterisks(self): path_rule1 = gitignore_parser("a/**/b")[0] self.assertEqual(len(path_rule1.matchers), 3) self.assertIsInstance(path_rule1.matchers[1], DoubleAsteriskMatcher) self.assertIsInstance(path_rule1.matchers[0], CompMatcher) path_rule2 = gitignore_parser("/a/**/b/**/b/b/")[0] self.assertEqual(len(path_rule2.matchers), 6)
def test_matches__root_relative(self): """Tests whether the produced rule behaves well when it is root relative""" path_rule1 = gitignore_parser("a/*/z")[0] self.assertTrue(path_rule1.is_root_relative) "It matches a path that is root relative" self.assertTrue(path_rule1.matches(CPath("a/b/z"))) "But it doesn't match inner path" self.assertFalse(path_rule1.matches(CPath("1/a/b/z"))) "But if the rule is not root relative" path_rule2 = gitignore_parser("a*z")[0] self.assertTrue(path_rule2.matches(CPath("ayz")))
def test_matches__directories_only(self): """Tests whether directories only rule matchers properly""" path_rule1 = gitignore_parser("z/?u*ns/")[0] "This is a directories only rule" self.assertTrue(path_rule1.directories_only) "And it matches as it should be" self.assertTrue(path_rule1.matches(CPath("z/humans/"))) path_rule2 = gitignore_parser("z/?uman")[0] "This is NOT a directories only rule" self.assertFalse(path_rule2.directories_only) "But it matches as it should be" self.assertTrue(path_rule2.matches(CPath("z/human"))) "It matches both filesCpath (above) and directories (below)" self.assertTrue(path_rule2.matches(CPath("z/human/")))
def test_matches(self): path_matcher1 = gitignore_parser("!a")[0] self.assertFalse(path_matcher1.matches(CPath("a"))) self.assertFalse(path_matcher1.matches(CPath("z/a"))) self.assertFalse(path_matcher1.matches(CPath("b")))
def test_matches_simple__negative(self): path_matcher1 = gitignore_parser("!a")[0] self.assertTrue(path_matcher1.matches_simple(CPath("a"))) self.assertTrue(path_matcher1.matches_simple(CPath("z/a"))) self.assertFalse(path_matcher1.matches_simple(CPath("b")))
def test_gitignore_parser__count_rules(self): path_matchers = gitignore_parser(""" # this is a comment - don't count. # the following is invalid, don't count / ** /** a/v """) self.assertEqual(len(path_matchers), 3)
def test_matches_simple__parent_dir_exclusion(self): path_matcher1 = gitignore_parser("logs")[0] "File will be excluded" self.assertTrue(path_matcher1.matches_simple(CPath("logs"))) "So does directory" self.assertTrue(path_matcher1.matches_simple(CPath("logs/"))) "And any file inside that dire" self.assertTrue( path_matcher1.matches_simple(CPath("logs/important.log"))) "And again any dir inside that dir" self.assertTrue( path_matcher1.matches_simple(CPath("logs/important.log/")))
def test_matches_simple__parent_dir_exclusion___dir_only(self): path_matcher2 = gitignore_parser("logs/")[0] "Notice here that it will not match" "Dir only pattern will not match file" self.assertFalse(path_matcher2.matches_simple(CPath("logs"))) "But will match dirs as usual" self.assertTrue(path_matcher2.matches_simple(CPath("logs/"))) "Subdirs will match as before" self.assertTrue( path_matcher2.matches_simple(CPath("logs/important.log/"))) "And also anything inside that dir pattern" self.assertTrue( path_matcher2.matches_simple(CPath("logs/important.log")))
def test_gitignore_parser__matchers_literal_double_asterisks(self): """ From gitignore doc: Two consecutive asterisks ("**") in patterns matched against full pathname may have special meaning: A leading "**" followed by a slash means match in all directories. For example, "**/foo" matches file or directory "foo" anywhere, the same as pattern "foo". "**/foo/bar" matches file or directory "bar" anywhere that is directly under directory "foo". A trailing "/**" matches everything inside. For example, "abc/**" matches all files inside directory "abc", relative to the location of the .gitignore file, with infinite depth. A slash followed by two consecutive asterisks then a slash matches zero or more directories. For example, "a/**/b" matches "a/b", "a/x/b", "a/x/y/b" and so on. Other consecutive asterisks are considered regular asterisks and will match according to the previous rules. I am up for the Other... port here """ path_rule1 = gitignore_parser("a**b")[0] self.assertEqual(len(path_rule1.matchers), 1) self.assertIsInstance(path_rule1.matchers[0], CompMatcher)
def test_matches__negative(self): """Tests whether negative matching works properly""" self.assertFalse(gitignore_parser("!a")[0].matches(CPath("b"))) self.assertFalse(gitignore_parser("!a")[0].matches(CPath("a")))
def test_gitignore_parser__literal_negation_sign(self): "Literal starting bang !" path_matcher3 = gitignore_parser(r"\!a/b/c")[0] self.assertFalse(path_matcher3.is_negative)
def test_gitignore_parser__appended_slash_non_root_relative(self): "But appending something with just a slash that is just a single directory will not make it root relative" self.assertFalse(gitignore_parser("a/")[0].is_root_relative)
def test_gitignore_parser__prepended_slash_root_relative(self): "Prepending something with just a slash that is just a single directory will make it root relative" self.assertTrue(gitignore_parser("/a.*")[0].is_root_relative)
def test_raw_rule(self): path_matcher1 = gitignore_parser("!a/**/b/*?l/x.txt/")[0] self.assertEqual(path_matcher1.raw_rule, "!a/**/b/*?l/x.txt/")
def __init__(self, text=""): self.__path_matchers: List[PathMatcher] = gitignore_parser(text)
def test_matches__critical_double_asterisks(self): # TODO: make many rules for critial double asterisks - including within it all other kinds of rules path_matcher1 = gitignore_parser("**/a/**/**/m/z/z/z/z")[0] self.assertFalse(path_matcher1.matches(CPath("a/m/z"))) self.assertTrue(path_matcher1.matches(CPath("l/a/z/a/m/z/z/z/z")))
def test_matchers(self): "Consecutive double asterisks will be counted as single double asterisk and non stand alone double asterisks \ are literal strings" path_matcher1 = gitignore_parser("!a/**/**/b/*?l/**/**/**/**x.txt/")[0] self.assertEqual(len(path_matcher1.matchers), 6)