From df9c84f4d1862fc334ea794922a6af5d7f533867 Mon Sep 17 00:00:00 2001
From: lukas leufen <l.leufen@fz-juelich.de>
Date: Tue, 12 May 2020 17:33:54 +0200
Subject: [PATCH] hopefully corrected all failing tests

---
 conftest.py                               | 21 +++++++++++++++++++++
 src/run_modules/run_environment.py        |  9 ++++++---
 test/test_modules/test_pre_processing.py  |  6 +++---
 test/test_modules/test_run_environment.py |  4 ++--
 test/test_modules/test_training.py        |  4 ++--
 5 files changed, 34 insertions(+), 10 deletions(-)

diff --git a/conftest.py b/conftest.py
index c59ec4dc..0726ea7c 100644
--- a/conftest.py
+++ b/conftest.py
@@ -1,4 +1,5 @@
 import os
+import re
 import shutil
 
 
@@ -20,5 +21,25 @@ def pytest_runtest_teardown(item, nextitem):
                 shutil.rmtree(os.path.join(path, "data"), ignore_errors=True)
             if "TestExperiment" in list_dir:
                 shutil.rmtree(os.path.join(path, "TestExperiment"), ignore_errors=True)
+            # remove all tracking json
+            remove_files_from_regex(list_dir, path, re.compile(r"tracking_\d*\.json"))
+            # remove all tracking pdf
+            remove_files_from_regex(list_dir, path, re.compile(r"tracking\.pdf"))
+            # remove all tracking json
+            remove_files_from_regex(list_dir, path, re.compile(r"logging_\d*\.log"))
     else:
         pass  # nothing to do if next test is from same test class
+
+
+def remove_files_from_regex(list_dir, path, regex):
+    r = list(filter(regex.search, list_dir))
+    if len(r) > 0:
+        for e in r:
+            del_path = os.path.join(path, e)
+            try:
+                if os.path.isfile(del_path):
+                    os.remove(del_path)
+                else:
+                    shutil.rmtree(os.path.join(path, e), ignore_errors=True)
+            except:
+                pass
diff --git a/src/run_modules/run_environment.py b/src/run_modules/run_environment.py
index b0027119..16d21ae9 100644
--- a/src/run_modules/run_environment.py
+++ b/src/run_modules/run_environment.py
@@ -114,9 +114,12 @@ class RunEnvironment(object):
             self.del_by_exit = True
             # copy log file and clear data store only if called as base class and not as super class
             if self.__class__.__name__ == "RunEnvironment":
-                TrackPlot(self.tracker_list, True, plot_folder=self.data_store.get_default("experiment_path", "."))
-                self.__save_tracking()
-                self.__copy_log_file()
+                try:
+                    TrackPlot(self.tracker_list, True, plot_folder=self.data_store.get_default("experiment_path", "."))
+                    self.__save_tracking()
+                    self.__copy_log_file()
+                except FileNotFoundError:
+                    pass
                 self.data_store.clear_data_store()
 
     def __enter__(self):
diff --git a/test/test_modules/test_pre_processing.py b/test/test_modules/test_pre_processing.py
index 338bb7fd..6abc7222 100644
--- a/test/test_modules/test_pre_processing.py
+++ b/test/test_modules/test_pre_processing.py
@@ -65,7 +65,7 @@ class TestPreProcessing:
         caplog.set_level(logging.DEBUG)
         obj_with_exp_setup.data_store.set("use_all_stations_on_all_data_sets", False, "general")
         obj_with_exp_setup.create_set_split(slice(0, 2), "awesome")
-        assert caplog.record_tuples[0] == ('root', 10, "Awesome stations (len=2): ['DEBW107', 'DEBY081']")
+        assert ('root', 10, "Awesome stations (len=2): ['DEBW107', 'DEBY081']") in caplog.record_tuples
         data_store = obj_with_exp_setup.data_store
         assert isinstance(data_store.get("generator", "general.awesome"), DataGenerator)
         with pytest.raises(NameNotFoundInScope):
@@ -75,8 +75,8 @@ class TestPreProcessing:
     def test_create_set_split_all_stations(self, caplog, obj_with_exp_setup):
         caplog.set_level(logging.DEBUG)
         obj_with_exp_setup.create_set_split(slice(0, 2), "awesome")
-        assert caplog.record_tuples[0] == ('root', 10, "Awesome stations (len=6): ['DEBW107', 'DEBY081', 'DEBW013', "
-                                                       "'DEBW076', 'DEBW087', 'DEBW001']")
+        message = "Awesome stations (len=6): ['DEBW107', 'DEBY081', 'DEBW013', 'DEBW076', 'DEBW087', 'DEBW001']"
+        assert ('root', 10, message) in caplog.record_tuples
         data_store = obj_with_exp_setup.data_store
         assert isinstance(data_store.get("generator", "general.awesome"), DataGenerator)
         with pytest.raises(NameNotFoundInScope):
diff --git a/test/test_modules/test_run_environment.py b/test/test_modules/test_run_environment.py
index d82675b5..59bb8535 100644
--- a/test/test_modules/test_run_environment.py
+++ b/test/test_modules/test_run_environment.py
@@ -17,7 +17,7 @@ class TestRunEnvironment:
         with RunEnvironment() as r:
             r.do_stuff(0.1)
         expression = PyTestRegex(r"RunEnvironment finished after \d+:\d+:\d+ \(hh:mm:ss\)")
-        assert caplog.record_tuples[-1] == ('root', 20, expression)
+        assert ('root', 20, expression) in caplog.record_tuples[-3:]
 
     def test_init(self, caplog):
         caplog.set_level(logging.INFO)
@@ -30,4 +30,4 @@ class TestRunEnvironment:
         r.do_stuff(0.2)
         del r
         expression = PyTestRegex(r"RunEnvironment finished after \d+:\d+:\d+ \(hh:mm:ss\)")
-        assert caplog.record_tuples[-1] == ('root', 20, expression)
+        assert ('root', 20, expression) in caplog.record_tuples[-3:]
diff --git a/test/test_modules/test_training.py b/test/test_modules/test_training.py
index 83b10994..33f9ddf6 100644
--- a/test/test_modules/test_training.py
+++ b/test/test_modules/test_training.py
@@ -202,8 +202,8 @@ class TestTraining:
         model_name = "test_model.h5"
         assert model_name not in os.listdir(path)
         init_without_run.save_model()
-        assert caplog.record_tuples[0] == (
-        "root", 10, PyTestRegex(f"save best model to {os.path.join(path, model_name)}"))
+        message = PyTestRegex(f"save best model to {os.path.join(path, model_name)}")
+        assert caplog.record_tuples[1] == ("root", 10, message)
         assert model_name in os.listdir(path)
 
     def test_load_best_model_no_weights(self, init_without_run, caplog):
-- 
GitLab