(self)
| 1255 | |
| 1256 | @os_helper.skip_unless_xattr |
| 1257 | def test_copyxattr(self): |
| 1258 | tmp_dir = self.mkdtemp() |
| 1259 | src = os.path.join(tmp_dir, 'foo') |
| 1260 | create_file(src, 'foo') |
| 1261 | dst = os.path.join(tmp_dir, 'bar') |
| 1262 | create_file(dst, 'bar') |
| 1263 | |
| 1264 | # no xattr == no problem |
| 1265 | shutil._copyxattr(src, dst) |
| 1266 | # common case |
| 1267 | os.setxattr(src, 'user.foo', b'42') |
| 1268 | os.setxattr(src, 'user.bar', b'43') |
| 1269 | shutil._copyxattr(src, dst) |
| 1270 | self.assertEqual(sorted(os.listxattr(src)), sorted(os.listxattr(dst))) |
| 1271 | self.assertEqual( |
| 1272 | os.getxattr(src, 'user.foo'), |
| 1273 | os.getxattr(dst, 'user.foo')) |
| 1274 | # check errors don't affect other attrs |
| 1275 | os.remove(dst) |
| 1276 | create_file(dst, 'bar') |
| 1277 | os_error = OSError(errno.EPERM, 'EPERM') |
| 1278 | |
| 1279 | def _raise_on_user_foo(fname, attr, val, **kwargs): |
| 1280 | if attr == 'user.foo': |
| 1281 | raise os_error |
| 1282 | else: |
| 1283 | orig_setxattr(fname, attr, val, **kwargs) |
| 1284 | try: |
| 1285 | orig_setxattr = os.setxattr |
| 1286 | os.setxattr = _raise_on_user_foo |
| 1287 | shutil._copyxattr(src, dst) |
| 1288 | self.assertIn('user.bar', os.listxattr(dst)) |
| 1289 | finally: |
| 1290 | os.setxattr = orig_setxattr |
| 1291 | # the source filesystem not supporting xattrs should be ok, too. |
| 1292 | def _raise_on_src(fname, *, follow_symlinks=True): |
| 1293 | if fname == src: |
| 1294 | raise OSError(errno.ENOTSUP, 'Operation not supported') |
| 1295 | return orig_listxattr(fname, follow_symlinks=follow_symlinks) |
| 1296 | try: |
| 1297 | orig_listxattr = os.listxattr |
| 1298 | os.listxattr = _raise_on_src |
| 1299 | shutil._copyxattr(src, dst) |
| 1300 | finally: |
| 1301 | os.listxattr = orig_listxattr |
| 1302 | |
| 1303 | # test that shutil.copystat copies xattrs |
| 1304 | src = os.path.join(tmp_dir, 'the_original') |
| 1305 | srcro = os.path.join(tmp_dir, 'the_original_ro') |
| 1306 | create_file(src, src) |
| 1307 | create_file(srcro, srcro) |
| 1308 | os.setxattr(src, 'user.the_value', b'fiddly') |
| 1309 | os.setxattr(srcro, 'user.the_value', b'fiddly') |
| 1310 | os.chmod(srcro, 0o444) |
| 1311 | dst = os.path.join(tmp_dir, 'the_copy') |
| 1312 | dstro = os.path.join(tmp_dir, 'the_copy_ro') |
| 1313 | create_file(dst, dst) |
| 1314 | create_file(dstro, dstro) |
nothing calls this directly
no test coverage detected