Andrew Geissler | 028142b | 2023-05-05 11:29:21 -0500 | [diff] [blame] | 1 | inherit cargo ptest |
| 2 | |
Andrew Geissler | 3eeda90 | 2023-05-19 10:14:02 -0500 | [diff] [blame] | 3 | RUST_TEST_ARGS ??= "" |
| 4 | RUST_TEST_ARGS[doc] = "Arguments to give to the test binaries (e.g. --shuffle)" |
| 5 | |
Andrew Geissler | 028142b | 2023-05-05 11:29:21 -0500 | [diff] [blame] | 6 | # I didn't find a cleaner way to share data between compile and install tasks |
| 7 | CARGO_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. |
| 11 | python 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) |
Patrick Williams | 2a25492 | 2023-08-11 09:48:11 -0500 | [diff] [blame] | 19 | project_manifest_path = os.path.normpath(manifest_path) |
| 20 | manifest_dir = os.path.dirname(manifest_path) |
Andrew Geissler | 028142b | 2023-05-05 11:29:21 -0500 | [diff] [blame] | 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: |
Andrew Geissler | 8f84068 | 2023-07-21 09:09:43 -0500 | [diff] [blame] | 28 | proc = subprocess.Popen(cmd, shell=True, env=env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) |
Andrew Geissler | 028142b | 2023-05-05 11:29:21 -0500 | [diff] [blame] | 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: |
Andrew Geissler | 8f84068 | 2023-07-21 09:09:43 -0500 | [diff] [blame] | 34 | data = line.strip('\n') |
Andrew Geissler | 028142b | 2023-05-05 11:29:21 -0500 | [diff] [blame] | 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: |
Patrick Williams | 2a25492 | 2023-08-11 09:48:11 -0500 | [diff] [blame] | 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 |
Andrew Geissler | 028142b | 2023-05-05 11:29:21 -0500 | [diff] [blame] | 54 | current_manifest_path = os.path.normpath(data['manifest_path']) |
Patrick Williams | 2a25492 | 2023-08-11 09:48:11 -0500 | [diff] [blame] | 55 | common_path = os.path.commonpath([current_manifest_path, project_manifest_path]) |
| 56 | if common_path in [manifest_dir, current_manifest_path]: |
Andrew Geissler | 8f84068 | 2023-07-21 09:09:43 -0500 | [diff] [blame] | 57 | if (data['target']['test'] or data['target']['doctest']) and data['executable']: |
Andrew Geissler | 028142b | 2023-05-05 11:29:21 -0500 | [diff] [blame] | 58 | test_bins.append(data['executable']) |
Patrick Williams | 2a25492 | 2023-08-11 09:48:11 -0500 | [diff] [blame] | 59 | except (KeyError, ValueError) as e: |
Andrew Geissler | 028142b | 2023-05-05 11:29:21 -0500 | [diff] [blame] | 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', 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 | |
| 77 | python 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 Geissler | 3eeda90 | 2023-05-19 10:14:02 -0500 | [diff] [blame] | 84 | rust_test_args = d.getVar('RUST_TEST_ARGS') or "" |
Andrew Geissler | 028142b | 2023-05-05 11:29:21 -0500 | [diff] [blame] | 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: |
Andrew Geissler | 3eeda90 | 2023-05-19 10:14:02 -0500 | [diff] [blame] | 105 | f.write(f"{test_path} {rust_test_args}\n") |
Andrew Geissler | 028142b | 2023-05-05 11:29:21 -0500 | [diff] [blame] | 106 | else: |
| 107 | with open(ptest_script, "a") as f: |
| 108 | f.write("#!/bin/sh\n") |
| 109 | for test_path in test_paths: |
Andrew Geissler | 3eeda90 | 2023-05-19 10:14:02 -0500 | [diff] [blame] | 110 | f.write(f"{test_path} {rust_test_args}\n") |
Andrew Geissler | 028142b | 2023-05-05 11:29:21 -0500 | [diff] [blame] | 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 | |
| 121 | do_install_ptest_cargo[dirs] = "${B}" |
| 122 | do_install_ptest_cargo[doc] = "Create or update the run-ptest script with rust test binaries generated" |
| 123 | do_compile_ptest_cargo[dirs] = "${B}" |
| 124 | do_compile_ptest_cargo[doc] = "Generate rust test binaries through cargo" |
| 125 | |
| 126 | addtask compile_ptest_cargo after do_compile before do_compile_ptest_base |
| 127 | addtask install_ptest_cargo after do_install_ptest_base before do_package |
| 128 | |
| 129 | python () { |
| 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 | } |