Migrate relative_command_path to BookTest

This commit is contained in:
Eric Huss 2025-04-22 08:52:34 -07:00
parent a38a30da1e
commit 2f10831a80
2 changed files with 54 additions and 57 deletions

View file

@ -1,57 +0,0 @@
//! Integration tests to make sure alternative backends work.
use mdbook::config::Config;
use mdbook::MDBook;
use std::fs;
use std::path::Path;
use tempfile::Builder as TempFileBuilder;
#[test]
fn relative_command_path() {
// Checks behavior of relative paths for the `command` setting.
let temp = TempFileBuilder::new().prefix("mdbook").tempdir().unwrap();
let renderers = temp.path().join("renderers");
fs::create_dir(&renderers).unwrap();
rust_exe(
&renderers,
"myrenderer",
r#"fn main() {
std::fs::write("output", "test").unwrap();
}"#,
);
let do_test = |cmd_path| {
let mut config = Config::default();
config
.set("output.html", toml::value::Table::new())
.unwrap();
config.set("output.myrenderer.command", cmd_path).unwrap();
let md = MDBook::init(temp.path())
.with_config(config)
.build()
.unwrap();
let output = temp.path().join("book/myrenderer/output");
assert!(!output.exists());
md.build().unwrap();
assert!(output.exists());
fs::remove_file(output).unwrap();
};
// Legacy paths work, relative to the output directory.
if cfg!(windows) {
do_test("../../renderers/myrenderer.exe");
} else {
do_test("../../renderers/myrenderer");
}
// Modern path, relative to the book directory.
do_test("renderers/myrenderer");
}
fn rust_exe(temp: &Path, name: &str, src: &str) {
let rs = temp.join(name).with_extension("rs");
fs::write(&rs, src).unwrap();
let status = std::process::Command::new("rustc")
.arg(rs)
.current_dir(temp)
.status()
.expect("rustc should run");
assert!(status.success());
}

View file

@ -208,3 +208,57 @@ fn backends_receive_render_context_via_stdin() {
let f = File::open(test.dir.join("book/out.txt")).unwrap();
RenderContext::from_json(f).unwrap();
}
// Legacy relative renderer paths.
//
// https://github.com/rust-lang/mdBook/pull/1418
#[test]
fn legacy_relative_command_path() {
let mut test = BookTest::init(|_| {});
test.rust_program(
"renderers/myrenderer",
r#"
fn main() {
use std::io::Read;
let mut s = String::new();
std::io::stdin().read_to_string(&mut s).unwrap();
std::fs::write("output", "test").unwrap();
}
"#,
)
// Test with a modern path, relative to the book directory.
.change_file(
"book.toml",
"[output.myrenderer]\n\
command = 'renderers/myrenderer'\n",
)
.run("build", |cmd| {
cmd.expect_stdout(str![[""]]).expect_stderr(str![[r#"
[TIMESTAMP] [INFO] (mdbook::book): Book building has started
[TIMESTAMP] [INFO] (mdbook::book): Running the myrenderer backend
[TIMESTAMP] [INFO] (mdbook::renderer): Invoking the "myrenderer" renderer
"#]]);
})
.check_file("book/output", "test");
std::fs::remove_file(test.dir.join("book/output")).unwrap();
// Test with legacy path, relative to the output directory.
test.change_file(
"book.toml",
&format!(
"[output.myrenderer]\n\
command = '../renderers/myrenderer{exe}'\n",
exe = std::env::consts::EXE_SUFFIX
),
)
.run("build", |cmd| {
cmd.expect_stdout(str![[""]]).expect_stderr(str![[r#"
[TIMESTAMP] [INFO] (mdbook::book): Book building has started
[TIMESTAMP] [INFO] (mdbook::book): Running the myrenderer backend
[TIMESTAMP] [INFO] (mdbook::renderer): Invoking the "myrenderer" renderer
[TIMESTAMP] [WARN] (mdbook::renderer): Renderer command `../renderers/myrenderer[EXE]` uses a path relative to the renderer output directory `[ROOT]/book`. This was previously accepted, but has been deprecated. Relative executable paths should be relative to the book root.
"#]]);
})
.check_file("book/output", "test");
}