Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions src/techui_builder/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ def setup(self):
self._extract_services()
synoptic_dir = self._write_directory

self.clean_bobs()
self.clean_files()

self.generator = Generator(synoptic_dir)

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

logger_.info("Cleaning synoptic/ of generated screens.")
# Remove any generated bobs that exist
for bob in self.generated_bobs:
logger_.debug(f"Removing generated screen: {bob.name}")
os.remove(bob)

try:
# Find the JsonMap file
json_map_file = next(self._write_directory.glob("JsonMap.json"))
# If it exists, we want to remove it too
generated_files = [*self.generated_bobs, json_map_file]
except StopIteration:
generated_files = self.generated_bobs

# Remove any generated files that exist
for file_ in generated_files:
logger_.debug(f"Removing generated file: {file_.name}")
os.remove(file_)

def _extract_services(self):
"""
Expand Down Expand Up @@ -195,9 +204,9 @@ def _get_macros(element: ObjectifiedElement):
if visited is None:
visited = set()

current_node = JsonMap(str(screen_path))
current_node = JsonMap(str(screen_path.relative_to(self._write_directory)))

abs_path = screen_path
abs_path = screen_path.absolute()
dest_path = dest_path
if abs_path in visited:
current_node.exists = True
Expand Down
23 changes: 18 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

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

b = Builder(techui_path)
Expand All @@ -28,21 +28,34 @@ def builder_with_setup(builder: Builder):
return builder


@pytest.fixture
def builder_with_test_files(builder: Builder):
builder._write_directory = Path("tests/test_files/").absolute()

return builder


@pytest.fixture
def test_files():
screen_path = Path("tests/test_files/test_bob.bob").absolute()
dest_path = Path("tests/test_files/").absolute()

return screen_path, dest_path


@pytest.fixture
def example_json_map():
# Create test json map with child json map
test_map_child = JsonMap("test_child_bob.bob", exists=False)
test_map = JsonMap("tests/test_files/test_bob.bob")
test_map = JsonMap("test_bob.bob")
test_map.children.append(test_map_child)

return test_map


@pytest.fixture
def generator():
synoptic_dir = Path(__file__).parent.parent.joinpath(
Path("example/t01-services/synoptic")
)
synoptic_dir = Path(__file__).parent.joinpath(Path("t01-services/synoptic"))

g = Generator(synoptic_dir)

Expand Down
1 change: 1 addition & 0 deletions tests/t01-services
50 changes: 29 additions & 21 deletions tests/test_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,17 +176,18 @@ def test_write_json_map(builder):
os.remove(dest_path)


def test_generate_json_map(builder, example_json_map):
screen_path = Path("tests/test_files/test_bob.bob")
dest_path = Path("tests/test_files/")
def test_generate_json_map(builder_with_test_files, example_json_map, test_files):
screen_path, dest_path = test_files

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

test_json_map = builder._generate_json_map(screen_path, dest_path)
test_json_map = builder_with_test_files._generate_json_map(
screen_path.absolute(), dest_path
)

assert test_json_map == example_json_map

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


def test_generate_json_map_get_macros(builder, example_json_map):
screen_path = Path("tests/test_files/test_bob.bob")
dest_path = Path("tests/test_files/")
def test_generate_json_map_get_macros(
builder_with_test_files, example_json_map, test_files
):
screen_path, dest_path = test_files

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

test_json_map = builder._generate_json_map(screen_path, dest_path)
test_json_map = builder_with_test_files._generate_json_map(
screen_path, dest_path
)

assert test_json_map == example_json_map


def test_generate_json_map_visited_node(builder, example_json_map):
screen_path = Path("tests/test_files/test_bob.bob")
dest_path = Path("tests/test_files/")
def test_generate_json_map_visited_node(
builder_with_test_files, example_json_map, test_files
):
screen_path, dest_path = test_files

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

test_json_map = builder._generate_json_map(screen_path, dest_path, visited)
test_json_map = builder_with_test_files._generate_json_map(
screen_path, dest_path, visited
)

assert test_json_map == example_json_map


def test_generate_json_map_xml_parse_error(builder):
screen_path = Path("tests/test_files/test_bob_bad.bob")
dest_path = Path("tests/test_files/")
def test_generate_json_map_xml_parse_error(builder_with_test_files, test_files):
screen_path = Path("tests/test_files/test_bob_bad.bob").absolute()
_, dest_path = test_files

test_json_map = builder._generate_json_map(screen_path, dest_path)
test_json_map = builder_with_test_files._generate_json_map(screen_path, dest_path)

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


def test_generate_json_map_other_exception(builder):
screen_path = Path("tests/test_files/test_bob.bob")
dest_path = Path("tests/test_files/")
def test_generate_json_map_other_exception(builder_with_test_files, test_files):
screen_path, dest_path = test_files

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

test_json_map = builder._generate_json_map(screen_path, dest_path)
test_json_map = builder_with_test_files._generate_json_map(
screen_path, dest_path
)

assert test_json_map.error != ""

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

assert json_ == {
"file": "tests/test_files/test_bob.bob",
"file": "test_bob.bob",
"children": [{"file": "test_child_bob.bob", "exists": False}],
}

Expand Down