From 7b8f57335c43054fe4008b7401d6ac2b3f710c1a Mon Sep 17 00:00:00 2001 From: Zac Medico Date: Wed, 6 Nov 2019 00:03:36 -0800 Subject: FileCopier: capture exceptions Use ForkExecutor to capture exceptions instead of showing a full traceback. FileCopier callers will now be responsible for displaying relevant exception messages. Bug: https://bugs.gentoo.org/699400 Signed-off-by: Zac Medico --- lib/portage/tests/util/test_file_copier.py | 44 ++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 lib/portage/tests/util/test_file_copier.py (limited to 'lib/portage/tests/util') diff --git a/lib/portage/tests/util/test_file_copier.py b/lib/portage/tests/util/test_file_copier.py new file mode 100644 index 000000000..01dfba494 --- /dev/null +++ b/lib/portage/tests/util/test_file_copier.py @@ -0,0 +1,44 @@ +# Copyright 2019 Gentoo Authors +# Distributed under the terms of the GNU General Public License v2 + +import errno +import os +import shutil +import tempfile + +from portage.tests import TestCase +from portage.util._async.FileCopier import FileCopier +from portage.util._eventloop.global_event_loop import global_event_loop + + +class FileCopierTestCase(TestCase): + + def testFileCopier(self): + loop = global_event_loop() + tempdir = tempfile.mkdtemp() + try: + + # regular successful copy + src_path = os.path.join(tempdir, 'src') + dest_path = os.path.join(tempdir, 'dest') + content = b'foo' + with open(src_path, 'wb') as f: + f.write(content) + copier = FileCopier(src_path=src_path, dest_path=dest_path, scheduler=loop) + copier.start() + loop.run_until_complete(copier.async_wait()) + self.assertEqual(copier.returncode, 0) + copier.future.result() + with open(dest_path, 'rb') as f: + self.assertEqual(f.read(), content) + + # failure due to nonexistent src_path + src_path = os.path.join(tempdir, 'does-not-exist') + copier = FileCopier(src_path=src_path, dest_path=dest_path, scheduler=loop) + copier.start() + loop.run_until_complete(copier.async_wait()) + self.assertEqual(copier.returncode, 1) + self.assertEqual(copier.future.exception().errno, errno.ENOENT) + self.assertEqual(copier.future.exception().filename, src_path.encode('utf8')) + finally: + shutil.rmtree(tempdir) -- cgit v1.2.3-65-gdbad