1use crate::context::Context;
14use std::{
15 panic::{UnwindSafe, catch_unwind, resume_unwind},
16 path::Path,
17 process::{Child, Output},
18};
19
20pub fn test_php_scripts(lib_path: impl AsRef<Path>, scripts: &[&dyn AsRef<Path>]) {
29 let condition = |output: Output| output.status.success();
30 let scripts = scripts
31 .iter()
32 .map(|s| (*s, &condition as _))
33 .collect::<Vec<_>>();
34 test_php_scripts_with_condition(lib_path, &scripts);
35}
36
37pub type ScriptCondition<'a> = (&'a dyn AsRef<Path>, &'a dyn Fn(Output) -> bool);
39
40pub fn test_php_scripts_with_condition(
51 lib_path: impl AsRef<Path>, scripts: &[ScriptCondition<'_>],
52) {
53 let context = Context::get_global();
54
55 for (script, condition) in scripts {
56 let mut cmd = context.create_command_with_lib(&lib_path, script);
57
58 let output = cmd.output().unwrap();
59 let path = script.as_ref().to_str().unwrap();
60
61 let mut stdout = String::from_utf8(output.stdout.clone()).unwrap();
62 if stdout.is_empty() {
63 stdout.push_str("<empty>");
64 }
65
66 let mut stderr = String::from_utf8(output.stderr.clone()).unwrap();
67 if stderr.is_empty() {
68 stderr.push_str("<empty>");
69 }
70
71 eprintln!(
72 "===== command =====\n{} {}\n===== stdout ======\n{}\n===== stderr ======\n{}",
73 &context.php_bin,
74 cmd.get_args().join(" "),
75 stdout,
76 stderr,
77 );
78 #[cfg(target_os = "linux")]
79 if output.status.code().is_none() {
80 use std::os::unix::process::ExitStatusExt;
81 eprintln!(
82 "===== signal ======\nExitStatusExt is None, the signal is: {:?}",
83 output.status.signal()
84 );
85 }
86
87 if !condition(output) {
88 panic!("test php file `{}` failed", path);
89 }
90 }
91}
92
93#[allow(clippy::zombie_processes)]
96pub fn test_long_term_php_script_with_condition(
97 lib_path: impl AsRef<Path>, script: impl AsRef<Path>,
98 condition: impl FnOnce(&Child) + UnwindSafe,
99) {
100 let context = Context::get_global();
101 let mut command = context.create_command_with_lib(lib_path, script);
102 let mut child = command.spawn().unwrap();
103 let r = catch_unwind(|| condition(&child));
104 child.kill().unwrap();
105 if let Err(e) = r {
106 resume_unwind(e);
107 }
108}