Skip to content

Commit b0c3871

Browse files
authored
Rework json map path logic (#177)
2 parents 19e41c4 + d6a94b4 commit b0c3871

File tree

4 files changed

+65
-34
lines changed

4 files changed

+65
-34
lines changed

src/techui_builder/builder.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ def setup(self):
6060
self._extract_services()
6161
synoptic_dir = self._write_directory
6262

63-
self.clean_bobs()
63+
self.clean_files()
6464

6565
self.generator = Generator(synoptic_dir)
6666

67-
def clean_bobs(self):
67+
def clean_files(self):
6868
exclude = {"index.bob"}
6969
bobs = [
7070
bob
@@ -82,10 +82,19 @@ def clean_bobs(self):
8282
logger_.debug(f"Screens to validate: {list(self.validator.validate.keys())}")
8383

8484
logger_.info("Cleaning synoptic/ of generated screens.")
85-
# Remove any generated bobs that exist
86-
for bob in self.generated_bobs:
87-
logger_.debug(f"Removing generated screen: {bob.name}")
88-
os.remove(bob)
85+
86+
try:
87+
# Find the JsonMap file
88+
json_map_file = next(self._write_directory.glob("JsonMap.json"))
89+
# If it exists, we want to remove it too
90+
generated_files = [*self.generated_bobs, json_map_file]
91+
except StopIteration:
92+
generated_files = self.generated_bobs
93+
94+
# Remove any generated files that exist
95+
for file_ in generated_files:
96+
logger_.debug(f"Removing generated file: {file_.name}")
97+
os.remove(file_)
8998

9099
def _extract_services(self):
91100
"""
@@ -195,9 +204,9 @@ def _get_macros(element: ObjectifiedElement):
195204
if visited is None:
196205
visited = set()
197206

198-
current_node = JsonMap(str(screen_path))
207+
current_node = JsonMap(str(screen_path.relative_to(self._write_directory)))
199208

200-
abs_path = screen_path
209+
abs_path = screen_path.absolute()
201210
dest_path = dest_path
202211
if abs_path in visited:
203212
current_node.exists = True

tests/conftest.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
@pytest.fixture
1212
def builder():
13-
ixx_services = Path(__file__).parent.parent.joinpath(Path("example/t01-services"))
13+
ixx_services = Path(__file__).parent.joinpath(Path("t01-services"))
1414
techui_path = ixx_services.joinpath("synoptic/techui.yaml")
1515

1616
b = Builder(techui_path)
@@ -28,21 +28,34 @@ def builder_with_setup(builder: Builder):
2828
return builder
2929

3030

31+
@pytest.fixture
32+
def builder_with_test_files(builder: Builder):
33+
builder._write_directory = Path("tests/test_files/").absolute()
34+
35+
return builder
36+
37+
38+
@pytest.fixture
39+
def test_files():
40+
screen_path = Path("tests/test_files/test_bob.bob").absolute()
41+
dest_path = Path("tests/test_files/").absolute()
42+
43+
return screen_path, dest_path
44+
45+
3146
@pytest.fixture
3247
def example_json_map():
3348
# Create test json map with child json map
3449
test_map_child = JsonMap("test_child_bob.bob", exists=False)
35-
test_map = JsonMap("tests/test_files/test_bob.bob")
50+
test_map = JsonMap("test_bob.bob")
3651
test_map.children.append(test_map_child)
3752

3853
return test_map
3954

4055

4156
@pytest.fixture
4257
def generator():
43-
synoptic_dir = Path(__file__).parent.parent.joinpath(
44-
Path("example/t01-services/synoptic")
45-
)
58+
synoptic_dir = Path(__file__).parent.joinpath(Path("t01-services/synoptic"))
4659

4760
g = Generator(synoptic_dir)
4861

tests/t01-services

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../example/t01-services

tests/test_builder.py

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -176,17 +176,18 @@ def test_write_json_map(builder):
176176
os.remove(dest_path)
177177

178178

179-
def test_generate_json_map(builder, example_json_map):
180-
screen_path = Path("tests/test_files/test_bob.bob")
181-
dest_path = Path("tests/test_files/")
179+
def test_generate_json_map(builder_with_test_files, example_json_map, test_files):
180+
screen_path, dest_path = test_files
182181

183182
# We don't want to access the _get_action_group function in this test
184183
with patch("techui_builder.builder._get_action_group") as mock_get_action_group:
185184
mock_xml = objectify.Element("action")
186185
mock_xml["file"] = "test_child_bob.bob"
187186
mock_get_action_group.return_value = mock_xml
188187

189-
test_json_map = builder._generate_json_map(screen_path, dest_path)
188+
test_json_map = builder_with_test_files._generate_json_map(
189+
screen_path.absolute(), dest_path
190+
)
190191

191192
assert test_json_map == example_json_map
192193

@@ -204,9 +205,10 @@ def test_generate_json_map(builder, example_json_map):
204205
# assert test_json_map == example_json_map
205206

206207

207-
def test_generate_json_map_get_macros(builder, example_json_map):
208-
screen_path = Path("tests/test_files/test_bob.bob")
209-
dest_path = Path("tests/test_files/")
208+
def test_generate_json_map_get_macros(
209+
builder_with_test_files, example_json_map, test_files
210+
):
211+
screen_path, dest_path = test_files
210212

211213
# Set a custom macro to test against
212214
example_json_map.children[0].macros = {"macro": "value"}
@@ -220,43 +222,49 @@ def test_generate_json_map_get_macros(builder, example_json_map):
220222
macros["macro"] = "value"
221223
mock_get_action_group.return_value = mock_xml
222224

223-
test_json_map = builder._generate_json_map(screen_path, dest_path)
225+
test_json_map = builder_with_test_files._generate_json_map(
226+
screen_path, dest_path
227+
)
224228

225229
assert test_json_map == example_json_map
226230

227231

228-
def test_generate_json_map_visited_node(builder, example_json_map):
229-
screen_path = Path("tests/test_files/test_bob.bob")
230-
dest_path = Path("tests/test_files/")
232+
def test_generate_json_map_visited_node(
233+
builder_with_test_files, example_json_map, test_files
234+
):
235+
screen_path, dest_path = test_files
231236

232237
visited = {screen_path}
233238
# Clear children as they will never be read
234239
example_json_map.children = []
235240
# Need to set this to true too
236241
example_json_map.duplicate = True
237242

238-
test_json_map = builder._generate_json_map(screen_path, dest_path, visited)
243+
test_json_map = builder_with_test_files._generate_json_map(
244+
screen_path, dest_path, visited
245+
)
239246

240247
assert test_json_map == example_json_map
241248

242249

243-
def test_generate_json_map_xml_parse_error(builder):
244-
screen_path = Path("tests/test_files/test_bob_bad.bob")
245-
dest_path = Path("tests/test_files/")
250+
def test_generate_json_map_xml_parse_error(builder_with_test_files, test_files):
251+
screen_path = Path("tests/test_files/test_bob_bad.bob").absolute()
252+
_, dest_path = test_files
246253

247-
test_json_map = builder._generate_json_map(screen_path, dest_path)
254+
test_json_map = builder_with_test_files._generate_json_map(screen_path, dest_path)
248255

249256
assert test_json_map.error.startswith("XML parse error:")
250257

251258

252-
def test_generate_json_map_other_exception(builder):
253-
screen_path = Path("tests/test_files/test_bob.bob")
254-
dest_path = Path("tests/test_files/")
259+
def test_generate_json_map_other_exception(builder_with_test_files, test_files):
260+
screen_path, dest_path = test_files
255261

256262
with patch("techui_builder.builder._get_action_group") as mock_get_action_group:
257263
mock_get_action_group.side_effect = Exception("Some exception")
258264

259-
test_json_map = builder._generate_json_map(screen_path, dest_path)
265+
test_json_map = builder_with_test_files._generate_json_map(
266+
screen_path, dest_path
267+
)
260268

261269
assert test_json_map.error != ""
262270

@@ -265,7 +273,7 @@ def test_serialise_json_map(example_json_map):
265273
json_ = _serialise_json_map(example_json_map) # type: ignore
266274

267275
assert json_ == {
268-
"file": "tests/test_files/test_bob.bob",
276+
"file": "test_bob.bob",
269277
"children": [{"file": "test_child_bob.bob", "exists": False}],
270278
}
271279

0 commit comments

Comments
 (0)