Coverage for tsfpga/vivado/test/test_simlib_ghdl.py: 100%

55 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-06-27 20:51 +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# -------------------------------------------------------------------------------------------------- 

8 

9""" 

10Test a subset of what is tested for commercial, since most of the code is inherited 

11from the common class. 

12""" 

13 

14from pathlib import Path 

15from unittest.mock import MagicMock, call, patch 

16 

17import pytest 

18 

19from tsfpga.vivado.simlib import VivadoSimlib 

20 

21# ruff: noqa: SLF001 

22 

23 

24@pytest.fixture 

25def simlib_test(tmp_path): 

26 class SimlibGhdlTestFixture: 

27 def __init__(self): 

28 self.output_path = tmp_path / "simlib" 

29 

30 self.vivado_simlib = self.get_vivado_simlib() 

31 

32 def get_vivado_simlib(self, ghdl_version_string="GHDL 0.36 ..."): 

33 with patch("tsfpga.vivado.simlib_ghdl.run_command", autospec=True) as run_command: 

34 run_command.return_value.stdout = ghdl_version_string 

35 

36 simulator_class = MagicMock() 

37 simulator_class.name = "ghdl" 

38 simulator_class.find_prefix.return_value = "/usr/bin" 

39 

40 vunit_proj = MagicMock() 

41 vunit_proj._simulator_class = simulator_class 

42 

43 return VivadoSimlib.init( 

44 self.output_path, vunit_proj, Path("/tools/xilinx/Vivado/2019.2/bin/vivado") 

45 ) 

46 

47 def assert_should_compile(self): 

48 assert self.vivado_simlib.compile_is_needed 

49 with patch( 

50 "tsfpga.vivado.simlib_ghdl.VivadoSimlibGhdl._compile", autospec=True 

51 ) as mock: 

52 self.vivado_simlib.compile_if_needed() 

53 mock.assert_called_once() 

54 

55 def assert_should_not_compile(self): 

56 assert not self.vivado_simlib.compile_is_needed 

57 with patch( 

58 "tsfpga.vivado.simlib_ghdl.VivadoSimlibGhdl._compile", autospec=True 

59 ) as mock: 

60 self.vivado_simlib.compile_if_needed() 

61 mock.assert_not_called() 

62 

63 return SimlibGhdlTestFixture() 

64 

65 

66def test_should_not_recompile(simlib_test): 

67 simlib_test.assert_should_compile() 

68 simlib_test.assert_should_not_compile() 

69 

70 

71def test_ghdl_version_string(simlib_test): 

72 assert ( 

73 ".ghdl_0_36_v0_36." 

74 in simlib_test.get_vivado_simlib( 

75 ghdl_version_string="GHDL 0.36 (v0.36) [Dunoon edition]" 

76 ).artifact_name 

77 ) 

78 assert ( 

79 ".ghdl_0_36." 

80 in simlib_test.get_vivado_simlib( 

81 ghdl_version_string="GHDL 0.36 [Dunoon edition]" 

82 ).artifact_name 

83 ) 

84 assert ( 

85 ".ghdl_0_36_v0_36." 

86 in simlib_test.get_vivado_simlib(ghdl_version_string="GHDL 0.36 (v0.36)").artifact_name 

87 ) 

88 assert ( 

89 ".ghdl_0_36" in simlib_test.get_vivado_simlib(ghdl_version_string="GHDL 0.36").artifact_name 

90 ) 

91 

92 assert ( 

93 ".ghdl_0_37_dev_v0_36_1605_ge4aa89cd" 

94 in simlib_test.get_vivado_simlib( 

95 ghdl_version_string="GHDL 0.37-dev (v0.36-1605-ge4aa89cd) [Dunoon edition]" 

96 ).artifact_name 

97 ) 

98 assert ( 

99 ".ghdl_0_37_dev_v0_36_1605_ge4aa89cd." 

100 in simlib_test.get_vivado_simlib( 

101 ghdl_version_string="GHDL 0.37-dev (v0.36-1605-ge4aa89cd)" 

102 ).artifact_name 

103 ) 

104 

105 

106def test_should_compile_file_by_file_on_windows_but_not_on_linux(simlib_test): 

107 library_name = "unisim" 

108 

109 unisim_path = simlib_test.vivado_simlib._libraries_path / library_name 

110 vhd_files = [unisim_path / "a.vhd", unisim_path / "b.vhd"] 

111 

112 def run_test(is_windows, expected_calls): 

113 with ( 

114 patch.object( 

115 simlib_test.vivado_simlib, "_execute_compile", autospec=True 

116 ) as execute_ghdl, 

117 patch( 

118 "tsfpga.vivado.simlib_open_source.system_is_windows", autospec=True 

119 ) as system_is_windows, 

120 ): 

121 system_is_windows.return_value = is_windows 

122 

123 simlib_test.vivado_simlib._compile_library( 

124 vhd_files=vhd_files, library_name=library_name 

125 ) 

126 

127 assert execute_ghdl.call_args_list == expected_calls 

128 

129 def get_expected_call(vhd_files): 

130 return call( 

131 output_path=simlib_test.vivado_simlib.output_path / library_name, 

132 library_name=library_name, 

133 vhd_files=vhd_files, 

134 ) 

135 

136 # One call with many files on e.g. Linux. 

137 expected_calls = [ 

138 get_expected_call( 

139 vhd_files=[ 

140 str(vhd_files[0]), 

141 str(vhd_files[1]), 

142 ] 

143 ) 

144 ] 

145 run_test(is_windows=False, expected_calls=expected_calls) 

146 

147 # Many calls with individual file on Windows. 

148 expected_calls = [ 

149 get_expected_call(vhd_files=[str(vhd_files[0])]), 

150 get_expected_call(vhd_files=[str(vhd_files[1])]), 

151 ] 

152 run_test(is_windows=True, expected_calls=expected_calls)