From 3536302aa170e8611b6dafc57ab2db33f0cf08df Mon Sep 17 00:00:00 2001 From: adonia Date: Fri, 13 Sep 2024 12:31:46 +0800 Subject: [PATCH] update: set output name of unit test --- build/common/rust/rust.gni | 11 --- build/common/rust/rust_build_tests.py | 111 ++++++++++++---------- core/src/ten_manager/.cargo/config.toml | 2 - core/src/ten_manager/BUILD.gn | 2 - core/src/ten_manager/tools/test_runner.py | 52 ---------- core/src/ten_rust/.cargo/config.toml | 2 - core/src/ten_rust/BUILD.gn | 2 - core/src/ten_rust/tools/test_runner.py | 52 ---------- 8 files changed, 62 insertions(+), 172 deletions(-) delete mode 100644 core/src/ten_manager/.cargo/config.toml delete mode 100755 core/src/ten_manager/tools/test_runner.py delete mode 100644 core/src/ten_rust/.cargo/config.toml delete mode 100755 core/src/ten_rust/tools/test_runner.py diff --git a/build/common/rust/rust.gni b/build/common/rust/rust.gni index d47daaf62c..d10b10e5d4 100644 --- a/build/common/rust/rust.gni +++ b/build/common/rust/rust.gni @@ -153,8 +153,6 @@ template("rust_target") { # why we need to add a separate target for building the tests only. template("rust_test") { assert(defined(invoker.project_path), "project_path is not defined") - assert(defined(invoker.unit_test_output_name), - "unit_test_output_name is required to determine the unit test binary.") _target_name = target_name target_path = target_gen_dir @@ -170,8 +168,6 @@ template("rust_test") { rebase_path(target_path), "--tg-timestamp-proxy-file", rebase_path(tg_timestamp_proxy_file), - "--unit-test-output-name", - invoker.unit_test_output_name, ] if (defined(invoker.integration_test_output_name) && @@ -267,13 +263,6 @@ template("rust_test") { target, ] - if (defined(invoker.through_real_test) && invoker.through_real_test) { - args += [ - "--through-real-test", - "True", - ] - } - forward_variables_from(invoker, [ "deps", diff --git a/build/common/rust/rust_build_tests.py b/build/common/rust/rust_build_tests.py index 335519b195..72de98d5ce 100644 --- a/build/common/rust/rust_build_tests.py +++ b/build/common/rust/rust_build_tests.py @@ -4,16 +4,16 @@ # information. # import argparse +import json import sys import os -import platform from build.scripts import cmd_exec, fs_utils, timestamp_proxy class ArgumentInfo(argparse.Namespace): def __init__(self): super().__init__() - self.through_real_test: bool + self.no_run: bool self.project_path: str self.build_type: str self.target_path: str @@ -21,15 +21,31 @@ def __init__(self): self.log_level: int self.test_output_dir: str self.tg_timestamp_proxy_file: str | None = None - self.unit_test_output_name: str self.integration_test_output_name: str | None = None +def get_crate_test_output_name(log_level: int) -> str: + cmd = ["cargo", "metadata", "--no-deps", "--format-version", "1"] + returncode, output = cmd_exec.run_cmd(cmd, log_level) + if returncode: + raise Exception(f"Failed to get crate name: {output}") + + # Get the last line of the output, as there might be some note or warning + # messages from cargo. + output = output.splitlines()[-1] + metadata = json.loads(output) + for target in metadata["packages"][0]["targets"]: + if target["test"]: + return target["name"] + + raise Exception("Failed to get crate name from targets.") + + def copy_test_binaries( - through_real_test: bool, source_dir: str, base_name: str, output_dir: str, + target_name: str, log_level: int = 0, ): if log_level > 0: @@ -37,52 +53,40 @@ def copy_test_binaries( f"Copying test binary {base_name} from {source_dir} to {output_dir}" ) - if through_real_test: - if platform.system() == "Windows": - executable_name = f"{base_name}.exe" - else: - executable_name = base_name + + for f in os.listdir(source_dir): + is_executable = os.access(os.path.join(source_dir, f), os.X_OK) + if not is_executable: + continue + + # The file name generated by `cargo build` always has a hash suffix, ex: + # -.. We need to remove the hash suffix. + + # We have to deal with the extension of the executable file, as if the + # `crate-type` is `cdylib`, the extension is `.so` on linux, which is + # not what we want. The extension is not present on linux or macos, and + # the extension is '.exe' on windows. + extension = os.path.splitext(f)[1] + if extension != "" and extension != ".exe": + continue + + binary_name = f[: f.rindex("-") if "-" in f else -1] + if binary_name != base_name: + continue + + dest = target_name + extension fs_utils.copy_file( - os.path.join(source_dir, executable_name), - os.path.join(output_dir, executable_name), + os.path.join(source_dir, f), + os.path.join(output_dir, dest), True, ) - else: - for f in os.listdir(source_dir): - is_executable = os.access(os.path.join(source_dir, f), os.X_OK) - if not is_executable: - continue - - # The file name generated by `cargo build` always has a hash suffix, - # ex: -.. We need to remove the hash - # suffix. - - # We have to deal with the extension of the executable file, as if - # the `crate-type` is `cdylib`, the extension is `.so` on linux, - # which is not what we want. The extension is not present on linux - # or macos, and the extension is '.exe' on windows. - extension = os.path.splitext(f)[1] - if extension != "" and extension != ".exe": - continue - - binary_name = f[: f.rindex("-") if "-" in f else -1] - if binary_name != base_name: - continue - - dest = binary_name + extension - - fs_utils.copy_file( - os.path.join(source_dir, f), - os.path.join(output_dir, dest), - True, - ) if __name__ == "__main__": parser = argparse.ArgumentParser() - parser.add_argument("--through-real-test", type=bool, default=False) + parser.add_argument("--no-run", type=bool, default=True) parser.add_argument("--project-path", type=str, required=True) parser.add_argument("--manifest-path", type=str, required=False) parser.add_argument("--build-type", type=str, required=True) @@ -96,7 +100,6 @@ def copy_test_binaries( parser.add_argument( "--tg-timestamp-proxy-file", type=str, default="", required=False ) - parser.add_argument("--unit-test-output-name", type=str, required=True) parser.add_argument( "--integration-test-output-name", type=str, required=False ) @@ -120,24 +123,30 @@ def copy_test_binaries( try: os.chdir(args.project_path) - if args.through_real_test: + # cargo build --tests: only compile the test source files, without + # running the test cases. + # cargo test: compile the test source files and run the test cases. + # + # `cargo build --tests` or `cargo test --no-run` will not trigger the + # `runner` script in .cargo/config.toml. + if args.no_run: cmd = [ "cargo", - "test", + "build", "--target-dir", args.target_path, "--target", args.target, + "--tests", ] else: cmd = [ "cargo", - "build", + "test", "--target-dir", args.target_path, "--target", args.target, - "--tests", ] if args.build_type == "release": @@ -149,28 +158,31 @@ def copy_test_binaries( else: print(logs) + # The prefix of the unit test binary is the crate name (i.e., the + # 'name' in [[bin]] or [[lib]] in a crate). + unit_test_output_name = get_crate_test_output_name(args.log_level) + # The output of the dependencies will be in / # /deps, while the output of the tests will be in / # //deps. if args.test_output_dir != "": # Copy the unit test binary. copy_test_binaries( - args.through_real_test, os.path.join( args.target_path, args.target, args.build_type, "deps", ), - args.unit_test_output_name, + unit_test_output_name, args.test_output_dir, + "unit_test", args.log_level, ) if args.integration_test_output_name: # Copy the integration test binary. copy_test_binaries( - args.through_real_test, os.path.join( args.target_path, args.target, @@ -179,6 +191,7 @@ def copy_test_binaries( ), args.integration_test_output_name, args.test_output_dir, + "integration_test", args.log_level, ) diff --git a/core/src/ten_manager/.cargo/config.toml b/core/src/ten_manager/.cargo/config.toml deleted file mode 100644 index 55e8540f5d..0000000000 --- a/core/src/ten_manager/.cargo/config.toml +++ /dev/null @@ -1,2 +0,0 @@ -[target.'cfg(test)'] -runner = "python ./tools/test_runner.py" diff --git a/core/src/ten_manager/BUILD.gn b/core/src/ten_manager/BUILD.gn index 553bdc1256..9f1e114daa 100644 --- a/core/src/ten_manager/BUILD.gn +++ b/core/src/ten_manager/BUILD.gn @@ -15,7 +15,6 @@ if (ten_enable_package_manager) { if (is_linux || is_win || (is_mac && target_cpu == "x64")) { rust_test("tman_test") { project_path = "//core/src/ten_manager" - unit_test_output_name = "unit_test" clingo_lib_folder = rebase_path("${root_out_dir}/gen/cmake/clingo/install/lib") @@ -26,7 +25,6 @@ if (ten_enable_package_manager) { env += [ "TEN_UTILS_LIBRARY_PATH=${utils_static_lib_path}" ] test_output_dir = "${root_out_dir}/tests/standalone/ten_manager" - through_real_test = true deps = [ "//core/src/ten_rust:ten_rust_static_lib", diff --git a/core/src/ten_manager/tools/test_runner.py b/core/src/ten_manager/tools/test_runner.py deleted file mode 100755 index 34d7730387..0000000000 --- a/core/src/ten_manager/tools/test_runner.py +++ /dev/null @@ -1,52 +0,0 @@ -# -# This file is part of the TEN Framework project. -# See https://github.com/TEN-framework/ten_framework/LICENSE for license -# information. -# -import os -import shutil -import sys -from pathlib import Path -import subprocess -import platform - - -def copy_executable_to_predefined_name(executable_path: str): - executable_dirname = Path(executable_path).parent - executable_filename = Path(executable_path).name - - # Determine if it's a unit test or an integration test based on the filename - # prefix - if executable_filename.startswith("integration_test"): - test_type = "integration_test" - elif executable_filename.startswith("tman"): - test_type = "unit_test" - else: - return - - if platform.system() == "Windows": - predefined_filename = f"{test_type}.exe" - else: - predefined_filename = test_type - - target_path = os.path.join(executable_dirname, predefined_filename) - shutil.copy(executable_path, target_path) - - -def main(executable_path: str, *args): - copy_executable_to_predefined_name(executable_path) - - # Prepare the command with arguments passed to the script. - command = [str(executable_path)] + list(args) - print("tman test command:", command) - - result = subprocess.run(command, capture_output=True, text=True) - - print("STDOUT:", result.stdout) - print("STDERR:", result.stderr) - - exit(result.returncode) - - -if __name__ == "__main__": - main(*sys.argv[1:]) diff --git a/core/src/ten_rust/.cargo/config.toml b/core/src/ten_rust/.cargo/config.toml deleted file mode 100644 index 55e8540f5d..0000000000 --- a/core/src/ten_rust/.cargo/config.toml +++ /dev/null @@ -1,2 +0,0 @@ -[target.'cfg(test)'] -runner = "python ./tools/test_runner.py" diff --git a/core/src/ten_rust/BUILD.gn b/core/src/ten_rust/BUILD.gn index b7c894cf3c..dad770ab62 100644 --- a/core/src/ten_rust/BUILD.gn +++ b/core/src/ten_rust/BUILD.gn @@ -16,9 +16,7 @@ if (enable_ten_rust) { rust_test("ten_rust_test") { project_path = "//core/src/ten_rust" test_output_dir = "${root_out_dir}/tests/standalone/ten_rust" - unit_test_output_name = "unit_test" integration_test_output_name = "integration_test" - through_real_test = true utils_static_lib_path = rebase_path("${root_gen_dir}/core/src/ten_utils") env = [ "TEN_UTILS_LIBRARY_PATH=${utils_static_lib_path}" ] diff --git a/core/src/ten_rust/tools/test_runner.py b/core/src/ten_rust/tools/test_runner.py deleted file mode 100755 index 0655b053a1..0000000000 --- a/core/src/ten_rust/tools/test_runner.py +++ /dev/null @@ -1,52 +0,0 @@ -# -# This file is part of the TEN Framework project. -# See https://github.com/TEN-framework/ten_framework/LICENSE for license -# information. -# -import os -import shutil -import sys -from pathlib import Path -import subprocess -import platform - - -def copy_executable_to_predefined_name(executable_path: str): - executable_dirname = Path(executable_path).parent - executable_filename = Path(executable_path).name - - # Determine if it's a unit test or an integration test based on the filename - # prefix - if executable_filename.startswith("integration_test"): - test_type = "integration_test" - elif executable_filename.startswith("ten_rust"): - test_type = "unit_test" - else: - return - - if platform.system() == "Windows": - predefined_filename = f"{test_type}.exe" - else: - predefined_filename = test_type - - target_path = os.path.join(executable_dirname, predefined_filename) - shutil.copy(executable_path, target_path) - - -def main(executable_path: str, *args): - copy_executable_to_predefined_name(executable_path) - - # Prepare the command with arguments passed to the script. - command = [str(executable_path)] + list(args) - print("ten_rust test command:", command) - - result = subprocess.run(command, capture_output=True, text=True) - - print("STDOUT:", result.stdout) - print("STDERR:", result.stderr) - - exit(result.returncode) - - -if __name__ == "__main__": - main(*sys.argv[1:])