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

55 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-02-21 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(simlib_test.vivado_simlib, "_execute_ghdl", autospec=True) as execute_ghdl, 

115 patch( 

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

117 ) as system_is_windows, 

118 ): 

119 system_is_windows.return_value = is_windows 

120 

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

122 

123 assert execute_ghdl.call_args_list == expected_calls 

124 

125 def get_expected_call(files): 

126 return call( 

127 workdir=simlib_test.vivado_simlib.output_path / library_name, 

128 library_name=library_name, 

129 files=files, 

130 ) 

131 

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

133 expected_calls = [ 

134 get_expected_call( 

135 files=[ 

136 str(vhd_files[0]), 

137 str(vhd_files[1]), 

138 ] 

139 ) 

140 ] 

141 run_test(is_windows=False, expected_calls=expected_calls) 

142 

143 # Many calls with individual file on Windows. 

144 expected_calls = [ 

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

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

147 ] 

148 run_test(is_windows=True, expected_calls=expected_calls)