From 4d163b8ee2d0b6a0ff32be754f8d9e5f61fa70ae Mon Sep 17 00:00:00 2001 From: Simon Jagoe Date: Sun, 3 Dec 2023 17:19:06 +0200 Subject: [PATCH] Use pathlib in relative path computation --- haas/plugins/discoverer.py | 8 +++++--- haas/tests/test_haas_application.py | 4 +--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/haas/plugins/discoverer.py b/haas/plugins/discoverer.py index 8290171a..73caf31b 100644 --- a/haas/plugins/discoverer.py +++ b/haas/plugins/discoverer.py @@ -8,6 +8,7 @@ from fnmatch import fnmatch from importlib import import_module +from pathlib import Path import logging import os import sys @@ -41,10 +42,11 @@ def test_error(self): def get_relpath(top_level_directory, fullpath): - normalized = os.path.normpath(fullpath) + top_level = Path(top_level_directory).resolve() + normalized = Path(fullpath).resolve() logger.debug('Determine if %r is within %r', - normalized, top_level_directory) - relpath = os.path.relpath(normalized, top_level_directory) + str(normalized), str(top_level_directory)) + relpath = str(normalized.relative_to(top_level)) logger.debug('Relative path is %r', relpath) if os.path.isabs(relpath) or relpath.startswith('..'): raise ValueError('Path not within project: {0}'.format(fullpath)) diff --git a/haas/tests/test_haas_application.py b/haas/tests/test_haas_application.py index 49820cfb..036d9875 100644 --- a/haas/tests/test_haas_application.py +++ b/haas/tests/test_haas_application.py @@ -380,9 +380,7 @@ def test_multiple_start_directories(self, runner_class, result_class, try: fixture.create(tempdir) - from pathlib import Path - - top_level = str(Path(os.path.join(tempdir, fixture.name)).resolve()) + top_level = os.path.join(tempdir, fixture.name) # When with cd(top_level):