|
| 1 | +use std::path::PathBuf; |
| 2 | +use std::process::Command; |
| 3 | +use std::{env, fs}; |
| 4 | + |
| 5 | +use libtest_mimic::{Arguments, Failed, Trial}; |
| 6 | + |
| 7 | +fn main() { |
| 8 | + if cfg!(target_os = "windows") { |
| 9 | + // Tests don't currently handle newline differences on Windows. |
| 10 | + return; |
| 11 | + } |
| 12 | + |
| 13 | + let args = Arguments::from_args(); |
| 14 | + libtest_mimic::run(&args, make_tests()).exit(); |
| 15 | +} |
| 16 | + |
| 17 | +fn make_tests() -> Vec<Trial> { |
| 18 | + let testinput = PathBuf::from("testinput/dwarf"); |
| 19 | + let testoutput = PathBuf::from("testoutput/dwarf"); |
| 20 | + |
| 21 | + let mut tests = Vec::new(); |
| 22 | + let mut dirs = vec![(testinput, testoutput)]; |
| 23 | + while let Some((in_dir, out_dir)) = dirs.pop() { |
| 24 | + for entry in out_dir.read_dir().unwrap() { |
| 25 | + let entry = entry.unwrap(); |
| 26 | + let out_path = entry.path(); |
| 27 | + let mut in_path = in_dir.clone(); |
| 28 | + in_path.push(entry.file_name()); |
| 29 | + |
| 30 | + let file_type = entry.file_type().unwrap(); |
| 31 | + if file_type.is_dir() { |
| 32 | + dirs.push((in_path, out_path)); |
| 33 | + } else if file_type.is_file() { |
| 34 | + tests.push(Trial::test( |
| 35 | + format!("addr2line -e {}", in_path.display()), |
| 36 | + move || run_test(in_path, out_path), |
| 37 | + )); |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + tests |
| 42 | +} |
| 43 | + |
| 44 | +fn run_test(in_path: PathBuf, out_path: PathBuf) -> Result<(), Failed> { |
| 45 | + let mut exe = env::current_exe().unwrap(); |
| 46 | + assert!(exe.pop()); |
| 47 | + if exe.file_name().unwrap().to_str().unwrap() == "deps" { |
| 48 | + assert!(exe.pop()); |
| 49 | + } |
| 50 | + exe.push("addr2line"); |
| 51 | + assert!(exe.is_file()); |
| 52 | + |
| 53 | + let mut cmd = Command::new(exe); |
| 54 | + cmd.env("RUST_BACKTRACE", "1"); |
| 55 | + cmd.arg("--exe").arg(in_path).arg("--all").arg("-afi"); |
| 56 | + |
| 57 | + let output = cmd.output().unwrap(); |
| 58 | + assert!(output.status.success()); |
| 59 | + let out_data = output.stdout; |
| 60 | + |
| 61 | + if env::var_os("ADDR2LINE_TESTOUTPUT_UPDATE").is_some() { |
| 62 | + fs::write(out_path, &out_data).unwrap(); |
| 63 | + return Ok(()); |
| 64 | + } |
| 65 | + |
| 66 | + let expect_out_data = fs::read(out_path).unwrap(); |
| 67 | + if out_data != expect_out_data { |
| 68 | + let out_str = String::from_utf8_lossy(&out_data); |
| 69 | + let expect_out_str = String::from_utf8_lossy(&expect_out_data); |
| 70 | + return Err( |
| 71 | + format!("output mismatch\nexpected:\n{expect_out_str}\nactual:\n{out_str}").into(), |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + Ok(()) |
| 76 | +} |
0 commit comments