test_paths.py
4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
"""U-003 tests: platform runtime directory is physically separate from any
target project directory, and stays out of this repo's own git history.
Run: python3 -m unittest src.test_paths -v
"""
import os
import tempfile
import unittest
from src import paths
_REPO_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
class PlatformHomeTest(unittest.TestCase):
def test_default_is_under_user_home(self):
home = paths.platform_home()
self.assertEqual(home, os.path.join(os.path.expanduser("~"), ".graphlemon"))
def test_default_is_not_inside_this_repo_checkout(self):
home = paths.platform_home()
self.assertFalse(
os.path.realpath(home).startswith(os.path.realpath(_REPO_ROOT) + os.sep)
)
def test_env_var_overrides_default(self):
with tempfile.TemporaryDirectory() as tmp:
custom = os.path.join(tmp, "custom-home")
old = os.environ.get("GRAPHLEMON_HOME")
os.environ["GRAPHLEMON_HOME"] = custom
try:
self.assertEqual(paths.platform_home(), os.path.abspath(custom))
finally:
if old is None:
os.environ.pop("GRAPHLEMON_HOME", None)
else:
os.environ["GRAPHLEMON_HOME"] = old
class LogFilePathTest(unittest.TestCase):
def test_matches_01_m_convention(self):
path = paths.log_file_path("proj-1", "task-1", "run-1", "node-1")
expected_suffix = os.path.join("logs", "proj-1", "task-1", "run-1", "node-1.jsonl")
self.assertTrue(path.endswith(expected_suffix), path)
self.assertTrue(path.startswith(paths.platform_home()))
class IsSeparateFromTest(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.addCleanup(self.tmp.cleanup)
self.old_home = os.environ.get("GRAPHLEMON_HOME")
self.platform_home = os.path.join(self.tmp.name, "platform-home")
os.makedirs(self.platform_home)
os.environ["GRAPHLEMON_HOME"] = self.platform_home
def tearDown(self):
if self.old_home is None:
os.environ.pop("GRAPHLEMON_HOME", None)
else:
os.environ["GRAPHLEMON_HOME"] = self.old_home
def test_unrelated_paths_are_separate(self):
target = os.path.join(self.tmp.name, "some-target-project")
os.makedirs(target)
self.assertTrue(paths.is_separate_from(target))
def test_target_equal_to_platform_home_is_not_separate(self):
self.assertFalse(paths.is_separate_from(self.platform_home))
def test_target_nested_inside_platform_home_is_not_separate(self):
nested = os.path.join(self.platform_home, "oops-a-target")
os.makedirs(nested)
self.assertFalse(paths.is_separate_from(nested))
def test_platform_home_nested_inside_target_is_not_separate(self):
# e.g. a misconfigured GRAPHLEMON_HOME pointed inside a chosen target folder
target = os.path.join(self.tmp.name, "some-target-project")
nested_home = os.path.join(target, ".graphlemon-home")
os.makedirs(nested_home)
os.environ["GRAPHLEMON_HOME"] = nested_home
self.assertFalse(paths.is_separate_from(target))
class RepoRootTest(unittest.TestCase):
def test_matches_computed_repo_root(self):
self.assertEqual(paths.repo_root(), _REPO_ROOT)
def test_contains_src_and_git(self):
root = paths.repo_root()
self.assertTrue(os.path.isdir(os.path.join(root, "src")))
self.assertTrue(os.path.isdir(os.path.join(root, ".git")))
class IsSelfTargetTest(unittest.TestCase):
def test_this_repos_own_checkout_is_self_target(self):
self.assertTrue(paths.is_self_target(_REPO_ROOT))
def test_other_directory_is_not_self_target(self):
with tempfile.TemporaryDirectory() as tmp:
self.assertFalse(paths.is_self_target(tmp))
def test_relative_path_to_repo_root_still_matches(self):
rel = os.path.relpath(_REPO_ROOT, os.getcwd())
self.assertTrue(paths.is_self_target(rel))
def test_explicit_repo_root_override(self):
with tempfile.TemporaryDirectory() as tmp:
self.assertTrue(paths.is_self_target(tmp, repo_root_path=tmp))
self.assertFalse(paths.is_self_target(_REPO_ROOT, repo_root_path=tmp))
if __name__ == "__main__":
unittest.main()