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 mock, TestCase 

10 

11import pytest 

12 

13from tsfpga.module import get_modules 

14from tsfpga.system_utils import create_file, delete 

15from tsfpga.vivado.ip_cores import VivadoIpCores 

16 

17# pylint: disable=unused-import 

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

19 

20 

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

22class TestVivadoIpCores(TestCase): 

23 

24 tmp_path = None 

25 

26 def setUp(self): 

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

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

29 

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

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

32 

33 modules = get_modules([self.modules_folder]) 

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

35 

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

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

38 create_file(self.vivado_ip_cores.compile_order_file) 

39 

40 @mock.patch("tsfpga.vivado.ip_cores.VivadoProject.create", autospec=True) 

41 def test_should_not_recreate(self, create): 

42 assert not self.vivado_ip_cores.create_vivado_project_if_needed() 

43 create.assert_not_called() 

44 

45 @mock.patch("tsfpga.vivado.ip_cores.VivadoProject.create", autospec=True) 

46 def test_should_recreate_if_compile_order_file_is_missing(self, create): 

47 delete(self.vivado_ip_cores.compile_order_file) 

48 assert self.vivado_ip_cores.create_vivado_project_if_needed() 

49 create.assert_called_once() 

50 

51 @mock.patch("tsfpga.vivado.ip_cores.VivadoProject.create", autospec=True) 

52 def test_should_recreate_if_hash_file_is_missing(self, create): 

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

54 assert self.vivado_ip_cores.create_vivado_project_if_needed() 

55 create.assert_called_once() 

56 

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

58 def test_should_not_recreate_if_nothing_is_changed(self, create): 

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

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

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

62 modules = get_modules([self.modules_folder]) 

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

64 

65 assert not vivado_ip_cores.create_vivado_project_if_needed() 

66 create.assert_not_called() 

67 

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

69 def test_should_recreate_if_ip_core_file_is_added(self, create): 

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

71 modules = get_modules([self.modules_folder]) 

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

73 

74 assert vivado_ip_cores.create_vivado_project_if_needed() 

75 create.assert_called_once() 

76 

77 @mock.patch("tsfpga.vivado.ip_cores.VivadoProject.create", autospec=True) 

78 def test_should_recreate_if_ip_core_file_is_removed(self, create): 

79 delete(self.hest_tcl) 

80 modules = get_modules([self.modules_folder]) 

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

82 

83 assert vivado_ip_cores.create_vivado_project_if_needed() 

84 create.assert_called_once() 

85 

86 @mock.patch("tsfpga.vivado.ip_cores.VivadoProject.create", autospec=True) 

87 def test_should_recreate_if_ip_core_file_is_changed(self, create): 

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

89 modules = get_modules([self.modules_folder]) 

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

91 

92 assert vivado_ip_cores.create_vivado_project_if_needed() 

93 create.assert_called_once()