Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# -------------------------------------------------------------------------------------------------- 

2# Copyright (c) Lukas Vik. All rights reserved. 

3# 

4# This file is part of the tsfpga project. 

5# https://tsfpga.com 

6# https://gitlab.com/tsfpga/tsfpga 

7# -------------------------------------------------------------------------------------------------- 

8 

9from unittest import TestCase 

10from unittest.mock import MagicMock, patch 

11 

12import pytest 

13 

14from tsfpga.ip_core_file import IpCoreFile 

15from tsfpga.module import BaseModule, get_modules 

16from tsfpga.system_utils import create_file, delete 

17from tsfpga.vivado.ip_cores import VivadoIpCores 

18 

19# pylint: disable=unused-import 

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

21 

22 

23def test_get_ip_core_files_is_called_with_the_correct_arguments(tmp_path): 

24 modules = [MagicMock(spec=BaseModule)] 

25 

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

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

28 

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

30 

31 

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

33class TestVivadoIpCores(TestCase): 

34 

35 tmp_path = None 

36 

37 def setUp(self): 

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

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

40 

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

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

43 

44 modules = get_modules([self.modules_folder]) 

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

46 

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

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

49 self._create_compile_order_file() 

50 

51 def _create_compile_order_file(self): 

52 create_file(self.vivado_ip_cores.compile_order_file) 

53 

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

55 def test_should_not_recreate(self, create): 

56 assert not self.vivado_ip_cores.create_vivado_project_if_needed() 

57 create.assert_not_called() 

58 

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

60 def test_should_recreate_if_compile_order_file_is_missing(self, create): 

61 delete(self.vivado_ip_cores.compile_order_file) 

62 assert self.vivado_ip_cores.create_vivado_project_if_needed() 

63 create.assert_called_once() 

64 

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

66 def test_should_recreate_if_hash_file_is_missing(self, create): 

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

68 assert self.vivado_ip_cores.create_vivado_project_if_needed() 

69 create.assert_called_once() 

70 

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

72 def test_should_not_recreate_if_nothing_is_changed(self, create): 

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

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

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

76 modules = get_modules([self.modules_folder]) 

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

78 

79 assert not vivado_ip_cores.create_vivado_project_if_needed() 

80 create.assert_not_called() 

81 

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

83 def test_should_recreate_if_ip_core_file_is_added(self, create): 

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

85 modules = get_modules([self.modules_folder]) 

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

87 

88 assert vivado_ip_cores.create_vivado_project_if_needed() 

89 create.assert_called_once() 

90 

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

92 def test_should_recreate_if_ip_core_file_is_removed(self, create): 

93 delete(self.hest_tcl) 

94 modules = get_modules([self.modules_folder]) 

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

96 

97 assert vivado_ip_cores.create_vivado_project_if_needed() 

98 create.assert_called_once() 

99 

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

101 def test_should_recreate_if_ip_core_file_is_changed(self, create): 

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

103 modules = get_modules([self.modules_folder]) 

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

105 

106 assert vivado_ip_cores.create_vivado_project_if_needed() 

107 create.assert_called_once() 

108 

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

110 def test_ip_core_variables(self, create): 

111 modules = [MagicMock(spec=BaseModule)] 

112 

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

114 

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

116 

117 assert vivado_ip_cores.create_vivado_project_if_needed() 

118 assert create.call_count == 1 

119 self._create_compile_order_file() 

120 

121 # Should not recreate until we change something 

122 assert not vivado_ip_cores.create_vivado_project_if_needed() 

123 assert create.call_count == 1 

124 

125 # Adding variables should recreate 

126 modules[0].get_ip_core_files.return_value = [ 

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

128 ] 

129 

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

131 

132 assert vivado_ip_cores.create_vivado_project_if_needed() 

133 assert create.call_count == 2 

134 self._create_compile_order_file() 

135 

136 # Changing order of variables should not recreate 

137 modules[0].get_ip_core_files.return_value = [ 

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

139 ] 

140 

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

142 

143 assert not vivado_ip_cores.create_vivado_project_if_needed() 

144 assert create.call_count == 2