MCPcopy Index your code
hub / github.com/python/cpython / test_chown

Method test_chown

Lib/test/test_shutil.py:2243–2333  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

2241 @unittest.skipUnless(UID_GID_SUPPORT, "Requires grp and pwd support")
2242 @unittest.skipUnless(hasattr(os, 'chown'), 'requires os.chown')
2243 def test_chown(self):
2244 dirname = self.mkdtemp()
2245 filename = tempfile.mktemp(dir=dirname)
2246 linkname = os.path.join(dirname, "chown_link")
2247 create_file(filename, 'testing chown function')
2248 os.symlink(filename, linkname)
2249
2250 with self.assertRaises(ValueError):
2251 shutil.chown(filename)
2252
2253 with self.assertRaises(LookupError):
2254 shutil.chown(filename, user='non-existing username')
2255
2256 with self.assertRaises(LookupError):
2257 shutil.chown(filename, group='non-existing groupname')
2258
2259 with self.assertRaises(TypeError):
2260 shutil.chown(filename, b'spam')
2261
2262 with self.assertRaises(TypeError):
2263 shutil.chown(filename, 3.14)
2264
2265 uid = os.getuid()
2266 gid = os.getgid()
2267
2268 def check_chown(path, uid=None, gid=None):
2269 s = os.stat(path)
2270 if uid is not None:
2271 self.assertEqual(uid, s.st_uid)
2272 if gid is not None:
2273 self.assertEqual(gid, s.st_gid)
2274
2275 shutil.chown(filename, uid, gid)
2276 check_chown(filename, uid, gid)
2277 shutil.chown(filename, uid)
2278 check_chown(filename, uid)
2279 shutil.chown(filename, user=uid)
2280 check_chown(filename, uid)
2281 shutil.chown(filename, group=gid)
2282 check_chown(filename, gid=gid)
2283
2284 shutil.chown(dirname, uid, gid)
2285 check_chown(dirname, uid, gid)
2286 shutil.chown(dirname, uid)
2287 check_chown(dirname, uid)
2288 shutil.chown(dirname, user=uid)
2289 check_chown(dirname, uid)
2290 shutil.chown(dirname, group=gid)
2291 check_chown(dirname, gid=gid)
2292
2293 try:
2294 user = pwd.getpwuid(uid)[0]
2295 group = grp.getgrgid(gid)[0]
2296 except KeyError:
2297 # On some systems uid/gid cannot be resolved.
2298 pass
2299 else:
2300 shutil.chown(filename, user, group)

Callers

nothing calls this directly

Calls 9

mkdtempMethod · 0.80
mktempMethod · 0.80
chownMethod · 0.80
addCleanupMethod · 0.80
create_fileFunction · 0.70
joinMethod · 0.45
assertRaisesMethod · 0.45
openMethod · 0.45
basenameMethod · 0.45

Tested by

no test coverage detected