|
| 1 | +"""Tests for ``openfold3.core.data.tools.utils``.""" |
| 2 | + |
| 3 | +import getpass |
| 4 | +import tempfile |
| 5 | +from pathlib import Path |
| 6 | +from unittest.mock import patch |
| 7 | + |
| 8 | +from openfold3.core.data.tools.utils import get_of3_tmpdir |
| 9 | + |
| 10 | + |
| 11 | +class TestGetOf3Tmpdir: |
| 12 | + """Tests for get_of3_tmpdir.""" |
| 13 | + |
| 14 | + def test_returns_path(self): |
| 15 | + result = get_of3_tmpdir("test_subdir") |
| 16 | + assert isinstance(result, Path) |
| 17 | + |
| 18 | + def test_directory_is_created(self, tmp_path): |
| 19 | + with patch.object(tempfile, "gettempdir", return_value=str(tmp_path)): |
| 20 | + result = get_of3_tmpdir("mysubdir") |
| 21 | + |
| 22 | + assert result.is_dir() |
| 23 | + |
| 24 | + def test_contains_username(self, tmp_path): |
| 25 | + with patch.object(tempfile, "gettempdir", return_value=str(tmp_path)): |
| 26 | + result = get_of3_tmpdir("subdir") |
| 27 | + |
| 28 | + assert f"of3-of-{getpass.getuser()}" in str(result) |
| 29 | + |
| 30 | + def test_subdir_appended(self, tmp_path): |
| 31 | + with patch.object(tempfile, "gettempdir", return_value=str(tmp_path)): |
| 32 | + result = get_of3_tmpdir("colabfold_msas") |
| 33 | + |
| 34 | + assert result.name == "colabfold_msas" |
| 35 | + assert result.parent.name == f"of3-of-{getpass.getuser()}" |
| 36 | + |
| 37 | + def test_no_subdir(self, tmp_path): |
| 38 | + with patch.object(tempfile, "gettempdir", return_value=str(tmp_path)): |
| 39 | + result = get_of3_tmpdir() |
| 40 | + |
| 41 | + assert result.name == f"of3-of-{getpass.getuser()}" |
| 42 | + assert result.is_dir() |
| 43 | + |
| 44 | + def test_respects_tmpdir_env(self, tmp_path, monkeypatch): |
| 45 | + custom_tmp = tmp_path / "custom_tmp" |
| 46 | + custom_tmp.mkdir() |
| 47 | + monkeypatch.setenv("TMPDIR", str(custom_tmp)) |
| 48 | + # Force tempfile to re-evaluate TMPDIR |
| 49 | + tempfile.tempdir = None |
| 50 | + |
| 51 | + result = get_of3_tmpdir("subdir") |
| 52 | + |
| 53 | + assert str(result).startswith(str(custom_tmp)) |
| 54 | + |
| 55 | + def test_idempotent(self, tmp_path): |
| 56 | + with patch.object(tempfile, "gettempdir", return_value=str(tmp_path)): |
| 57 | + first = get_of3_tmpdir("subdir") |
| 58 | + second = get_of3_tmpdir("subdir") |
| 59 | + |
| 60 | + assert first == second |
| 61 | + |
| 62 | + def test_different_subdirs_are_isolated(self, tmp_path): |
| 63 | + with patch.object(tempfile, "gettempdir", return_value=str(tmp_path)): |
| 64 | + a = get_of3_tmpdir("aaa") |
| 65 | + b = get_of3_tmpdir("bbb") |
| 66 | + |
| 67 | + assert a != b |
| 68 | + assert a.parent == b.parent |
| 69 | + |
| 70 | + def test_different_users_are_isolated(self, tmp_path): |
| 71 | + with patch.object(tempfile, "gettempdir", return_value=str(tmp_path)): |
| 72 | + real = get_of3_tmpdir("data") |
| 73 | + with patch.object(getpass, "getuser", return_value="other_user"): |
| 74 | + other = get_of3_tmpdir("data") |
| 75 | + |
| 76 | + assert real != other |
| 77 | + assert "of3-of-other_user" in str(other) |
0 commit comments