Skip to content

Commit

Permalink
Various type fixes and improvements in CLI, repo, KPM
Browse files Browse the repository at this point in the history
  • Loading branch information
mszalkowski-ant committed Oct 15, 2024
1 parent 530b975 commit fb66f8c
Show file tree
Hide file tree
Showing 20 changed files with 233 additions and 224 deletions.
11 changes: 6 additions & 5 deletions tests/test_repo/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pathlib import Path

import pytest
from pyfakefs.fake_filesystem import FakeFilesystem

from topwrap.repo.files import HttpGetFile, LocalFile, TemporaryFile

Expand Down Expand Up @@ -39,7 +40,7 @@ def test_copy(self, fs, create_file):
EXISTING_FILE_NAME = "existing_file.txt"
fs.create_file(EXISTING_FILE_NAME)
with pytest.raises(FileExistsError):
file.copy(EXISTING_FILE_NAME)
file.copy(Path(EXISTING_FILE_NAME))

DEST_DIR_NAME = "dest_dir"
DEST_FILE_NAME = "dest_file.txt"
Expand Down Expand Up @@ -130,15 +131,15 @@ def test_deleting_files(self):

class TestLocalFile(TestFile):
@pytest.fixture()
def create_file(self, fs):
return lambda: LocalFile(fs.create_file("myfile.v").path)
def create_file(self, fs: FakeFilesystem):
return lambda: LocalFile(Path(fs.create_file("myfile.v").path))

@pytest.mark.usefixtures("fs")
def test_using_non_existing_file(self):
with pytest.raises(FileNotFoundError):
LocalFile("non_existing_file.txt")
LocalFile(Path("non_existing_file.txt"))

def test_using_existing_file(self, fs):
def test_using_existing_file(self, fs: FakeFilesystem):
file_path = Path(fs.create_file("existing_file.txt").path)
local_file = LocalFile(file_path)
assert local_file.path == file_path, "The file has an incorrect path"
Expand Down
31 changes: 16 additions & 15 deletions tests/test_repo/test_user_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging
import tempfile
from pathlib import Path
from typing import List, Tuple

import pytest

Expand Down Expand Up @@ -67,15 +68,15 @@ def iface_desc():


@pytest.fixture
def cores(fs, request, num_cores=2):
def cores(fs, request, num_cores=2) -> List[Core]:
cores = []
for i in range(num_cores):
additional_path = ""
if hasattr(request, "param"):
additional_path = request.param[i]
mod_name = f"{additional_path}mymod_{i}"
verilog_file = f"{mod_name}.v"
design_file = f"{mod_name}.yaml"
verilog_file = Path(f"{mod_name}.v")
design_file = Path(f"{mod_name}.yaml")

fs.create_file(verilog_file)
fs.create_file(design_file)
Expand All @@ -89,7 +90,7 @@ def ifaces(fs, num_ifaces=2, iface_path="example/path/"):
ifaces = []
for i in range(num_ifaces):
iface_name = f"{iface_path}iface_{i}"
iface_file = f"{iface_name}.yaml"
iface_file = Path(f"{iface_name}.yaml")

fs.create_file(iface_file)
iface = InterfaceDescription(iface_name, LocalFile(iface_file))
Expand Down Expand Up @@ -224,7 +225,7 @@ def test_parse(self, verilog_modules, caplog):
files.append(f)

repo = UserRepo()
repo.add_files(VerilogFileHandler([LocalFile(f.name) for f in files]))
repo.add_files(VerilogFileHandler([LocalFile(Path(f.name)) for f in files]))

