|
| 1 | +"""Tests for path resolution utilities.""" |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | +import tempfile |
| 5 | +import os |
| 6 | + |
| 7 | +from entropy.utils import resolve_relative_to, make_relative_to |
| 8 | + |
| 9 | + |
| 10 | +class TestResolveRelativeTo: |
| 11 | + """Tests for resolve_relative_to function.""" |
| 12 | + |
| 13 | + def test_relative_path_resolved(self): |
| 14 | + """Relative paths should be resolved against base file's directory.""" |
| 15 | + result = resolve_relative_to( |
| 16 | + "population.yaml", Path("/project/study/scenario.yaml") |
| 17 | + ) |
| 18 | + assert result == Path("/project/study/population.yaml") |
| 19 | + |
| 20 | + def test_absolute_path_unchanged(self): |
| 21 | + """Absolute paths should be returned unchanged.""" |
| 22 | + result = resolve_relative_to( |
| 23 | + "/abs/path/pop.yaml", Path("/project/scenario.yaml") |
| 24 | + ) |
| 25 | + assert result == Path("/abs/path/pop.yaml") |
| 26 | + |
| 27 | + def test_nested_relative_path(self): |
| 28 | + """Nested relative paths should resolve correctly.""" |
| 29 | + result = resolve_relative_to( |
| 30 | + "data/agents.json", Path("/project/study/scenario.yaml") |
| 31 | + ) |
| 32 | + assert result == Path("/project/study/data/agents.json") |
| 33 | + |
| 34 | + def test_parent_relative_path(self): |
| 35 | + """Parent-relative paths (../) should resolve correctly.""" |
| 36 | + result = resolve_relative_to( |
| 37 | + "../common/pop.yaml", Path("/project/study/scenario.yaml") |
| 38 | + ) |
| 39 | + assert result == Path("/project/common/pop.yaml") |
| 40 | + |
| 41 | + def test_path_object_input(self): |
| 42 | + """Should accept Path objects as input.""" |
| 43 | + result = resolve_relative_to( |
| 44 | + Path("population.yaml"), Path("/project/study/scenario.yaml") |
| 45 | + ) |
| 46 | + assert result == Path("/project/study/population.yaml") |
| 47 | + |
| 48 | + |
| 49 | +class TestMakeRelativeTo: |
| 50 | + """Tests for make_relative_to function.""" |
| 51 | + |
| 52 | + def test_sibling_file_relative(self): |
| 53 | + """Files in same directory should return just filename.""" |
| 54 | + result = make_relative_to( |
| 55 | + "/project/study/population.yaml", Path("/project/study/scenario.yaml") |
| 56 | + ) |
| 57 | + assert result == "population.yaml" |
| 58 | + |
| 59 | + def test_nested_file_relative(self): |
| 60 | + """Files in subdirectory should return relative path.""" |
| 61 | + result = make_relative_to( |
| 62 | + "/project/study/data/agents.json", Path("/project/study/scenario.yaml") |
| 63 | + ) |
| 64 | + assert result == "data/agents.json" |
| 65 | + |
| 66 | + def test_unrelated_path_absolute(self): |
| 67 | + """Unrelated paths should return absolute path.""" |
| 68 | + result = make_relative_to( |
| 69 | + "/other/path/pop.yaml", Path("/project/study/scenario.yaml") |
| 70 | + ) |
| 71 | + assert result == "/other/path/pop.yaml" |
| 72 | + |
| 73 | + def test_relative_input_resolved(self): |
| 74 | + """Relative input paths should be resolved against cwd first.""" |
| 75 | + # Use a temp directory to test this |
| 76 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 77 | + # Create structure: tmpdir/study/scenario.yaml and tmpdir/study/pop.yaml |
| 78 | + study_dir = Path(tmpdir) / "study" |
| 79 | + study_dir.mkdir() |
| 80 | + scenario = study_dir / "scenario.yaml" |
| 81 | + pop = study_dir / "population.yaml" |
| 82 | + scenario.touch() |
| 83 | + pop.touch() |
| 84 | + |
| 85 | + # Save original cwd |
| 86 | + orig_cwd = os.getcwd() |
| 87 | + try: |
| 88 | + os.chdir(tmpdir) |
| 89 | + # When cwd is tmpdir, "study/population.yaml" should become "population.yaml" |
| 90 | + # relative to "study/scenario.yaml" |
| 91 | + result = make_relative_to("study/population.yaml", Path("study/scenario.yaml")) |
| 92 | + assert result == "population.yaml" |
| 93 | + finally: |
| 94 | + os.chdir(orig_cwd) |
| 95 | + |
| 96 | + |
| 97 | +class TestRoundTrip: |
| 98 | + """Test that make_relative_to and resolve_relative_to are inverses.""" |
| 99 | + |
| 100 | + def test_roundtrip_same_directory(self): |
| 101 | + """Roundtrip for files in same directory.""" |
| 102 | + base = Path("/project/study/scenario.yaml") |
| 103 | + original = "/project/study/population.yaml" |
| 104 | + |
| 105 | + relative = make_relative_to(original, base) |
| 106 | + resolved = resolve_relative_to(relative, base) |
| 107 | + |
| 108 | + assert str(resolved) == original |
| 109 | + |
| 110 | + def test_roundtrip_nested(self): |
| 111 | + """Roundtrip for nested files.""" |
| 112 | + base = Path("/project/study/scenario.yaml") |
| 113 | + original = "/project/study/data/agents.json" |
| 114 | + |
| 115 | + relative = make_relative_to(original, base) |
| 116 | + resolved = resolve_relative_to(relative, base) |
| 117 | + |
| 118 | + assert str(resolved) == original |
0 commit comments