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.mock import MagicMock, patch 

10import pytest 

11 

12from tsfpga.git_simulation_subset import GitSimulationSubset 

13from tsfpga.system_utils import create_file 

14 

15 

16def test_supplying_only_one_of_vunit_preprocessed_path_or_modules_should_raise_exception(): 

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

18 GitSimulationSubset( 

19 repo_root=None, 

20 reference_branch=None, 

21 vunit_proj=None, 

22 vunit_preprocessed_path="dummy", 

23 modules="dummy", 

24 ) 

25 

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

27 

28 with pytest.raises(ValueError) as exception_info: 

29 GitSimulationSubset( 

30 repo_root=None, 

31 reference_branch=None, 

32 vunit_proj=None, 

33 vunit_preprocessed_path="dummy", 

34 ) 

35 assert str(exception_info.value) == assertion_message 

36 

37 with pytest.raises(ValueError) as exception_info: 

38 GitSimulationSubset( 

39 repo_root=None, 

40 reference_branch=None, 

41 vunit_proj=None, 

42 modules="dummy", 

43 ) 

44 assert str(exception_info.value) == assertion_message 

45 

46 

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

48 

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

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

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

52 # 

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

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

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

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

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

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

59 

60 vunit_proj = MagicMock() 

61 

62 git_simulation_subset = GitSimulationSubset( 

63 repo_root=tmp_path, reference_branch="origin/master", vunit_proj=vunit_proj 

64 ) 

65 

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

67 repo = mocked_repo.return_value 

68 head_commit = repo.head.commit 

69 

70 reference_commit = repo.commit.return_value 

71 

72 def diff_commit(arg): 

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

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

75 diff = MagicMock() 

76 if arg is None: 

77 diff.b_path = tb_vhd_with_diff 

78 elif arg is reference_commit: 

79 diff.b_path = vhd_with_diff 

80 else: 

81 assert False 

82 return [diff] 

83 

84 head_commit.diff.side_effect = diff_commit 

85 

86 source_file_vhd_with_diff = MagicMock() 

87 source_file_vhd_with_diff.name = str(vhd_with_diff) 

88 source_file_vhd_with_diff.library.name = "apa" 

89 

90 source_file_vhd_with_no_diff = MagicMock() 

91 source_file_vhd_with_no_diff.name = str(vhd_with_no_diff) 

92 source_file_vhd_with_no_diff.library.name = "hest" 

93 

94 source_file_tb_vhd_with_diff = MagicMock() 

95 source_file_tb_vhd_with_diff.name = str(tb_vhd_with_diff) 

96 source_file_tb_vhd_with_diff.library.name = "zebra" 

97 

98 source_file_tb_vhd_with_no_diff = MagicMock() 

99 source_file_tb_vhd_with_no_diff.name = str(tb_vhd_with_no_diff) 

100 source_file_tb_vhd_with_no_diff.library.name = "foo" 

101 

102 vunit_proj.get_source_files.return_value = [ 

103 source_file_tb_vhd_with_diff, 

104 source_file_vhd_with_diff, 

105 source_file_vhd_with_no_diff, 

106 source_file_tb_vhd_with_no_diff, 

107 ] 

108 

109 def get_implementation_subset(arg): 

110 if arg == [source_file_tb_vhd_with_diff]: 

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

112 return [source_file_tb_vhd_with_diff, source_file_vhd_with_no_diff] 

113 if arg == [source_file_tb_vhd_with_no_diff]: 

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

115 return [source_file_tb_vhd_with_no_diff, source_file_vhd_with_diff] 

116 assert False 

117 return None 

118 

119 vunit_proj.get_implementation_subset.side_effect = get_implementation_subset 

120 

121 assert git_simulation_subset.find_subset() == [ 

122 ("tb_file_with_diff", "zebra"), 

123 ("tb_file_with_no_diff", "foo"), 

124 ] 

125 

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