def test_copy(self): one = Matcher('some/path', root=self.path('/one-root')) other = Matcher(one, root=self.path('/different-root')) self.assertIsNone(other.match('some/path')) self.assertIsNone(other.match(self.path('/one-root/some/path'))) self.assertIsNotNone( other.match(self.path('/different-root/some/path')))
def test_copy(self): one = Matcher('some/path', root=self.path('/one-root')) other = Matcher(one, root=self.path('/different-root')) self.assertIsNone(other.match('some/path')) self.assertIsNone( other.match(self.path('/one-root/some/path')) ) self.assertIsNotNone( other.match(self.path('/different-root/some/path')) )
def test_variable(self): r1 = self.path('/one-root') r2 = self.path('/other-root') one = Matcher('{var}/path', env={'var': 'relative-dir'}, root=r1) self.assertIsNone(one.match('relative-dir/path')) self.assertIsNotNone( one.match(self.path('/one-root/relative-dir/path'))) other = Matcher(one, env={'var': r2}) self.assertIsNone(other.match( self.path('/one-root/relative-dir/path'))) self.assertIsNotNone(other.match(self.path('/other-root/path')))
def test_rooted(self): r1 = self.path('/one-root') r2 = self.path('/other-root') one = Matcher(self.path('/one-root/full/path'), root=r2) self.assertIsNone(one.match(self.path('/other-root/full/path'))) # concat r2 and r1. r1 is absolute, so we gotta trick that concat_root = r2 if not r1.startswith('/'): # windows absolute paths don't start with '/', add one concat_root += '/' concat_root += r1 self.assertIsNone(one.match(concat_root + '/full/path')) self.assertIsNotNone(one.match(self.path('/one-root/full/path')))
def test_match(self): # test matches as well as groupdict aliasing. one = Matcher('values-{android_locale}/strings.xml') self.assertEqual( one.match('values-de/strings.xml'), { 'android_locale': 'de', 'locale': 'de' } ) self.assertEqual( one.match('values-de-rDE/strings.xml'), { 'android_locale': 'de-rDE', 'locale': 'de-DE' } ) self.assertEqual( one.match('values-b+sr+Latn/strings.xml'), { 'android_locale': 'b+sr+Latn', 'locale': 'sr-Latn' } ) self.assertEqual( one.with_env( {'locale': 'de'} ).match('values-de/strings.xml'), { 'android_locale': 'de', 'locale': 'de' } ) self.assertEqual( one.with_env( {'locale': 'de-DE'} ).match('values-de-rDE/strings.xml'), { 'android_locale': 'de-rDE', 'locale': 'de-DE' } ) self.assertEqual( one.with_env( {'locale': 'sr-Latn'} ).match('values-b+sr+Latn/strings.xml'), { 'android_locale': 'b+sr+Latn', 'locale': 'sr-Latn' } )
def test_variable(self): r1 = self.path('/one-root') r2 = self.path('/other-root') one = Matcher( '{var}/path', env={'var': 'relative-dir'}, root=r1 ) self.assertIsNone(one.match('relative-dir/path')) self.assertIsNotNone( one.match(self.path('/one-root/relative-dir/path')) ) other = Matcher(one, env={'var': r2}) self.assertIsNone( other.match(self.path('/one-root/relative-dir/path')) ) self.assertIsNotNone( other.match(self.path('/other-root/path')) )
def test_aliases(self): # test legacy locale code mapping # he <-> iw, id <-> in, yi <-> ji one = Matcher('values-{android_locale}/strings.xml') for legacy, standard in six.iteritems(ANDROID_STANDARD_MAP): self.assertDictEqual( one.match('values-{}/strings.xml'.format(legacy)), { 'android_locale': legacy, 'locale': standard }) self.assertEqual( one.with_env({ 'locale': standard }).prefix, 'values-{}/strings.xml'.format(legacy))
def test_aliases(self): # test legacy locale code mapping # he <-> iw, id <-> in, yi <-> ji one = Matcher('values-{android_locale}/strings.xml') for legacy, standard in six.iteritems(ANDROID_STANDARD_MAP): self.assertDictEqual( one.match('values-{}/strings.xml'.format(legacy)), { 'android_locale': legacy, 'locale': standard } ) self.assertEqual( one.with_env({'locale': standard}).prefix, 'values-{}/strings.xml'.format(legacy) )
def test_encoded_matcher(self): one = Matcher('foo/*', encoding='utf-8') self.assertTrue(one.match(b'foo/bar')) other = Matcher('bar/*', encoding='utf-8') self.assertEqual(one.sub(other, b'foo/baz'), b'bar/baz')
def test_root_path(self): one = Matcher('some/path', root=self.root) self.assertIsNone(one.match('some/path')) self.assertIsNotNone(one.match(self.path('/some/path')))
def test_matcher(self): one = Matcher('foo/*') self.assertTrue(one.match('foo/baz')) self.assertFalse(one.match('foo/baz/qux')) other = Matcher('bar/*') self.assertTrue(other.match('bar/baz')) self.assertFalse(other.match('bar/baz/qux')) self.assertEqual(one.sub(other, 'foo/baz'), 'bar/baz') self.assertIsNone(one.sub(other, 'bar/baz')) one = Matcher('foo/**') self.assertTrue(one.match('foo/baz')) self.assertTrue(one.match('foo/baz/qux')) other = Matcher('bar/**') self.assertTrue(other.match('bar/baz')) self.assertTrue(other.match('bar/baz/qux')) self.assertEqual(one.sub(other, 'foo/baz'), 'bar/baz') self.assertEqual(one.sub(other, 'foo/baz/qux'), 'bar/baz/qux') one = Matcher('foo/*/one/**') self.assertTrue(one.match('foo/baz/one/qux')) self.assertFalse(one.match('foo/baz/bez/one/qux')) other = Matcher('bar/*/other/**') self.assertTrue(other.match('bar/baz/other/qux')) self.assertFalse(other.match('bar/baz/bez/other/qux')) self.assertEqual(one.sub(other, 'foo/baz/one/qux'), 'bar/baz/other/qux') self.assertEqual(one.sub(other, 'foo/baz/one/qux/zzz'), 'bar/baz/other/qux/zzz') self.assertIsNone(one.sub(other, 'foo/baz/bez/one/qux')) one = Matcher('foo/**/bar/**') self.assertTrue(one.match('foo/bar/baz.qux')) self.assertTrue(one.match('foo/tender/bar/baz.qux')) self.assertFalse(one.match('foo/nobar/baz.qux')) self.assertFalse(one.match('foo/tender/bar')) other = Matcher('baz/**/qux/**') self.assertEqual(one.sub(other, 'foo/bar/baz.qux'), 'baz/qux/baz.qux') self.assertEqual(one.sub(other, 'foo/tender/bar/baz.qux'), 'baz/tender/qux/baz.qux')
def test_matcher(self): one = Matcher('foo/*') self.assertTrue(one.match('foo/baz')) self.assertFalse(one.match('foo/baz/qux')) other = Matcher('bar/*') self.assertTrue(other.match('bar/baz')) self.assertFalse(other.match('bar/baz/qux')) self.assertEqual(one.sub(other, 'foo/baz'), 'bar/baz') self.assertIsNone(one.sub(other, 'bar/baz')) one = Matcher('foo/**') self.assertTrue(one.match('foo/baz')) self.assertTrue(one.match('foo/baz/qux')) other = Matcher('bar/**') self.assertTrue(other.match('bar/baz')) self.assertTrue(other.match('bar/baz/qux')) self.assertEqual(one.sub(other, 'foo/baz'), 'bar/baz') self.assertEqual(one.sub(other, 'foo/baz/qux'), 'bar/baz/qux') one = Matcher('foo/*/one/**') self.assertTrue(one.match('foo/baz/one/qux')) self.assertFalse(one.match('foo/baz/bez/one/qux')) other = Matcher('bar/*/other/**') self.assertTrue(other.match('bar/baz/other/qux')) self.assertFalse(other.match('bar/baz/bez/other/qux')) self.assertEqual(one.sub(other, 'foo/baz/one/qux'), 'bar/baz/other/qux') self.assertEqual(one.sub(other, 'foo/baz/one/qux/zzz'), 'bar/baz/other/qux/zzz') self.assertIsNone(one.sub(other, 'foo/baz/bez/one/qux')) one = Matcher('foo/**/bar/**') self.assertTrue(one.match('foo/bar/baz.qux')) self.assertTrue(one.match('foo/tender/bar/baz.qux')) self.assertFalse(one.match('foo/nobar/baz.qux')) self.assertFalse(one.match('foo/tender/bar'))