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

87 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# Standard libraries 

10from unittest import TestCase 

11from unittest.mock import MagicMock, patch 

12 

13# Third party libraries 

14import pytest 

15 

16# First party libraries 

17from tsfpga.ip_core_file import IpCoreFile 

18from tsfpga.module import BaseModule, get_modules 

19from tsfpga.system_utils import create_file, delete 

20 

21# pylint: disable=unused-import 

22from tsfpga.test.conftest import fixture_tmp_path # noqa: F401 

23from tsfpga.vivado.ip_cores import VivadoIpCores 

24 

25 

26def test_get_ip_core_files_is_called_with_the_correct_arguments(tmp_path): 

27 modules = [MagicMock(spec=BaseModule)] 

28 

29 with patch("tsfpga.vivado.ip_cores.VivadoIpCoreProject.create", autospec=True): 

30 VivadoIpCores(modules, tmp_path, part_name="test_part") 

31 

32 modules[0].get_ip_core_files.assert_called_once_with(generics={}, part="test_part") 

33 

34 

35@pytest.mark.usefixtures("fixture_tmp_path") 

36class TestVivadoIpCores(TestCase): 

37 

38 tmp_path = None 

39 

40 def setUp(self): 

41 self.project_folder = self.tmp_path / "ip_project" 

42 self.modules_folder = self.tmp_path / "modules" 

43 

44 self.apa_tcl = create_file(self.modules_folder / "apa" / "ip_cores" / "apa.tcl", "apa") 

45 self.hest_tcl = create_file(self.modules_folder / "hest" / "ip_cores" / "hest.tcl", "hest") 

46 

47 modules = get_modules([self.modules_folder]) 

48 self.vivado_ip_cores = VivadoIpCores(modules, self.project_folder, part_name="-") 

49 

50 # Create initial hash and (empty) compile order file 

51 self.vivado_ip_cores._save_hash() # pylint: disable=protected-access 

52 self._create_compile_order_file() 

53 

54 def _create_compile_order_file(self): 

55 create_file(self.vivado_ip_cores.compile_order_file) 

56 

57 @patch("tsfpga.vivado.ip_cores.VivadoIpCoreProject.create", autospec=True) 

58 def test_should_not_recreate(self, create): 

59 assert not self.vivado_ip_cores.create_vivado_project_if_needed() 

60 create.assert_not_called() 

61 

62 @patch("tsfpga.vivado.ip_cores.VivadoIpCoreProject.create", autospec=True) 

63 def test_should_recreate_if_compile_order_file_is_missing(self, create): 

64 delete(self.vivado_ip_cores.compile_order_file) 

65 assert self.vivado_ip_cores.create_vivado_project_if_needed() 

66 create.assert_called_once() 

67 

68 @patch("tsfpga.vivado.ip_cores.VivadoIpCoreProject.create", autospec=True) 

69 def test_should_recreate_if_hash_file_is_missing(self, create): 

70 delete(self.vivado_ip_cores._hash_file) # pylint: disable=protected-access 

71 assert self.vivado_ip_cores.create_vivado_project_if_needed() 

72 create.assert_called_once() 

73 

74 @patch("tsfpga.vivado.ip_cores.VivadoIpCoreProject.create", autospec=True) 

75 def test_should_not_recreate_if_nothing_is_changed(self, create): 

76 # This test shows that the pattern used in the upcoming tests: 

77 # change something -> get modules -> create new VivadoIpCores object 

78 # should not result in a recreate unless we actually change something. 

79 modules = get_modules([self.modules_folder]) 

80 vivado_ip_cores = VivadoIpCores(modules, self.project_folder, part_name="-") 

81 

82 assert not vivado_ip_cores.create_vivado_project_if_needed() 

83 create.assert_not_called() 

84 

85 @patch("tsfpga.vivado.ip_cores.VivadoIpCoreProject.create", autospec=True) 

86 def test_should_recreate_if_ip_core_file_is_added(self, create): 

87 create_file(self.modules_folder / "zebra" / "ip_cores" / "zebra.tcl", "zebra") 

88 modules = get_modules([self.modules_folder]) 

89 vivado_ip_cores = VivadoIpCores(modules, self.project_folder, part_name="-") 

90 

91 assert vivado_ip_cores.create_vivado_project_if_needed() 

92 create.assert_called_once() 

93 

94 @patch("tsfpga.vivado.ip_cores.VivadoIpCoreProject.create", autospec=True) 

95 def test_should_recreate_if_ip_core_file_is_removed(self, create): 

96 delete(self.hest_tcl) 

97 modules = get_modules([self.modules_folder]) 

98 vivado_ip_cores = VivadoIpCores(modules, self.project_folder, part_name="-") 

99 

100 assert vivado_ip_cores.create_vivado_project_if_needed() 

101 create.assert_called_once() 

102 

103 @patch("tsfpga.vivado.ip_cores.VivadoIpCoreProject.create", autospec=True) 

104 def test_should_recreate_if_ip_core_file_is_changed(self, create): 

105 create_file(self.apa_tcl, "blaha blaha") 

106 modules = get_modules([self.modules_folder]) 

107 vivado_ip_cores = VivadoIpCores(modules, self.project_folder, part_name="-") 

108 

109 assert vivado_ip_cores.create_vivado_project_if_needed() 

110 create.assert_called_once() 

111 

112 @patch("tsfpga.vivado.ip_cores.VivadoIpCoreProject.create", autospec=True) 

113 def test_ip_core_variables(self, create): 

114 modules = [MagicMock(spec=BaseModule)] 

115 

116 modules[0].get_ip_core_files.return_value = [IpCoreFile(path=self.apa_tcl)] 

117 

118 vivado_ip_cores = VivadoIpCores(modules, self.project_folder, part_name="-") 

119 

120 assert vivado_ip_cores.create_vivado_project_if_needed() 

121 assert create.call_count == 1 

122 self._create_compile_order_file() 

123 

124 # Should not recreate until we change something 

125 assert not vivado_ip_cores.create_vivado_project_if_needed() 

126 assert create.call_count == 1 

127 

128 # Adding variables should recreate 

129 modules[0].get_ip_core_files.return_value = [ 

130 IpCoreFile(path=self.apa_tcl, zz="123", aa="456") 

131 ] 

132 

133 vivado_ip_cores = VivadoIpCores(modules, self.project_folder, part_name="-") 

134 

135 assert vivado_ip_cores.create_vivado_project_if_needed() 

136 assert create.call_count == 2 

137 self._create_compile_order_file() 

138 

139 # Changing order of variables should not recreate 

140 modules[0].get_ip_core_files.return_value = [ 

141 IpCoreFile(path=self.apa_tcl, aa="456", zz="123") 

142 ] 

143 

144 vivado_ip_cores = VivadoIpCores(modules, self.project_folder, part_name="-") 

145 

146 assert not vivado_ip_cores.create_vivado_project_if_needed() 

147 assert create.call_count == 2