1# SPDX-License-Identifier: GPL-2.0
2# Copyright (C) 2020 Sean Anderson
3
4import pytest
5
6@pytest.mark.buildconfigspec('cmd_dm')
7def test_dm_compat(ubman):
8    """Test that each driver in `dm tree` is also listed in `dm compat`."""
9    response = ubman.run_command('dm tree')
10    driver_index = response.find('Driver')
11    assert driver_index != -1
12    drivers = (line[driver_index:].split()[0]
13               for line in response[:-1].split('\n')[2:])
14
15    response = ubman.run_command('dm compat')
16    bad_drivers = set()
17    for driver in drivers:
18        if not driver in response:
19            bad_drivers.add(driver)
20    assert not bad_drivers
21
22    # check sorting - output looks something like this:
23    #  testacpi      0  [   ]   testacpi_drv          |-- acpi-test
24    #  testacpi      1  [   ]   testacpi_drv          |   `-- child
25    #  pci_emul_p    1  [   ]   pci_emul_parent_drv   |-- pci-emul2
26    #  pci_emul      5  [   ]   sandbox_swap_case_em  |   `-- emul2@1f,0
27
28    # The number of '|   ' and '--' matches indicate the indent level. We start
29    # checking sorting only after UCLASS_AXI_EMUL after which the names should
30    # be sorted.
31
32    response = ubman.run_command('dm tree -s')
33    lines = response.split('\n')[2:]
34    stack = []   # holds where we were up to at the previous indent level
35    prev = ''    # uclass name of previous line
36    start = False
37    for line in lines:
38        indent = line.count('|   ') + ('--' in line)
39        cur = line.split()[0]
40        if not start:
41            if cur != 'axi_emul':
42                continue
43            start = True
44
45        # Handle going up or down an indent level
46        if indent > len(stack):
47            stack.append(prev)
48            prev = ''
49        elif indent < len(stack):
50            prev = stack.pop()
51
52        # Check that the current uclass name is not alphabetically before the
53        # previous one
54        if 'emul' not in cur and cur < prev:
55            print('indent', cur >= prev, indent, prev, cur, stack)
56            assert cur >= prev
57            prev = cur
58
59
60@pytest.mark.buildconfigspec('cmd_dm')
61def test_dm_drivers(ubman):
62    """Test that each driver in `dm compat` is also listed in `dm drivers`."""
63    response = ubman.run_command('dm compat')
64    drivers = (line[:20].rstrip() for line in response[:-1].split('\n')[2:])
65    response = ubman.run_command('dm drivers')
66    for driver in drivers:
67        assert driver in response
68
69@pytest.mark.buildconfigspec('cmd_dm')
70def test_dm_static(ubman):
71    """Test that each driver in `dm static` is also listed in `dm drivers`."""
72    response = ubman.run_command('dm static')
73    drivers = (line[:25].rstrip() for line in response[:-1].split('\n')[2:])
74    response = ubman.run_command('dm drivers')
75    for driver in drivers:
76        assert driver in response
77
78@pytest.mark.buildconfigspec("cmd_dm")
79def test_dm_uclass(ubman):
80    response = ubman.run_command("dm uclass")
81
82@pytest.mark.buildconfigspec("cmd_dm")
83def test_dm_devres(ubman):
84    response = ubman.run_command("dm devres")
85