Migrate run_mdbook_init_with_custom_book_and_src_locations to BookTest

This commit is contained in:
Eric Huss 2025-04-21 19:02:06 -07:00
parent 4019060ef4
commit 29338b5ade
2 changed files with 46 additions and 41 deletions

View file

@ -1,47 +1,7 @@
use mdbook::config::Config;
use mdbook::MDBook;
use pretty_assertions::assert_eq;
use std::fs;
use std::fs::File;
use std::io::prelude::*;
use std::path::PathBuf;
use tempfile::Builder as TempFileBuilder;
/// Set some custom arguments for where to place the source and destination
/// files, then call `mdbook init`.
#[test]
fn run_mdbook_init_with_custom_book_and_src_locations() {
let created_files = vec!["out", "in", "in/SUMMARY.md", "in/chapter_1.md"];
let temp = TempFileBuilder::new().prefix("mdbook").tempdir().unwrap();
for file in &created_files {
assert!(
!temp.path().join(file).exists(),
"{file} shouldn't exist yet!"
);
}
let mut cfg = Config::default();
cfg.book.src = PathBuf::from("in");
cfg.build.build_dir = PathBuf::from("out");
MDBook::init(temp.path()).with_config(cfg).build().unwrap();
for file in &created_files {
let target = temp.path().join(file);
assert!(
target.exists(),
"{file} should have been created by `mdbook init`"
);
}
let contents = fs::read_to_string(temp.path().join("book.toml")).unwrap();
assert_eq!(
contents,
"[book]\nauthors = []\nlanguage = \"en\"\nsrc = \"in\"\n\n[build]\nbuild-dir = \"out\"\ncreate-missing = true\nextra-watch-dirs = []\nuse-default-preprocessors = true\n"
);
}
#[test]
fn copy_theme() {
let temp = TempFileBuilder::new().prefix("mdbook").tempdir().unwrap();

View file

@ -1,7 +1,8 @@
//! Tests for `mdbook init`.
use crate::prelude::*;
use mdbook::MDBook;
use mdbook::{Config, MDBook};
use std::path::PathBuf;
// Tests "init" with no args.
#[test]
@ -162,3 +163,47 @@ fn init_from_summary() {
"#]],
);
}
// Set some custom arguments for where to place the source and destination
// files, then call `mdbook init`.
#[test]
fn init_with_custom_book_and_src_locations() {
let mut test = BookTest::empty();
let mut cfg = Config::default();
cfg.book.src = PathBuf::from("in");
cfg.build.build_dir = PathBuf::from("out");
MDBook::init(&test.dir).with_config(cfg).build().unwrap();
test.check_file(
"book.toml",
str![[r#"
[book]
authors = []
language = "en"
src = "in"
[build]
build-dir = "out"
create-missing = true
extra-watch-dirs = []
use-default-preprocessors = true
"#]],
)
.check_file(
"in/SUMMARY.md",
str![[r#"
# Summary
- [Chapter 1](./chapter_1.md)
"#]],
)
.check_file(
"in/chapter_1.md",
str![[r#"
# Chapter 1
"#]],
);
assert!(test.dir.join("out").exists());
}