Skip to content

Commit

Permalink
Given that we're already monkeypatching here, use this to also
Browse files Browse the repository at this point in the history
fake out os.path.exists to get the old behaviour for the test. Add
a new assert to test the new behaviour first.
  • Loading branch information
dagewa committed Nov 22, 2024
1 parent 68938bf commit 533e5c4
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions tests/serialize/test_filename.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
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")
new_path = os.path.join("~", "$HELLO_WORLD", "path")

# First try a path that does not exist
filename = str(uuid.uuid4())
new_path = os.path.join("~", "$HELLO_WORLD", filename)
path = resolve_path(new_path)
assert path == os.path.join(os.path.expanduser("~"), "EXPANDED", "path")
new_path = os.path.join("$HELLO_WORLD", "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)
path = resolve_path(new_path)
assert path == os.path.abspath(os.path.join("EXPANDED", "path"))
assert path == os.path.abspath(os.path.join("EXPANDED", filename))

0 comments on commit 533e5c4

Please sign in to comment.