From 0c893389fae60d6f11c74d9332dec189110ef734 Mon Sep 17 00:00:00 2001 From: GeorgWa Date: Wed, 4 Oct 2023 23:35:43 +0200 Subject: [PATCH] FIX randomize tempfolder --- tests/unit_tests/test_reporting.py | 62 ++++++++++++++++++------------ 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/tests/unit_tests/test_reporting.py b/tests/unit_tests/test_reporting.py index e9fddd2f..45e456d6 100644 --- a/tests/unit_tests/test_reporting.py +++ b/tests/unit_tests/test_reporting.py @@ -7,9 +7,17 @@ from alphadia.extraction.workflow import reporting +def _random_tempfolder(): + tempdir = tempfile.gettempdir() + # 6 alphanumeric characters + random_foldername = "".join(np.random.choice(list("abcdefghijklmnopqrstuvwxyz0123456789"), 6)) + path = os.path.join(tempdir, random_foldername) + os.mkdir(path) + return path + def test_logging(): - tempfolder = tempfile.gettempdir() + tempfolder = _random_tempfolder() if os.path.exists(os.path.join(tempfolder, "log.txt")): os.remove(os.path.join(tempfolder, "log.txt")) @@ -44,7 +52,9 @@ def test_backend(): def test_figure_backend(): - figure_backend = reporting.FigureBackend(path = tempfile.gettempdir()) + tempfolder = _random_tempfolder() + + figure_backend = reporting.FigureBackend(path = tempfolder) fig, ax = plt.subplots(1, 1, figsize=(5, 5)) ax.scatter(np.random.rand(10), np.random.rand(10)) @@ -52,59 +62,61 @@ def test_figure_backend(): figure_backend.log_figure("scatter", fig) plt.close(fig) - assert os.path.exists(os.path.join(tempfile.gettempdir(), figure_backend.FIGURE_PATH, "scatter.png")) - os.remove(os.path.join(tempfile.gettempdir(), figure_backend.FIGURE_PATH, "scatter.png")) + assert os.path.exists(os.path.join(tempfolder, figure_backend.FIGURE_PATH, "scatter.png")) + os.remove(os.path.join(tempfolder, figure_backend.FIGURE_PATH, "scatter.png")) time.sleep(1) test_figure_backend() def test_jsonl_backend(): + + tempfolder = _random_tempfolder() - with reporting.JSONLBackend(path = tempfile.gettempdir()) as jsonl_backend: + with reporting.JSONLBackend(path = tempfolder) as jsonl_backend: jsonl_backend.log_event("start_extraction", None) jsonl_backend.log_metric("accuracy", 0.9) jsonl_backend.log_string("test") - assert os.path.exists(os.path.join(tempfile.gettempdir(), "events.jsonl")) - with open(os.path.join(tempfile.gettempdir(), "events.jsonl"), "r") as f: + assert os.path.exists(os.path.join(tempfolder, "events.jsonl")) + with open(os.path.join(tempfolder, "events.jsonl"), "r") as f: assert len(f.readlines()) == 5 time.sleep(1) - os.remove(os.path.join(tempfile.gettempdir(), "events.jsonl")) + os.remove(os.path.join(tempfolder, "events.jsonl")) time.sleep(1) test_jsonl_backend() def test_log_backend(): - tempdir = tempfile.gettempdir() + tempfolder = _random_tempfolder() - if os.path.exists(os.path.join(tempdir, "log.txt")): - os.remove(os.path.join(tempdir, "log.txt")) + if os.path.exists(os.path.join(tempfolder, "log.txt")): + os.remove(os.path.join(tempfolder, "log.txt")) - stdout_backend = reporting.LogBackend(path = tempdir) + stdout_backend = reporting.LogBackend(path = tempfolder) stdout_backend.log_string("test", verbosity='progress') stdout_backend.log_string("test", verbosity='info') stdout_backend.log_string("test", verbosity='warning') stdout_backend.log_string("test", verbosity='error') stdout_backend.log_string("test", verbosity='critical') - assert os.path.exists(os.path.join(tempdir, "log.txt")) - with open(os.path.join(tempdir, "log.txt"), "r") as f: + assert os.path.exists(os.path.join(tempfolder, "log.txt")) + with open(os.path.join(tempfolder, "log.txt"), "r") as f: assert len(f.readlines()) == 5 time.sleep(1) - os.remove(os.path.join(tempdir, "log.txt")) + os.remove(os.path.join(tempfolder, "log.txt")) test_log_backend() def test_pipeline(): - tempdir = tempfile.gettempdir() + tempfolder = _random_tempfolder() pipeline = reporting.Pipeline( backends = [ - reporting.LogBackend(path = tempdir), - reporting.JSONLBackend(path = tempdir), - reporting.FigureBackend(path = tempdir) + reporting.LogBackend(path = tempfolder), + reporting.JSONLBackend(path = tempfolder), + reporting.FigureBackend(path = tempfolder) ] ) @@ -119,13 +131,13 @@ def test_pipeline(): pipeline.log_figure("scatter", fig) plt.close(fig) - assert os.path.exists(os.path.join(tempdir, "log.txt")) - assert os.path.exists(os.path.join(tempdir, "events.jsonl")) - assert os.path.exists(os.path.join(tempdir, "figures", "scatter.png")) + assert os.path.exists(os.path.join(tempfolder, "log.txt")) + assert os.path.exists(os.path.join(tempfolder, "events.jsonl")) + assert os.path.exists(os.path.join(tempfolder, "figures", "scatter.png")) - os.remove(os.path.join(tempdir, "log.txt")) - os.remove(os.path.join(tempdir, "events.jsonl")) - os.remove(os.path.join(tempdir, "figures", "scatter.png")) + os.remove(os.path.join(tempfolder, "log.txt")) + os.remove(os.path.join(tempfolder, "events.jsonl")) + os.remove(os.path.join(tempfolder, "figures", "scatter.png")) # sleep 1 second to ensure that the file has been deleted time.sleep(1)