Skip to content

Commit

Permalink
update: set output name of unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
leoadonia committed Sep 13, 2024
1 parent dfca881 commit 3536302
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 172 deletions.
11 changes: 0 additions & 11 deletions build/common/rust/rust.gni
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) &&
Expand Down Expand Up @@ -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",
Expand Down
111 changes: 62 additions & 49 deletions build/common/rust/rust_build_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,85 +4,89 @@
# 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
self.env: list[str]
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:
print(
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:
# <library_name>-<hash>.<extension>. 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: <library_name>-<hash>.<extension>. 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)
Expand All @@ -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
)
Expand All @@ -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":
Expand All @@ -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 <target_path>/<build_type>
# /deps, while the output of the tests will be in <target_path>/<target>
# /<build_type>/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,
Expand All @@ -179,6 +191,7 @@ def copy_test_binaries(
),
args.integration_test_output_name,
args.test_output_dir,
"integration_test",
args.log_level,
)

Expand Down
2 changes: 0 additions & 2 deletions core/src/ten_manager/.cargo/config.toml

This file was deleted.

2 changes: 0 additions & 2 deletions core/src/ten_manager/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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",
Expand Down
52 changes: 0 additions & 52 deletions core/src/ten_manager/tools/test_runner.py

This file was deleted.

2 changes: 0 additions & 2 deletions core/src/ten_rust/.cargo/config.toml

This file was deleted.

2 changes: 0 additions & 2 deletions core/src/ten_rust/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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}" ]
Expand Down
52 changes: 0 additions & 52 deletions core/src/ten_rust/tools/test_runner.py

This file was deleted.

0 comments on commit 3536302

Please sign in to comment.