1# SPDX-License-Identifier: GPL-2.0
2# Copyright 2020 Google LLC
3# Written by Simon Glass <sjg@chromium.org>
4
5import os.path
6import pytest
7
8@pytest.mark.buildconfigspec('spl_unit_test')
9def test_ut_spl_init(u_boot_console):
10    """Initialize data for ut spl tests."""
11
12    fn = u_boot_console.config.source_dir + '/spi.bin'
13    if not os.path.exists(fn):
14        data = b'\x00' * (2 * 1024 * 1024)
15        with open(fn, 'wb') as fh:
16            fh.write(data)
17
18def test_spl(u_boot_console, ut_spl_subtest):
19    """Execute a "ut" subtest.
20
21    The subtests are collected in function generate_ut_subtest() from linker
22    generated lists by applying a regular expression to the lines of file
23    spl/u-boot-spl.sym. The list entries are created using the C macro
24    UNIT_TEST().
25
26    Strict naming conventions have to be followed to match the regular
27    expression. Use UNIT_TEST(foo_test_bar, _flags, foo_test) for a test bar in
28    test suite foo that can be executed via command 'ut foo bar' and is
29    implemented in C function foo_test_bar().
30
31    Args:
32        u_boot_console (ConsoleBase): U-Boot console
33        ut_subtest (str): SPL test to be executed (e.g. 'dm platdata_phandle')
34    """
35    try:
36        cons = u_boot_console
37        cons.restart_uboot_with_flags(['-u', '-k', ut_spl_subtest.split()[1]])
38        output = cons.get_spawn_output().replace('\r', '')
39        assert 'Failures: 0' in output
40    finally:
41        # Restart afterward in case a non-SPL test is run next. This should not
42        # happen since SPL tests are run in their own invocation of test.py, but
43        # the cost of doing this is not too great at present.
44        u_boot_console.restart_uboot()
45