blob: c46df362bfeebd7b4284149942e3d0b0df0fd67f [file] [log] [blame]
Andrew Geissler028142b2023-05-05 11:29:21 -05001inherit cargo ptest
2
Andrew Geissler3eeda902023-05-19 10:14:02 -05003RUST_TEST_ARGS ??= ""
4RUST_TEST_ARGS[doc] = "Arguments to give to the test binaries (e.g. --shuffle)"
5
Andrew Geissler028142b2023-05-05 11:29:21 -05006# 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)
Patrick Williams169d7bc2024-01-05 11:33:25 -060018 manifest_path = d.getVar("CARGO_MANIFEST_PATH", True)
Patrick Williams2a254922023-08-11 09:48:11 -050019 project_manifest_path = os.path.normpath(manifest_path)
20 manifest_dir = os.path.dirname(manifest_path)
Andrew Geissler028142b2023-05-05 11:29:21 -050021
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:
Andrew Geissler8f840682023-07-21 09:09:43 -050028 proc = subprocess.Popen(cmd, shell=True, env=env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
Andrew Geissler028142b2023-05-05 11:29:21 -050029 except subprocess.CalledProcessError as e:
30 bb.fatal(f"Cannot build test with cargo: {e}")
31
32 lines = []
33 for line in proc.stdout:
Andrew Geissler8f840682023-07-21 09:09:43 -050034 data = line.strip('\n')
Andrew Geissler028142b2023-05-05 11:29:21 -050035 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:
Patrick Williams2a254922023-08-11 09:48:11 -050051 # 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
Andrew Geissler028142b2023-05-05 11:29:21 -050054 current_manifest_path = os.path.normpath(data['manifest_path'])
Patrick Williams2a254922023-08-11 09:48:11 -050055 common_path = os.path.commonpath([current_manifest_path, project_manifest_path])
56 if common_path in [manifest_dir, current_manifest_path]:
Andrew Geissler8f840682023-07-21 09:09:43 -050057 if (data['target']['test'] or data['target']['doctest']) and data['executable']:
Andrew Geissler028142b2023-05-05 11:29:21 -050058 test_bins.append(data['executable'])
Patrick Williams2a254922023-08-11 09:48:11 -050059 except (KeyError, ValueError) as e:
Andrew Geissler028142b2023-05-05 11:29:21 -050060 # 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', True)
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", True)
81 pn = d.getVar("PN", True)
82 ptest_path = d.getVar("PTEST_PATH", True)
83 cargo_test_binaries_file = d.getVar('CARGO_TEST_BINARIES_FILES', True)
Andrew Geissler3eeda902023-05-19 10:14:02 -050084 rust_test_args = d.getVar('RUST_TEST_ARGS') or ""
Andrew Geissler028142b2023-05-05 11:29:21 -050085
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:
Andrew Geissler3eeda902023-05-19 10:14:02 -0500105 f.write(f"{test_path} {rust_test_args}\n")
Andrew Geissler028142b2023-05-05 11:29:21 -0500106 else:
107 with open(ptest_script, "a") as f:
108 f.write("#!/bin/sh\n")
109 for test_path in test_paths:
Andrew Geissler3eeda902023-05-19 10:14:02 -0500110 f.write(f"{test_path} {rust_test_args}\n")
Andrew Geissler028142b2023-05-05 11:29:21 -0500111 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}