Beispiel #1
0
 def test_string_methods(self):
     t = Path('/some/path/file.txt')
     self.assertTrue(t.endswith('txt'))
     self.assertIsInstance(t.rstrip('.'), Path)
     self.assertIsInstance(t.rfind('.'), int)
     self.assertIsInstance(t.rfind('.'), int)
     self.assertIsInstance(t.islower(), bool)
Beispiel #2
0
 def test_no_trailing_slash_construction(self):
     tests = (
         ('', ''),
         ('.', '.'),
         ('..', '..'),
         ('/', '/'),
         ('/..', '/'),
         ('/../', '/'),
         ('../', '..'),
         ('path/..', ''),
         ('path/../', ''),
         ('path/../../', '..'),
         ('foo.txt', 'foo.txt'),
         ('some/path////', 'some/path'),
         ('/some/abs/path/', '/some/abs/path'),
         ('full/path/foo.txt', 'full/path/foo.txt'),
         ('normalize/../me/prleaze.txt', 'me/prleaze.txt'),
         )
     for test, expected in tests:
         t = Path(test, keep_trailing_slash=False)
         self.assertEqual(expected, t)
     t = Path('from', 'bits/', keep_trailing_slash=False)
     self.assertEqual('from/bits', t)
     t = Path('from', 'bits/', keep_trailing_slash=False)
     self.assertEqual('from/bits/trail', t.join('trail/'))
Beispiel #3
0
 def test_group_owner(self):
     self.assertEqual('root', Path('/tmp').owner)
     self.assertEqual('root', Path('/etc/passwd').group)
     fh = tmpfile()
     try:
         t = Path(fh.name)
         # these aren't actually changing anything (need to be root for that)
         # but they do at least exercise the code
         t.owner = pwd.getpwuid(os.getuid()).pw_name
         t.group = grp.getgrgid(os.getgid()).gr_name
         t.owner = '%s:%s' % (t.owner, t.group)
         t.chown(t.owner)
         t.chown(t.owner, t.group)
         t.chown('%s:%s' % (t.owner, t.group), recursive=True)
     finally:
         os.remove(fh.name)
Beispiel #4
0
 def test_mode(self):
     #self.assertEqual('root', Path('/etc/passwd').mode)
     #self.assertEqual('root', Path('/tmp').mode)
     fh = tmpfile()
     try:
         t = Path(fh.name)
         t.mode = 644
         t.chmod(666)
         t.chmod('u+x')
         t.chmod('0777', recursive=True)
     finally:
         os.remove(fh.name)
Beispiel #5
0
 def test_common_suffix_construction(self):
     tests = (
         ((), ''),
         (('', ''), ''),
         (('/', '/'), '/'),
         (('var', 'var'), 'var'),
         (('var', '/var'), 'var'),
         (('/var', '/var'), '/var'),
         (('/var', '/var/'), '/var'),
         (('/var/', '/var/'), '/var/'),
         (('bar/log', '/var/log'), '/log'),
         (('/some/longible', '/varible'), ''),
         (('/var/log', 'bar/logged'), ''),
         (('/kernel/log', '/sys/log', '/lady/log'), '/log'),
         )
     for test, expected in tests:
         result = Path.common_suffix(test)
         self.assertIsInstance(result, Path, test)
         self.assertEqual(expected, result, test)
         if expected:
             self.assertEqual(expected, Path.common_suffix(*test), test)
Beispiel #6
0
 def test_exists(self):
     t = Path('/tmp')
     self.assertTrue(t.exists())
     t = Path('/etc/passwd')
     self.assertTrue(t.exists())
     t = Path('/I/like/oatmeal/cookies')
     self.assertFalse(t.exists())
Beispiel #7
0
 def test_common_prefix_construction(self):
     tests = (
         ((), ''),
         (('', ''), ''),
         (('/', '/var'), '/'),
         (('var', 'var'), 'var'),
         (('var', 'var/'), 'var'),
         (('/var', '/var'), '/var'),
         (('/var', '/var/'), '/var'),
         (('/var/', '/var/'), '/var'),
         (('/var', 'var'), ''),
         (('/var', '/varible'), '/'),
         (('/var/log', 'var/log'), ''),
         (('/var/log/', '/var/log/kernel'), '/var/log'),
         (('/var/log', '/var/logged', '/var/log/kernel'), '/var'),
         )
     for test, expected in tests:
         result = Path.common_prefix(test)
         self.assertEqual(expected, result)
         self.assertIsInstance(result, Path)
         if expected:
             self.assertEqual(expected, Path.common_prefix(*test))
