Skip to content

Commit 83b3b97

Browse files
committed
tests: use the Rust test runner instead of our manually generated one
This way, things like `#[cfg]` and `#[should_panic]` work. This seems simpler than trying to parse the `#[cfg]`s from Python and keeping the old way. This generates a much simpler `lib.rs` instead of a `main.rs` that tries to be its own simple test runner. Rust's test runner doesn't distinguish between expected and unexpected failures (it should counts an expected failure as a success), so we lose this output information, but an expected failure really is a success so this should be fine.
1 parent 9c72bd3 commit 83b3b97

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+162
-75
lines changed

scripts/test_translator.py

Lines changed: 34 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ def __init__(self, path: str, test_functions: Optional[List[TestFunction]] = Non
213213

214214

215215
class TestDirectory:
216+
rs_test_files: list[TestFile]
217+
216218
def __init__(self, full_path: str, files: 're.Pattern', keep: List[str], log_level: str) -> None:
217219
self.c_files = []
218220
self.rs_test_files = []
@@ -269,9 +271,8 @@ def __init__(self, full_path: str, files: 're.Pattern', keep: List[str], log_lev
269271
elif (filename.startswith("test_") and ext == ".rs" and
270272
files.search(filename)):
271273
rs_test_file = self._read_rust_test_file(path)
272-
273274
self.rs_test_files.append(rs_test_file)
274-
275+
275276
def _read_c_file(self, path: str) -> Optional[CFile]:
276277
file_config = None
277278
file_flags = set()
@@ -451,7 +452,6 @@ def run(self) -> List[TestOutcome]:
451452
rust_file_builder.add_mod(RustMod(extensionless_rust_file,
452453
RustVisibility.Public))
453454

454-
match_arms = []
455455
rustc_extra_args = ["-C", "target-cpu=native"]
456456

457457
# Build one binary that can call all the tests
@@ -462,6 +462,8 @@ def run(self) -> List[TestOutcome]:
462462

463463
_, file_name = os.path.split(test_file.path)
464464
extensionless_file_name, _ = os.path.splitext(file_name)
465+
if test_file.pass_expected:
466+
rust_file_builder.add_mod(RustMod(extensionless_file_name, RustVisibility.Private))
465467

466468
if not test_file.pass_expected:
467469
try:
@@ -484,32 +486,11 @@ def run(self) -> List[TestOutcome]:
484486

485487
continue
486488

487-
for test_function in test_file.test_functions:
488-
rust_file_builder.add_mod(RustMod(extensionless_file_name,
489-
RustVisibility.Public))
490-
left = "Some(\"{}::{}\")".format(extensionless_file_name,
491-
test_function.name)
492-
right = "{}::{}()".format(extensionless_file_name,
493-
test_function.name)
494-
match_arms.append((left, right))
495-
496-
match_arms.append(("e",
497-
"panic!(\"Tried to run unknown test: {:?}\", e)"))
498-
499-
test_main_body = [
500-
RustMatch("std::env::args().nth(1).as_ref().map(AsRef::<str>::as_ref)", match_arms),
501-
]
502-
test_main = RustFunction("main",
503-
visibility=RustVisibility.Public,
504-
body=[str(stmt) for stmt in test_main_body])
505-
506-
rust_file_builder.add_function(test_main)
507-
508-
main_file = rust_file_builder.build(self.full_path + "/src/main.rs")
489+
lib_file = rust_file_builder.build(self.full_path + "/src/lib.rs")
509490

510-
self.generated_files["rust_src"].append(main_file)
491+
self.generated_files["rust_src"].append(lib_file)
511492

512-
# Try and build test binary
493+
# Build
513494
with pb.local.cwd(self.full_path):
514495
args = ["build"]
515496

@@ -522,65 +503,45 @@ def run(self) -> List[TestOutcome]:
522503
retcode, stdout, stderr = cargo[args].run(retcode=None)
523504

524505
if retcode != 0:
525-
_, main_file_path_short = os.path.split(main_file.path)
506+
_, lib_file_path_short = os.path.split(lib_file.path)
526507

527-
self.print_status(Colors.FAIL, "FAILED", "compile {}".format(main_file_path_short))
508+
self.print_status(Colors.FAIL, "FAILED", "compile {}".format(lib_file_path_short))
528509
sys.stdout.write('\n')
529510
sys.stdout.write(stderr)
530511

531512
outcomes.append(TestOutcome.UnexpectedFailure)
532513

533514
return outcomes
534515

535-
for test_file in self.rs_test_files:
536-
if not test_file.pass_expected:
537-
continue
538-
539-
_, file_name = os.path.split(test_file.path)
540-
extensionless_file_name, _ = os.path.splitext(file_name)
541-
542-
for test_function in test_file.test_functions:
543-
args = ["run", "{}::{}".format(extensionless_file_name, test_function.name)]
544-
545-
if c.BUILD_TYPE == 'release':
546-
args.append('--release')
547-
548-
with pb.local.cwd(self.full_path):
549-
retcode, stdout, stderr = cargo[args].run(retcode=None)
550-
551-
logging.debug("stdout:%s\n", stdout)
552-
553-
test_str = file_name + ' - ' + test_function.name
554-
555-
if retcode == 0:
556-
if test_function.pass_expected:
557-
self.print_status(Colors.OKGREEN, "OK", " test " + test_str)
558-
sys.stdout.write('\n')
559-
560-
outcomes.append(TestOutcome.Success)
561-
else:
562-
self.print_status(Colors.FAIL, "FAILED", "test " + test_str)
563-
sys.stdout.write('\n')
516+
# Test
517+
with pb.local.cwd(self.full_path):
518+
args = ["test"]
564519

565-
outcomes.append(TestOutcome.UnexpectedSuccess)
520+
if c.BUILD_TYPE == 'release':
521+
args.append('--release')
566522

567-
elif retcode != 0:
568-
if test_function.pass_expected:
569-
self.print_status(Colors.FAIL, "FAILED", "test " + test_str)
570-
sys.stdout.write('\n')
571-
sys.stdout.write(stderr)
523+
if self.target:
524+
args += ["--target", self.target]
572525

573-
outcomes.append(TestOutcome.UnexpectedFailure)
574-
else:
575-
self.print_status(Colors.OKBLUE, "FAILED", "test " + test_str)
576-
sys.stdout.write('\n')
526+
retcode, stdout, stderr = cargo[args].run(retcode=None)
527+
528+
if retcode != 0:
529+
_, lib_file_path_short = os.path.split(lib_file.path)
577530

578-
outcomes.append(TestOutcome.Failure)
531+
self.print_status(Colors.FAIL, "FAILED", "test {}".format(lib_file_path_short))
532+
sys.stdout.write('\n')
533+
sys.stdout.write(stdout)
534+
else:
535+
sys.stdout.write("\n".join(line for line in stdout.split("\n") if "... ok" in line or "... FAILED" in line))
536+
537+
# Don't distinguish between expected and unexpected failures.
538+
# `#[should_panic]` is used for that instead of `// xfail` now.
539+
# Also, `cargo test -- --format json` is unstable, so it's easier to just parse very simply.
540+
for _ in range(stdout.count("... ok")):
541+
outcomes.append(TestOutcome.Success)
542+
for _ in range(stdout.count("... FAILED")):
543+
outcomes.append(TestOutcome.UnexpectedFailure)
579544

580-
if not outcomes:
581-
display_text = " No rust file(s) matching " + self.files.pattern
582-
display_text += " within this folder\n"
583-
self.print_status(Colors.OKBLUE, "N/A", display_text)
584545
return outcomes
585546

586547
def cleanup(self) -> None:

tests/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ extern "C" {
2323
// The length can be any value
2424
const BUFFER_SIZE: usize = 1024;
2525
26+
#[test]
2627
pub fn test_example() {
2728
let mut buffer = [0; BUFFER_SIZE];
2829
let mut rust_buffer = [0; BUFFER_SIZE];
@@ -42,7 +43,8 @@ The C code can do one of two things: modify some sort of buffer or return a valu
4243

4344
To completely skip the translation of a C file, you must add the comment `//! skip_translation` at the top of the file. That will prevent the case from showing up as red in the console output.
4445

45-
You can also mark a Rust file as unexpected to compile, by adding `//! xfail` to the top of the file, or just expect an individual test function to fail to run by adding `// xfail` prior to the function definition.
46+
You can also mark a Rust file as unexpected to compile by adding `//! xfail` to the top of the file.
47+
For an individual test function, use the normal Rust `#[should_panic]` for `#[test]`s.
4648

4749
Adding `//! extern_crate_X` to the top of a test file will ensure `extern crate X;` gets added to the main binary driver.
4850

tests/arrays/src/test_arrays.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,21 @@ const BUFFER_SIZE: usize = 49;
2727
const BUFFER_SIZE2: usize = 2;
2828
const BUFFER_SIZEV: usize = 88;
2929

30+
#[test]
3031
pub fn test_sized_array_impls() {
3132
unsafe {
3233
assert_eq!(rust_test_sized_array(), test_sized_array());
3334
}
3435
}
3536

37+
#[test]
3638
pub fn test_global_incomplete_array() {
3739
unsafe {
3840
assert_eq!(rust_check_some_ints(), check_some_ints());
3941
}
4042
}
4143

44+
#[test]
4245
pub fn test_buffer() {
4346
let mut buffer = [0; BUFFER_SIZE];
4447
let mut rust_buffer = [0; BUFFER_SIZE];
@@ -59,6 +62,7 @@ pub fn test_buffer() {
5962
}
6063
}
6164

65+
#[test]
6266
pub fn test_buffer2() {
6367
let mut buffer = [0; BUFFER_SIZE2];
6468
let mut rust_buffer = [0; BUFFER_SIZE2];
@@ -73,6 +77,7 @@ pub fn test_buffer2() {
7377
assert_eq!(buffer, expected_buffer);
7478
}
7579

80+
#[test]
7681
pub fn test_variable_arrays() {
7782
let mut buffer = [0; BUFFER_SIZEV];
7883
let mut rust_buffer = [0; BUFFER_SIZEV];
@@ -93,6 +98,7 @@ pub fn test_variable_arrays() {
9398
}
9499
}
95100

101+
#[test]
96102
pub fn test_alloca_arrays() {
97103
let mut buffer = [0; BUFFER_SIZEV];
98104
let mut rust_buffer = [0; BUFFER_SIZEV];

tests/asm.aarch64/src/test_asm.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ extern "C" {
1010

1111
const BUFFER_SIZE: usize = 6;
1212

13+
#[test]
1314
pub fn test_buffer() {
1415
let mut buffer = [0; BUFFER_SIZE];
1516
let mut rust_buffer = [0; BUFFER_SIZE];

tests/asm.arm/src/test_asm.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ extern "C" {
88

99
const BUFFER_SIZE: usize = 1;
1010

11+
#[test]
1112
pub fn test_buffer() {
1213
let mut buffer = [0; BUFFER_SIZE];
1314
let mut rust_buffer = [0; BUFFER_SIZE];

tests/asm.x86_64/src/test_asm.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ extern "C" {
1010

1111
const BUFFER_SIZE: usize = 6;
1212

13+
#[test]
1314
pub fn test_buffer() {
1415
let mut buffer = [0; BUFFER_SIZE];
1516
let mut rust_buffer = [0; BUFFER_SIZE];

tests/builtins/src/test_builtins.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ extern "C" {
2121
const BUFFER_SIZE: usize = 1024;
2222
const BUFFER_SIZE2: usize = 10;
2323

24+
#[test]
2425
pub fn test_atomics() {
2526
let mut buffer = [0; BUFFER_SIZE];
2627
let mut rust_buffer = [0; BUFFER_SIZE];
@@ -35,6 +36,7 @@ pub fn test_atomics() {
3536
}
3637
}
3738

39+
#[test]
3840
pub fn test_new_atomics() {
3941
let mut buffer = [0; BUFFER_SIZE];
4042
let mut rust_buffer = [0; BUFFER_SIZE];
@@ -50,6 +52,7 @@ pub fn test_new_atomics() {
5052
}
5153
}
5254

55+
#[test]
5356
pub fn test_mem_fns() {
5457
let const_string = "I am ten!\0";
5558
let mut buffer = [0; BUFFER_SIZE2];
@@ -66,6 +69,7 @@ pub fn test_mem_fns() {
6669
}
6770
}
6871

72+
#[test]
6973
pub fn test_ffs() {
7074
for i in 0..256 {
7175
let ffs_ret = unsafe { ffs(i) };
@@ -85,6 +89,7 @@ pub fn test_ffs() {
8589
}
8690
}
8791

92+
#[test]
8893
pub fn test_clang9_intrinsics() {
8994
let pinf = 1.0 / 0.0;
9095
let ninf = -1.0 / 0.0;
@@ -121,6 +126,7 @@ pub fn test_clang9_intrinsics() {
121126
}
122127
}
123128

129+
#[test]
124130
pub fn test_assume_aligned() {
125131
let null = std::ptr::null_mut();
126132

tests/casts/src/test_casts.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ extern "C" {
1818

1919
const BUFFER_SIZE: usize = 1;
2020

21+
#[test]
2122
pub fn test_compiles() {
2223
unsafe {
2324
cast_stuff();
2425
rust_cast_stuff();
2526
}
2627
}
2728

29+
#[test]
2830
pub fn test_buffer() {
2931
let mut buffer = [0; BUFFER_SIZE];
3032
let mut rust_buffer = [0; BUFFER_SIZE];
@@ -37,6 +39,7 @@ pub fn test_buffer() {
3739
assert_eq!(buffer, rust_buffer);
3840
}
3941

42+
#[test]
4043
pub fn test_identity() {
4144
for i in 0..10 {
4245
let id = unsafe { identity(i) };

tests/comments/src/test_comments.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::comments::{rust_test_fn, CONSTANT, CONSTANT1};
22

3+
#[test]
34
pub fn test_comments() {
45
let val = unsafe { rust_test_fn() };
56
assert_eq!(6, val);

tests/conditionals/src/test_conditionals.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const BUFFER_SIZE: usize = 4;
2626
const BUFFER_SIZE2: usize = 30;
2727
const BUFFER_SIZE3: usize = 6;
2828

29+
#[test]
2930
pub fn test_buffer() {
3031
let mut buffer = [0; BUFFER_SIZE];
3132
let mut rust_buffer = [0; BUFFER_SIZE];
@@ -40,6 +41,7 @@ pub fn test_buffer() {
4041
assert_eq!(buffer, expected_buffer);
4142
}
4243

44+
#[test]
4345
pub fn test_buffer2() {
4446
let mut buffer = [0; BUFFER_SIZE2];
4547
let mut rust_buffer = [0; BUFFER_SIZE2];
@@ -56,6 +58,7 @@ pub fn test_buffer2() {
5658
assert_eq!(buffer, expected_buffer);
5759
}
5860

61+
#[test]
5962
pub fn test_binary_conditionals() {
6063
let mut buffer = [0; BUFFER_SIZE3];
6164
let mut rust_buffer = [0; BUFFER_SIZE3];
@@ -70,6 +73,7 @@ pub fn test_binary_conditionals() {
7073
assert_eq!(buffer, expected_buffer);
7174
}
7275

76+
#[test]
7377
pub fn test_unused_conditional() {
7478
unsafe {
7579
assert_eq!(unused_conditional1(), rust_unused_conditional1());
@@ -78,6 +82,7 @@ pub fn test_unused_conditional() {
7882
}
7983
}
8084

85+
#[test]
8186
pub fn test_else_if_chain(){
8287
unsafe {
8388
assert_eq!(entry4(0) , rust_entry4(0));

0 commit comments

Comments
 (0)