mdbook/tests/testsuite/build.rs
Eric Huss 3e673ce424 Switch from log to tracing
This switches to using the tracing crate instead of log. Tracing
provides a lot of nice features which we can take advantage of moving
forward.

This also adjusts the output fairly significantly. This includes:

- Switched the environment variable from RUST_LOG to MDBOOK_LOG.
- Dropped the timestamp. I experimented with various different time
  displays, but ultimately decided to omit it for now. I don't think
  I've ever found it to be useful, and it takes up a very significant
  amount of space. It could potentially be useful for basic profiling,
  but I think there are other, better mechanisms for that. We could
  consider leveraging tracing itself for doing some basic profiling
  (like using something like tracing-chrome).
- Dropped the target unless MDBOOK_LOG is set. The target tends to be
  pretty noisy, and doesn't really convey much information unless you
  are debugging or otherwise trying to adjust the log output.
- Added color.
- Slightly reworked the way the error cause trace is displayed.
- Slightly changed the way html5ever filtering is done, as well as add
  handlebars to the list since they both are very noisy. You can
  override this now by explicitly listing them as targets.

I still expect that mdbook will eventually change how it displays things
to the console, possibly switching away from tracing and printing things
itself. However, that is a larger project for the future.
2025-09-12 06:13:45 -07:00

87 lines
2.5 KiB
Rust

//! General build tests.
//!
//! More specific tests should usually go into a module based on the feature.
//! This module should just have general build tests, or misc small things.
use crate::prelude::*;
// Simple smoke test that building works.
#[test]
fn basic_build() {
BookTest::from_dir("build/basic_build").run("build", |cmd| {
cmd.expect_stderr(str![[r#"
INFO Book building has started
INFO Running the html backend
INFO HTML book written to `[ROOT]/book`
"#]]);
});
}
// Ensure building fails if `create-missing` is false and one of the files does
// not exist.
#[test]
fn failure_on_missing_file() {
BookTest::from_dir("build/missing_file").run("build", |cmd| {
cmd.expect_failure().expect_stderr(str![[r#"
ERROR Chapter file not found, ./chapter_1.md
[TAB]Caused by: [NOT_FOUND]
"#]]);
});
}
// Ensure a missing file is created if `create-missing` is true.
#[test]
fn create_missing() {
let test = BookTest::from_dir("build/create_missing");
assert!(test.dir.join("src/SUMMARY.md").exists());
assert!(!test.dir.join("src/chapter_1.md").exists());
test.load_book();
assert!(test.dir.join("src/chapter_1.md").exists());
}
// Checks that it fails if the summary has a reserved filename.
#[test]
fn no_reserved_filename() {
BookTest::from_dir("build/no_reserved_filename").run("build", |cmd| {
cmd.expect_failure().expect_stderr(str![[r#"
INFO Book building has started
INFO Running the html backend
ERROR Rendering failed
[TAB]Caused by: print.md is reserved for internal use
"#]]);
});
}
// Build without book.toml should be OK.
#[test]
fn book_toml_isnt_required() {
let mut test = BookTest::init(|_| {});
std::fs::remove_file(test.dir.join("book.toml")).unwrap();
test.build();
test.check_main_file(
"book/chapter_1.html",
str![[r##"<h1 id="chapter-1"><a class="header" href="#chapter-1">Chapter 1</a></h1>"##]],
);
}
// Dest dir relative path behavior.
#[test]
fn dest_dir_relative_path() {
let mut test = BookTest::from_dir("build/basic_build");
let current_dir = test.dir.join("work");
std::fs::create_dir_all(&current_dir).unwrap();
test.run("build", |cmd| {
cmd.args(&["--dest-dir", "foo", ".."])
.current_dir(&current_dir)
.expect_stderr(str![[r#"
INFO Book building has started
INFO Running the html backend
INFO HTML book written to `[ROOT]/work/foo`
"#]]);
});
assert!(current_dir.join("foo/index.html").exists());
}