Skip to content

Commit 974f417

Browse files
committed
Additional unit test
1 parent 5cf3329 commit 974f417

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ clap = { version = "4.3.19", features = ["derive"] }
1414

1515
[dev-dependencies]
1616
gag = "1.0"
17+
serial_test = "2.0"
1718
testdir = "0.8"
1819

1920
[profile.release]

build.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
fn main() {
2-
println!("cargo:rustc-env=RUST_TEST_NOCAPTURE=1");
2+
// This is needed to be able to test the println! output that some code produces
3+
println!("cargo:rustc-env=RUST_TEST_NOCAPTURE=1");
4+
5+
// // This is needed to avoid multiple tests to concurrently capture stdout
6+
// println!("cargo:rustc-env=RUST_TEST_THREADS=1");
37
}

src/xml_util.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,14 @@ impl XMLUtil {
191191
#[cfg(test)]
192192
mod tests {
193193
use super::XMLUtil;
194+
use serial_test::serial;
194195
use std::{fs, io};
195196
use std::path::Path;
196197
use testdir::testdir;
197198

198199
// Macro to wrap around any statement to capture stdout.
200+
// Note tests using this need to be annotated with #[serial] as multiple concurrent
201+
// redirections of stdout fail.
199202
macro_rules! capture_stdout {
200203
($test:expr) => {{
201204
use gag::BufferRedirect;
@@ -214,12 +217,22 @@ mod tests {
214217
}
215218

216219
#[test]
217-
fn test_cat() -> io::Result<()> {
220+
#[serial] // This test has to run serially to avoid multiple tests to capture stdout
221+
fn test_cat() {
218222
let out = capture_stdout!(XMLUtil::cat("./src/test/test_tree2", "my-file.docx"));
219223
assert!(out.contains("my-file.docx: Testing 123"));
220224
assert!(out.contains("my-file.docx: Here’s a hyperlink:"));
225+
}
221226

222-
Ok(())
227+
#[test]
228+
#[serial] // This test has to run serially to avoid multiple tests to capture stdout
229+
fn test_grep() {
230+
let out = capture_stdout!(XMLUtil::grep_xml("./src/test/test_tree2", "doc123.docx", "[oe]re"));
231+
assert!(out.contains("doc123.docx: And some some more text"));
232+
assert!(out.contains("doc123.docx: Something here"));
233+
assert!(out.contains("doc123.docx: Here’s a hyperlink:"));
234+
assert!(out.contains("doc123.docx: And here’s just some text:"));
235+
assert!(!out.contains("Target"));
223236
}
224237

225238
#[test]

0 commit comments

Comments
 (0)