Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Pass through resolve_path when the path is not accessible" #776

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion newsfragments/772.bugfix

This file was deleted.

14 changes: 5 additions & 9 deletions src/dxtbx/serialize/filename.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,11 @@ def resolve_path(path, directory=None):
directory (Optional[str]): The local path to resolve relative links

Returns:
str: The absolute path to the file to read if accessible, otherwise
return the original path as provided
str: The absolute path to the file to read
"""
if not path:
return ""
trial_path = os.path.expanduser(os.path.expandvars(path))
if directory and not os.path.isabs(trial_path):
trial_path = os.path.join(directory, trial_path)
if os.path.exists(trial_path):
return os.path.abspath(trial_path)
else:
return path
path = os.path.expanduser(os.path.expandvars(path))
if directory and not os.path.isabs(path):
path = os.path.join(directory, path)
return os.path.abspath(path)
23 changes: 4 additions & 19 deletions tests/serialize/test_filename.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,15 @@
from __future__ import annotations

import os
import uuid

from dxtbx.serialize.filename import resolve_path


def test_resolve_path(monkeypatch):
# Resolve path
def alwaystrue(path):
return True

# Set an environment variable
monkeypatch.setenv("HELLO_WORLD", "EXPANDED")

# First try a path that does not exist
filename = str(uuid.uuid4())
new_path = os.path.join("~", "$HELLO_WORLD", filename)
new_path = os.path.join("~", "$HELLO_WORLD", "path")
path = resolve_path(new_path)
assert path == new_path

# Now pretend the path exists
monkeypatch.setattr(os.path, "exists", alwaystrue)
path = resolve_path(new_path)
assert path == os.path.join(os.path.expanduser("~"), "EXPANDED", filename)

new_path = os.path.join("$HELLO_WORLD", filename)
assert path == os.path.join(os.path.expanduser("~"), "EXPANDED", "path")
new_path = os.path.join("$HELLO_WORLD", "path")
path = resolve_path(new_path)
assert path == os.path.abspath(os.path.join("EXPANDED", filename))
assert path == os.path.abspath(os.path.join("EXPANDED", "path"))
Loading