Coverage for tsfpga/test/test_git_simulation_subset.py: 97%

69 statements  

« prev     ^ index     » next       coverage.py v7.6.9, created at 2024-12-20 20:52 +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# Standard libraries 

10from unittest.mock import MagicMock, patch 

11 

12# First party libraries 

13from tsfpga.git_simulation_subset import GitSimulationSubset 

14from tsfpga.module import get_modules 

15from tsfpga.system_utils import create_file 

16 

17 

18def test_find_subset(tmp_path): # pylint: disable=too-many-statements,too-many-locals 

19 """ 

20 Set up a scenario with a few files that have diffs and a few files that do not. 

21 TBs without diffs will be set up so that it depends on the source file that 

22 has diffs. 

23 This means that these TBs should also be returned by find_subset(). 

24 """ 

25 module_paths = tmp_path / "modules" 

26 

27 vhd_with_diff = create_file(module_paths / "foo" / "file_with_diff.vhdl") 

28 vhd_with_no_diff = create_file(module_paths / "foo" / "file_with_no_diff.vhd") 

29 tb_vhd_with_diff = create_file(module_paths / "foo" / "tb_file_with_diff.vhd") 

30 tb_vhd1_with_no_diff = create_file(module_paths / "foo" / "file1_with_no_diff_tb.vhdl") 

31 tb_vhd2_with_no_diff = create_file(module_paths / "foo" / "file2_with_no_diff_tb.vhdl") 

32 tb_vhd3_with_no_diff = create_file(module_paths / "foo" / "file3_with_no_diff_tb.vhdl") 

33 regs_toml_with_diff = create_file(module_paths / "bar" / "regs_bar.toml") 

34 regs_pkg_vhd_untracked = create_file(module_paths / "bar" / "regs_src" / "bar_regs_pkg.vhd") 

35 

36 modules = get_modules(modules_folder=module_paths) 

37 

38 vunit_proj = MagicMock() 

39 

40 git_simulation_subset = GitSimulationSubset( 

41 repo_root=tmp_path, reference_branch="origin/master", vunit_proj=vunit_proj, modules=modules 

42 ) 

43 

44 with patch("tsfpga.git_simulation_subset.Repo", autospec=True) as mocked_repo: 

45 repo = mocked_repo.return_value 

46 head_commit = repo.head.commit 

47 

48 reference_commit = repo.commit.return_value 

49 

50 def diff_commit(arg): 

51 """ 

52 Return the files that have diffs. One or the other depending on what the argument 

53 (reference commit, or None=local tree) is. 

54 """ 

55 if arg is None: 

56 diff = MagicMock() 

57 diff.b_path = tb_vhd_with_diff 

58 return [diff] 

59 

60 if arg is reference_commit: 

61 diff1 = MagicMock() 

62 diff1.b_path = vhd_with_diff 

63 

64 diff2 = MagicMock() 

65 diff2.b_path = regs_toml_with_diff 

66 

67 return [diff1, diff2] 

68 

69 assert False 

70 

71 head_commit.diff.side_effect = diff_commit 

72 

73 source_file_vhd_with_diff = MagicMock() 

74 source_file_vhd_with_diff.name = str(vhd_with_diff) 

75 source_file_vhd_with_diff.library.name = "apa" 

76 

77 source_file_vhd_with_no_diff = MagicMock() 

78 source_file_vhd_with_no_diff.name = str(vhd_with_no_diff) 

79 source_file_vhd_with_no_diff.library.name = "hest" 

80 

81 source_file_tb_vhd_with_diff = MagicMock() 

82 source_file_tb_vhd_with_diff.name = str(tb_vhd_with_diff) 

83 source_file_tb_vhd_with_diff.library.name = "zebra" 

84 

85 source_file_tb_vhd1_with_no_diff = MagicMock() 

86 source_file_tb_vhd1_with_no_diff.name = str(tb_vhd1_with_no_diff) 

87 source_file_tb_vhd1_with_no_diff.library.name = "foo" 

88 

89 source_file_tb_vhd2_with_no_diff = MagicMock() 

90 source_file_tb_vhd2_with_no_diff.name = str(tb_vhd2_with_no_diff) 

91 source_file_tb_vhd2_with_no_diff.library.name = "bar" 

92 

93 source_file_tb_vhd3_with_no_diff = MagicMock() 

94 source_file_tb_vhd3_with_no_diff.name = str(tb_vhd3_with_no_diff) 

95 source_file_tb_vhd3_with_no_diff.library.name = "bar" 

96 

97 source_file_regs_pkg_vhd_untracked = MagicMock() 

98 source_file_regs_pkg_vhd_untracked.name = str(regs_pkg_vhd_untracked) 

99 source_file_regs_pkg_vhd_untracked.library.name = "bar" 

100 

101 vunit_proj.get_source_files.return_value = [ 

102 source_file_vhd_with_diff, 

103 source_file_vhd_with_no_diff, 

104 source_file_tb_vhd_with_diff, 

105 source_file_tb_vhd1_with_no_diff, 

106 source_file_tb_vhd2_with_no_diff, 

107 source_file_tb_vhd3_with_no_diff, 

108 source_file_regs_pkg_vhd_untracked, 

109 ] 

110 

111 def get_implementation_subset(arg): 

112 if arg == [source_file_tb_vhd_with_diff]: 

113 # This tb, which has a diff, depends on a vhd file with no diff 

114 return [source_file_tb_vhd_with_diff, source_file_vhd_with_no_diff] 

115 

116 if arg == [source_file_tb_vhd1_with_no_diff]: 

117 # This tb, which has no diff, depends on a vhd file that does have a diff 

118 return [source_file_tb_vhd1_with_no_diff, source_file_vhd_with_diff] 

119 

120 if arg == [source_file_tb_vhd2_with_no_diff]: 

121 # This tb, which has no diff, depends on the register package generated 

122 # from toml which has diff. 

123 return [source_file_tb_vhd2_with_no_diff, source_file_regs_pkg_vhd_untracked] 

124 

125 if arg == [source_file_tb_vhd3_with_no_diff]: 

126 # This tb, which has no diff, depends only on itself, and should not 

127 # be listed by find_subset(). 

128 return [source_file_tb_vhd3_with_no_diff] 

129 

130 assert False 

131 

132 vunit_proj.get_implementation_subset.side_effect = get_implementation_subset 

133 

134 assert git_simulation_subset.find_subset() == [ 

135 (tb_vhd_with_diff.stem, "zebra"), 

136 (tb_vhd1_with_no_diff.stem, "foo"), 

137 (tb_vhd2_with_no_diff.stem, "bar"), 

138 ] 

139 

140 repo.commit.assert_called_once_with("origin/master")