|
14 | 14 |
|
15 | 15 | import qgis # NOQA |
16 | 16 |
|
| 17 | +import tempfile |
| 18 | +import os |
17 | 19 | from qgis.core import QgsFileUtils |
18 | 20 | from qgis.testing import unittest |
19 | 21 |
|
@@ -61,6 +63,35 @@ def testStringToSafeFilename(self): |
61 | 63 | QgsFileUtils.stringToSafeFilename('rendered map_final? rev (12-03-1017)_real/\\?%*:|"<>.tif'), |
62 | 64 | 'rendered map_final_ rev (12-03-1017)_real__________.tif') |
63 | 65 |
|
| 66 | + def testFindClosestExistingPath(self): |
| 67 | + self.assertEqual(QgsFileUtils.findClosestExistingPath(''), '') |
| 68 | + self.assertEqual(QgsFileUtils.findClosestExistingPath('.'), '') |
| 69 | + self.assertEqual(QgsFileUtils.findClosestExistingPath('a_very_unlikely_path_to_really_exist/because/no_one_would_have_a_folder_called/MapInfo is the bestest/'), '') |
| 70 | + # sorry anyone not on linux! |
| 71 | + self.assertEqual(QgsFileUtils.findClosestExistingPath('/usr/youve_been_hacked/by_the_l77t_krew'), '/usr') |
| 72 | + |
| 73 | + base_path = tempfile.mkdtemp() |
| 74 | + file = os.path.join(base_path, 'test.csv') |
| 75 | + with open(file, 'wt') as f: |
| 76 | + f.write('\n') |
| 77 | + |
| 78 | + self.assertEqual(QgsFileUtils.findClosestExistingPath(os.path.join(base_path, 'a file name.bmp')), base_path) # non-existant file |
| 79 | + self.assertEqual(QgsFileUtils.findClosestExistingPath(file), base_path) # real file! |
| 80 | + self.assertEqual(QgsFileUtils.findClosestExistingPath(os.path.join(base_path, 'non/existant/subfolder')), base_path) |
| 81 | + |
| 82 | + sub_folder1 = os.path.join(base_path, 'subfolder1') |
| 83 | + os.mkdir(sub_folder1) |
| 84 | + sub_folder2 = os.path.join(sub_folder1, 'subfolder2') |
| 85 | + os.mkdir(sub_folder2) |
| 86 | + bad_sub_folder = os.path.join(sub_folder2, 'nooo') |
| 87 | + self.assertEqual(QgsFileUtils.findClosestExistingPath(bad_sub_folder), sub_folder2) |
| 88 | + self.assertEqual(QgsFileUtils.findClosestExistingPath(sub_folder2), sub_folder2) |
| 89 | + self.assertEqual(QgsFileUtils.findClosestExistingPath(sub_folder2 + '/.'), sub_folder2) |
| 90 | + self.assertEqual(QgsFileUtils.findClosestExistingPath(sub_folder2 + '/..'), sub_folder1) |
| 91 | + self.assertEqual(QgsFileUtils.findClosestExistingPath(sub_folder2 + '/../ddddddd'), sub_folder1) |
| 92 | + self.assertEqual(QgsFileUtils.findClosestExistingPath(sub_folder2 + '/../subfolder2'), sub_folder2) |
| 93 | + self.assertEqual(QgsFileUtils.findClosestExistingPath(sub_folder2 + '/../subfolder2/zxcv/asfdasd'), sub_folder2) |
| 94 | + |
64 | 95 |
|
65 | 96 | if __name__ == '__main__': |
66 | 97 | unittest.main() |
0 commit comments