xref: /openbmc/openbmc/poky/meta/classes-recipe/ptest-cargo.bbclass (revision 8460358c3d24c71d9d38fd126c745854a6301564)
1inherit cargo ptest
2
3RUST_TEST_ARGS ??= ""
4RUST_TEST_ARGS[doc] = "Arguments to give to the test binaries (e.g. --shuffle)"
5
6# I didn't find a cleaner way to share data between compile and install tasks
7CARGO_TEST_BINARIES_FILES ?= "${B}/test_binaries_list"
8
9# Sadly, generated test binaries have no deterministic names (https://github.com/rust-lang/cargo/issues/1924)
10# This forces us to parse the cargo output in json format to find those test binaries.
11python do_compile_ptest_cargo() {
12    import subprocess
13    import json
14
15    cargo = bb.utils.which(d.getVar("PATH"), d.getVar("CARGO"))
16    cargo_build_flags = d.getVar("CARGO_BUILD_FLAGS")
17    rust_flags = d.getVar("RUSTFLAGS")
18    manifest_path = d.getVar("CARGO_MANIFEST_PATH")
19    project_manifest_path = os.path.normpath(manifest_path)
20    manifest_dir = os.path.dirname(manifest_path)
21
22    env = os.environ.copy()
23    env['RUSTFLAGS'] = rust_flags
24    cmd = f"{cargo} build --tests --message-format json {cargo_build_flags}"
25    bb.note(f"Building tests with cargo ({cmd})")
26
27    try:
28        proc = subprocess.Popen(cmd, shell=True, env=env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
29    except subprocess.CalledProcessError as e:
30        bb.fatal(f"Cannot build test with cargo: {e}")
31
32    lines = []
33    for line in proc.stdout:
34        data = line.strip('\n')
35        lines.append(data)
36        bb.note(data)
37    proc.communicate()
38    if proc.returncode != 0:
39        bb.fatal(f"Unable to compile test with cargo, '{cmd}' failed")
40
41    # Definition of the format: https://doc.rust-lang.org/cargo/reference/external-tools.html#json-messages
42    test_bins = []
43    for line in lines:
44        try:
45            data = json.loads(line)
46        except json.JSONDecodeError:
47            # skip lines that are not a json
48            pass
49        else:
50            try:
51                # Filter the test packages coming from the current project:
52                #    - test binaries from the root manifest
53                #    - test binaries from sub manifest of the current project if any
54                current_manifest_path = os.path.normpath(data['manifest_path'])
55                common_path = os.path.commonpath([current_manifest_path, project_manifest_path])
56                if common_path in [manifest_dir, current_manifest_path]:
57                    if (data['target']['test'] or data['target']['doctest']) and data['executable']:
58                        test_bins.append(data['executable'])
59            except (KeyError, ValueError) as e:
60                # skip lines that do not meet the requirements
61                pass
62
63    # All rust project will generate at least one unit test binary
64    # It will just run a test suite with 0 tests, if the project didn't define some
65    # So it is not expected to have an empty list here
66    if not test_bins:
67        bb.fatal("Unable to find any test binaries")
68
69    cargo_test_binaries_file = d.getVar('CARGO_TEST_BINARIES_FILES')
70    bb.note(f"Found {len(test_bins)} tests, write their paths into {cargo_test_binaries_file}")
71    with open(cargo_test_binaries_file, "w") as f:
72        for test_bin in test_bins:
73            f.write(f"{test_bin}\n")
74
75}
76
77python do_install_ptest_cargo() {
78    import shutil
79
80    dest_dir = d.getVar("D")
81    pn = d.getVar("PN")
82    ptest_path = d.getVar("PTEST_PATH")
83    cargo_test_binaries_file = d.getVar('CARGO_TEST_BINARIES_FILES')
84    rust_test_args = d.getVar('RUST_TEST_ARGS') or ""
85
86    ptest_dir = os.path.join(dest_dir, ptest_path.lstrip('/'))
87    os.makedirs(ptest_dir, exist_ok=True)
88
89    test_bins = []
90    with open(cargo_test_binaries_file, "r") as f:
91        for line in f.readlines():
92            test_bins.append(line.strip('\n'))
93
94    test_paths = []
95    for test_bin in test_bins:
96        shutil.copy2(test_bin, ptest_dir)
97        test_paths.append(os.path.join(ptest_path, os.path.basename(test_bin)))
98
99    ptest_script = os.path.join(ptest_dir, "run-ptest")
100    if os.path.exists(ptest_script):
101        with open(ptest_script, "a") as f:
102            f.write(f"\necho \"\"\n")
103            f.write(f"echo \"## starting to run rust tests ##\"\n")
104            for test_path in test_paths:
105                f.write(f"{test_path} {rust_test_args}\n")
106    else:
107        with open(ptest_script, "a") as f:
108            f.write("#!/bin/sh\n")
109            for test_path in test_paths:
110                f.write(f"{test_path} {rust_test_args}\n")
111        os.chmod(ptest_script, 0o755)
112
113    # this is chown -R root:root ${D}${PTEST_PATH}
114    for root, dirs, files in os.walk(ptest_dir):
115        for d in dirs:
116            shutil.chown(os.path.join(root, d), "root", "root")
117        for f in files:
118            shutil.chown(os.path.join(root, f), "root", "root")
119}
120
121do_install_ptest_cargo[dirs] = "${B}"
122do_install_ptest_cargo[doc] = "Create or update the run-ptest script with rust test binaries generated"
123do_compile_ptest_cargo[dirs] = "${B}"
124do_compile_ptest_cargo[doc] = "Generate rust test binaries through cargo"
125
126addtask compile_ptest_cargo after do_compile            before do_compile_ptest_base
127addtask install_ptest_cargo after do_install_ptest_base before do_package
128
129python () {
130    if not bb.data.inherits_class('native', d) and not bb.data.inherits_class('cross', d):
131        d.setVarFlag('do_install_ptest_cargo', 'fakeroot', '1')
132        d.setVarFlag('do_install_ptest_cargo', 'umask', '022')
133
134    # Remove all '*ptest_cargo' tasks when ptest is not enabled
135    if not(d.getVar('PTEST_ENABLED') == "1"):
136        for i in ['do_compile_ptest_cargo', 'do_install_ptest_cargo']:
137            bb.build.deltask(i, d)
138}
139