Coverage for tsfpga/test/test_git_simulation_subset.py: 97%
69 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-02-21 20:51 +0000
« prev ^ index » next coverage.py v7.6.12, created at 2025-02-21 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# --------------------------------------------------------------------------------------------------
9from unittest.mock import MagicMock, patch
11from tsfpga.git_simulation_subset import GitSimulationSubset
12from tsfpga.module import get_modules
13from tsfpga.system_utils import create_file
16def test_find_subset(tmp_path): # noqa: PLR0915
17 """
18 Set up a scenario with a few files that have diffs and a few files that do not.
19 TBs without diffs will be set up so that it depends on the source file that
20 has diffs.
21 This means that these TBs should also be returned by find_subset().
22 """
23 module_paths = tmp_path / "modules"
25 vhd_with_diff = create_file(module_paths / "foo" / "file_with_diff.vhdl")
26 vhd_with_no_diff = create_file(module_paths / "foo" / "file_with_no_diff.vhd")
27 tb_vhd_with_diff = create_file(module_paths / "foo" / "tb_file_with_diff.vhd")
28 tb_vhd1_with_no_diff = create_file(module_paths / "foo" / "file1_with_no_diff_tb.vhdl")
29 tb_vhd2_with_no_diff = create_file(module_paths / "foo" / "file2_with_no_diff_tb.vhdl")
30 tb_vhd3_with_no_diff = create_file(module_paths / "foo" / "file3_with_no_diff_tb.vhdl")
31 regs_toml_with_diff = create_file(module_paths / "bar" / "regs_bar.toml")
32 regs_pkg_vhd_untracked = create_file(module_paths / "bar" / "regs_src" / "bar_regs_pkg.vhd")
34 modules = get_modules(modules_folder=module_paths)
36 vunit_proj = MagicMock()
38 git_simulation_subset = GitSimulationSubset(
39 repo_root=tmp_path, reference_branch="origin/master", vunit_proj=vunit_proj, modules=modules
40 )
42 with patch("tsfpga.git_simulation_subset.Repo", autospec=True) as mocked_repo:
43 repo = mocked_repo.return_value
44 head_commit = repo.head.commit
46 reference_commit = repo.commit.return_value
48 def diff_commit(arg):
49 """
50 Return the files that have diffs. One or the other depending on what the argument
51 (reference commit, or None=local tree) is.
52 """
53 if arg is None:
54 diff = MagicMock()
55 diff.b_path = tb_vhd_with_diff
56 return [diff]
58 if arg is reference_commit:
59 diff1 = MagicMock()
60 diff1.b_path = vhd_with_diff
62 diff2 = MagicMock()
63 diff2.b_path = regs_toml_with_diff
65 return [diff1, diff2]
67 raise AssertionError
69 head_commit.diff.side_effect = diff_commit
71 source_file_vhd_with_diff = MagicMock()
72 source_file_vhd_with_diff.name = str(vhd_with_diff)
73 source_file_vhd_with_diff.library.name = "apa"
75 source_file_vhd_with_no_diff = MagicMock()
76 source_file_vhd_with_no_diff.name = str(vhd_with_no_diff)
77 source_file_vhd_with_no_diff.library.name = "hest"
79 source_file_tb_vhd_with_diff = MagicMock()
80 source_file_tb_vhd_with_diff.name = str(tb_vhd_with_diff)
81 source_file_tb_vhd_with_diff.library.name = "zebra"
83 source_file_tb_vhd1_with_no_diff = MagicMock()
84 source_file_tb_vhd1_with_no_diff.name = str(tb_vhd1_with_no_diff)
85 source_file_tb_vhd1_with_no_diff.library.name = "foo"
87 source_file_tb_vhd2_with_no_diff = MagicMock()
88 source_file_tb_vhd2_with_no_diff.name = str(tb_vhd2_with_no_diff)
89 source_file_tb_vhd2_with_no_diff.library.name = "bar"
91 source_file_tb_vhd3_with_no_diff = MagicMock()
92 source_file_tb_vhd3_with_no_diff.name = str(tb_vhd3_with_no_diff)
93 source_file_tb_vhd3_with_no_diff.library.name = "bar"
95 source_file_regs_pkg_vhd_untracked = MagicMock()
96 source_file_regs_pkg_vhd_untracked.name = str(regs_pkg_vhd_untracked)
97 source_file_regs_pkg_vhd_untracked.library.name = "bar"
99 vunit_proj.get_source_files.return_value = [
100 source_file_vhd_with_diff,
101 source_file_vhd_with_no_diff,
102 source_file_tb_vhd_with_diff,
103 source_file_tb_vhd1_with_no_diff,
104 source_file_tb_vhd2_with_no_diff,
105 source_file_tb_vhd3_with_no_diff,
106 source_file_regs_pkg_vhd_untracked,
107 ]
109 def get_implementation_subset(arg):
110 if arg == [source_file_tb_vhd_with_diff]:
111 # This tb, which has a diff, depends on a vhd file with no diff
112 return [source_file_tb_vhd_with_diff, source_file_vhd_with_no_diff]
114 if arg == [source_file_tb_vhd1_with_no_diff]:
115 # This tb, which has no diff, depends on a vhd file that does have a diff
116 return [source_file_tb_vhd1_with_no_diff, source_file_vhd_with_diff]
118 if arg == [source_file_tb_vhd2_with_no_diff]:
119 # This tb, which has no diff, depends on the register package generated
120 # from toml which has diff.
121 return [source_file_tb_vhd2_with_no_diff, source_file_regs_pkg_vhd_untracked]
123 if arg == [source_file_tb_vhd3_with_no_diff]:
124 # This tb, which has no diff, depends only on itself, and should not
125 # be listed by find_subset().
126 return [source_file_tb_vhd3_with_no_diff]
128 raise AssertionError
130 vunit_proj.get_implementation_subset.side_effect = get_implementation_subset
132 assert git_simulation_subset.find_subset() == [
133 (tb_vhd_with_diff.stem, "zebra"),
134 (tb_vhd1_with_no_diff.stem, "foo"),
135 (tb_vhd2_with_no_diff.stem, "bar"),
136 ]
138 repo.commit.assert_called_once_with("origin/master")