Skip to content

Commit 4637d5f

Browse files
committed
Add a test for a relative preprocessor path
1 parent 4bac548 commit 4637d5f

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

tests/testsuite/book_test.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,26 @@ impl BookTest {
247247
self
248248
}
249249

250+
/// Removes a file or directory relative to the test root.
251+
pub fn rm_r(&mut self, path: impl AsRef<Path>) -> &mut Self {
252+
let path = self.dir.join(path.as_ref());
253+
let meta = match path.symlink_metadata() {
254+
Ok(meta) => meta,
255+
Err(e) => panic!("failed to remove {path:?}, could not read: {e:?}"),
256+
};
257+
// There is a race condition between fetching the metadata and
258+
// actually performing the removal, but we don't care all that much
259+
// for our tests.
260+
if meta.is_dir() {
261+
if let Err(e) = std::fs::remove_dir_all(&path) {
262+
panic!("failed to remove {path:?}: {e:?}");
263+
}
264+
} else if let Err(e) = std::fs::remove_file(&path) {
265+
panic!("failed to remove {path:?}: {e:?}")
266+
}
267+
self
268+
}
269+
250270
/// Builds a Rust program with the given src.
251271
///
252272
/// The given path should be the path where to output the executable in
@@ -319,6 +339,12 @@ impl BookCommand {
319339
self
320340
}
321341

342+
/// Sets the directory used for running the command.
343+
pub fn current_dir<S: AsRef<std::path::Path>>(&mut self, path: S) -> &mut Self {
344+
self.dir = self.dir.join(path.as_ref());
345+
self
346+
}
347+
322348
/// Use this to debug a command.
323349
///
324350
/// Pass the value that you would normally pass to `RUST_LOG`, and this

tests/testsuite/preprocessor.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,56 @@ fn example_doesnt_support_not_supported() {
9595

9696
assert_eq!(got, false);
9797
}
98+
99+
// Checks the behavior of a relative path to a preprocessor.
100+
#[test]
101+
fn relative_command_path() {
102+
let mut test = BookTest::init(|_| {});
103+
test.rust_program(
104+
"preprocessors/my-preprocessor",
105+
r#"
106+
fn main() {
107+
let mut args = std::env::args().skip(1);
108+
if args.next().as_deref() == Some("supports") {
109+
std::fs::write("support-check", args.next().unwrap()).unwrap();
110+
return;
111+
}
112+
use std::io::Read;
113+
let mut s = String::new();
114+
std::io::stdin().read_to_string(&mut s).unwrap();
115+
std::fs::write("preprocessor-ran", "test").unwrap();
116+
println!("{{\"sections\": []}}");
117+
}
118+
"#,
119+
)
120+
.change_file(
121+
"book.toml",
122+
"[preprocessor.my-preprocessor]\n\
123+
command = 'preprocessors/my-preprocessor'\n",
124+
)
125+
.run("build", |cmd| {
126+
cmd.expect_stdout(str![""]).expect_stderr(str![[r#"
127+
[TIMESTAMP] [INFO] (mdbook_driver::mdbook): Book building has started
128+
[TIMESTAMP] [INFO] (mdbook_driver::mdbook): Running the html backend
129+
[TIMESTAMP] [INFO] (mdbook_html::html_handlebars::hbs_renderer): HTML book written to `[ROOT]/book`
130+
131+
"#]]);
132+
})
133+
.check_file("support-check", "html")
134+
.check_file("preprocessor-ran", "test")
135+
// Try again, but outside of the book root to check relative path behavior.
136+
.rm_r("support-check")
137+
.rm_r("preprocessor-ran")
138+
.run("build ..", |cmd| {
139+
cmd.current_dir(cmd.dir.join("src"))
140+
.expect_stdout(str![""])
141+
.expect_stderr(str![[r#"
142+
[TIMESTAMP] [INFO] (mdbook_driver::mdbook): Book building has started
143+
[TIMESTAMP] [WARN] (mdbook_driver::builtin_preprocessors::cmd): The command wasn't found, is the "my-preprocessor" preprocessor installed?
144+
[TIMESTAMP] [WARN] (mdbook_driver::builtin_preprocessors::cmd): [TAB]Command: preprocessors/my-preprocessor
145+
[TIMESTAMP] [INFO] (mdbook_driver::mdbook): Running the html backend
146+
[TIMESTAMP] [INFO] (mdbook_html::html_handlebars::hbs_renderer): HTML book written to `[ROOT]/src/../book`
147+
148+
"#]]);
149+
});
150+
}

0 commit comments

Comments
 (0)