]> git.ipfire.org Git - nitsi.git/blobdiff - src/nitsi/test.py
Fix check if we get correct files for settings and recipe
[nitsi.git] / src / nitsi / test.py
index 67968dcc8bfbd4265f2b3a558f28aa5ec0e40ba7..9cc1188c2b1bdb09144af37055b0eecb1758104e 100755 (executable)
@@ -17,47 +17,95 @@ class TestException(Exception):
         self.message = message
 
 class Test():
-    def __init__(self, path, log_path, settings=None):
+    def __init__(self, log_path, dir=None, recipe_file=None, settings_file=None, cmd_settings=None):
         # init settings var
         self.settings = {}
 
-        try:
-            self.path = os.path.abspath(path)
-            self.log = logger.getChild(os.path.basename(self.path))
-        except BaseException as e:
-            logger.error("Could not get absolute path")
-            raise e
-
-        self.log.debug("Path of this test is: {}".format(self.path))
+        # Set default values for the settings dict
+        self.settings["name"] = ""
+        self.settings["description"] = ""
+        self.settings["copy_from"] = None
+        self.settings["copy_to"] = None
+        self.settings["virtual_environ_path"] = None
 
+        self.cmd_settings = cmd_settings
         self.log_path = log_path
 
-        self.settings_file = "{}/settings".format(self.path)
-        if not os.path.isfile(self.settings_file):
-            self.log.error("No such file: {}".format(self.settings_file))
-            raise TestException("No settings file found")
+        # Init all vars with None
+        self.settings_file = None
+        self.recipe_file = None
+        self.path = None
 
-        self.recipe_file = "{}/recipe".format(self.path)
-        if not os.path.isfile(self.recipe_file):
-            self.log.error("No such file: {}".format(self.recipe_file))
-            raise TestException("No recipe file found")
+        # We need at least a path to a recipe file or a dir to a test
+        if not dir and not recipe:
+            raise TestException("Did not get a path to a test or to a recipe file")
 
-        self.cmd_settings = settings
+        # We cannot decide which to use when we get both
+        if (dir and recipe_file) or (dir and settings_file):
+            raise TestException("Get dir and path to recipe or settings file")
 
-    def read_settings(self):
-        self.log.debug("Going to read all settings from the ini file")
+        if dir:
+            try:
+                if not os.path.isabs(dir):
+                    self.path = os.path.abspath(dir)
+            except BaseException as e:
+                logger.error("Could not get absolute path")
+                raise e
+
+            logger.debug("Path of this test is: {}".format(self.path))
+
+            self.recipe_file = "{}/recipe".format(self.path)
+            self.settings_file = "{}/settings".format(self.path)
+
+        if recipe_file:
+            if not os.path.isabs(recipe_file):
+                self.recipe_file = os.path.abspath(recipe_file)
+            else:
+                self.recipe_file = recipe_file
+
+        if settings_file:
+            if not os.path.isabs(settings_file):
+                self.settings_file = os.path.abspath(settings_file)
+            else:
+                self.settings_file = settings_file
+
+        # We can also go on without a settings file
+        if self.settings_file:
+            if not os.path.isfile(self.settings_file):
+                logger.error("No such file: {}".format(self.settings_file))
+                raise TestException("No settings file found")
+
+        # os.path.isfile fails if self.recipe_file is None so we need to catch exceptions here
         try:
-            self.config = configparser.ConfigParser()
-            self.config.read(self.settings_file)
-        except BaseException as e:
-            self.log.error("Failed to parse the config")
-            raise e
+            if not (self.recipe_file or os.path.isfile(self.recipe_file)):
+                logger.error("No such file: {}".format(self.recipe_file))
+                raise TestException("No recipe file found")
+        except BaseException:
+            pass
+
 
-        self.settings["name"] = self.config.get("GENERAL","name", fallback="")
-        self.settings["description"] = self.config.get("GENERAL", "description",  fallback="")
-        self.settings["copy_to"] = self.config.get("GENERAL", "copy_to", fallback=None)
-        self.settings["copy_from"] = self.config.get("GENERAL", "copy_from", fallback=None)
-        self.settings["virtual_environ_path"] = self.config.get("VIRTUAL_ENVIRONMENT", "path", fallback=None)
+        # Init logging
+        if dir:
+             self.log = logger.getChild(os.path.basename(self.path))
+
+        if recipe:
+            self.log = logger.getChild(os.path.basename(self.recipe_file))
+
+    def read_settings(self):
+        if self.settings_file:
+            self.log.debug("Going to read all settings from the ini file")
+            try:
+                self.config = configparser.ConfigParser()
+                self.config.read(self.settings_file)
+            except BaseException as e:
+                self.log.error("Failed to parse the config")
+                raise e
+
+            self.settings["name"] = self.config.get("GENERAL","name", fallback="")
+            self.settings["description"] = self.config.get("GENERAL", "description",  fallback="")
+            self.settings["copy_to"] = self.config.get("GENERAL", "copy_to", fallback=None)
+            self.settings["copy_from"] = self.config.get("GENERAL", "copy_from", fallback=None)
+            self.settings["virtual_environ_path"] = self.config.get("VIRTUAL_ENVIRONMENT", "path", fallback=None)
 
         if not self.settings["virtual_environ_path"]:
             self.log.error("No path for virtual environment found.")