diff options
author | Patrik Teivonen <patrik.teivonen@qt.io> | 2022-06-30 13:39:46 +0300 |
---|---|---|
committer | Patrik Teivonen <patrik.teivonen@qt.io> | 2022-07-05 11:59:59 +0000 |
commit | 409972e451bedb7a691464db2949ba65c3ba10e9 (patch) | |
tree | ea31a247264f475b03e71c569db9573e0d1cd08c | |
parent | e14a8608d10f885152cea2d2a737446f91e007c6 (diff) |
Fix test_content_cleaner.py for Python 3.6 and 3.7
Unit tests test_remove_content and test_remove_empty_directories were
not successful on Python 3.7 and below due to failure during cleanup in
tempfile.TemporaryDirectory context manager.
Catch the FileNotFoundError exception.
Change-Id: Ia49b4f80086c6b6df30e1f631b60e1d11e812486
Reviewed-by: Akseli Salovaara <akseli.salovaara@qt.io>
-rw-r--r-- | packaging-tools/tests/test_content_cleaner.py | 47 |
1 files changed, 28 insertions, 19 deletions
diff --git a/packaging-tools/tests/test_content_cleaner.py b/packaging-tools/tests/test_content_cleaner.py index f9bf92aa0..813f704c9 100644 --- a/packaging-tools/tests/test_content_cleaner.py +++ b/packaging-tools/tests/test_content_cleaner.py @@ -144,15 +144,19 @@ class TestContentCleaner(unittest.TestCase): remove_rules: List[str], verify_removed_files: List[str], ) -> None: - - with tempfile.TemporaryDirectory(dir=os.getcwd()) as tmp_base_dir: - test_base_dir = os.path.join(tmp_base_dir, "test-base-dir") - self.generate_test_content(test_base_dir, test_content) - remove_content(test_base_dir, remove_rules) - for file_path in verify_removed_files: - for path in test_content: - if file_path in os.path.join(test_base_dir, path): - self.assertFalse(os.path.isfile(os.path.join(test_base_dir, path))) + try: + with tempfile.TemporaryDirectory(dir=os.getcwd()) as tmp_base_dir: + test_base_dir = os.path.join(tmp_base_dir, "test-base-dir") + self.generate_test_content(test_base_dir, test_content) + remove_content(test_base_dir, remove_rules) + for file_path in verify_removed_files: + for path in test_content: + if file_path in os.path.join(test_base_dir, path): + self.assertFalse(os.path.isfile(os.path.join(test_base_dir, path))) + # Python 3.7 and below will throw FileNotFoundError on cleanup when exiting context + # if the TemporaryDirectory was removed. + except FileNotFoundError: + pass @data( (["test/path/test-file", "test/path/.test-file"], False), @@ -160,16 +164,21 @@ class TestContentCleaner(unittest.TestCase): ) @unpack def test_remove_empty_directories(self, test_content: str, remove_dir: bool) -> None: - with tempfile.TemporaryDirectory(dir=os.getcwd()) as tmp_base_dir: - test_base_dir = os.path.join(tmp_base_dir, "test-base-dir") - self.generate_test_content(test_base_dir, test_content) - remove_empty_directories(test_base_dir) - for path in test_content: - verify_path = os.path.join(test_base_dir, path) - if remove_dir: - self.assertFalse(os.path.exists(verify_path)) - else: - self.assertTrue(os.path.exists(verify_path)) + try: + with tempfile.TemporaryDirectory(dir=os.getcwd()) as tmp_base_dir: + test_base_dir = os.path.join(tmp_base_dir, "test-base-dir") + self.generate_test_content(test_base_dir, test_content) + remove_empty_directories(test_base_dir) + for path in test_content: + verify_path = os.path.join(test_base_dir, path) + if remove_dir: + self.assertFalse(os.path.exists(verify_path)) + else: + self.assertTrue(os.path.exists(verify_path)) + # Python 3.7 and below will throw FileNotFoundError on cleanup when exiting context + # if the temporary directory was removed. + except FileNotFoundError: + pass if __name__ == "__main__": |