Skip to content

Commit 322c8c6

Browse files
committed
'contains' action tests of common backend test suite
1 parent dce866a commit 322c8c6

File tree

8 files changed

+137
-18
lines changed

8 files changed

+137
-18
lines changed

contextshell/backends/node.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -92,40 +92,40 @@ def __init__(self):
9292
self.root = Node(None)
9393
self.install_default_actions()
9494

95-
def create_action(self, target: NodePath, path: str, value=None): # NOCOVER
95+
def create_action(self, target: NodePath, path: str, value=None):
9696
self.create(NodePath.join(target, path), value)
9797

98-
def contains_action(self, target: NodePath, path: str) -> bool: # NOCOVER
98+
def contains_action(self, target: NodePath, path: str) -> bool:
9999
return self.contains(NodePath.join(target, path))
100100

101-
def get_action(self, target: NodePath): # NOCOVER
101+
def get_action(self, target: NodePath):
102102
return self.get(target)
103103

104-
def set_action(self, target: NodePath, new_value): # NOCOVER
104+
def set_action(self, target: NodePath, new_value):
105105
return self.set(target, new_value)
106106

107-
def list_action(self, target: NodePath): # NOCOVER
107+
def list_action(self, target: NodePath):
108108
all_list = self.list(target)
109109
return list(filter(lambda p: not NodePath(p).is_attribute, all_list))
110110

111-
def list_all_action(self, target: NodePath): # NOCOVER
111+
def list_all_action(self, target: NodePath):
112112
return self.list(target)
113113

114-
def list_attributes_action(self, target: NodePath): # NOCOVER
114+
def list_attributes_action(self, target: NodePath):
115115
all_list = self.list(target)
116116
return list(filter(lambda p: NodePath(p).is_attribute, all_list))
117117

118-
def list_actions_action(self, target: NodePath): # NOCOVER
118+
def list_actions_action(self, target: NodePath):
119119
# FIXME: use the same mechanism as in self.find_action
120120
# CHECK: consider using find.all.actions action
121121
actions_branch = NodePath.join(target, "@actions")
122122
return self.list(actions_branch)
123123

124-
def remove_action(self, target: NodePath): # NOCOVER
124+
def remove_action(self, target: NodePath):
125125
# CHECK: use 'path' argument?
126126
return self.remove(target)
127127

128-
def find_type_action(self, target: NodePath, type_name: str): # NOCOVER
128+
def find_type_action(self, target: NodePath, type_name: str):
129129
return self.find_type(target, NodePath(type_name))
130130

131131
def install_default_actions(self):

tests/unit/ActionExecutorTests.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from typing import Optional
44

55
from contextshell.action import Action, ActionExecutor, pack_argument_tree
6-
from tests.unit.Fakes import FakeAction
6+
from tests.unit.fakes import FakeAction
77

88
from contextshell.path import NodePath, NodePath as np # isort:skip
99

tests/unit/BuiltinExecutorTests.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from contextshell.action import BuiltinExecutor
44
from contextshell.path import NodePath
5-
from tests.unit.Fakes import FakeAction
5+
from tests.unit.fakes import FakeAction
66

77

88
class RegisterAction(unittest.TestCase):
+49-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,56 @@
1+
import os
2+
import tempfile
13
import unittest
4+
from pathlib import Path
5+
from typing import List
26

3-
from contextshell.path import NodePath as np
7+
from contextshell.action import Executor
8+
9+
from .bases import Base
10+
11+
from contextshell.path import NodePath, NodePath as np # isort:skip
412

513

614
def create_filesystem_tree(*args, **kwargs):
715
from contextshell.backends.filesystem import FilesystemTree
816
return FilesystemTree(*args, **kwargs)
17+
18+
19+
class TemporaryDirectoryTestsBase(unittest.TestCase):
20+
"""Test base for tests wishing to use actual filesystem directory"""
21+
def _make_test_path(self, relative_path):
22+
return Path(self.test_directory.name).joinpath(relative_path)
23+
24+
def create_file(self, path: str, contents: str=''):
25+
with open(self._make_test_path(path), 'w') as file:
26+
file.write(contents)
27+
28+
def create_directory(self, path):
29+
os.mkdir(self._make_test_path(path))
30+
31+
def setUp(self):
32+
super().setUp()
33+
temp_dir_suffix = type(self).__name__
34+
self.test_directory = tempfile.TemporaryDirectory(temp_dir_suffix)
35+
36+
def tearDown(self):
37+
self.test_directory.cleanup()
38+
super().tearDown()
39+
40+
41+
class L0ActionsTests(TemporaryDirectoryTestsBase, Base.L0ActionsTests):
42+
def setUp(self):
43+
super().setUp()
44+
self.create_file('file')
45+
self.create_directory('directory')
46+
47+
def create_backend(self) -> Executor:
48+
return create_filesystem_tree(self.test_directory.name)
49+
50+
@property
51+
def existing_paths(self) -> List[NodePath]:
52+
return [np('file'), np('directory')]
53+
54+
@property
55+
def nonexistent_paths(self) -> List[NodePath]:
56+
return [np('nonexistent')]

