Mercurial > public > mercurial-scm > hg-stable
diff tests/test-match.py @ 43954:8b1a9ba375e5
match: make sure `root` argument is always an absolute path (API)
The `root` argument should already be an absolute path, but we had
tests that passed a relative path. This patch fixes up the tests and
adds an assertion.
This assumes that `os.path.isabs('/repo')` will be `True` on all
platforms we care to run tests on. Augie tested for me that it does
work on Windows, so that's good enough for me.
Differential Revision: https://phab.mercurial-scm.org/D7649
author | Martin von Zweigbergk <martinvonz@google.com> |
---|---|
date | Fri, 13 Dec 2019 11:21:31 -0800 |
parents | c2c3ee8794dd |
children | 8f67735344ae |
line wrap: on
line diff
--- a/tests/test-match.py Fri Dec 06 20:29:02 2019 -0500 +++ b/tests/test-match.py Fri Dec 13 11:21:31 2019 -0800 @@ -10,6 +10,9 @@ ) +noop_auditor = lambda name: None + + class BaseMatcherTests(unittest.TestCase): def testVisitdir(self): m = matchmod.basematcher() @@ -63,7 +66,7 @@ class PatternMatcherTests(unittest.TestCase): def testVisitdirPrefix(self): - m = matchmod.match(b'x', b'', patterns=[b'path:dir/subdir']) + m = matchmod.match(b'/repo', b'', patterns=[b'path:dir/subdir']) assert isinstance(m, matchmod.patternmatcher) self.assertTrue(m.visitdir(b'')) self.assertTrue(m.visitdir(b'dir')) @@ -73,7 +76,7 @@ self.assertFalse(m.visitdir(b'folder')) def testVisitchildrensetPrefix(self): - m = matchmod.match(b'x', b'', patterns=[b'path:dir/subdir']) + m = matchmod.match(b'/repo', b'', patterns=[b'path:dir/subdir']) assert isinstance(m, matchmod.patternmatcher) self.assertEqual(m.visitchildrenset(b''), b'this') self.assertEqual(m.visitchildrenset(b'dir'), b'this') @@ -83,7 +86,7 @@ self.assertEqual(m.visitchildrenset(b'folder'), set()) def testVisitdirRootfilesin(self): - m = matchmod.match(b'x', b'', patterns=[b'rootfilesin:dir/subdir']) + m = matchmod.match(b'/repo', b'', patterns=[b'rootfilesin:dir/subdir']) assert isinstance(m, matchmod.patternmatcher) self.assertFalse(m.visitdir(b'dir/subdir/x')) self.assertFalse(m.visitdir(b'folder')) @@ -93,7 +96,7 @@ self.assertFalse(m.visitdir(b'dir/subdir')) def testVisitchildrensetRootfilesin(self): - m = matchmod.match(b'x', b'', patterns=[b'rootfilesin:dir/subdir']) + m = matchmod.match(b'/repo', b'', patterns=[b'rootfilesin:dir/subdir']) assert isinstance(m, matchmod.patternmatcher) self.assertEqual(m.visitchildrenset(b'dir/subdir/x'), set()) self.assertEqual(m.visitchildrenset(b'folder'), set()) @@ -104,7 +107,7 @@ self.assertEqual(m.visitchildrenset(b'dir/subdir'), set()) def testVisitdirGlob(self): - m = matchmod.match(b'x', b'', patterns=[b'glob:dir/z*']) + m = matchmod.match(b'/repo', b'', patterns=[b'glob:dir/z*']) assert isinstance(m, matchmod.patternmatcher) self.assertTrue(m.visitdir(b'')) self.assertTrue(m.visitdir(b'dir')) @@ -114,7 +117,7 @@ self.assertTrue(m.visitdir(b'dir/subdir/x')) def testVisitchildrensetGlob(self): - m = matchmod.match(b'x', b'', patterns=[b'glob:dir/z*']) + m = matchmod.match(b'/repo', b'', patterns=[b'glob:dir/z*']) assert isinstance(m, matchmod.patternmatcher) self.assertEqual(m.visitchildrenset(b''), b'this') self.assertEqual(m.visitchildrenset(b'folder'), set()) @@ -126,7 +129,7 @@ class IncludeMatcherTests(unittest.TestCase): def testVisitdirPrefix(self): - m = matchmod.match(b'x', b'', include=[b'path:dir/subdir']) + m = matchmod.match(b'/repo', b'', include=[b'path:dir/subdir']) assert isinstance(m, matchmod.includematcher) self.assertTrue(m.visitdir(b'')) self.assertTrue(m.visitdir(b'dir')) @@ -136,7 +139,7 @@ self.assertFalse(m.visitdir(b'folder')) def testVisitchildrensetPrefix(self): - m = matchmod.match(b'x', b'', include=[b'path:dir/subdir']) + m = matchmod.match(b'/repo', b'', include=[b'path:dir/subdir']) assert isinstance(m, matchmod.includematcher) self.assertEqual(m.visitchildrenset(b''), {b'dir'}) self.assertEqual(m.visitchildrenset(b'dir'), {b'subdir'}) @@ -146,7 +149,7 @@ self.assertEqual(m.visitchildrenset(b'folder'), set()) def testVisitdirRootfilesin(self): - m = matchmod.match(b'x', b'', include=[b'rootfilesin:dir/subdir']) + m = matchmod.match(b'/repo', b'', include=[b'rootfilesin:dir/subdir']) assert isinstance(m, matchmod.includematcher) self.assertTrue(m.visitdir(b'')) self.assertTrue(m.visitdir(b'dir')) @@ -155,7 +158,7 @@ self.assertFalse(m.visitdir(b'folder')) def testVisitchildrensetRootfilesin(self): - m = matchmod.match(b'x', b'', include=[b'rootfilesin:dir/subdir']) + m = matchmod.match(b'/repo', b'', include=[b'rootfilesin:dir/subdir']) assert isinstance(m, matchmod.includematcher) self.assertEqual(m.visitchildrenset(b''), {b'dir'}) self.assertEqual(m.visitchildrenset(b'dir'), {b'subdir'}) @@ -164,7 +167,7 @@ self.assertEqual(m.visitchildrenset(b'folder'), set()) def testVisitdirGlob(self): - m = matchmod.match(b'x', b'', include=[b'glob:dir/z*']) + m = matchmod.match(b'/repo', b'', include=[b'glob:dir/z*']) assert isinstance(m, matchmod.includematcher) self.assertTrue(m.visitdir(b'')) self.assertTrue(m.visitdir(b'dir')) @@ -174,7 +177,7 @@ self.assertTrue(m.visitdir(b'dir/subdir/x')) def testVisitchildrensetGlob(self): - m = matchmod.match(b'x', b'', include=[b'glob:dir/z*']) + m = matchmod.match(b'/repo', b'', include=[b'glob:dir/z*']) assert isinstance(m, matchmod.includematcher) self.assertEqual(m.visitchildrenset(b''), {b'dir'}) self.assertEqual(m.visitchildrenset(b'folder'), set()) @@ -286,7 +289,7 @@ def testVisitdirM2SubdirPrefix(self): m1 = matchmod.alwaysmatcher() - m2 = matchmod.match(b'', b'', patterns=[b'path:dir/subdir']) + m2 = matchmod.match(b'/repo', b'', patterns=[b'path:dir/subdir']) dm = matchmod.differencematcher(m1, m2) self.assertEqual(dm.visitdir(b''), True) self.assertEqual(dm.visitdir(b'dir'), True) @@ -301,7 +304,7 @@ def testVisitchildrensetM2SubdirPrefix(self): m1 = matchmod.alwaysmatcher() - m2 = matchmod.match(b'', b'', patterns=[b'path:dir/subdir']) + m2 = matchmod.match(b'/repo', b'', patterns=[b'path:dir/subdir']) dm = matchmod.differencematcher(m1, m2) self.assertEqual(dm.visitchildrenset(b''), b'this') self.assertEqual(dm.visitchildrenset(b'dir'), b'this') @@ -317,8 +320,8 @@ # We're using includematcher instead of patterns because it behaves slightly # better (giving narrower results) than patternmatcher. def testVisitdirIncludeInclude(self): - m1 = matchmod.match(b'', b'', include=[b'path:dir/subdir']) - m2 = matchmod.match(b'', b'', include=[b'rootfilesin:dir']) + m1 = matchmod.match(b'/repo', b'', include=[b'path:dir/subdir']) + m2 = matchmod.match(b'/repo', b'', include=[b'rootfilesin:dir']) dm = matchmod.differencematcher(m1, m2) self.assertEqual(dm.visitdir(b''), True) self.assertEqual(dm.visitdir(b'dir'), True) @@ -332,8 +335,8 @@ self.assertEqual(dm.visitdir(b'dir/subdir/x'), True) def testVisitchildrensetIncludeInclude(self): - m1 = matchmod.match(b'', b'', include=[b'path:dir/subdir']) - m2 = matchmod.match(b'', b'', include=[b'rootfilesin:dir']) + m1 = matchmod.match(b'/repo', b'', include=[b'path:dir/subdir']) + m2 = matchmod.match(b'/repo', b'', include=[b'rootfilesin:dir']) dm = matchmod.differencematcher(m1, m2) self.assertEqual(dm.visitchildrenset(b''), {b'dir'}) self.assertEqual(dm.visitchildrenset(b'dir'), {b'subdir'}) @@ -402,7 +405,7 @@ def testVisitdirM2SubdirPrefix(self): m1 = matchmod.alwaysmatcher() - m2 = matchmod.match(b'', b'', patterns=[b'path:dir/subdir']) + m2 = matchmod.match(b'/repo', b'', patterns=[b'path:dir/subdir']) im = matchmod.intersectmatchers(m1, m2) self.assertEqual(im.visitdir(b''), True) self.assertEqual(im.visitdir(b'dir'), True) @@ -417,7 +420,7 @@ def testVisitchildrensetM2SubdirPrefix(self): m1 = matchmod.alwaysmatcher() - m2 = matchmod.match(b'', b'', include=[b'path:dir/subdir']) + m2 = matchmod.match(b'/repo', b'', include=[b'path:dir/subdir']) im = matchmod.intersectmatchers(m1, m2) self.assertEqual(im.visitchildrenset(b''), {b'dir'}) self.assertEqual(im.visitchildrenset(b'dir'), {b'subdir'}) @@ -431,8 +434,8 @@ # We're using includematcher instead of patterns because it behaves slightly # better (giving narrower results) than patternmatcher. def testVisitdirIncludeInclude(self): - m1 = matchmod.match(b'', b'', include=[b'path:dir/subdir']) - m2 = matchmod.match(b'', b'', include=[b'rootfilesin:dir']) + m1 = matchmod.match(b'/repo', b'', include=[b'path:dir/subdir']) + m2 = matchmod.match(b'/repo', b'', include=[b'rootfilesin:dir']) im = matchmod.intersectmatchers(m1, m2) self.assertEqual(im.visitdir(b''), True) self.assertEqual(im.visitdir(b'dir'), True) @@ -443,8 +446,8 @@ self.assertFalse(im.visitdir(b'dir/subdir/x')) def testVisitchildrensetIncludeInclude(self): - m1 = matchmod.match(b'', b'', include=[b'path:dir/subdir']) - m2 = matchmod.match(b'', b'', include=[b'rootfilesin:dir']) + m1 = matchmod.match(b'/repo', b'', include=[b'path:dir/subdir']) + m2 = matchmod.match(b'/repo', b'', include=[b'rootfilesin:dir']) im = matchmod.intersectmatchers(m1, m2) self.assertEqual(im.visitchildrenset(b''), {b'dir'}) self.assertEqual(im.visitchildrenset(b'dir'), b'this') @@ -457,8 +460,8 @@ # We're using includematcher instead of patterns because it behaves slightly # better (giving narrower results) than patternmatcher. def testVisitdirIncludeInclude2(self): - m1 = matchmod.match(b'', b'', include=[b'path:dir/subdir']) - m2 = matchmod.match(b'', b'', include=[b'path:folder']) + m1 = matchmod.match(b'/repo', b'', include=[b'path:dir/subdir']) + m2 = matchmod.match(b'/repo', b'', include=[b'path:folder']) im = matchmod.intersectmatchers(m1, m2) # FIXME: is True correct here? self.assertEqual(im.visitdir(b''), True) @@ -470,8 +473,8 @@ self.assertFalse(im.visitdir(b'dir/subdir/x')) def testVisitchildrensetIncludeInclude2(self): - m1 = matchmod.match(b'', b'', include=[b'path:dir/subdir']) - m2 = matchmod.match(b'', b'', include=[b'path:folder']) + m1 = matchmod.match(b'/repo', b'', include=[b'path:dir/subdir']) + m2 = matchmod.match(b'/repo', b'', include=[b'path:folder']) im = matchmod.intersectmatchers(m1, m2) # FIXME: is set() correct here? self.assertEqual(im.visitchildrenset(b''), set()) @@ -485,8 +488,8 @@ # We're using includematcher instead of patterns because it behaves slightly # better (giving narrower results) than patternmatcher. def testVisitdirIncludeInclude3(self): - m1 = matchmod.match(b'', b'', include=[b'path:dir/subdir/x']) - m2 = matchmod.match(b'', b'', include=[b'path:dir/subdir']) + m1 = matchmod.match(b'/repo', b'', include=[b'path:dir/subdir/x']) + m2 = matchmod.match(b'/repo', b'', include=[b'path:dir/subdir']) im = matchmod.intersectmatchers(m1, m2) self.assertEqual(im.visitdir(b''), True) self.assertEqual(im.visitdir(b'dir'), True) @@ -498,8 +501,8 @@ self.assertEqual(im.visitdir(b'dir/subdir/x'), True) def testVisitchildrensetIncludeInclude3(self): - m1 = matchmod.match(b'', b'', include=[b'path:dir/subdir/x']) - m2 = matchmod.match(b'', b'', include=[b'path:dir/subdir']) + m1 = matchmod.match(b'/repo', b'', include=[b'path:dir/subdir/x']) + m2 = matchmod.match(b'/repo', b'', include=[b'path:dir/subdir']) im = matchmod.intersectmatchers(m1, m2) self.assertEqual(im.visitchildrenset(b''), {b'dir'}) self.assertEqual(im.visitchildrenset(b'dir'), {b'subdir'}) @@ -513,8 +516,8 @@ # We're using includematcher instead of patterns because it behaves slightly # better (giving narrower results) than patternmatcher. def testVisitdirIncludeInclude4(self): - m1 = matchmod.match(b'', b'', include=[b'path:dir/subdir/x']) - m2 = matchmod.match(b'', b'', include=[b'path:dir/subdir/z']) + m1 = matchmod.match(b'/repo', b'', include=[b'path:dir/subdir/x']) + m2 = matchmod.match(b'/repo', b'', include=[b'path:dir/subdir/z']) im = matchmod.intersectmatchers(m1, m2) # OPT: these next three could probably be False as well. self.assertEqual(im.visitdir(b''), True) @@ -526,8 +529,8 @@ self.assertFalse(im.visitdir(b'dir/subdir/x')) def testVisitchildrensetIncludeInclude4(self): - m1 = matchmod.match(b'', b'', include=[b'path:dir/subdir/x']) - m2 = matchmod.match(b'', b'', include=[b'path:dir/subdir/z']) + m1 = matchmod.match(b'/repo', b'', include=[b'path:dir/subdir/x']) + m2 = matchmod.match(b'/repo', b'', include=[b'path:dir/subdir/z']) im = matchmod.intersectmatchers(m1, m2) # OPT: these next two could probably be set() as well. self.assertEqual(im.visitchildrenset(b''), {b'dir'}) @@ -620,7 +623,7 @@ def testVisitdirM2SubdirPrefix(self): m1 = matchmod.alwaysmatcher() - m2 = matchmod.match(b'', b'', patterns=[b'path:dir/subdir']) + m2 = matchmod.match(b'/repo', b'', patterns=[b'path:dir/subdir']) um = matchmod.unionmatcher([m1, m2]) self.assertEqual(um.visitdir(b''), b'all') self.assertEqual(um.visitdir(b'dir'), b'all') @@ -632,7 +635,7 @@ def testVisitchildrensetM2SubdirPrefix(self): m1 = matchmod.alwaysmatcher() - m2 = matchmod.match(b'', b'', include=[b'path:dir/subdir']) + m2 = matchmod.match(b'/repo', b'', include=[b'path:dir/subdir']) um = matchmod.unionmatcher([m1, m2]) self.assertEqual(um.visitchildrenset(b''), b'all') self.assertEqual(um.visitchildrenset(b'dir'), b'all') @@ -645,8 +648,8 @@ # We're using includematcher instead of patterns because it behaves slightly # better (giving narrower results) than patternmatcher. def testVisitdirIncludeInclude(self): - m1 = matchmod.match(b'', b'', include=[b'path:dir/subdir']) - m2 = matchmod.match(b'', b'', include=[b'rootfilesin:dir']) + m1 = matchmod.match(b'/repo', b'', include=[b'path:dir/subdir']) + m2 = matchmod.match(b'/repo', b'', include=[b'rootfilesin:dir']) um = matchmod.unionmatcher([m1, m2]) self.assertEqual(um.visitdir(b''), True) self.assertEqual(um.visitdir(b'dir'), True) @@ -658,8 +661,8 @@ self.assertEqual(um.visitdir(b'dir/subdir/x'), True) def testVisitchildrensetIncludeInclude(self): - m1 = matchmod.match(b'', b'', include=[b'path:dir/subdir']) - m2 = matchmod.match(b'', b'', include=[b'rootfilesin:dir']) + m1 = matchmod.match(b'/repo', b'', include=[b'path:dir/subdir']) + m2 = matchmod.match(b'/repo', b'', include=[b'rootfilesin:dir']) um = matchmod.unionmatcher([m1, m2]) self.assertEqual(um.visitchildrenset(b''), {b'dir'}) self.assertEqual(um.visitchildrenset(b'dir'), b'this') @@ -673,8 +676,8 @@ # We're using includematcher instead of patterns because it behaves slightly # better (giving narrower results) than patternmatcher. def testVisitdirIncludeInclude2(self): - m1 = matchmod.match(b'', b'', include=[b'path:dir/subdir']) - m2 = matchmod.match(b'', b'', include=[b'path:folder']) + m1 = matchmod.match(b'/repo', b'', include=[b'path:dir/subdir']) + m2 = matchmod.match(b'/repo', b'', include=[b'path:folder']) um = matchmod.unionmatcher([m1, m2]) self.assertEqual(um.visitdir(b''), True) self.assertEqual(um.visitdir(b'dir'), True) @@ -686,8 +689,8 @@ self.assertEqual(um.visitdir(b'dir/subdir/x'), True) def testVisitchildrensetIncludeInclude2(self): - m1 = matchmod.match(b'', b'', include=[b'path:dir/subdir']) - m2 = matchmod.match(b'', b'', include=[b'path:folder']) + m1 = matchmod.match(b'/repo', b'', include=[b'path:dir/subdir']) + m2 = matchmod.match(b'/repo', b'', include=[b'path:folder']) um = matchmod.unionmatcher([m1, m2]) self.assertEqual(um.visitchildrenset(b''), {b'folder', b'dir'}) self.assertEqual(um.visitchildrenset(b'dir'), {b'subdir'}) @@ -701,8 +704,8 @@ # We're using includematcher instead of patterns because it behaves slightly # better (giving narrower results) than patternmatcher. def testVisitdirIncludeInclude3(self): - m1 = matchmod.match(b'', b'', include=[b'path:dir/subdir/x']) - m2 = matchmod.match(b'', b'', include=[b'path:dir/subdir']) + m1 = matchmod.match(b'/repo', b'', include=[b'path:dir/subdir/x']) + m2 = matchmod.match(b'/repo', b'', include=[b'path:dir/subdir']) um = matchmod.unionmatcher([m1, m2]) self.assertEqual(um.visitdir(b''), True) self.assertEqual(um.visitdir(b'dir'), True) @@ -714,8 +717,8 @@ self.assertEqual(um.visitdir(b'dir/subdir/z'), True) def testVisitchildrensetIncludeInclude3(self): - m1 = matchmod.match(b'', b'', include=[b'path:dir/subdir/x']) - m2 = matchmod.match(b'', b'', include=[b'path:dir/subdir']) + m1 = matchmod.match(b'/repo', b'', include=[b'path:dir/subdir/x']) + m2 = matchmod.match(b'/repo', b'', include=[b'path:dir/subdir']) um = matchmod.unionmatcher([m1, m2]) self.assertEqual(um.visitchildrenset(b''), {b'dir'}) self.assertEqual(um.visitchildrenset(b'dir'), {b'subdir'}) @@ -729,8 +732,8 @@ # We're using includematcher instead of patterns because it behaves slightly # better (giving narrower results) than patternmatcher. def testVisitdirIncludeInclude4(self): - m1 = matchmod.match(b'', b'', include=[b'path:dir/subdir/x']) - m2 = matchmod.match(b'', b'', include=[b'path:dir/subdir/z']) + m1 = matchmod.match(b'/repo', b'', include=[b'path:dir/subdir/x']) + m2 = matchmod.match(b'/repo', b'', include=[b'path:dir/subdir/z']) um = matchmod.unionmatcher([m1, m2]) # OPT: these next three could probably be False as well. self.assertEqual(um.visitdir(b''), True) @@ -742,8 +745,8 @@ self.assertEqual(um.visitdir(b'dir/subdir/x'), b'all') def testVisitchildrensetIncludeInclude4(self): - m1 = matchmod.match(b'', b'', include=[b'path:dir/subdir/x']) - m2 = matchmod.match(b'', b'', include=[b'path:dir/subdir/z']) + m1 = matchmod.match(b'/repo', b'', include=[b'path:dir/subdir/x']) + m2 = matchmod.match(b'/repo', b'', include=[b'path:dir/subdir/z']) um = matchmod.unionmatcher([m1, m2]) self.assertEqual(um.visitchildrenset(b''), {b'dir'}) self.assertEqual(um.visitchildrenset(b'dir'), {b'subdir'}) @@ -756,7 +759,7 @@ class SubdirMatcherTests(unittest.TestCase): def testVisitdir(self): - m = matchmod.match(b'', b'', include=[b'path:dir/subdir']) + m = matchmod.match(b'/repo', b'', include=[b'path:dir/subdir']) sm = matchmod.subdirmatcher(b'dir', m) self.assertEqual(sm.visitdir(b''), True) @@ -767,7 +770,7 @@ self.assertFalse(sm.visitdir(b'foo')) def testVisitchildrenset(self): - m = matchmod.match(b'', b'', include=[b'path:dir/subdir']) + m = matchmod.match(b'/repo', b'', include=[b'path:dir/subdir']) sm = matchmod.subdirmatcher(b'dir', m) self.assertEqual(sm.visitchildrenset(b''), {b'subdir'}) @@ -781,7 +784,10 @@ class PrefixdirMatcherTests(unittest.TestCase): def testVisitdir(self): m = matchmod.match( - util.localpath(b'root/d'), b'e/f', [b'../a.txt', b'b.txt'] + util.localpath(b'/root/d'), + b'e/f', + [b'../a.txt', b'b.txt'], + auditor=noop_auditor, ) pm = matchmod.prefixdirmatcher(b'd', m) @@ -814,7 +820,10 @@ def testVisitchildrenset(self): m = matchmod.match( - util.localpath(b'root/d'), b'e/f', [b'../a.txt', b'b.txt'] + util.localpath(b'/root/d'), + b'e/f', + [b'../a.txt', b'b.txt'], + auditor=noop_auditor, ) pm = matchmod.prefixdirmatcher(b'd', m)