Coverage for tsfpga/vivado/test/test_simlib_ghdl.py: 100%
56 statements
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-20 20:52 +0000
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-20 20:52 +0000
1# --------------------------------------------------------------------------------------------------
2# Copyright (c) Lukas Vik. All rights reserved.
3#
4# This file is part of the tsfpga project, a project platform for modern FPGA development.
5# https://tsfpga.com
6# https://github.com/tsfpga/tsfpga
7# --------------------------------------------------------------------------------------------------
9"""
10Test a subset of what is tested for commercial, since most of the code is inherited
11from the common class.
12"""
14# Standard libraries
15from pathlib import Path
16from unittest.mock import MagicMock, call, patch
18# Third party libraries
19import pytest
21# First party libraries
22from tsfpga.vivado.simlib import VivadoSimlib
25# pylint: disable=redefined-outer-name
26@pytest.fixture
27def simlib_test(tmp_path):
28 class SimlibGhdlTestFixture:
29 def __init__(self):
30 self.output_path = tmp_path / "simlib"
32 self.vivado_simlib = self.get_vivado_simlib()
34 def get_vivado_simlib(self, ghdl_version_string="GHDL 0.36 ..."):
35 with patch("tsfpga.vivado.simlib_ghdl.run_command", autospec=True) as run_command:
36 run_command.return_value.stdout = ghdl_version_string
38 simulator_class = MagicMock()
39 simulator_class.name = "ghdl"
40 simulator_class.find_prefix.return_value = "/usr/bin"
42 vunit_proj = MagicMock()
43 vunit_proj._simulator_class = simulator_class # pylint: disable=protected-access
45 vivado_simlib = VivadoSimlib.init(
46 self.output_path, vunit_proj, Path("/tools/xilinx/Vivado/2019.2/bin/vivado")
47 )
49 return vivado_simlib
51 def assert_should_compile(self):
52 assert self.vivado_simlib.compile_is_needed
53 with patch(
54 "tsfpga.vivado.simlib_ghdl.VivadoSimlibGhdl._compile", autospec=True
55 ) as mock:
56 self.vivado_simlib.compile_if_needed()
57 mock.assert_called_once()
59 def assert_should_not_compile(self):
60 assert not self.vivado_simlib.compile_is_needed
61 with patch(
62 "tsfpga.vivado.simlib_ghdl.VivadoSimlibGhdl._compile", autospec=True
63 ) as mock:
64 self.vivado_simlib.compile_if_needed()
65 mock.assert_not_called()
67 return SimlibGhdlTestFixture()
70def test_should_not_recompile(simlib_test):
71 simlib_test.assert_should_compile()
72 simlib_test.assert_should_not_compile()
75def test_ghdl_version_string(simlib_test):
76 assert (
77 ".ghdl_0_36_v0_36."
78 in simlib_test.get_vivado_simlib(
79 ghdl_version_string="GHDL 0.36 (v0.36) [Dunoon edition]"
80 ).artifact_name
81 )
82 assert (
83 ".ghdl_0_36."
84 in simlib_test.get_vivado_simlib(
85 ghdl_version_string="GHDL 0.36 [Dunoon edition]"
86 ).artifact_name
87 )
88 assert (
89 ".ghdl_0_36_v0_36."
90 in simlib_test.get_vivado_simlib(ghdl_version_string="GHDL 0.36 (v0.36)").artifact_name
91 )
92 assert (
93 ".ghdl_0_36" in simlib_test.get_vivado_simlib(ghdl_version_string="GHDL 0.36").artifact_name
94 )
96 assert (
97 ".ghdl_0_37_dev_v0_36_1605_ge4aa89cd"
98 in simlib_test.get_vivado_simlib(
99 ghdl_version_string="GHDL 0.37-dev (v0.36-1605-ge4aa89cd) [Dunoon edition]"
100 ).artifact_name
101 )
102 assert (
103 ".ghdl_0_37_dev_v0_36_1605_ge4aa89cd."
104 in simlib_test.get_vivado_simlib(
105 ghdl_version_string="GHDL 0.37-dev (v0.36-1605-ge4aa89cd)"
106 ).artifact_name
107 )
110def test_should_compile_file_by_file_on_windows_but_not_on_linux(simlib_test):
111 library_name = "unisim"
113 # pylint: disable=protected-access
114 unisim_path = simlib_test.vivado_simlib._libraries_path / library_name
115 vhd_files = [unisim_path / "a.vhd", unisim_path / "b.vhd"]
117 def run_test(is_windows, expected_calls):
118 with patch.object(
119 simlib_test.vivado_simlib, "_execute_ghdl", autospec=True
120 ) as execute_ghdl, patch(
121 "tsfpga.vivado.simlib_ghdl.system_is_windows", autospec=True
122 ) as system_is_windows:
123 system_is_windows.return_value = is_windows
125 simlib_test.vivado_simlib._compile_ghdl(vhd_files=vhd_files, library_name=library_name)
127 assert execute_ghdl.call_args_list == expected_calls
129 def get_expected_call(files):
130 return call(
131 workdir=simlib_test.vivado_simlib.output_path / library_name,
132 library_name=library_name,
133 files=files,
134 )
136 # One call with many files on e.g. Linux.
137 expected_calls = [
138 get_expected_call(
139 files=[
140 str(vhd_files[0]),
141 str(vhd_files[1]),
142 ]
143 )
144 ]
145 run_test(is_windows=False, expected_calls=expected_calls)
147 # Many calls with individual file on Windows.
148 expected_calls = [
149 get_expected_call(files=[str(vhd_files[0])]),
150 get_expected_call(files=[str(vhd_files[1])]),
151 ]
152 run_test(is_windows=True, expected_calls=expected_calls)