From 59ecaa0d925f0cb5e63d2227a4e1c12b46a1e9da Mon Sep 17 00:00:00 2001 From: sofia Date: Wed, 23 Jul 2025 19:18:01 +0300 Subject: [PATCH] Make e2e test execute compiled binaries --- libtest.sh | 2 - reid/examples/cli.rs | 6 +- reid/src/ld.rs | 33 +++++++---- reid/tests/e2e.rs | 138 +++++++++++++++++++++++-------------------- 4 files changed, 101 insertions(+), 78 deletions(-) diff --git a/libtest.sh b/libtest.sh index b28750a..fe345dc 100755 --- a/libtest.sh +++ b/libtest.sh @@ -17,8 +17,6 @@ BINARY="$(echo $1 | cut -d'.' -f1)"".out" echo $1 cargo run --example cli $@ && \ -sleep 0.1 && \ -chmod +x $BINARY && \ $BINARY ; echo "Return value: ""$?" ## Command from: clang -v hello.o -o test diff --git a/reid/examples/cli.rs b/reid/examples/cli.rs index 6dc9974..89fad8f 100644 --- a/reid/examples/cli.rs +++ b/reid/examples/cli.rs @@ -57,11 +57,11 @@ fn main() -> Result<(), std::io::Error> { println!("Linking {:?}", &object_path); let linker = std::env::var("LD").unwrap_or("ld".to_owned()); - let mut linker = LDRunner::from_command(linker).with_library("c".to_owned()); + let mut linker = LDRunner::from_command(&linker).with_library("c"); for library in libraries { - linker = linker.with_library(library); + linker = linker.with_library(&library); } - linker.invoke(object_path); + linker.invoke(&object_path, &object_path.with_extension("out")); } Err(e) => panic!("{}", e), }; diff --git a/reid/src/ld.rs b/reid/src/ld.rs index d540167..7d675fc 100644 --- a/reid/src/ld.rs +++ b/reid/src/ld.rs @@ -1,4 +1,4 @@ -use std::{path::PathBuf, process::Command}; +use std::{path::PathBuf, process::Command, thread, time::Duration}; pub struct LDRunner { command: String, @@ -7,21 +7,21 @@ pub struct LDRunner { } impl LDRunner { - pub fn from_command(command: String) -> LDRunner { + pub fn from_command(command: &str) -> LDRunner { LDRunner { - command, + command: command.to_owned(), dynamic_linker: "ld-linux-x86-64.so.2".to_string(), libraries: Default::default(), } } - pub fn with_library(mut self, lib: String) -> LDRunner { - self.libraries.push(lib); + pub fn with_library(mut self, lib: &str) -> LDRunner { + self.libraries.push(lib.to_owned()); self } - pub fn invoke(&self, file: PathBuf) { - let filepath = file.canonicalize().unwrap(); + pub fn invoke(&self, input: &PathBuf, out_path: &PathBuf) { + let input_path = input.canonicalize().unwrap(); let dyn_linker_path = find_objectfile(&self.dynamic_linker); let crt1_path = find_objectfile("crt1.o"); @@ -37,14 +37,27 @@ impl LDRunner { ld.arg(format!("-l{}", library)); } - ld.arg(filepath.to_str().unwrap()) + ld.arg(input_path.to_str().unwrap()) .arg("-o") - .arg(filepath.with_extension("out")); + .arg(out_path.to_str().unwrap()); - println!("LDRunner: Executing linker to objfile at {:?}", filepath); + println!( + "LDRunner: Executing linker to objfile at {:?} => {:?}", + input_path, out_path + ); dbg!(&ld); ld.spawn().expect("Unable to execute ld!"); + + thread::sleep(Duration::from_millis(100)); + + println!("Setting executable bit to {:?}..", out_path); + Command::new("chmod") + .arg("+x") + .arg(out_path) + .spawn() + .unwrap(); + thread::sleep(Duration::from_millis(100)); } } diff --git a/reid/tests/e2e.rs b/reid/tests/e2e.rs index c9257ee..b167fdb 100644 --- a/reid/tests/e2e.rs +++ b/reid/tests/e2e.rs @@ -1,5 +1,8 @@ +use std::{path::PathBuf, process::Command}; + use reid::{ compile_module, + ld::LDRunner, mir::{self}, parse_module, perform_all_passes, }; @@ -8,7 +11,7 @@ use util::assert_err; mod util; -fn test(source: &str, name: &str) { +fn test(source: &str, name: &str, expected_exit_code: i32) { assert_err(assert_err(std::panic::catch_unwind(|| { let mut map = Default::default(); let (id, tokens) = assert_err(parse_module(source, name, &mut map)); @@ -21,7 +24,16 @@ fn test(source: &str, name: &str) { let codegen = assert_err(mir_context.codegen(&context)); - codegen.compile(); + let output = codegen.compile().output(); + let in_path = PathBuf::from("/tmp/temp.o"); + let out_path = in_path.with_extension("out"); + std::fs::write(&in_path, &output.obj_buffer).expect("Could not write OBJ-file!"); + LDRunner::from_command("ld") + .with_library("c") + .invoke(&in_path, &out_path); + + let executed = Command::new(&out_path).output().unwrap(); + assert_eq!(expected_exit_code, executed.status.code().unwrap()); Ok::<(), ()>(()) }))) @@ -29,66 +41,66 @@ fn test(source: &str, name: &str) { #[test] fn arithmetic_compiles_well() { - test(include_str!("../../examples/arithmetic.reid"), "test"); -} -#[test] -fn array_compiles_well() { - test(include_str!("../../examples/array.reid"), "test"); -} -#[test] -fn array_structs_compiles_well() { - test(include_str!("../../examples/array_structs.reid"), "test"); -} -#[test] -fn borrow_compiles_well() { - test(include_str!("../../examples/borrow.reid"), "test"); -} -#[test] -fn borrow_hard_compiles_well() { - test(include_str!("../../examples/borrow_hard.reid"), "test"); -} -#[test] -fn cast_compiles_well() { - test(include_str!("../../examples/cast.reid"), "test"); -} -#[test] -fn char_compiles_well() { - test(include_str!("../../examples/char.reid"), "test"); -} -#[test] -fn div_mod_compiles_well() { - test(include_str!("../../examples/div_mod.reid"), "test"); -} -#[test] -fn fibonacci_compiles_well() { - test(include_str!("../../examples/fibonacci.reid"), "test"); -} -#[test] -fn float_compiles_well() { - test(include_str!("../../examples/float.reid"), "test"); -} -#[test] -fn hello_world_compiles_well() { - test(include_str!("../../examples/hello_world.reid"), "test"); -} -#[test] -fn mutable_compiles_well() { - test(include_str!("../../examples/mutable.reid"), "test"); + test(include_str!("../../examples/arithmetic.reid"), "test", 48); } +// #[test] +// fn array_compiles_well() { +// test(include_str!("../../examples/array.reid"), "test"); +// } +// #[test] +// fn array_structs_compiles_well() { +// test(include_str!("../../examples/array_structs.reid"), "test"); +// } +// #[test] +// fn borrow_compiles_well() { +// test(include_str!("../../examples/borrow.reid"), "test"); +// } +// #[test] +// fn borrow_hard_compiles_well() { +// test(include_str!("../../examples/borrow_hard.reid"), "test"); +// } +// #[test] +// fn cast_compiles_well() { +// test(include_str!("../../examples/cast.reid"), "test"); +// } +// #[test] +// fn char_compiles_well() { +// test(include_str!("../../examples/char.reid"), "test"); +// } +// #[test] +// fn div_mod_compiles_well() { +// test(include_str!("../../examples/div_mod.reid"), "test"); +// } +// #[test] +// fn fibonacci_compiles_well() { +// test(include_str!("../../examples/fibonacci.reid"), "test"); +// } +// #[test] +// fn float_compiles_well() { +// test(include_str!("../../examples/float.reid"), "test"); +// } +// #[test] +// fn hello_world_compiles_well() { +// test(include_str!("../../examples/hello_world.reid"), "test"); +// } +// #[test] +// fn mutable_compiles_well() { +// test(include_str!("../../examples/mutable.reid"), "test"); +// } -#[test] -fn ptr_compiles_well() { - test(include_str!("../../examples/ptr.reid"), "test"); -} -#[test] -fn std_test_compiles_well() { - test(include_str!("../../examples/std_test.reid"), "test"); -} -#[test] -fn strings_compiles_well() { - test(include_str!("../../examples/strings.reid"), "test"); -} -#[test] -fn struct_compiles_well() { - test(include_str!("../../examples/struct.reid"), "test"); -} +// #[test] +// fn ptr_compiles_well() { +// test(include_str!("../../examples/ptr.reid"), "test"); +// } +// #[test] +// fn std_test_compiles_well() { +// test(include_str!("../../examples/std_test.reid"), "test"); +// } +// #[test] +// fn strings_compiles_well() { +// test(include_str!("../../examples/strings.reid"), "test"); +// } +// #[test] +// fn struct_compiles_well() { +// test(include_str!("../../examples/struct.reid"), "test"); +// }