Beispiel #8
0
 def test_touch(self):
     fh = tmpfile()
     try:
         t = Path(fh.name)
         t.touch(1970)
         foo = os.stat(fh.name)
         self.assertEqual(1970, foo.st_atime)
         self.assertEqual(1970, foo.st_mtime)
         t.touch(12, mtime=False)
         foo = os.stat(fh.name)
         self.assertEqual(12, foo.st_atime)
         self.assertEqual(1970, foo.st_mtime)
         t.touch(28, atime=False)
         foo = os.stat(fh.name)
         self.assertEqual(12, foo.st_atime)
         self.assertEqual(28, foo.st_mtime)
         t.touch()
         foo = os.stat(fh.name)
         self.assertNotEqual(12, foo.st_atime)
         self.assertNotEqual(28, foo.st_mtime)
     finally:
         os.remove(fh.name)
Beispiel #9
0
 def test_isabs(self):
     abs = (
         '/tmp',
         )
     relative = (
         'tmp',
         './tmp',
         '../tmp',
             )
     for path in abs:
         t = Path(path)
         self.assertTrue(t.is_abs())
         self.assertFalse(t.is_relative())
     for path in relative:
         t = Path(path)
         self.assertFalse(t.is_absolute())
         self.assertTrue(t.is_relative())
Beispiel #10
0
 def test_mkdir(self):
     t = Path('/tmp/cuprum_test_mkdir')
     if os.path.exists(str(t)):
         os.rmdir(str(t))
     t.mkdir()
     t.mkdir()  # silently ignores existing
Beispiel #11
0
 def test_walk_iter(self):
     t = Path('/etc/passwd')
     for f in t.walk_iter():
         pass
     t = Path('/doesnot_exist')
Beispiel #12
0
 def test_glob(self):
     t = Path('/foo/bar')
     self.assertEqual([], list(t.glob('*')))
Beispiel #13
0
 def test_expand(self):
     t = Path('/foo/bar')
     self.assertIsInstance(t.expand(), Path)
Beispiel #14
0
 def test_realpath(self):
     t = Path('/foo/bar')
     self.assertIsInstance(t.realpath(), Path)
Beispiel #15
0
 def test_normpath(self):
     t = Path('/foo/bar')
     self.assertIsInstance(t.normpath(), Path)
Beispiel #16
0
 def test_abspath(self):
     t = Path('/foo/bar')
     self.assertIsInstance(t.abspath(), Path)
Beispiel #17
0
 def test_cwd_construction(self):
     self.assertIsInstance(Path.cwd(), Path)
     self.assertIsInstance(Path.getcwd(), Path)
Beispiel #18
0
 def test_stat(self):
     t = Path('/tmp')
     t.stat()
     t.stat(followlinks=False)  # TODO: test actual symlink
     t.statfs()
     t.statvfs()
Beispiel #19
0
 def test_group_owner_chown(self):
     PATH = '/tmp/delme.txt'
     path = Path(PATH)
     os.system('rm %s' % PATH)
     os.system('touch %s' % PATH)
     self.assertTrue('nobody' != path.owner)
     self.assertTrue('nogroup' != path.group)
     # chown group
     path.chown(group='nogroup')
     self.assertEqual('nogroup', path.group)
     self.assertTrue('nobody' != path.owner)
     # chown owner
     path.chown('nobody')
     self.assertEqual('nobody', path.owner)
     # chown both / numerical ids
     path.chown(uid=0, gid=0)
     self.assertEqual('root', path.owner)
     self.assertEqual('root', path.group)
     # recursive
     path.chown('root', recursive=True)
     # set properties
     path.owner = 'nobody'
     self.assertEqual('nobody', path.owner)
     path.group = 'nogroup'
     self.assertEqual('nogroup', path.group)
     path.delete()