Skip to content

Commit faf303b

Browse files
committed
feat: Add logging and setup functions for improved debugging in tests
1 parent 8265ad4 commit faf303b

File tree

10 files changed

+251
-49
lines changed

10 files changed

+251
-49
lines changed

Cargo.lock

Lines changed: 141 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phper-test/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ license = { workspace = true }
2323
cargo_metadata = "0.20.0"
2424
fastcgi-client = "0.9.0"
2525
libc = "0.2.169"
26+
log = { version = "0.4.27", features = ["kv"] }
2627
phper-macros = { workspace = true }
2728
tempfile = "3.17.1"
2829
tokio = { version = "1.43.0", features = ["net"] }

phper-test/src/cargo.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//! Cargo build utilities for building and analyzing Rust libraries.
1212
1313
use cargo_metadata::Message;
14+
use log::debug;
1415
use std::{
1516
io::{self, BufReader},
1617
path::{Path, PathBuf},
@@ -60,6 +61,7 @@ impl CargoBuilder {
6061
let reader = BufReader::new(stdout);
6162
let mut messages = Vec::new();
6263
for message in cargo_metadata::Message::parse_stream(reader) {
64+
debug!(message:?; "cargo build message");
6365
let message = message?;
6466
messages.push(message);
6567
}

phper-test/src/cli.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//! Test tools for php cli program.
1212
1313
use crate::context::Context;
14+
use log::debug;
1415
use std::{
1516
panic::{UnwindSafe, catch_unwind, resume_unwind},
1617
path::Path,
@@ -72,31 +73,32 @@ pub fn test_php_scripts_with_condition(
7273
let output = cmd.output().unwrap();
7374
let path = script.as_ref().to_str().unwrap();
7475

75-
let mut stdout = String::from_utf8(output.stdout.clone()).unwrap();
76+
let mut stdout = String::from_utf8_lossy(&output.stdout).to_string();
7677
if stdout.is_empty() {
7778
stdout.push_str("<empty>");
7879
}
7980

80-
let mut stderr = String::from_utf8(output.stderr.clone()).unwrap();
81+
let mut stderr = String::from_utf8_lossy(&output.stderr).to_string();
8182
if stderr.is_empty() {
8283
stderr.push_str("<empty>");
83-
}
84+
};
8485

85-
eprintln!(
86-
"===== command =====\n{} {}\n===== stdout ======\n{}\n===== stderr ======\n{}",
87-
&context.php_bin,
88-
cmd.get_args().join(" "),
89-
stdout,
90-
stderr,
91-
);
92-
#[cfg(target_os = "linux")]
93-
if output.status.code().is_none() {
94-
use std::os::unix::process::ExitStatusExt;
95-
eprintln!(
96-
"===== signal ======\nExitStatusExt is None, the signal is: {:?}",
97-
output.status.signal()
98-
);
99-
}
86+
debug!(command:% = cmd.get_command().join(" ".as_ref()).to_string_lossy(),
87+
status:? = output.status.code(),
88+
stdout = &*stdout,
89+
stderr:%,
90+
signal:? = {
91+
#[cfg(unix)]
92+
{
93+
use std::os::unix::process::ExitStatusExt as _;
94+
output.status.signal()
95+
}
96+
#[cfg(not(unix))]
97+
{
98+
None
99+
}
100+
};
101+
"execute php test command");
100102

101103
if !condition(output) {
102104
panic!("test php file `{}` failed", path);

phper-test/src/context.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use crate::utils;
1212
use std::{
1313
env,
14+
ffi::OsStr,
1415
fs::read_to_string,
1516
io::Write,
1617
ops::{Deref, DerefMut},
@@ -125,6 +126,14 @@ pub struct ContextCommand {
125126
}
126127

127128
impl ContextCommand {
129+
pub fn get_command(&self) -> Vec<&OsStr> {
130+
let program = self.cmd.get_program();
131+
let args = self.cmd.get_args();
132+
let mut command = vec![program];
133+
command.extend(args);
134+
command
135+
}
136+
128137
pub fn get_args(&self) -> &[String] {
129138
&self.args
130139
}

phper-test/src/fpm.rs

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
use crate::{context::Context, utils::spawn_command};
1313
use fastcgi_client::{Client, Params, Request};
1414
use libc::{SIGTERM, atexit, kill, pid_t};
15+
use log::debug;
1516
use std::{
17+
borrow::Cow,
1618
fs,
1719
path::Path,
1820
process::Child,
@@ -82,13 +84,11 @@ impl FpmHandle {
8284
"-y",
8385
&fpm_conf_file.path().display().to_string(),
8486
];
85-
eprintln!("===== setup php-fpm =====");
86-
eprintln!("{}", argv.join(" "));
87+
debug!(argv:% = argv.join(" "); "setup php-fpm");
8788

8889
let child = spawn_command(&argv, Some(Duration::from_secs(3)));
8990
let log = fs::read_to_string("/tmp/.php-fpm.log").unwrap();
90-
eprintln!("===== php-fpm log =====");
91-
eprintln!("{}", log);
91+
debug!(log:%; "php-fpm log");
9292
// fs::remove_file("/tmp/.php-fpm.log").unwrap();
9393

9494
let handle = FpmHandle {
@@ -191,24 +191,16 @@ impl FpmHandle {
191191

192192
let no_error = stderr.is_empty();
193193

194-
let f = |out: Vec<u8>| {
195-
String::from_utf8(out)
196-
.map(|out| {
197-
if out.is_empty() {
198-
"<empty>".to_owned()
199-
} else {
200-
out
201-
}
202-
})
203-
.unwrap_or_else(|_| "<not utf8 string>".to_owned())
194+
let f = |out| {
195+
let out = String::from_utf8_lossy(out);
196+
if out.is_empty() {
197+
Cow::Borrowed("<empty>")
198+
} else {
199+
out
200+
}
204201
};
205202

206-
eprintln!("===== request =====");
207-
eprintln!("{}", request_uri);
208-
eprintln!("===== stdout ======");
209-
eprintln!("{}", f(stdout));
210-
eprintln!("===== stderr ======");
211-
eprintln!("{}", f(stderr));
203+
debug!(uri:% = request_uri, stdout:% = f(&stdout), stderr:% = f(&stderr); "test php request");
212204

213205
assert!(no_error, "request not success: {}", request_uri);
214206
}

tests/integration/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ indexmap = "2.7.1"
2525
phper = { workspace = true }
2626

2727
[dev-dependencies]
28+
env_logger = { version = "0.11.8", features = ["kv"] }
29+
log = { version = "0.4.27", features = ["kv"] }
2830
phper-test = { workspace = true }
2931
tokio = { version = "1.43.0", features = ["full"] }
3032

0 commit comments

Comments
 (0)