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

57 statements  

« prev     ^ index     » next       coverage.py v7.2.1, created at 2023-09-27 20:00 +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://gitlab.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( 

36 "tsfpga.vivado.simlib_ghdl.subprocess.check_output", autospec=True 

37 ) as check_output: 

38 check_output.return_value = ghdl_version_string.encode("UTF-8") 

39 

40 simulator_class = MagicMock() 

41 simulator_class.name = "ghdl" 

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

43 

44 vunit_proj = MagicMock() 

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

46 

47 vivado_simlib = VivadoSimlib.init( 

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

49 ) 

50 

51 return vivado_simlib 

52 

53 def assert_should_compile(self): 

54 assert self.vivado_simlib.compile_is_needed 

55 with patch( 

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

57 ) as mock: 

58 self.vivado_simlib.compile_if_needed() 

59 mock.assert_called_once() 

60 

61 def assert_should_not_compile(self): 

62 assert not self.vivado_simlib.compile_is_needed 

63 with patch( 

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

65 ) as mock: 

66 self.vivado_simlib.compile_if_needed() 

67 mock.assert_not_called() 

68 

69 return SimlibGhdlTestFixture() 

70 

71 

72def test_should_not_recompile(simlib_test): 

73 simlib_test.assert_should_compile() 

74 simlib_test.assert_should_not_compile() 

75 

76 

77def test_ghdl_version_string(simlib_test): 

78 assert ( 

79 ".ghdl_0_36_v0_36." 

80 in simlib_test.get_vivado_simlib( 

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

82 ).artifact_name 

83 ) 

84 assert ( 

85 ".ghdl_0_36." 

86 in simlib_test.get_vivado_simlib( 

87 ghdl_version_string="GHDL 0.36 [Dunoon edition]" 

88 ).artifact_name 

89 ) 

90 assert ( 

91 ".ghdl_0_36_v0_36." 

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

93 ) 

94 assert ( 

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

96 ) 

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) [Dunoon edition]" 

102 ).artifact_name 

103 ) 

104 assert ( 

105 ".ghdl_0_37_dev_v0_36_1605_ge4aa89cd." 

106 in simlib_test.get_vivado_simlib( 

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

108 ).artifact_name 

109 ) 

110 

111 

112def test_should_compile_file_by_file_on_windows_but_not_on_linux(simlib_test): 

113 library_name = "unisim" 

114 

115 # pylint: disable=protected-access 

116 unisim_path = simlib_test.vivado_simlib._libraries_path / library_name 

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

118 

119 def run_test(is_windows, expected_calls): 

120 with patch.object( 

121 simlib_test.vivado_simlib, "_execute_ghdl", autospec=True 

122 ) as execute_ghdl, patch( 

123 "tsfpga.vivado.simlib_ghdl.system_is_windows", autospec=True 

124 ) as system_is_windows: 

125 system_is_windows.return_value = is_windows 

126 

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

128 

129 assert execute_ghdl.call_args_list == expected_calls 

130 

131 def get_expected_call(files): 

132 return call( 

133 workdir=simlib_test.vivado_simlib.output_path / library_name, 

134 library_name=library_name, 

135 files=files, 

136 ) 

137 

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

139 expected_calls = [ 

140 get_expected_call( 

141 files=[ 

142 str(vhd_files[0]), 

143 str(vhd_files[1]), 

144 ] 

145 ) 

146 ] 

147 run_test(is_windows=False, expected_calls=expected_calls) 

148 

149 # Many calls with individual file on Windows. 

150 expected_calls = [ 

151 get_expected_call(files=[str(vhd_files[0])]), 

152 get_expected_call(files=[str(vhd_files[1])]), 

153 ] 

154 run_test(is_windows=True, expected_calls=expected_calls)