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

44 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-11-29 20:01 +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, 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.subprocess.check_output") as check_output: 

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

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 @staticmethod 

52 def assert_should_compile(vivado_simlib): 

53 assert vivado_simlib.compile_is_needed 

54 with patch( 

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

56 ) as mock: 

57 vivado_simlib.compile_if_needed() 

58 mock.assert_called_once() 

59 

60 @staticmethod 

61 def assert_should_not_compile(vivado_simlib): 

62 assert not vivado_simlib.compile_is_needed 

63 with patch( 

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

65 ) as mock: 

66 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(simlib_test.vivado_simlib) 

74 simlib_test.assert_should_not_compile(simlib_test.vivado_simlib) 

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 )