summaryrefslogtreecommitdiff
path: root/zephyr/zmake/tests/test_build_config.py
blob: d8355da7683a0f8a3e053b76919360a0b1bd3b32 (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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# Copyright 2021 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Tests of zmake's build config system."""

import argparse
import os
import pathlib
import string
import tempfile

import hypothesis  # pylint:disable=import-error
import hypothesis.strategies as st  # pylint:disable=import-error
import pytest  # pylint:disable=import-error
import zmake.jobserver
import zmake.util as util
from zmake.build_config import BuildConfig

# pylint:disable=redefined-outer-name,unused-argument

# Strategies for use with hypothesis
filenames = st.text(
    alphabet=set(string.printable) - {"/", ";"}, min_size=1, max_size=254
).filter(lambda name: name not in (".", ".."))
paths = st.builds(
    lambda parts: pathlib.Path("/", *parts), st.iterables(filenames, min_size=1)
)
config_keys = st.text(alphabet=set(string.ascii_uppercase) | {"_"}, min_size=1)
config_values = st.builds(str, st.just("y") | st.just("n") | st.integers())
config_dicts = st.dictionaries(keys=config_keys, values=config_values)
config_dicts_at_least_one_entry = st.dictionaries(
    keys=config_keys, values=config_values, min_size=1
)

build_configs = st.builds(
    BuildConfig,
    cmake_defs=config_dicts,
    kconfig_defs=config_dicts,
    kconfig_files=st.lists(paths),
)
build_configs_no_kconfig = st.builds(BuildConfig, cmake_defs=config_dicts)
build_configs_with_at_least_one_kconfig = st.builds(
    BuildConfig,
    cmake_defs=config_dicts,
    kconfig_defs=config_dicts_at_least_one_entry,
)


@hypothesis.given(st.data(), build_configs)
def test_merge(coins, combined):
    """Test that when splitting a config in half and merging the two
    halves, we get the original config back.
    """

    def split(iterable):
        left = []
        right = []
        bools = st.booleans()
        for item in iterable:
            if coins.draw(bools):
                left.append(item)
            else:
                right.append(item)
        return left, right

    # Split the original config into two
    cmake1, cmake2 = split(combined.cmake_defs.items())
    kconf1, kconf2 = split(combined.kconfig_defs.items())
    files1, files2 = split(combined.kconfig_files)

    config1 = BuildConfig(
        cmake_defs=dict(cmake1),
        kconfig_defs=dict(kconf1),
        kconfig_files=files1,
    )
    config2 = BuildConfig(
        cmake_defs=dict(cmake2),
        kconfig_defs=dict(kconf2),
        kconfig_files=files2,
    )

    # Merge the split configs
    merged = config1 | config2

    # Assert that the merged split configs is the original config
    assert merged.cmake_defs == combined.cmake_defs
    assert merged.kconfig_defs == combined.kconfig_defs
    assert set(merged.kconfig_files) == set(combined.kconfig_files)


class FakeJobClient(zmake.jobserver.JobClient):
    """Simple job client to capture argv/environ."""

    def __init__(self):
        self.captured_argv = []
        self.captured_env = {}

    def get_job(self):
        return zmake.jobserver.JobHandle(lambda: None)

    def popen(self, argv, **kwargs):
        kwargs.setdefault("env", {})
        self.captured_argv = [str(arg) for arg in argv]
        self.captured_env = {str(k): str(v) for k, v in kwargs["env"].items()}


def parse_cmake_args(argv):
    """Parse command line arguments like cmake does.

    This is an intenionally minimal implementation, which only
    understands the subset of arguments actually used by zmake.

    Args:
        argv: The argument list.

    Returns:
        A 2-tuple of a namespace from argparse and the corresponding
        parsed Cmake definitions.
    """
    assert argv[0] == "/usr/bin/cmake"

    parser = argparse.ArgumentParser(add_help=False)
    parser.add_argument("-S", dest="source_dir", type=pathlib.Path)
    parser.add_argument("-B", dest="build_dir", type=pathlib.Path)
    parser.add_argument("-G", dest="generator")
    parser.add_argument("-D", dest="defs", action="append", default=[])
    args = parser.parse_args(argv[1:])

    # Build the definition dictionary
    cmake_defs = {}
    for defn in args.defs:
        key, sep, val = defn.partition("=")
        if not sep:
            val = "1"
        assert key not in cmake_defs
        cmake_defs[key] = val

    return args, cmake_defs


@hypothesis.given(build_configs_no_kconfig, paths, paths)
@hypothesis.settings(deadline=60000)
def test_popen_cmake_no_kconfig(conf: BuildConfig, project_dir, build_dir):
    """Test popen_cmake for a config with no kconfig definitions."""
    job_client = FakeJobClient()
    conf.popen_cmake(job_client, project_dir, build_dir)

    _, cmake_defs = parse_cmake_args(job_client.captured_argv)

    assert cmake_defs == conf.cmake_defs


@hypothesis.given(build_configs_with_at_least_one_kconfig, paths, paths)
@hypothesis.settings(deadline=60000)
def test_popen_cmake_kconfig_but_no_file(
    conf: BuildConfig, project_dir, build_dir
):
    """Test that running popen_cmake with Kconfig definitions to write
    out, but no path to do so, should raise an error.
    """
    job_client = FakeJobClient()

    with pytest.raises(ValueError):
        conf.popen_cmake(job_client, project_dir, build_dir)


@hypothesis.given(build_configs, paths, paths)
@hypothesis.settings(deadline=60000)
def test_popen_cmake_kconfig(conf: BuildConfig, project_dir, build_dir):
    """Test calling popen_cmake and verifying the kconfig_files."""
    job_client = FakeJobClient()

    with tempfile.NamedTemporaryFile("w", delete=False) as file:
        temp_path = file.name

    try:
        conf.popen_cmake(
            job_client,
            project_dir,
            build_dir,
            kconfig_path=pathlib.Path(temp_path),
        )

        _, cmake_defs = parse_cmake_args(job_client.captured_argv)

        expected_kconfig_files = set(str(f) for f in conf.kconfig_files)
        expected_kconfig_files.add(temp_path)

        if expected_kconfig_files:
            kconfig_files = set(cmake_defs.pop("CONF_FILE").split(";"))
        else:
            assert "CONF_FILE" not in cmake_defs
            kconfig_files = set()

        assert cmake_defs == conf.cmake_defs
        assert kconfig_files == expected_kconfig_files

        kconfig_defs = util.read_kconfig_file(temp_path)
        assert kconfig_defs == conf.kconfig_defs
    finally:
        os.unlink(temp_path)


@pytest.fixture
def fake_kconfig_files(tmp_path):
    """Provide a list of 4 different fake kconfig file paths."""

    paths = [tmp_path / f"{letter}.conf" for letter in "ABCD"]

    for path, cfg_name in zip(paths, ("ONE", "TWO", "THREE", "FOUR")):
        path.write_text(
            f"# Fake kconfig file for testing.\nCONFIG_{cfg_name}=y\n"
        )

    return paths


def test_build_config_json_stability(fake_kconfig_files):
    """as_json() should return equivalent strings for two equivalent
    build configs.
    """
    config_a = BuildConfig(
        cmake_defs={
            "Z": "Y",
            "X": "W",
        },
        kconfig_defs={
            "CONFIG_A": "y",
            "CONFIG_B": "n",
        },
        kconfig_files=fake_kconfig_files,
    )

    # Dict ordering is intentionally reversed in b.
    config_b = BuildConfig(
        cmake_defs={
            "X": "W",
            "Z": "Y",
        },
        kconfig_defs={
            "CONFIG_B": "n",
            "CONFIG_A": "y",
        },
        kconfig_files=list(fake_kconfig_files),
    )

    assert config_a.as_json() == config_b.as_json()


def test_build_config_json_inequality():
    """Two differing build configs should not have the same json
    representation.
    """
    config_a = BuildConfig(cmake_defs={"A": "B"})
    config_b = BuildConfig(kconfig_defs={"CONFIG_A": "y"})

    assert config_a.as_json() != config_b.as_json()


def test_build_config_json_inequality_dtc_changes(tmp_path):
    """When DTC overlay files change, so should the JSON."""
    dts_file_1 = tmp_path / "overlay1.dts"
    dts_file_1.write_text("/* blah */\n")

    dts_file_2 = tmp_path / "overlay2.dts"
    dts_file_2.write_text("/* zonks! */\n")

    cfg = BuildConfig(
        cmake_defs={
            "DTC_OVERLAY_FILE": f"{dts_file_1};{dts_file_2}",
        },
    )

    orig_json = cfg.as_json()

    # Now, change dts_file_2!
    dts_file_2.write_text("/* I changed!! */\n")

    new_json = cfg.as_json()

    assert orig_json != new_json


def test_kconfig_file_duplicates(fake_kconfig_files):
    """Kconfig files should be like the "uniq" command.  Repeats should
    be removed, but not duplicates."""
    cfg = BuildConfig(
        kconfig_files=[
            fake_kconfig_files[0],
            fake_kconfig_files[0],
            fake_kconfig_files[1],
            fake_kconfig_files[0],
        ]
    )
    assert cfg.kconfig_files == [
        fake_kconfig_files[0],
        fake_kconfig_files[1],
        fake_kconfig_files[0],
    ]