requirements.cli.setup_project
Requirements index · View definition
Requirement
habeas setupMUST find an ancestor containing bothCargo.tomlandbook.toml, require a nonempty relative mdBook source path without parent components and aSUMMARY.mdat that path, and report an error when these prerequisites are absent or invalid.
Rationale
The three marked tests in tests/setup.rs cover each obligation in requirements.cli.setup_project.
Ancestor with both Cargo.toml and book.toml
setup_finds_the_nearest_cargo_and_mdbook_ancestorrunssetupfrom a nested directory with onlyCargo.toml, onlybook.toml, and neither. Each run must fail with “cannot find a project with Cargo.toml and book.toml”.- It also covers a split layout, where
Cargo.tomlis at the root andbook.tomlis inbook/while the command runs frombook/child. That run must fail with the same message. - Its positive case runs from
src/nestedinside a valid fixture. It asserts that outputs land in the project root (tests/habeas.rsandmd/habeas.css) and not in the nested directory. That shows the ancestor is found. - The requirement says “an ancestor”, not the nearest one, so no nearest-versus-outer-ancestor test is needed.
Nonempty, relative source path without parent components
setup_rejects_invalid_book_configurationchecks failure and the stderr message for a non-tablebook, a non-stringbook.src, an emptysrc, an absolute/outside, a leading../outside, and an embeddedmd/../outside. These cover the nonempty, relative and no-parent-components clauses.- Each case asserts both a non-success exit status and the expected message.
SUMMARY.md at the source path
- The same test removes
md/SUMMARY.mdand asserts failure with “cannot find mdBook SUMMARY.md”. setup_uses_src_when_book_table_or_source_setting_is_absentcovers the defaultsrcdirectory. With an emptybook.tomland with a[book]table lackingsrc, setup succeeds usingsrc/SUMMARY.md. Afterbook.srcis set to the integer 7, the session-start output advises running setup again.
Error reporting
- Every invalid case asserts a failing exit status and a specific stderr message.
I found no unaddressed obligation or boundary. The remaining book.toml cases in that test, for additional-css and fold, belong to other requirements.
Inputs
Tests
Input 2: outline tests/setup.rs fn[setup_uses_src_when_book_table_or_source_setting_is_absent]
- File:
tests/setup.rs - Selector:
fn[setup_uses_src_when_book_table_or_source_setting_is_absent] - Kind:
fn tests/setup.rs:425–446
// {test}`requirements.cli.setup_project`
// {test}`requirements.integration.symposium_setup_hook`
// {test}`requirements.cli.setup`
#[test]
fn setup_uses_src_when_book_table_or_source_setting_is_absent() {
for configuration in ["", "[book]\ntitle = 'Example'\n"] {
let temp = fixture();
let project = temp.path();
fs::write(project.join("book.toml"), configuration).unwrap();
fs::write(project.join("src/SUMMARY.md"), "# Summary\n").unwrap();
run(project, &["setup", "--path", env!("CARGO_MANIFEST_DIR")]);
assert!(project.join("src/habeas.css").is_file());
assert_eq!(session_start(project, project), skill_sentence_only());
let mut book: toml_edit::DocumentMut = fs::read_to_string(project.join("book.toml"))
.unwrap()
.parse()
.unwrap();
book["book"]["src"] = toml_edit::value(7);
fs::write(project.join("book.toml"), book.to_string()).unwrap();
assert!(advises_setup(&session_start(project, project)));
}
}
Input 3: outline tests/setup.rs fn[setup_rejects_invalid_book_configuration]
- File:
tests/setup.rs - Selector:
fn[setup_rejects_invalid_book_configuration] - Kind:
fn tests/setup.rs:448–505
// {test}`requirements.cli.setup_project`
// {test}`requirements.cli.setup_css`
// {test}`requirements.cli.setup`
#[test]
fn setup_rejects_invalid_book_configuration() {
let temp = fixture();
let project = temp.path();
let binary = env!("CARGO_BIN_EXE_habeas");
for (book, expected) in [
("book = 'wrong'\n", "book must be a table"),
("[book]\nsrc = 7\n", "book.src must be a string"),
("[book]\nsrc = ''\n", "book.src must be a directory"),
("[book]\nsrc = '/outside'\n", "book.src must be a directory"),
(
"[book]\nsrc = \"../outside\"\n",
"book.src must be a directory",
),
(
"[book]\nsrc = 'md/../outside'\n",
"book.src must be a directory",
),
(
"[book]\nsrc = \"md\"\n\n[output.html]\nadditional-css = \"wrong\"\n",
"output.html.additional-css must be an array",
),
(
"[book]\nsrc = \"md\"\n\n[output.html]\nfold = true\n",
"output.html.fold must be a table",
),
] {
fs::write(project.join("book.toml"), book).unwrap();
let output = Command::new(binary)
.args(["setup", "--path", env!("CARGO_MANIFEST_DIR")])
.current_dir(project)
.env("CARGO_NET_OFFLINE", "true")
.output()
.unwrap();
assert!(!output.status.success());
assert!(
String::from_utf8_lossy(&output.stderr).contains(expected),
"{:?}: {}",
book,
String::from_utf8_lossy(&output.stderr)
);
}
fs::write(project.join("book.toml"), "[book]\nsrc = \"md\"\n").unwrap();
fs::remove_file(project.join("md/SUMMARY.md")).unwrap();
let output = Command::new(binary)
.args(["setup", "--path", env!("CARGO_MANIFEST_DIR")])
.current_dir(project)
.env("CARGO_NET_OFFLINE", "true")
.output()
.unwrap();
assert!(!output.status.success());
assert!(String::from_utf8_lossy(&output.stderr).contains("cannot find mdBook SUMMARY.md"));
}
Input 4: outline tests/setup.rs fn[setup_finds_the_nearest_cargo_and_mdbook_ancestor]
- File:
tests/setup.rs - Selector:
fn[setup_finds_the_nearest_cargo_and_mdbook_ancestor] - Kind:
fn tests/setup.rs:756–813
// {test}`requirements.cli.setup_project`
#[test]
fn setup_finds_the_nearest_cargo_and_mdbook_ancestor() {
let binary = env!("CARGO_BIN_EXE_habeas");
for present in [
["Cargo.toml"].as_slice(),
["book.toml"].as_slice(),
[].as_slice(),
] {
let temp = TempDir::new().unwrap();
for file in present {
fs::write(temp.path().join(file), "").unwrap();
}
let nested = temp.path().join("nested");
fs::create_dir_all(&nested).unwrap();
let output = Command::new(binary)
.args(["setup", "--path", env!("CARGO_MANIFEST_DIR")])
.current_dir(&nested)
.env("CARGO_NET_OFFLINE", "true")
.output()
.unwrap();
assert!(!output.status.success(), "{present:?}");
assert!(
String::from_utf8_lossy(&output.stderr)
.contains("cannot find a project with Cargo.toml and book.toml"),
"{present:?}: {}",
String::from_utf8_lossy(&output.stderr)
);
}
let split = TempDir::new().unwrap();
fs::write(
split.path().join("Cargo.toml"),
"[package]\nname='split'\nversion='0.1.0'\n",
)
.unwrap();
fs::create_dir_all(split.path().join("book/child")).unwrap();
fs::write(split.path().join("book/book.toml"), "[book]\nsrc='md'\n").unwrap();
let result = Command::new(binary)
.arg("setup")
.current_dir(split.path().join("book/child"))
.output()
.unwrap();
assert!(!result.status.success());
assert!(
String::from_utf8_lossy(&result.stderr)
.contains("cannot find a project with Cargo.toml and book.toml")
);
let temp = fixture();
let project = temp.path();
let nested = project.join("src/nested");
fs::create_dir_all(&nested).unwrap();
run(&nested, &["setup", "--path", env!("CARGO_MANIFEST_DIR")]);
assert!(project.join("tests/habeas.rs").is_file());
assert!(project.join("md/habeas.css").is_file());
assert!(!nested.join("tests").exists());
}
Other sources
Input 1: requirements selection requirements.cli.setup_project
requirements.cli.setup_project:md/requirements/cli.md:565–567
:::{req} setup_project
`habeas setup` MUST find an ancestor containing both `Cargo.toml` and `book.toml`, require a nonempty relative mdBook source path without parent components and a `SUMMARY.md` at that path, and report an error when these prerequisites are absent or invalid.
:::
Input 5: outline tests/setup.rs fn[fixture]
- File:
tests/setup.rs - Selector:
fn[fixture] - Kind:
fn tests/setup.rs:9–27
// {cite}`habeas:util`
fn fixture() -> TempDir {
let temp = TempDir::new().unwrap();
fs::create_dir_all(temp.path().join("src")).unwrap();
fs::create_dir_all(temp.path().join("md")).unwrap();
fs::write(
temp.path().join("Cargo.toml"),
"[package]\nname = \"sample-project\"\nversion = \"0.1.0\"\nedition = \"2024\"\n",
)
.unwrap();
fs::write(temp.path().join("src/main.rs"), "fn main() {}\n").unwrap();
fs::write(
temp.path().join("book.toml"),
"[book]\nsrc = \"md\"\n\n[output.html]\nadditional-css = [\"md/custom.css\"]\n",
)
.unwrap();
fs::write(temp.path().join("md/SUMMARY.md"), "# Summary\n").unwrap();
temp
}