get_uri, resolve_uri - refactor #6605

1) Use pathlib for consistency
2) Ensure consistent output - always use posix format and resolve paths (but keep the symlinks)
This commit is contained in:
Andrej730
2025-04-29 14:23:45 +05:00
parent 7b71e2ce2a
commit e792cf8fa0
2 changed files with 138 additions and 22 deletions
+101
View File
@@ -22,6 +22,8 @@ import test.bim.bootstrap
import bonsai.core.tool
import bonsai.tool as tool
import pytest
import tempfile
from pathlib import Path
from bonsai.tool.ifc import Ifc as subject
@@ -237,3 +239,102 @@ class TestUnlink(test.bim.bootstrap.NewFile):
subject.unlink(element=element, obj=obj)
assert subject.get_entity(obj) == element
assert subject.get_object(element) == obj
class TestUri(test.bim.bootstrap.NewFile):
def test_get_uri(self):
ifc = ifcopenshell.file()
subject.set(ifc)
with tempfile.TemporaryDirectory() as tmp_dir:
base_path = Path(tmp_dir)
project_dir = base_path / "project"
project_dir.mkdir()
path = project_dir / "project.ifc"
test_name = "test.xml"
test_filepath = project_dir / test_name
test_filepath.touch()
path.touch()
subject.set_path(subject.normalize_path(path))
# Test absolute filepaths.
abs_filepath = str(test_filepath)
assert subject.get_uri(abs_filepath, False) == subject.normalize_path(abs_filepath)
assert subject.get_uri(abs_filepath, True) == test_name
# Test relative filepaths.
rel_filepath = test_name
with pytest.raises(ValueError):
subject.get_uri(rel_filepath, False)
assert subject.get_uri(rel_filepath, True) == test_name
# Test that paths are resolved, but keeping symlinks.
link_name = "link.xml"
link_path = base_path / link_name
link_path.symlink_to(test_filepath)
# Test absolute paths.
abs_filepath = test_filepath.parent / ".." / link_name
assert subject.get_uri(abs_filepath, False) == subject.normalize_path(abs_filepath)
assert subject.get_uri(abs_filepath, True) == "../" + link_name
# Test relative paths.
rel_filepath = "../" + link_name
with pytest.raises(ValueError):
subject.get_uri(rel_filepath, False)
assert subject.get_uri(rel_filepath, True) == rel_filepath
def test_resolve_uri(self):
with tempfile.TemporaryDirectory() as tmp_dir:
base_path = Path(tmp_dir)
project_dir = base_path / "project"
project_dir.mkdir()
path = project_dir / "project.ifc"
path.touch()
subject.set_path(subject.normalize_path(path))
test_name = "test.xml"
test_filepath = project_dir / test_name
test_filepath.touch()
# Test absolute filepaths.
abs_filepath = str(test_filepath)
assert subject.resolve_uri(abs_filepath) == subject.normalize_path(abs_filepath)
rel_filepath = test_name
assert subject.resolve_uri(rel_filepath) == subject.normalize_path(abs_filepath)
# Test that paths are resolved, but keeping symlinks.
link_name = "link.xml"
link_path = base_path / link_name
link_path.symlink_to(test_filepath)
# Test absolute path.
abs_filepath = test_filepath.parent / ".." / link_name
assert subject.resolve_uri(abs_filepath) == subject.normalize_path(abs_filepath)
# Test relative path.
rel_filepath = "../" + link_name
assert subject.resolve_uri(rel_filepath) == subject.normalize_path(abs_filepath)
def test_normalize_path(self):
# Posix format.
path = r"test\test.txt"
assert subject.normalize_path(path) == "test/test.txt"
with tempfile.TemporaryDirectory() as tmp_dir:
base_path = Path(tmp_dir)
project_path = base_path / "project"
project_path.mkdir()
original_file = project_path / "test.xml"
original_file.touch()
link_name = "link.xml"
link_path = project_path / link_name
link_path.symlink_to(original_file)
# Absolute path.
abs_path = project_path / ".." / link_name
assert subject.normalize_path(abs_path) == (base_path / link_name).as_posix()
rel_path = "../" + link_name
assert subject.normalize_path(rel_path) == rel_path