Kaynağa Gözat

[Tests] Utils methods

monf 8 yıl önce
ebeveyn
işleme
859f0cae87
2 değiştirilmiş dosya ile 83 ekleme ve 1 silme
  1. 82 0
      Tests/test_utils.py
  2. 1 1
      kalliope/core/Utils/Utils.py

+ 82 - 0
Tests/test_utils.py

@@ -34,3 +34,85 @@ class TestUtils(unittest.TestCase):
                           expected_result,
                           "fail getting the parent parent path from the given path")
 
+    def test_get_real_file_path(self):
+        """
+        Expect to load the proper file following the order :
+            - Provided absolute path
+            - Current user path + file_name
+            - /etc/kalliope + file_name
+            - /path/to/kalliope/ +file_name
+        """
+        ###
+        # Test the absolute path
+        dir_path = "/tmp/kalliope/tests/"
+        file_name = "test_real_file_path"
+        absolute_path_to_test = os.path.join(dir_path,file_name)
+        expected_result = absolute_path_to_test
+        if not os.path.exists(dir_path):
+            os.makedirs(dir_path)
+
+        # touch the file
+        open(absolute_path_to_test, 'a').close()
+
+        self.assertEquals(Utils.get_real_file_path(absolute_path_to_test),
+                          expected_result,
+                          "Fail to match the given absolute path ")
+        # Clean up
+        if os.path.exists(absolute_path_to_test):
+            os.remove(absolute_path_to_test)
+
+        ###
+        # test the Current path
+        file_name = "test_real_file_path"
+        expected_result = os.getcwd() + os.sep + file_name
+
+        # touch the file
+        open(file_name, 'a').close()
+
+        self.assertEquals(Utils.get_real_file_path(file_name),
+                          expected_result,
+                          "Fail to match the Current path ")
+        # Clean up
+        if os.path.exists(file_name):
+            os.remove(file_name)
+
+        ###
+        # test /etc/kalliope
+        # /!\ need permissions
+        # dir_path = "/etc/kalliope/"
+        # file_name = "test_real_file_path"
+        # path_to_test = os.path.join(dir_path,file_name)
+        # expected_result = "/etc/kalliope" + os.sep + file_name
+        # if not os.path.exists(dir_path):
+        #     os.makedirs(dir_path)
+        #
+        # # touch the file
+        # open(path_to_test, 'a').close()
+        #
+        # self.assertEquals(Utils.get_real_file_path(file_name),
+        #                   expected_result,
+        #                   "Fail to match the /etc/kalliope path")
+        # # Clean up
+        # if os.path.exists(file_name):
+        #     os.remove(file_name)
+
+        ###
+        # /an/unknown/path/kalliope/
+        dir_path = "../kalliope/"
+        file_name = "test_real_file_path"
+        path_to_test = os.path.join(dir_path, file_name)
+        expected_result = os.path.normpath(os.getcwd() + os.sep + os.pardir + os.sep +"kalliope" + os.sep + file_name)
+        if not os.path.exists(dir_path):
+            os.makedirs(dir_path)
+
+        # touch the file
+        open(path_to_test, 'a').close()
+
+        pp = Utils.get_real_file_path(file_name)
+
+        self.assertEquals(Utils.get_real_file_path(file_name),
+                          expected_result,
+                          "Fail to match the /an/unknown/path/kalliope path")
+        # Clean up
+        if os.path.exists(file_name):
+            os.remove(file_name)

+ 1 - 1
kalliope/core/Utils/Utils.py

@@ -152,7 +152,7 @@ class Utils(object):
                 2: "/etc/kalliope" + os.sep + file_path_to_test,
                 # In this case 'get_current_file_parent_parent_path' is corresponding to kalliope root path
                 # from /an/unknown/path/kalliope/core/Utils to /an/unknown/path/kalliope
-                3: cls.get_current_file_parent_path(current_script_path) + os.sep + file_path_to_test
+                3: cls.get_current_file_parent_parent_path(current_script_path) + os.sep + file_path_to_test
             }
 
             for key in sorted(path_order):