tests/unit/backends/NodeTreeTests.py

+27-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import unittest
2+
from typing import List
23

3-
from contextshell.path import NodePath as np
4-
from tests.unit.Fakes import FakeAction
4+
from contextshell.action import Executor
5+
6+
from ..fakes import FakeAction
7+
from .bases import Base
8+
9+
from contextshell.path import NodePath, NodePath as np # isort:skip
510

611

712
def create_tree(*args, **kwargs):
@@ -61,7 +66,7 @@ def test_resolve_optional_existing(self):
6166

6267
resolved_node = tree._resolve_optional_path(existing_path)
6368

64-
self.assertIs('BAR', resolved_node.get())
69+
self.assertEqual('BAR', resolved_node.get())
6570

6671
def test_resolve_optional_nonexistent(self):
6772
tree = create_tree()
@@ -260,7 +265,7 @@ def test_node_specific_actions(self):
260265

261266

262267
class FindFirstInTests(unittest.TestCase):
263-
def test_no_canditates_provided(self):
268+
def test_no_candidates_provided(self):
264269
tree = create_tree()
265270

266271
with self.assertRaises(ValueError):
@@ -392,3 +397,21 @@ def test_resolve_order_target_before_global(self):
392397
found_type = tree.find_type(np('.target'), type_name)
393398

394399
self.assertIs(target_type, found_type)
400+
401+
402+
class L0ActionsTests(Base.L0ActionsTests):
403+
def setUp(self):
404+
super().setUp()
405+
self.root = create_tree()
406+
self.root.create(np('.node'))
407+
408+
def create_backend(self) -> Executor:
409+
return self.root
410+
411+
@property
412+
def existing_paths(self) -> List[NodePath]:
413+
return [np('node')]
414+
415+
@property
416+
def nonexistent_paths(self) -> List[NodePath]:
417+
return [np('nonexistent')]

tests/unit/backends/VirtualTreeTests.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from contextshell.action import pack_argument_tree
44
from contextshell.path import NodePath as np
5-
from tests.unit.Fakes import FakeActionExecutor
5+
from tests.unit.fakes import FakeActionExecutor
66

77

88
def create_virtual_tree():

tests/unit/backends/bases.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import unittest
2+
from abc import ABC, abstractmethod
3+
from typing import List
4+
5+
from contextshell.action import Executor, pack_argument_tree
6+
7+
from contextshell.path import NodePath, NodePath as np # isort:skip
8+
9+
10+
class Base:
11+
class ContainsActionTests(ABC, unittest.TestCase):
12+
@abstractmethod
13+
def create_backend(self) -> Executor:
14+
raise NotImplementedError()
15+
16+
@property
17+
@abstractmethod
18+
def existing_paths(self) -> List[NodePath]:
19+
raise NotImplementedError()
20+
21+
@property
22+
@abstractmethod
23+
def nonexistent_paths(self) -> List[NodePath]:
24+
raise NotImplementedError()
25+
26+
def test_existing_paths(self):
27+
backend = self.create_backend()
28+
29+
for path in self.existing_paths:
30+
with self.subTest("Existing path", path=path):
31+
args = pack_argument_tree(path)
32+
exists = backend.execute(np("."), np('contains'), args)
33+
self.assertTrue(exists, f"{path} doesn't exist (but should)")
34+
35+
def test_nonexistent_paths(self):
36+
backend = self.create_backend()
37+
38+
for path in self.nonexistent_paths:
39+
with self.subTest("Nonexistent path", path=path):
40+
args = pack_argument_tree(path)
41+
exists = backend.execute(np("."), np('contains'), args)
42+
self.assertFalse(exists, f"{path} exist (but shouldn't)")
43+
44+
class L0ActionsTests(
45+
ContainsActionTests,
46+
ABC
47+
):
48+
pass
File renamed without changes.

0 commit comments

Comments
 (0)