From a7542ec544027383d9047598c012d503e418526f Mon Sep 17 00:00:00 2001 From: Chirag Dhawan Date: Fri, 3 Apr 2026 20:00:27 -0700 Subject: [PATCH 1/7] Allow test_translator.py to use an explicit transpiler path of the c2rust-transpile's debug build. --- ...test_translator_uses_current_transpiler.rs | 20 +++++++++++++++++++ scripts/test_translator.py | 14 +++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 c2rust/tests/test_translator_uses_current_transpiler.rs diff --git a/c2rust/tests/test_translator_uses_current_transpiler.rs b/c2rust/tests/test_translator_uses_current_transpiler.rs new file mode 100644 index 0000000000..44a331cd8d --- /dev/null +++ b/c2rust/tests/test_translator_uses_current_transpiler.rs @@ -0,0 +1,20 @@ +use std::process::Command; + +#[test] +fn test_translator_uses_current_transpiler() { + let transpile_path = env!("CARGO_BIN_EXE_c2rust-transpile"); + + let status = Command::new("python") + .arg("../scripts/test_translator.py") + .arg("../tests/unit") + .arg("--transpiler") + .arg(transpile_path) + .status() + .expect("failed to run python script"); + + assert!( + status.success(), + "test_translator.py failed with status: {}", + status + ); +} diff --git a/scripts/test_translator.py b/scripts/test_translator.py index dbb0a1ea62..2ec9dcf698 100755 --- a/scripts/test_translator.py +++ b/scripts/test_translator.py @@ -578,7 +578,7 @@ def run(self) -> List[TestOutcome]: args += ["--target", self.target] retcode, stdout, stderr = cargo[args].run(retcode=None) - + if retcode != 0: _, lib_file_path_short = os.path.split(lib_file.path) @@ -591,7 +591,7 @@ def run(self) -> List[TestOutcome]: if "... ok" in line: self.print_status(Colors.OKGREEN, "OK", "{}".format(line)) sys.stdout.write('\n') - + # Don't distinguish between expected and unexpected failures. # `#[should_panic]` is used for that instead of `// xfail` now. # Also, `cargo test -- --format json` is unstable, so it's easier to just parse very simply. @@ -654,6 +654,10 @@ def main() -> None: desc = 'run regression / unit / feature tests.' parser = argparse.ArgumentParser(description=desc) parser.add_argument('directory', type=readable_directory) + parser.add_argument( + '--transpiler', dest='transpiler', + default=None, help='Override the path to the c2rust transpiler binary' + ) parser.add_argument( '--only-files', dest='regex_files', type=regex, default='.*', help="Regular expression to filter which tests to run" @@ -681,6 +685,10 @@ def main() -> None: args = parser.parse_args() c.update_args(args) + + if args.transpiler is not None: + c.TRANSPILER = args.transpiler + test_directories = get_testdirectories(args.directory, args.regex_files, args.keep, @@ -689,11 +697,13 @@ def main() -> None: logging.debug("args: %s", " ".join(sys.argv)) + # Set whether we are using nix. C2RUST_USE_NIX=args.use_nix # check that the binaries have been built first bins = [c.TRANSPILER] + for b in bins: if not os.path.isfile(b): msg = b + " not found; run cargo build --release first?" From 99d6bab6cce090243a4296785b3dd7226c89c4ff Mon Sep 17 00:00:00 2001 From: Chirag Dhawan Date: Wed, 8 Apr 2026 18:26:11 -0700 Subject: [PATCH 2/7] Addressed feedbacks and comments --- c2rust/tests/test_translator.rs | 16 +++++++++++++++ ...test_translator_uses_current_transpiler.rs | 20 ------------------- scripts/test_translator.py | 2 -- 3 files changed, 16 insertions(+), 22 deletions(-) create mode 100644 c2rust/tests/test_translator.rs delete mode 100644 c2rust/tests/test_translator_uses_current_transpiler.rs diff --git a/c2rust/tests/test_translator.rs b/c2rust/tests/test_translator.rs new file mode 100644 index 0000000000..b0235b5a8a --- /dev/null +++ b/c2rust/tests/test_translator.rs @@ -0,0 +1,16 @@ +use std::process::Command; + +#[test] +fn test_translator() { + let transpile_path = env!("CARGO_BIN_EXE_c2rust-transpile"); + + let mut cmd = Command::new("../scripts/test_translator.py"); + cmd.args(["../tests/unit", "--transpiler"]); + cmd.arg(transpile_path); + + let status = cmd + .status() + .unwrap_or_else(|e| panic!("{cmd:?} failed: {e}")); + + assert!(status.success(), "{cmd:?} failed with {status}"); +} diff --git a/c2rust/tests/test_translator_uses_current_transpiler.rs b/c2rust/tests/test_translator_uses_current_transpiler.rs deleted file mode 100644 index 44a331cd8d..0000000000 --- a/c2rust/tests/test_translator_uses_current_transpiler.rs +++ /dev/null @@ -1,20 +0,0 @@ -use std::process::Command; - -#[test] -fn test_translator_uses_current_transpiler() { - let transpile_path = env!("CARGO_BIN_EXE_c2rust-transpile"); - - let status = Command::new("python") - .arg("../scripts/test_translator.py") - .arg("../tests/unit") - .arg("--transpiler") - .arg(transpile_path) - .status() - .expect("failed to run python script"); - - assert!( - status.success(), - "test_translator.py failed with status: {}", - status - ); -} diff --git a/scripts/test_translator.py b/scripts/test_translator.py index 2ec9dcf698..5d6544c440 100755 --- a/scripts/test_translator.py +++ b/scripts/test_translator.py @@ -697,13 +697,11 @@ def main() -> None: logging.debug("args: %s", " ".join(sys.argv)) - # Set whether we are using nix. C2RUST_USE_NIX=args.use_nix # check that the binaries have been built first bins = [c.TRANSPILER] - for b in bins: if not os.path.isfile(b): msg = b + " not found; run cargo build --release first?" From 6db274d7001abab7ba6dceae3be53619de6fc0e9 Mon Sep 17 00:00:00 2001 From: Chirag Dhawan Date: Wed, 8 Apr 2026 18:42:06 -0700 Subject: [PATCH 3/7] Addressed feedbacks and comments. Whitespace changes also addressed. --- scripts/test_translator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/test_translator.py b/scripts/test_translator.py index 5d6544c440..848b237025 100755 --- a/scripts/test_translator.py +++ b/scripts/test_translator.py @@ -591,7 +591,7 @@ def run(self) -> List[TestOutcome]: if "... ok" in line: self.print_status(Colors.OKGREEN, "OK", "{}".format(line)) sys.stdout.write('\n') - + # Don't distinguish between expected and unexpected failures. # `#[should_panic]` is used for that instead of `// xfail` now. # Also, `cargo test -- --format json` is unstable, so it's easier to just parse very simply. From 55ccd9d9593382d7becfd852d1f982d9e7edb42d Mon Sep 17 00:00:00 2001 From: Chirag Dhawan Date: Wed, 8 Apr 2026 18:57:07 -0700 Subject: [PATCH 4/7] Addressed feedbacks and comments. All whitespace changes finally addressed. --- scripts/test_translator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/test_translator.py b/scripts/test_translator.py index 848b237025..65d2a99fdd 100755 --- a/scripts/test_translator.py +++ b/scripts/test_translator.py @@ -578,7 +578,7 @@ def run(self) -> List[TestOutcome]: args += ["--target", self.target] retcode, stdout, stderr = cargo[args].run(retcode=None) - + if retcode != 0: _, lib_file_path_short = os.path.split(lib_file.path) From 49b42378eb9e13205e5dfa5082906863e2220953 Mon Sep 17 00:00:00 2001 From: Chirag Dhawan Date: Thu, 9 Apr 2026 19:43:31 -0700 Subject: [PATCH 5/7] Suggested changes to test_translator.rs done. --- c2rust/tests/test_translator.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/c2rust/tests/test_translator.rs b/c2rust/tests/test_translator.rs index b0235b5a8a..2b33336fe6 100644 --- a/c2rust/tests/test_translator.rs +++ b/c2rust/tests/test_translator.rs @@ -5,8 +5,10 @@ fn test_translator() { let transpile_path = env!("CARGO_BIN_EXE_c2rust-transpile"); let mut cmd = Command::new("../scripts/test_translator.py"); - cmd.args(["../tests/unit", "--transpiler"]); - cmd.arg(transpile_path); + cmd.args(["../tests/unit", "--transpiler", transpile_path]); + + // `test_translator.py` needs to be able to re-select the toolchain for different editions. + cmd.env_remove("RUSTUP_TOOLCHAIN"); let status = cmd .status() From 92c3e846b97f712ef688c98079fb58519b60190c Mon Sep 17 00:00:00 2001 From: Chirag Dhawan Date: Tue, 21 Apr 2026 18:09:45 -0700 Subject: [PATCH 6/7] README Comments added as well --- docs/README-developers.md | 2 ++ scripts/run_ci_checks.sh | 2 ++ tests/unit/README.md | 2 ++ 3 files changed, 6 insertions(+) diff --git a/docs/README-developers.md b/docs/README-developers.md index 071aa02692..0a3b408922 100644 --- a/docs/README-developers.md +++ b/docs/README-developers.md @@ -89,6 +89,8 @@ More details about tests can be found in the [tests folder](../tests/). *Note*: These run integration tests that invoke `c2rust transpile`. `cargo test` only runs unit tests and doc tests as of now. +*Note*: The tests executed by `test_translator.py` can also be invoked simply by running `cargo nextest run` in the main c2rust workspace. `cargo nextest run` even ensures that the repo tests use the latest version of `c2rust-transpile` binary. + ## Documentation Local documentation can be built with the normal `cargo doc` and `cargo doc --open`. diff --git a/scripts/run_ci_checks.sh b/scripts/run_ci_checks.sh index 293b51596f..fab20ddd2e 100755 --- a/scripts/run_ci_checks.sh +++ b/scripts/run_ci_checks.sh @@ -40,6 +40,8 @@ test() { cargo test --release --workspace } +# The tests executed by `test_translator.py` can also be invoked simply by running `cargo nextest run` in the main c2rust workspace. +# `cargo nextest run` even ensures that the repo tests use the latest version of `c2rust-transpile` binary # `test_translator.py` compiles translated code, # which has tons of warnings. # `RUSTFLAGS="-D warnings"` would be inherited by that, diff --git a/tests/unit/README.md b/tests/unit/README.md index 675fc64d1c..266fb5f9fc 100644 --- a/tests/unit/README.md +++ b/tests/unit/README.md @@ -66,6 +66,8 @@ $ ./scripts/test_translator.py --keep=all tests/unit $ ./scripts/test_translator.py --help ``` +*Note*: The tests executed by `test_translator.py` can also be invoked simply by running `cargo nextest run` in the main c2rust workspace. `cargo nextest run` even ensures that the repo tests use the latest version of `c2rust-transpile` binary + ## What happens under the hood This `tests/unit` directory contains regression, feature, and unit tests. A test directory goes through the following set of steps: From 7cce002364014816e99c0be1b9f437f1b32f8545 Mon Sep 17 00:00:00 2001 From: Chirag Dhawan Date: Mon, 4 May 2026 19:09:36 -0700 Subject: [PATCH 7/7] Comments changed --- docs/README-developers.md | 27 ++++++++++++++++++++++++++- tests/unit/README.md | 2 +- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/docs/README-developers.md b/docs/README-developers.md index 0a3b408922..39fd6d5b2f 100644 --- a/docs/README-developers.md +++ b/docs/README-developers.md @@ -89,7 +89,32 @@ More details about tests can be found in the [tests folder](../tests/). *Note*: These run integration tests that invoke `c2rust transpile`. `cargo test` only runs unit tests and doc tests as of now. -*Note*: The tests executed by `test_translator.py` can also be invoked simply by running `cargo nextest run` in the main c2rust workspace. `cargo nextest run` even ensures that the repo tests use the latest version of `c2rust-transpile` binary. +Most tests are found in the [`tests`](../tests/) folder. + +Most tests can also be run with: + +```sh +cargo nextest run +``` + +which includes running the [`tests/unit`](../tests/unit) tests. +The tests in `tests/unit` can also be run manually using: + +```sh +./scripts/test_translator.py tests/unit +``` + +which also allows you to run with other options. + +The [`tests/integration`](../tests/integration) tests aren't run automatically by `cargo nextest run`. To run them, run: + +```sh +# Needs to be run from `tests/integration/` (or further inside) +# to correctly load the `pyproject.toml`. +cargo build --release +(export PATH=$PWD/target/release:$PATH && export C2RUST_DIR=$PWD && cd tests/integration && ./test.py curl json-c lua nginx zstd li +bxml2 python2 libmcs) +``` ## Documentation diff --git a/tests/unit/README.md b/tests/unit/README.md index 266fb5f9fc..111f2ca2f1 100644 --- a/tests/unit/README.md +++ b/tests/unit/README.md @@ -66,7 +66,7 @@ $ ./scripts/test_translator.py --keep=all tests/unit $ ./scripts/test_translator.py --help ``` -*Note*: The tests executed by `test_translator.py` can also be invoked simply by running `cargo nextest run` in the main c2rust workspace. `cargo nextest run` even ensures that the repo tests use the latest version of `c2rust-transpile` binary +*Note*: The tests in [`tests/unit`](../tests/unit) can also be executed by running `cargo nextest run` in the main c2rust workspace. ## What happens under the hood