def test_pattern(self): """Tests the high-level Pattern class.""" for pattern in ('/usr/l*/**/*.txt', b'/usr/l*/**/*.txt'): pattern = Pattern(pattern) self.assertTrue(pattern.matches('/usr/lib/irc/test.txt')) self.assertTrue(pattern.matches(b'/usr/local/lib/test.txt')) self.assertFalse(pattern.matches('/usr/bin/test.txt')) self.assertTrue(pattern.may_contain_matches('/usr/lib')) self.assertTrue(pattern.may_contain_matches('/usr')) self.assertFalse(pattern.may_contain_matches(b'/usr/bin')) self.assertTrue(pattern.matches('usr/lib/irc/test.txt')) self.assertFalse(pattern.matches('smthg/usr/lib/irc/test.txt')) self.assertTrue(pattern.may_contain_matches('usr/lib')) self.assertTrue(pattern.may_contain_matches('usr')) self.assertTrue( pattern.matches( WindowsPath('usr\\localuser\\Binaries\\readme.txt'))) self.assertFalse( pattern.matches( WindowsPath('usr\\otheruser\\Binaries\\readme.txt'))) self.assertEqual(pattern.matches('usr\\lib\\thing\\readme.txt'), issubclass(Path, WindowsPath))
def test_rel_path_to(self): """Tests the rel_path_to method.""" self.assertEqual(WindowsPath('.').rel_path_to(WindowsPath('')).path, '.') self.assertEqual(WindowsPath('\\var\\log\\apache2\\').rel_path_to( '\\var\\www\\cat.jpg').path, '..\\..\\www\\cat.jpg') self.assertEqual(WindowsPath('C:\\var\\log\\apache2\\').rel_path_to( 'C:\\tmp\\access.log').path, '..\\..\\..\\tmp\\access.log') self.assertEqual(WindowsPath('var\\log').rel_path_to( 'var\\log\\apache2\\access.log').path, 'apache2\\access.log') self.assertEqual(WindowsPath('\\var\\log\\apache2').rel_path_to( '\\var\\log\\apache2').path, '.') self.assertEqual(WindowsPath('C:\\').rel_path_to( 'C:\\var\\log\\apache2\\access.log').path, 'var\\log\\apache2\\access.log') self.assertEqual(WindowsPath('\\tmp\\secretdir\\').rel_path_to( '\\').path, '..\\..') self.assertEqual(WindowsPath('C:\\tmp\\secretdir\\').rel_path_to( 'D:\\other\\file.txt').path, 'D:\\other\\file.txt') with self.assertRaises(TypeError): WindowsPath('C:\\mydir\\').rel_path_to(PosixPath('/tmp/file'))
def test_plus(self): """Tests the plus operator.""" self.assertEqual((WindowsPath('some\\file.txt') + '.bak').path, 'some\\file.txt.bak') with self.assertRaises(TypeError): WindowsPath('some\\file.txt') + WindowsPath('.bak') with self.assertRaises(ValueError): WindowsPath('some\\file.txt') + '.bak/kidding' with self.assertRaises(ValueError): WindowsPath('some\\file.txt') + '/backup'
def test_parts(self): """Tests parent, ancestor, name, stem, ext.""" relative = WindowsPath(u'directory/users\\r\xE9mi/file.txt') absolute = WindowsPath(u'\\some/other\\thing.h\xE9h\xE9') self.assertEqual(relative.parent.path, u'directory\\users\\r\xE9mi') self.assertEqual(absolute.parent.path, u'\\some\\other') self.assertEqual(absolute.ancestor(10).path, u'\\') self.assertEqual(relative.name, u'file.txt') self.assertEqual(absolute.name, u'thing.h\xE9h\xE9') self.assertEqual(absolute.unicodename, u'thing.h\xE9h\xE9') self.assertEqual(absolute.stem, u'thing') self.assertEqual(absolute.ext, u'.h\xE9h\xE9')
def test_comparisons(self): """Tests the comparison operators.""" self.assertTrue(PosixPath(b'/tmp/r\xE9mi') == b'/tmp/r\xE9mi') self.assertTrue(PosixPath(b'/file') != b'/FILE') self.assertFalse(PosixPath(b'file') == PosixPath(b'dir')) self.assertFalse(WindowsPath('some/file') == PosixPath('some/file')) self.assertTrue(PosixPath(b'path/to/file1') < b'path/to/file2') self.assertFalse(b'path/to/file1' >= PosixPath(b'path/to/file2')) if PY3: with self.assertRaises(TypeError): WindowsPath('some/file') < PosixPath('other/file')
def test_str(self): """Tests getting string representations (repr/bytes/unicode).""" latin = WindowsPath(u'C:\\r\xE9mi') nonlatin = WindowsPath(u'C:\\you like\u203D.txt') # repr() self.assertEqual(repr(latin), "WindowsPath(u'C:\\\\r\\xe9mi')") self.assertEqual(repr(nonlatin), "WindowsPath(u'C:\\\\you like\\u203d.txt')") # bytes() self.assertEqual(bytes(latin), b'C:\\r\xe9mi') self.assertEqual(bytes(nonlatin), b'C:\\you like?.txt') # unicode() self.assertEqual(unicode(latin), u'C:\\r\xe9mi') self.assertEqual(unicode(nonlatin), u'C:\\you like\u203d.txt')
def test_comparisons(self): """Tests the comparison operators.""" self.assertTrue(WindowsPath('\\tmp') == WindowsPath('\\tmp')) self.assertFalse(WindowsPath('C:\\file') != 'c:\\FILE') self.assertTrue('c:\\FILE' == WindowsPath('C:\\file')) self.assertFalse(WindowsPath('C:\\file') == WindowsPath('C:\\dir')) self.assertFalse(WindowsPath('some/file') == PosixPath('some/file')) self.assertTrue(WindowsPath('path/to/file1') < 'path/to/file2') self.assertFalse('path/to/file1' >= WindowsPath('path/to/file2')) if PY3: with self.assertRaises(TypeError): WindowsPath('some/file') < PosixPath('other/file')
def test_root(self): """Tests roots, drives and UNC shares.""" a = WindowsPath(b'some/relative/path') b = WindowsPath(u'alsorelative') c = WindowsPath(b'/this/is/absolute') d = WindowsPath(u'C:\\') e = WindowsPath(b'C:\\also/absolute') f = WindowsPath(u'\\\\SOMEMACHINE\\share\\some\\file') def split_root(f): return tuple(p.path for p in f.split_root()) self.assertEqual(split_root(a), (u'.', u'some\\relative\\path')) self.assertEqual(split_root(b), (u'.', u'alsorelative')) self.assertFalse(b.is_absolute) self.assertEqual(split_root(c), (u'\\', u'this\\is\\absolute')) self.assertTrue(c.is_absolute) self.assertEqual(split_root(d), (u'C:\\', u'.')) self.assertTrue(d.is_absolute) self.assertEqual(d.root.path, u'C:\\') self.assertEqual(split_root(e), (u'C:\\', u'also\\absolute')) # FIXME : normpath() doesn't behave consistently: puts \ at the end on # PY3, not on PY2. self.assertIn(split_root(f), [(u'\\\\SOMEMACHINE\\share', u'some\\file'), (u'\\\\SOMEMACHINE\\share\\', u'some\\file')])
def test_construct(self): """Tests building paths.""" self.assertEqual( PosixPath(u'/', PosixPath(b'r\xE9mis/dir'), u'with', 'files.txt').path, b'/r\xE9mis/dir/with/files.txt') with self.assertRaises(TypeError): PosixPath('/tmp/test', WindowsPath('folder'), 'cat.gif') self.assertEqual( (PosixPath(b'/tmp/dir') / PosixPath(u'r\xE9mis/files/')).path, b'/tmp/dir/r\xC3\xA9mis/files') if PY3: self.assertEqual( PosixPath(u'/tmp/r\uDCE9mi').path, b'/tmp/r\xE9mi')
def test_construct(self): """Tests building paths.""" self.assertEqual(WindowsPath('C:\\', WindowsPath('some/dir'), 'with', 'files.txt').path, 'C:\\some\\dir\\with\\files.txt') with self.assertRaises(TypeError): WindowsPath(WindowsPath('C:\\somedir'), PosixPath('file.sh')) self.assertEqual((WindowsPath('Users\\R\xE9mi/Desktop') / WindowsPath(b'pictures/m\xE9chant.jpg')).path, 'Users\\R\xE9mi\\Desktop\\pictures\\m\xE9chant.jpg') self.assertEqual((WindowsPath('C:\\dir') / WindowsPath('D:\\other')).path, 'D:\\other')
def test_parts(self): """Tests parent, ancestor, name, stem, ext.""" relative = WindowsPath('directory/users\\r\xE9mi/file.txt') absolute = WindowsPath('\\some/other\\thing.h\xE9h\xE9') self.assertEqual(relative.parent.path, 'directory\\users\\r\xE9mi') self.assertEqual(absolute.parent.path, '\\some\\other') self.assertEqual(absolute.ancestor(10).path, '\\') self.assertEqual(relative.name, 'file.txt') self.assertEqual(absolute.name, 'thing.h\xE9h\xE9') self.assertEqual(absolute.unicodename, 'thing.h\xE9h\xE9') self.assertEqual(absolute.stem, 'thing') self.assertEqual(absolute.ext, '.h\xE9h\xE9') self.assertEqual(relative._components(), ['directory', 'users', 'r\xE9mi', 'file.txt']) self.assertEqual(absolute._components(), ['\\', 'some', 'other', 'thing.h\xE9h\xE9'])
def test_lies_under(self): """ Tests the lies_under method.""" self.assertTrue(WindowsPath(u'\\tmp').lies_under(u'\\')) self.assertFalse(WindowsPath(u'C:\\tmp').lies_under(u'C:\\var')) self.assertFalse(WindowsPath(u'\\tmp').lies_under(u'C:\\tmp')) self.assertFalse(WindowsPath(u'C:\\').lies_under(u'D:\\tmp')) self.assertTrue( WindowsPath(u'\\tmp\\some\\file\\here').lies_under(u'\\tmp\\some')) self.assertFalse( WindowsPath(u'\\tmp\\some\\file\\here').lies_under(u'\\tmp\\no')) self.assertFalse( WindowsPath(u'C:\\tmp\\some\\file\\here').lies_under( u'C:\\no\\tmp\\some')) self.assertFalse( WindowsPath(u'\\tmp\\some\\file\\here').lies_under(u'\\no\\some')) self.assertTrue( WindowsPath(u'C:\\tmp\\some\\file\\here').lies_under( u'C:\\tmp\\some\\file\\here')) self.assertTrue(WindowsPath(u'\\').lies_under(u'\\')) self.assertTrue(WindowsPath(u'').lies_under(u'')) self.assertTrue(WindowsPath(u'test').lies_under(u'')) self.assertFalse(WindowsPath(u'').lies_under(u'test')) self.assertFalse(WindowsPath(u'test').lies_under(u'\\'))