diff options
author | Sandro Tosi <sandro.tosi@gmail.com> | 2011-08-22 21:28:27 (GMT) |
---|---|---|
committer | Sandro Tosi <sandro.tosi@gmail.com> | 2011-08-22 21:28:27 (GMT) |
commit | d902a14dd0ed29bda061f3c0e4cfe355f2763462 (patch) | |
tree | 37633787b7ecbbd8d71e558ee92429c1248d44d0 /Lib/test/test_shutil.py | |
parent | 6f2a683a0c4ec0b3fe3ed840336853ebf26004c0 (diff) | |
download | cpython-d902a14dd0ed29bda061f3c0e4cfe355f2763462.zip cpython-d902a14dd0ed29bda061f3c0e4cfe355f2763462.tar.gz cpython-d902a14dd0ed29bda061f3c0e4cfe355f2763462.tar.bz2 |
#12191: add shutil.chown() to change user and/or group owner of a given path also specifying their names.
Diffstat (limited to 'Lib/test/test_shutil.py')
-rw-r--r-- | Lib/test/test_shutil.py | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/Lib/test/test_shutil.py b/Lib/test/test_shutil.py index 792b6cd..9f61ef7 100644 --- a/Lib/test/test_shutil.py +++ b/Lib/test/test_shutil.py @@ -712,6 +712,65 @@ class TestShutil(unittest.TestCase): self.assertGreaterEqual(usage.total, usage.used) self.assertGreater(usage.total, usage.free) + @unittest.skipUnless(UID_GID_SUPPORT, "Requires grp and pwd support") + @unittest.skipUnless(hasattr(os, 'chown'), 'requires os.chown') + def test_chown(self): + + # cleaned-up automatically by TestShutil.tearDown method + dirname = self.mkdtemp() + filename = tempfile.mktemp(dir=dirname) + write_file(filename, 'testing chown function') + + with self.assertRaises(ValueError): + shutil.chown(filename) + + with self.assertRaises(LookupError): + shutil.chown(filename, user='non-exising username') + + with self.assertRaises(LookupError): + shutil.chown(filename, group='non-exising groupname') + + with self.assertRaises(TypeError): + shutil.chown(filename, b'spam') + + with self.assertRaises(TypeError): + shutil.chown(filename, 3.14) + + uid = os.getuid() + gid = os.getgid() + + def check_chown(path, uid=None, gid=None): + s = os.stat(filename) + if uid is not None: + self.assertEqual(uid, s.st_uid) + if gid is not None: + self.assertEqual(gid, s.st_gid) + + shutil.chown(filename, uid, gid) + check_chown(filename, uid, gid) + shutil.chown(filename, uid) + check_chown(filename, uid) + shutil.chown(filename, user=uid) + check_chown(filename, uid) + shutil.chown(filename, group=gid) + check_chown(filename, gid) + + shutil.chown(dirname, uid, gid) + check_chown(dirname, uid, gid) + shutil.chown(dirname, uid) + check_chown(dirname, uid) + shutil.chown(dirname, user=uid) + check_chown(dirname, uid) + shutil.chown(dirname, group=gid) + check_chown(dirname, gid) + + user = pwd.getpwuid(uid)[0] + group = grp.getgrgid(gid)[0] + shutil.chown(filename, user, group) + check_chown(filename, uid, gid) + shutil.chown(dirname, user, group) + check_chown(dirname, uid, gid) + class TestMove(unittest.TestCase): |