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

57 statements  

« prev     ^ index     » next       coverage.py v7.5.1, created at 2024-05-06 20:51 +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# Third party libraries 

13import pytest 

14 

15# First party libraries 

16from tsfpga.git_simulation_subset import GitSimulationSubset 

17from tsfpga.system_utils import create_file 

18 

19 

20def test_supplying_only_one_of_vunit_preprocessed_path_or_modules_should_raise_exception(): 

21 GitSimulationSubset(repo_root=None, reference_branch=None, vunit_proj=None) 

22 GitSimulationSubset( 

23 repo_root=None, 

24 reference_branch=None, 

25 vunit_proj=None, 

26 vunit_preprocessed_path="dummy", 

27 modules="dummy", 

28 ) 

29 

30 assertion_message = "Can not supply only one of vunit_preprocessed_path and modules" 

31 

32 with pytest.raises(ValueError) as exception_info: 

33 GitSimulationSubset( 

34 repo_root=None, 

35 reference_branch=None, 

36 vunit_proj=None, 

37 vunit_preprocessed_path="dummy", 

38 ) 

39 assert str(exception_info.value) == assertion_message 

40 

41 with pytest.raises(ValueError) as exception_info: 

42 GitSimulationSubset( 

43 repo_root=None, 

44 reference_branch=None, 

45 vunit_proj=None, 

46 modules="dummy", 

47 ) 

48 assert str(exception_info.value) == assertion_message 

49 

50 

51def test_find_subset(tmp_path): # pylint: disable=too-many-locals 

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

53 # The tb without diffs will be set up so that it depends on the source file that 

54 # has diffs. This means that both tb's should be returned by find_subset(). 

55 # 

56 # It is quite a complicated test. I am not sure that it adds a tremendous amount of value. 

57 # It is really just a practice in using mocks. :) 

58 vhd_with_diff = create_file(tmp_path / "file_with_diff.vhd") 

59 vhd_with_no_diff = create_file(tmp_path / "file_with_no_diff.vhd") 

60 tb_vhd_with_diff = create_file(tmp_path / "tb_file_with_diff.vhd") 

61 tb_vhd_with_no_diff = create_file(tmp_path / "tb_file_with_no_diff.vhd") 

62 

63 vunit_proj = MagicMock() 

64 

65 git_simulation_subset = GitSimulationSubset( 

66 repo_root=tmp_path, reference_branch="origin/main", vunit_proj=vunit_proj 

67 ) 

68 

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

70 repo = mocked_repo.return_value 

71 head_commit = repo.head.commit 

72 

73 reference_commit = repo.commit.return_value 

74 

75 def diff_commit(arg): 

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

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

78 diff = MagicMock() 

79 if arg is None: 

80 diff.b_path = tb_vhd_with_diff 

81 elif arg is reference_commit: 

82 diff.b_path = vhd_with_diff 

83 else: 

84 assert False 

85 return [diff] 

86 

87 head_commit.diff.side_effect = diff_commit 

88 

89 source_file_vhd_with_diff = MagicMock() 

90 source_file_vhd_with_diff.name = str(vhd_with_diff) 

91 source_file_vhd_with_diff.library.name = "apa" 

92 

93 source_file_vhd_with_no_diff = MagicMock() 

94 source_file_vhd_with_no_diff.name = str(vhd_with_no_diff) 

95 source_file_vhd_with_no_diff.library.name = "hest" 

96 

97 source_file_tb_vhd_with_diff = MagicMock() 

98 source_file_tb_vhd_with_diff.name = str(tb_vhd_with_diff) 

99 source_file_tb_vhd_with_diff.library.name = "zebra" 

100 

101 source_file_tb_vhd_with_no_diff = MagicMock() 

102 source_file_tb_vhd_with_no_diff.name = str(tb_vhd_with_no_diff) 

103 source_file_tb_vhd_with_no_diff.library.name = "foo" 

104 

105 vunit_proj.get_source_files.return_value = [ 

106 source_file_tb_vhd_with_diff, 

107 source_file_vhd_with_diff, 

108 source_file_vhd_with_no_diff, 

109 source_file_tb_vhd_with_no_diff, 

110 ] 

111 

112 def get_implementation_subset(arg): 

113 if arg == [source_file_tb_vhd_with_diff]: 

114 # This tb, which has a diff, depends on itself and a vhd file with no diff 

115 return [source_file_tb_vhd_with_diff, source_file_vhd_with_no_diff] 

116 if arg == [source_file_tb_vhd_with_no_diff]: 

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

118 return [source_file_tb_vhd_with_no_diff, source_file_vhd_with_diff] 

119 assert False 

120 return None 

121 

122 vunit_proj.get_implementation_subset.side_effect = get_implementation_subset 

123 

124 assert git_simulation_subset.find_subset() == [ 

125 ("tb_file_with_diff", "zebra"), 

126 ("tb_file_with_no_diff", "foo"), 

127 ] 

128 

129 repo.commit.assert_called_once_with("origin/main")