|
11 | 11 | """ |
12 | 12 |
|
13 | 13 | import unittest |
14 | | -from os import environ, unsetenv |
| 14 | +from os import environ, getenv, unsetenv |
15 | 15 | from os.path import expanduser, expandvars |
16 | 16 | from pathlib import Path |
17 | 17 | from sys import platform as opersys |
|
27 | 27 | class TestConstants(unittest.TestCase): |
28 | 28 | """Test package static variables.""" |
29 | 29 |
|
| 30 | + def setUp(self) -> None: |
| 31 | + if "QDT_QGIS_EXE_PATH" in environ or getenv("QDT_QGIS_EXE_PATH"): |
| 32 | + unsetenv("QDT_QGIS_EXE_PATH") |
| 33 | + environ.pop("QDT_QGIS_EXE_PATH") |
| 34 | + |
30 | 35 | def test_constants(self): |
31 | 36 | """Test types.""" |
32 | 37 | self.assertIsInstance(constants.OS_CONFIG, dict) |
@@ -60,43 +65,57 @@ def test_get_qdt_working_folder(self): |
60 | 65 | ) |
61 | 66 |
|
62 | 67 | # using environment variable |
63 | | - # environ["QDT_LOCAL_WORK_DIR"] = "~/.cache/qdt/unit-tests-env-var/" |
64 | | - # self.assertEqual( |
65 | | - # constants.get_qdt_working_directory(), |
66 | | - # Path(Path.home(), ".cache/qdt/unit-tests-env-var/"), |
67 | | - # ) |
68 | | - # unsetenv("QDT_LOCAL_WORK_DIR") |
| 68 | + environ["QDT_LOCAL_WORK_DIR"] = "~/.cache/qdt/unit-tests-env-var/" |
| 69 | + self.assertEqual( |
| 70 | + constants.get_qdt_working_directory(), |
| 71 | + Path(Path.home(), ".cache/qdt/unit-tests-env-var/"), |
| 72 | + ) |
| 73 | + unsetenv("QDT_LOCAL_WORK_DIR") |
69 | 74 |
|
70 | 75 | def test_get_qgis_bin_path(self): |
71 | 76 | """Test get GIS exe path helper property""" |
72 | 77 | os_config: constants.OS_CONFIG = constants.OS_CONFIG.get(opersys) |
73 | | - unsetenv("QDT_QGIS_EXE_PATH") |
74 | | - |
75 | 78 | # default value |
76 | 79 | self.assertEqual(os_config.get_qgis_bin_path, os_config.qgis_bin_exe_path) |
77 | 80 |
|
78 | | - # with environment var set as str |
| 81 | + def test_get_qgis_bin_path_with_env_var_str(self): |
| 82 | + """Test with environment var set as str""" |
| 83 | + if "QDT_QGIS_EXE_PATH" in environ: |
| 84 | + environ.pop("QDT_QGIS_EXE_PATH") |
79 | 85 | environ["QDT_QGIS_EXE_PATH"] = "/usr/bin/toto" |
| 86 | + os_config: constants.OS_CONFIG = constants.OS_CONFIG.get(opersys) |
80 | 87 | self.assertEqual(os_config.get_qgis_bin_path, Path("/usr/bin/toto")) |
| 88 | + |
| 89 | + environ.pop("QDT_QGIS_EXE_PATH") |
81 | 90 | environ["QDT_QGIS_EXE_PATH"] = "~/qgis-ltr-bin.exe" |
| 91 | + os_config: constants.OS_CONFIG = constants.OS_CONFIG.get(opersys) |
82 | 92 | self.assertEqual( |
83 | 93 | os_config.get_qgis_bin_path, |
84 | 94 | Path(expanduser("~/qgis-ltr-bin.exe")).resolve(), |
85 | 95 | ) |
86 | 96 |
|
| 97 | + environ.pop("QDT_QGIS_EXE_PATH") |
| 98 | + |
| 99 | + def test_get_qgis_bin_path_with_env_var_dict(self): |
| 100 | + """Test get GIS exe path helper property""" |
| 101 | + if "QDT_QGIS_EXE_PATH" in environ: |
| 102 | + environ.pop("QDT_QGIS_EXE_PATH") |
87 | 103 | # with environment var set as dict |
88 | 104 | d_test = { |
89 | 105 | "linux": "/usr/bin/qgis", |
90 | 106 | "darwin": "/usr/bin/qgis", |
91 | 107 | "win32": "%PROGRAMFILES%/QGIS/3_22/bin/qgis-ltr-bin.exe", |
92 | 108 | } |
93 | 109 | environ["QDT_QGIS_EXE_PATH"] = str(d_test) |
| 110 | + |
| 111 | + os_config: constants.OS_CONFIG = constants.OS_CONFIG.get(opersys) |
| 112 | + |
94 | 113 | self.assertEqual( |
95 | 114 | os_config.get_qgis_bin_path, |
96 | 115 | Path(expandvars(expanduser(d_test.get(opersys)))), |
97 | 116 | ) |
98 | 117 |
|
99 | | - unsetenv("QDT_QGIS_EXE_PATH") |
| 118 | + environ.pop("QDT_QGIS_EXE_PATH") |
100 | 119 |
|
101 | 120 |
|
102 | 121 | # ############################################################################ |
|
0 commit comments