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