From fe300fe56d3334b12464232d939b18b53d29a97f Mon Sep 17 00:00:00 2001 From: John <43506685+Coniferish@users.noreply.github.com> Date: Tue, 12 Mar 2024 17:16:39 -0500 Subject: [PATCH] fix: teardown fixture for tests and update pre-commit-config (#2565) Files were being created as a side effect from running tests in `test_unstructured/metrics/test_evaluate.py`. The updated decorator removes the created directory and its files after the tests run. Testing on the main branch, run `make test` or `pytest test_unstructured/metrics/test_evaluate.py` and files will be created. On this branch no files are created --- test_unstructured/metrics/test_evaluate.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/test_unstructured/metrics/test_evaluate.py b/test_unstructured/metrics/test_evaluate.py index 1c93edda8..6e52f6694 100644 --- a/test_unstructured/metrics/test_evaluate.py +++ b/test_unstructured/metrics/test_evaluate.py @@ -56,15 +56,21 @@ DUMMY_DF_ELEMENT_TYPE = pd.DataFrame( @pytest.fixture() def _cleanup_after_test(): - # This is where the test runs + """Fixture for removing side-effects of running tests in this file.""" + + def remove_generated_directories(): + """Remove directories created from running tests.""" + + # Directories to be removed: + target_dir_names = ["test_evaluate_results_cct", "test_evaluate_results_cct_txt"] + subdirs = (d for d in os.scandir(TESTING_FILE_DIR) if d.is_dir()) + for d in subdirs: + if d.name in target_dir_names: + shutil.rmtree(d.path) + + # Run test as normal yield - - os.path.join(TESTING_FILE_DIR, UNSTRUCTURED_OUTPUT_DIRNAME) - export_dir = os.path.join(TESTING_FILE_DIR, "test_evaluate_results_cct") - - # Cleanup the directory and file - if os.path.exists(export_dir): - shutil.rmtree(export_dir) + remove_generated_directories() @pytest.mark.skipif(is_in_docker, reason="Skipping this test in Docker container")