assert self.contains_warnings_in_log(
caplog, '"missing_dep" of module "mydep1" was not found'
Expand All @@ -236,7 +237,7 @@ def test_parse(self, verilog_modules, caplog):
assert len(repo.resources[Core]) == len(
module_names
), f"Should have {len(module_names)} resources (Core)"
for resource in repo.resources[Core]:
for resource in repo.get_resources(Core):
assert resource.name in module_names, "Resource names differ"

assert (
Expand All @@ -249,7 +250,7 @@ def test_parse(self, fs, iface_desc):
my_interface = Path("my_interface.yaml")
fs.create_file(my_interface.name, contents=iface_desc)

handler = InterfaceFileHandler([LocalFile(my_interface.name)])
handler = InterfaceFileHandler([LocalFile(my_interface)])
resources = handler.parse()
assert len(resources) == 1, "Should have 1 resources (InterfaceDescription)"

Expand All @@ -271,27 +272,27 @@ def demo_user_repo(self, cores, ifaces):
}
return demo_user_repo

def test_getting_config_core_designs(self, yamlfiles, cores, demo_user_repo):
def test_getting_config_core_designs(
self, yamlfiles: Tuple[str, ...], cores: List[Core], demo_user_repo: UserRepo
):
extended_yamlfiles = demo_user_repo.get_core_designs()
extended_yamlfiles += yamlfiles
extended_yamlfiles += [Path(p) for p in yamlfiles]

assert len(extended_yamlfiles) == len(yamlfiles) + len(
cores
), f"Number of yaml files differs. Expected {len(extended_yamlfiles)}, got {len(yamlfiles) + len(cores)}"

for yamlfile in yamlfiles:
assert yamlfile in extended_yamlfiles, "User yamlfile is missing"
assert Path(yamlfile) in extended_yamlfiles, "User yamlfile is missing"

for core in cores:
assert (
str(core.design.path) in extended_yamlfiles
), "Core file from resources is missing"
assert core.design.path in extended_yamlfiles, "Core file from resources is missing"

EXAMPLE_PATH_MODIFIERS = ["~/test/path/long/", "/my/example/path/to/file/"]

@pytest.mark.parametrize("cores", [EXAMPLE_PATH_MODIFIERS], indirect=True)
def test_getting_srcs_dirs_for_cores(self, cores, demo_user_repo):
EXPECTED_PATHS = [str(Path(path).expanduser()) for path in self.EXAMPLE_PATH_MODIFIERS]
def test_getting_srcs_dirs_for_cores(self, cores, demo_user_repo: UserRepo):
EXPECTED_PATHS = [Path(path).expanduser() for path in self.EXAMPLE_PATH_MODIFIERS]
demo_user_repo.resources[Core] = cores
dirs_from_config = demo_user_repo.get_srcs_dirs_for_cores()

Expand Down
70 changes: 35 additions & 35 deletions tests/tests_kpm/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,64 +13,64 @@
from .common import read_json_file


def pwm_ipcores_yamls_data() -> List[str]:
_pwm_yamls_prefix = "examples/pwm/ipcores/"
def pwm_ipcores_yamls_data() -> List[Path]:
_pwm_yamls_prefix = Path("examples/pwm/ipcores/")
return [
"topwrap/ips/axi/axi_axil_adapter.yaml",
_pwm_yamls_prefix + "ps7.yaml",
_pwm_yamls_prefix + "litex_pwm.yml",
Path("topwrap/ips/axi/axi_axil_adapter.yaml"),
_pwm_yamls_prefix / "ps7.yaml",
_pwm_yamls_prefix / "litex_pwm.yml",
]


@pytest.fixture
def pwm_ipcores_yamls() -> List[str]:
def pwm_ipcores_yamls() -> List[Path]:
return pwm_ipcores_yamls_data()


def hdmi_ipcores_yamls_data() -> List[str]:
_hdmi_yamls_prefix = "examples/hdmi/ipcores/"
_axi_yamls_prefix = "topwrap/ips/axi/"
def hdmi_ipcores_yamls_data() -> List[Path]:
_hdmi_yamls_prefix = Path("examples/hdmi/ipcores/")
_axi_yamls_prefix = Path("topwrap/ips/axi/")
return [
_hdmi_yamls_prefix + "axi_dispctrl.yaml",
_hdmi_yamls_prefix + "clock_crossing.yaml",
_hdmi_yamls_prefix + "dma_axi_in_axis_out.yaml",
_hdmi_yamls_prefix + "hdmi_tx.yaml",
_hdmi_yamls_prefix + "litex_mmcm.yaml",
_hdmi_yamls_prefix + "proc_sys_reset.yaml",
_hdmi_yamls_prefix + "ps7.yaml",
_axi_yamls_prefix + "axi_axil_adapter.yaml",
_axi_yamls_prefix + "axi_interconnect.yaml",
_axi_yamls_prefix + "axi_protocol_converter.yaml",
_axi_yamls_prefix + "axis_dwidth_converter.yaml",
_axi_yamls_prefix + "axis_async_fifo.yaml",
_hdmi_yamls_prefix / "axi_dispctrl.yaml",
_hdmi_yamls_prefix / "clock_crossing.yaml",
_hdmi_yamls_prefix / "dma_axi_in_axis_out.yaml",
_hdmi_yamls_prefix / "hdmi_tx.yaml",
_hdmi_yamls_prefix / "litex_mmcm.yaml",
_hdmi_yamls_prefix / "proc_sys_reset.yaml",
_hdmi_yamls_prefix / "ps7.yaml",
_axi_yamls_prefix / "axi_axil_adapter.yaml",
_axi_yamls_prefix / "axi_interconnect.yaml",
_axi_yamls_prefix / "axi_protocol_converter.yaml",
_axi_yamls_prefix / "axis_dwidth_converter.yaml",
_axi_yamls_prefix / "axis_async_fifo.yaml",
]


@pytest.fixture
def hdmi_ipcores_yamls() -> List[str]:
def hdmi_ipcores_yamls() -> List[Path]:
return hdmi_ipcores_yamls_data()


def hierarchy_ipcores_yamls_data() -> List[str]:
_hierarchy_yamls_prefix = "examples/hierarchy/repo/cores/"
def hierarchy_ipcores_yamls_data() -> List[Path]:
_hierarchy_yamls_prefix = Path("examples/hierarchy/repo/cores/")
return [
_hierarchy_yamls_prefix + "c_mod_1/c_mod_1.yaml",
_hierarchy_yamls_prefix + "c_mod_2/c_mod_2.yaml",
_hierarchy_yamls_prefix + "c_mod_3/c_mod_3.yaml",
_hierarchy_yamls_prefix + "s1_mod_1/s1_mod_1.yaml",
_hierarchy_yamls_prefix + "s1_mod_2/s1_mod_2.yaml",
_hierarchy_yamls_prefix + "s1_mod_3/s1_mod_3.yaml",
_hierarchy_yamls_prefix + "s2_mod_1/s2_mod_1.yaml",
_hierarchy_yamls_prefix + "s2_mod_2/s2_mod_2.yaml",
_hierarchy_yamls_prefix / "c_mod_1/c_mod_1.yaml",
_hierarchy_yamls_prefix / "c_mod_2/c_mod_2.yaml",
_hierarchy_yamls_prefix / "c_mod_3/c_mod_3.yaml",
_hierarchy_yamls_prefix / "s1_mod_1/s1_mod_1.yaml",
_hierarchy_yamls_prefix / "s1_mod_2/s1_mod_2.yaml",
_hierarchy_yamls_prefix / "s1_mod_3/s1_mod_3.yaml",
_hierarchy_yamls_prefix / "s2_mod_1/s2_mod_1.yaml",
_hierarchy_yamls_prefix / "s2_mod_2/s2_mod_2.yaml",
]


@pytest.fixture
def hierarchy_ipcores_yamls() -> List[str]:
def hierarchy_ipcores_yamls() -> List[Path]:
return hierarchy_ipcores_yamls_data()


def all_yaml_files_data() -> Dict[str, List[str]]:
def all_yaml_files_data() -> Dict[str, List[Path]]:
return {
"pwm": pwm_ipcores_yamls_data(),
"hdmi": hdmi_ipcores_yamls_data(),
Expand All @@ -80,7 +80,7 @@ def all_yaml_files_data() -> Dict[str, List[str]]:


@pytest.fixture
def all_yaml_files() -> Dict[str, List[str]]:
def all_yaml_files() -> Dict[str, List[Path]]:
return all_yaml_files_data()


Expand Down
13 changes: 7 additions & 6 deletions tests/tests_parse/test_ip_desc.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import tempfile
from collections import defaultdict
from collections.abc import Mapping
from pathlib import Path

import pytest
import yaml
Expand Down Expand Up @@ -86,15 +87,15 @@ def ip_core_description(self, axi_verilog_module: VerilogModule) -> IPCoreDescri

@pytest.fixture
def expected_output(self) -> IPCoreDescription:
return IPCoreDescription.load("tests/data/data_parse/axi_axil_adapter.yaml", False)
return IPCoreDescription.load(Path("tests/data/data_parse/axi_axil_adapter.yaml"), False)

@pytest.fixture
def clog2_ip_core_description(self, clog2_test_module: VerilogModule) -> IPCoreDescription:
return clog2_test_module.to_ip_core_description(standard_iface_grouper())

@pytest.fixture
def clog2_expected_output(self) -> IPCoreDescription:
return IPCoreDescription.load("tests/data/data_parse/clog2_core.yaml", False)
return IPCoreDescription.load(Path("tests/data/data_parse/clog2_core.yaml"), False)

@pytest.fixture
def force_compliance(self):
Expand Down Expand Up @@ -150,14 +151,14 @@ def test_builtins_compliance(self, force_compliance):

def test_load_fallback(self):
with pytest.raises(FileNotFoundError):
IPCoreDescription.load("axi_interconnect.yaml", False)
IPCoreDescription.load(Path("axi_interconnect.yaml"), False)

IPCoreDescription.load("axi_interconnect.yaml")
IPCoreDescription.load(Path("axi_interconnect.yaml"))

def test_save(self, expected_output: IPCoreDescription):
with tempfile.NamedTemporaryFile(suffix=".yaml") as f:
expected_output.save(f.name)
loaded = IPCoreDescription.load(f.name, False)
expected_output.save(Path(f.name))
loaded = IPCoreDescription.load(Path(f.name), False)
assert DeepDiff(expected_output, loaded) == {}

def test_invalid_syntax(self, completely_invalid_core):
Expand Down
Loading

0 comments on commit fb66f8c

Please sign in to comment.