From 13db217b627dd17819e33a79b6823e76ed8c8855 Mon Sep 17 00:00:00 2001 From: Ivan Date: Thu, 23 Jan 2025 12:02:23 -0300 Subject: [PATCH] Fixed erroneous path resolution Fixed wrong path resolution when fusesoc.conf file is in current dir, also improved path resolution to avoid erros when comparing paths --- fusesoc/config.py | 4 ++-- tests/test_config.py | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/fusesoc/config.py b/fusesoc/config.py index bec501ad..0436736f 100644 --- a/fusesoc/config.py +++ b/fusesoc/config.py @@ -103,8 +103,8 @@ def _resolve_path_from_cfg(self, path): if os.path.isabs(expanded): return expanded else: - cfg_file_dir = os.path.dirname(self._path) - return os.path.join(cfg_file_dir, expanded) + cfg_file_dir = os.path.dirname(os.path.abspath(self._path)) + return os.path.normpath(os.path.join(cfg_file_dir, expanded)) def _path_from_cfg(self, name): as_str = self._cp.get(Config.default_section, name, fallback=None) diff --git a/tests/test_config.py b/tests/test_config.py index f3da1917..c6d75573 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -92,6 +92,47 @@ def test_config_relative_path(): assert getattr(conf, name) == os.path.join(abs_td, name) +def test_config_relative_path_starts_with_dot(): + with tempfile.TemporaryDirectory() as td: + config_path = os.path.join(td, "fusesoc.conf") + with open(config_path, "w") as tcf: + tcf.write( + EXAMPLE_CONFIG.format( + build_root="./build_root", + cache_root="./cache_root", + cores_root="./cores_root", + library_root="./library_root", + ) + ) + + conf = Config(tcf.name) + for name in ["build_root", "cache_root", "library_root"]: + abs_td = os.path.abspath(td) + assert getattr(conf, name) == os.path.join(abs_td, name) + + +def test_config_relative_path_with_local_config(): + prev_dir = os.getcwd() + with tempfile.TemporaryDirectory() as td: + os.chdir(td) + config_path = "fusesoc.conf" + with open(config_path, "w") as tcf: + tcf.write( + EXAMPLE_CONFIG.format( + build_root="build_root", + cache_root="cache_root", + cores_root="cores_root", + library_root="library_root", + ) + ) + + conf = Config(tcf.name) + for name in ["build_root", "cache_root", "library_root"]: + abs_td = os.path.abspath(td) + assert getattr(conf, name) == os.path.join(abs_td, name) + os.chdir(prev_dir) + + def test_config_libraries(): tcf = tempfile.NamedTemporaryFile(mode="w+") tcf.write(