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

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 

14# Standard libraries 

15from pathlib import Path 

16from unittest.mock import MagicMock, call, patch 

17 

18# Third party libraries 

19import pytest 

20 

21# First party libraries 

22from tsfpga.vivado.simlib import VivadoSimlib 

23 

24 

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" 

31 

32 self.vivado_simlib = self.get_vivado_simlib() 

33 

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 

37 

38 simulator_class = MagicMock() 

39 simulator_class.name = "ghdl" 

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

41 

42 vunit_proj = MagicMock() 

43 vunit_proj._simulator_class = simulator_class # pylint: disable=protected-access 

44 

45 vivado_simlib = VivadoSimlib.init( 

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

47 ) 

48 

49 return vivado_simlib 

50 

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() 

58 

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() 

66 

67 return SimlibGhdlTestFixture() 

68 

69 

70def test_should_not_recompile(simlib_test): 

71 simlib_test.assert_should_compile() 

72 simlib_test.assert_should_not_compile() 

73 

74 

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 ) 

95 

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 ) 

108 

109 

110def test_should_compile_file_by_file_on_windows_but_not_on_linux(simlib_test): 

111 library_name = "unisim" 

112 

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"] 

116 

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 

124 

125 simlib_test.vivado_simlib._compile_ghdl(vhd_files=vhd_files, library_name=library_name) 

126 

127 assert execute_ghdl.call_args_list == expected_calls 

128 

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 ) 

135 

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) 

146 

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)