1use crate::context::Context;
14use std::{
15 panic::{catch_unwind, resume_unwind, UnwindSafe},
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(
52 lib_path: impl AsRef<Path>, scripts: &[ScriptCondition<'_>],
53) {
54 let context = Context::get_global();
55
56 for (script, condition) in scripts {
57 let mut cmd = context.create_command_with_lib(&lib_path, script);
58
59 let output = cmd.output().unwrap();
60 let path = script.as_ref().to_str().unwrap();
61
62 let mut stdout = String::from_utf8(output.stdout.clone()).unwrap();
63 if stdout.is_empty() {
64 stdout.push_str("<empty>");
65 }
66
67 let mut stderr = String::from_utf8(output.stderr.clone()).unwrap();
68 if stderr.is_empty() {
69 stderr.push_str("<empty>");
70 }
71
72 eprintln!(
73 "===== command =====\n{} {}\n===== stdout ======\n{}\n===== stderr ======\n{}",
74 &context.php_bin,
75 cmd.get_args().join(" "),
76 stdout,
77 stderr,
78 );
79 #[cfg(target_os = "linux")]
80 if output.status.code().is_none() {
81 use std::os::unix::process::ExitStatusExt;
82 eprintln!(
83 "===== signal ======\nExitStatusExt is None, the signal is: {:?}",
84 output.status.signal()
85 );
86 }
87
88 if !condition(output) {
89 panic!("test php file `{}` failed", path);
90 }
91 }
92}
93
94pub 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}