summaryrefslogtreecommitdiff
path: root/util/check_zephyr_project_config_unittest.py
blob: ad28d8e12800207d1d8cb92235a31ccefe04e6f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/usr/bin/env python3

# Copyright 2023 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Unit tests for check_zephyr_project_config.py"""

import os
import pathlib
import unittest

import check_zephyr_project_config
import mock  # pylint:disable=import-error

# pylint:disable=protected-access


class TestKconfigCheck(unittest.TestCase):
    """Tests for KconfigCheck."""

    @mock.patch("check_zephyr_project_config.KconfigCheck._init_kconfig")
    def setUp(self, init_kconfig_mock):  # pylint:disable=arguments-differ
        mock_kconfig = mock.Mock()
        init_kconfig_mock.return_value = mock_kconfig

        self.kcheck = check_zephyr_project_config.KconfigCheck(True)

        self.assertEqual(self.kcheck.fail_count, 0)
        self.assertEqual(self.kcheck.program_kconf, {None: mock_kconfig})

    @mock.patch("zephyr_module.parse_modules")
    @mock.patch("zephyr_module.process_kconfig")
    @mock.patch("kconfiglib.Kconfig")
    def test_init_kconfig(
        self, kconfig_mock, process_kconfig_mock, parse_modules_mock
    ):
        """Initialize a Kconfig with the upstream Kconfig file."""
        mock_module = mock.Mock()
        mock_module.project = "project"
        mock_module.meta = "meta"
        parse_modules_mock.return_value = [mock_module]
        process_kconfig_mock.return_value = "fake kconfig"

        os.environ["ZEPHYR_BASE"] = "/totally/not/zephyr/base"
        os.environ["srctree"] = "/also/not/zephyr/base"
        os.environ["ARCH_DIR"] = "not_an_arch_dir"
        os.environ["ARCH"] = "not_a_star"
        os.environ["BOARD_DIR"] = "something_else"

        self.kcheck._init_kconfig(None)

        parse_modules_mock.assert_called_once_with(
            check_zephyr_project_config.ZEPHYR_BASE,
            modules=[check_zephyr_project_config.EC_BASE],
        )
        process_kconfig_mock.assert_called_once_with("project", "meta")
        kconfig_mock.assert_called_once_with(
            str(check_zephyr_project_config.ZEPHYR_BASE / "Kconfig")
        )

        self.assertEqual(
            os.environ["ZEPHYR_BASE"],
            str(check_zephyr_project_config.ZEPHYR_BASE),
        )
        self.assertEqual(
            os.environ["srctree"], str(check_zephyr_project_config.ZEPHYR_BASE)
        )
        self.assertEqual(os.environ["ARCH_DIR"], "arch")
        self.assertEqual(os.environ["ARCH"], "*")
        self.assertEqual(os.environ["BOARD_DIR"], "boards/*/*")

    @mock.patch("zephyr_module.parse_modules")
    @mock.patch("zephyr_module.process_kconfig")
    @mock.patch("kconfiglib.Kconfig")
    def test_init_kconfig_filename(
        self, kconfig_mock, process_kconfig_mock, parse_modules_mock
    ):
        """Initialize a Kconfig with a specific path."""
        kconfig_path = "my/project/Kconfig"

        self.kcheck._init_kconfig(kconfig_path)

        kconfig_mock.assert_called_once_with(kconfig_path)
        self.assertEqual(process_kconfig_mock.call_count, 0)
        parse_modules_mock.assert_called_once_with(mock.ANY, modules=mock.ANY)

    @mock.patch("pathlib.Path.is_file")
    @mock.patch("check_zephyr_project_config.KconfigCheck._init_kconfig")
    def test_kconf_from_path(self, kconfig_mock, is_file_mock):
        """Test the cached Kconfig load from path."""
        fake_kconfig_upstream = mock.Mock()
        fake_kconfig_program = mock.Mock()
        fake_kconfig_program_new = mock.Mock()
        kconfig_mock.return_value = fake_kconfig_program_new
        self.kcheck.program_kconf = {
            None: fake_kconfig_upstream,
            "cached_program": fake_kconfig_program,
        }

        # random project
        out = self.kcheck._kconf_from_path("random/path")

        self.assertEqual(out, fake_kconfig_upstream)
        self.assertEqual(kconfig_mock.call_count, 0)
        self.assertEqual(is_file_mock.call_count, 0)

        # already loaded
        is_file_mock.return_value = True
        fake_path = pathlib.Path(
            check_zephyr_project_config.EC_BASE,
            "zephyr",
            "program",
            "cached_program",
        )

        out = self.kcheck._kconf_from_path(fake_path)

        self.assertEqual(out, fake_kconfig_program)
        self.assertEqual(kconfig_mock.call_count, 0)
        is_file_mock.assert_called_once_with()
        is_file_mock.reset_mock()

        # project with no kconfig
        is_file_mock.return_value = False
        fake_path = pathlib.Path(
            check_zephyr_project_config.EC_BASE,
            "zephyr",
            "program",
            "program_with_no_kconfig",
        )

        out = self.kcheck._kconf_from_path(fake_path)

        self.assertEqual(out, fake_kconfig_upstream)
        self.assertEqual(kconfig_mock.call_count, 0)
        is_file_mock.assert_called_once_with()
        is_file_mock.reset_mock()

        # project with kconfig
        is_file_mock.return_value = True
        fake_path = pathlib.Path(
            check_zephyr_project_config.EC_BASE,
            "zephyr",
            "program",
            "program_with_kconfig",
        )

        out = self.kcheck._kconf_from_path(fake_path)

        self.assertEqual(out, fake_kconfig_program_new)
        kconfig_mock.assert_called_once_with(pathlib.Path(fake_path, "Kconfig"))
        is_file_mock.assert_called_once_with()
        is_file_mock.reset_mock()

        # final cache state
        self.assertEqual(
            self.kcheck.program_kconf,
            {
                None: fake_kconfig_upstream,
                "cached_program": fake_kconfig_program,
                "program_with_kconfig": fake_kconfig_program_new,
                "program_with_no_kconfig": fake_kconfig_upstream,
            },
        )

    def test_fail(self):
        """Test the fail method."""
        with mock.patch.object(self.kcheck, "log") as log_mock:
            self.assertEqual(self.kcheck.fail_count, 0)

            self.kcheck._fail("broken")

            self.assertEqual(self.kcheck.fail_count, 1)
            log_mock.error.assert_called_once_with("broken")

    def test_filter_config_files(self):
        """Test the config file filter."""
        file_no_exist = mock.Mock()
        file_no_exist.exists.return_value = False

        file_exists = mock.Mock()
        file_exists.exists.return_value = True
        file_exists.name = "definitely_not_a_conf"

        file_conf = mock.Mock()
        file_conf.exists.return_value = True
        file_conf.name = "blah.conf"

        files = [file_no_exist, file_exists, file_conf]

        out = list(self.kcheck._filter_config_files(files))

        self.assertEqual(out, [file_conf])

    @mock.patch("kconfiglib.expr_str")
    @mock.patch("check_zephyr_project_config.KconfigCheck._kconf_from_path")
    @mock.patch("check_zephyr_project_config.KconfigCheck._fail")
    def test_check_dt_has(self, fail_mock, kconf_from_path_mock, expr_str_mock):
        """Test the DT_HAS_ check."""
        fake_symbol_no_dt = mock.Mock()
        fake_symbol_no_dt.direct_dep = "nothing with devicetree"

        fake_symbol_dt = mock.Mock()
        fake_symbol_dt.direct_dep = (
            "a bunch of stuff with DT_HAS_something in the middle"
        )

        fake_kconf = mock.Mock()
        fake_kconf.syms = {"NO_DT": fake_symbol_no_dt, "DT": fake_symbol_dt}

        kconf_from_path_mock.return_value = fake_kconf
        expr_str_mock.side_effect = lambda val: val

        data = """
# some comment
CONFIG_NOT_RELATED=n
CONFIG_NO_DT=y
CONFIG_NO_DT=n
CONFIG_DT=y
CONFIG_DT=y # and a comment
CONFIG_DT=n
CONFIG_SOMETHING_ELSE=y
"""
        with mock.patch("builtins.open", mock.mock_open(read_data=data)):
            self.kcheck._check_dt_has("the_file")

        self.assertListEqual(
            fail_mock.call_args_list,
            [
                mock.call(
                    mock.ANY,
                    "the_file",
                    6,
                    "CONFIG_DT=y",
                    fake_symbol_dt.direct_dep,
                ),
                mock.call(
                    mock.ANY,
                    "the_file",
                    7,
                    "CONFIG_DT=y",
                    fake_symbol_dt.direct_dep,
                ),
            ],
        )

    @mock.patch("check_zephyr_project_config.KconfigCheck._check_dt_has")
    @mock.patch("check_zephyr_project_config.KconfigCheck._filter_config_files")
    def test_run_checks_dt_has(
        self, filter_config_files_mock, check_dt_has_mock
    ):
        """Test the run_check method for dt_has."""
        filter_config_files_mock.return_value = ["a", "b"]

        self.kcheck.run_checks([], True)

        self.assertListEqual(
            check_dt_has_mock.call_args_list, [mock.call("a"), mock.call("b")]
        )


if __name__ == "__main__":
    unittest.main()