| 1 |
use std::fs::read_to_string; |
| 2 |
use std::path::PathBuf; |
| 3 |
use std::process::Command; |
| 4 |
|
| 5 |
static CBINDGEN_PATH: &str = env!("CARGO_BIN_EXE_cbindgen"); |
| 6 |
|
| 7 |
fn test_project(project_path: &str) { |
| 8 |
let mut cmake_cmd = Command::new("cmake"); |
| 9 |
cmake_cmd.arg("--version"); |
| 10 |
cmake_cmd |
| 11 |
.output() |
| 12 |
.expect("CMake --version failed - Is CMake installed?"); |
| 13 |
|
| 14 |
let mut cmake_configure = Command::new("cmake"); |
| 15 |
let build_dir = PathBuf::from(project_path).join("build"); |
| 16 |
if build_dir.exists() { |
| 17 |
std::fs::remove_dir_all(&build_dir).expect("Failed to remove old build directory"); |
| 18 |
} |
| 19 |
let project_dir = PathBuf::from(project_path); |
| 20 |
|
| 21 |
let cbindgen_define = format!("-DCBINDGEN_PATH={}", CBINDGEN_PATH); |
| 22 |
cmake_configure |
| 23 |
.arg("-S") |
| 24 |
.arg(project_path) |
| 25 |
.arg("-B") |
| 26 |
.arg(&build_dir) |
| 27 |
.arg(cbindgen_define); |
| 28 |
let output = cmake_configure.output().expect("Failed to execute process"); |
| 29 |
let stdout_str = String::from_utf8(output.stdout).unwrap(); |
| 30 |
let stderr_str = String::from_utf8(output.stderr).unwrap(); |
| 31 |
assert!( |
| 32 |
output.status.success(), |
| 33 |
"Configuring test project failed: stdout: `{}`, stderr: `{}`", |
| 34 |
stdout_str, |
| 35 |
stderr_str |
| 36 |
); |
| 37 |
let depfile_path = build_dir.join("depfile.d"); |
| 38 |
assert!( |
| 39 |
!depfile_path.exists(), |
| 40 |
"depfile should not exist before building" |
| 41 |
); |
| 42 |
|
| 43 |
// Do the clean first build |
| 44 |
let mut cmake_build = Command::new("cmake"); |
| 45 |
cmake_build.arg("--build").arg(&build_dir); |
| 46 |
let output = cmake_build.output().expect("Failed to execute process"); |
| 47 |
assert!(output.status.success(), "Building test project failed"); |
| 48 |
let out_str = String::from_utf8(output.stdout).unwrap(); |
| 49 |
assert!( |
| 50 |
out_str.contains("Running cbindgen"), |
| 51 |
"cbindgen rule did not run. Output: {}", |
| 52 |
out_str |
| 53 |
); |
| 54 |
|
| 55 |
assert!( |
| 56 |
depfile_path.exists(), |
| 57 |
"depfile does not exist after building" |
| 58 |
); |
| 59 |
|
| 60 |
let expected_dependencies_filepath = PathBuf::from(project_path) |
| 61 |
.join("expectations") |
| 62 |
.join("dependencies"); |
| 63 |
assert!( |
| 64 |
expected_dependencies_filepath.exists(), |
| 65 |
"Test did not define expected dependencies. Please read the Readme.md" |
| 66 |
); |
| 67 |
let expected_deps = |
| 68 |
read_to_string(expected_dependencies_filepath).expect("Failed to read dependencies"); |
| 69 |
let depinfo = read_to_string(depfile_path).expect("Failed to read dependencies"); |
| 70 |
// Assumes a single rule in the file - all deps are listed to the rhs of the `:`. |
| 71 |
let actual_deps = depinfo.split(':').collect::<Vec<_>>()[1]; |
| 72 |
// Strip the line breaks. |
| 73 |
let actual_deps = actual_deps.replace("\\\n", " "); |
| 74 |
// I don't want to deal with supporting escaped whitespace when splitting at whitespace, |
| 75 |
// so the tests don't support being run in a directory containing whitespace. |
| 76 |
assert!( |
| 77 |
!actual_deps.contains("\\ "), |
| 78 |
"The tests directory may not contain any whitespace" |
| 79 |
); |
| 80 |
let dep_list: Vec<&str> = actual_deps.split_ascii_whitespace().collect(); |
| 81 |
let expected_dep_list: Vec<String> = expected_deps |
| 82 |
.lines() |
| 83 |
.map(|dep| project_dir.join(dep).to_str().unwrap().to_string()) |
| 84 |
.collect(); |
| 85 |
assert_eq!(dep_list, expected_dep_list); |
| 86 |
|
| 87 |
let output = cmake_build.output().expect("Failed to execute process"); |
| 88 |
assert!(output.status.success(), "Building test project failed"); |
| 89 |
let out_str = String::from_utf8(output.stdout).unwrap(); |
| 90 |
assert!( |
| 91 |
!out_str.contains("Running cbindgen"), |
| 92 |
"cbindgen rule ran on second build" |
| 93 |
); |
| 94 |
|
| 95 |
std::fs::remove_dir_all(build_dir).expect("Failed to remove old build directory"); |
| 96 |
() |
| 97 |
} |
| 98 |
|
| 99 |
macro_rules! test_file { |
| 100 |
($test_function_name:ident, $name:expr, $file:tt) => { |
| 101 |
#[test] |
| 102 |
fn $test_function_name() { |
| 103 |
test_project($file); |
| 104 |
} |
| 105 |
}; |
| 106 |
} |
| 107 |
|
| 108 |
// This file is generated by build.rs |
| 109 |
include!(concat!(env!("OUT_DIR"), "/depfile_tests.rs")); |