~funderscore blog cgit wiki get in touch
aboutsummaryrefslogtreecommitdiff
blob: 5206ff73ec73211352489ce67ac5624eaedf4199 (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
# SPDX-License-Identifier: GPL-2.0
# Copyright (c) 2023 Tobias Deiminger <tdmg@linutronix.de>

"""Test for unexpected leftovers after make clean"""

import itertools
import os
import pathlib
import shutil
import sys

import pytest

# pylint: disable=redefined-outer-name


@pytest.fixture
def tmp_copy_of_builddir(u_boot_config, tmp_path):
    """For each test, provide a temporary copy of the initial build directory."""
    shutil.copytree(
        u_boot_config.build_dir,
        tmp_path,
        symlinks=True,
        dirs_exist_ok=True,
    )
    return tmp_path


@pytest.fixture(scope="module")
def run_make(u_boot_log):
    """Provide function to run and log make without connecting to u-boot console."""
    runner = u_boot_log.get_runner("make", sys.stdout)

    def _run_make(build_dir, target):
        cmd = ["make", f"O={build_dir}", target]
        runner.run(cmd)

    yield _run_make
    runner.close()


@pytest.fixture(scope="module")
def most_generated_files():
    """Path.glob style patterns to describe what should be removed by 'make clean'."""
    return (
        "**/*.c",
        "**/*.dtb",
        "**/*.dtbo",
        "**/*.o",
        "**/*.py",
        "**/*.pyc",
        "**/*.so",
        "**/*.srec",
        "u-boot*",
        "[svt]pl/u-boot*",
    )


@pytest.fixture(scope="module")
def all_generated_files(most_generated_files):
    """Path.glob style patterns to describe what should be removed by 'make mrproper'."""
    return most_generated_files + (".config", "**/*.h")


def find_files(search_dir, include_patterns, exclude_dirs=None):
    """Find files matching include_patterns, unless it's in one of exclude_dirs.

    include_patterns -- Path.glob style pattern relative to search dir
    exclude_dir -- directories to exclude, expected relative to search dir
    """
    matches = []
    exclude_dirs = [] if exclude_dirs is None else exclude_dirs
    for abs_path in itertools.chain.from_iterable(
        pathlib.Path(search_dir).glob(pattern) for pattern in include_patterns
    ):
        if abs_path.is_dir():
            continue
        rel_path = pathlib.Path(os.path.relpath(abs_path, search_dir))
        if not any(
            rel_path.is_relative_to(exclude_dir) for exclude_dir in exclude_dirs
        ):
            matches.append(rel_path)
    return matches


def test_clean(run_make, tmp_copy_of_builddir, most_generated_files):
    """Test if 'make clean' deletes most generated files."""
    run_make(tmp_copy_of_builddir, "clean")
    leftovers = find_files(
        tmp_copy_of_builddir,
        most_generated_files,
        exclude_dirs=["scripts", "test/overlay"],
    )
    assert not leftovers, f"leftovers: {', '.join(map(str, leftovers))}"


def test_mrproper(run_make, tmp_copy_of_builddir, all_generated_files):
    """Test if 'make mrproper' deletes current configuration and all generated files."""
    run_make(tmp_copy_of_builddir, "mrproper")
    leftovers = find_files(
        tmp_copy_of_builddir,
        all_generated_files,
        exclude_dirs=["test/overlay"],
    )
    assert not leftovers, f"leftovers: {', '.join(map(str, leftovers))}"