Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

requirements.cli.setup_local_path

Requirements index · View definition

evidence Valid

Requirement

When given --path <crate-dir> and no existing Habeas dev dependency, habeas setup MUST add that local crate as the dev dependency. Repeating setup MUST NOT add a duplicate Habeas dependency.

Rationale

The tests cover every obligation in requirements.cli.setup_local_path.

Obligations:

  1. With --path <crate-dir> and no existing Habeas dev dependency, setup adds that local crate as the dev dependency.
  2. Repeating setup does not add a duplicate Habeas dependency.
  3. The “no existing Habeas dev dependency” condition limits when setup adds anything.

Coverage:

  • Adds the local crate. tests/setup.rs#fn[local_path_setup_adds_exactly_that_crate_once] starts from a fixture manifest with no Habeas dependency. It runs setup --path with a relative path on unix and an absolute path elsewhere. It uses habeas_dependencies to check that exactly one habeas entry exists across dependencies, dev-dependencies and build-dependencies. It checks that the entry is in dev-dependencies and has a path key. It also checks that the path resolves, from the project directory, to the canonicalized CARGO_MANIFEST_DIR. That confirms the crate added is the one named.
  • No duplicate on repeat. The same test runs setup a second time with the absolute path and asserts the Cargo.toml text is byte-identical to the first result. tests/setup.rs#fn[setup_is_repeatable] also runs setup twice, but its assertions on the dependency are weak: it only checks that [dev-dependencies], habeas and path = appear. The first test carries the real check.
  • Existing dev dependency. tests/setup.rs#fn[local_path_setup_keeps_an_existing_habeas_dev_dependency] pre-seeds habeas = "0.1" under [dev-dependencies]. It runs setup with --path and asserts the manifest is unchanged. So no second dependency is added and the existing one is not replaced.
  • Invalid path (an extra boundary). tests/setup.rs#fn[local_path_setup_rejects_a_missing_crate_directory] passes a nonexistent directory. It asserts that setup fails, the manifest is unchanged, and no Habeas dependency exists.

The existing-dependency test uses only one form, a version string. A different existing form, such as a path dependency or a table form, belongs to the same input class. The requirement draws no distinction between forms, so this is not a gap.

The helpers fixture, run and habeas_dependencies are sound. run asserts a successful exit.

Inputs

Tests

Input 2: outline tests/setup.rs fn[local_path_setup_adds_exactly_that_crate_once]
// {test}`requirements.cli.setup_local_path`
#[test]
fn local_path_setup_adds_exactly_that_crate_once() {
    let local_crate = fs::canonicalize(env!("CARGO_MANIFEST_DIR")).unwrap();
    let temp = fixture();
    let project = temp.path();
    // A relative `--path` is resolved from the invocation directory.
    let relative: std::path::PathBuf = if cfg!(unix) {
        let up: std::path::PathBuf = project
            .canonicalize()
            .unwrap()
            .components()
            .skip(1)
            .map(|_| "..")
            .collect();
        up.join(local_crate.strip_prefix("/").unwrap())
    } else {
        local_crate.clone()
    };
    assert!(cfg!(not(unix)) || relative.is_relative());
    run(project, &["setup", "--path", relative.to_str().unwrap()]);
    let first = fs::read_to_string(project.join("Cargo.toml")).unwrap();
    let dependencies = habeas_dependencies(project);
    assert_eq!(dependencies.len(), 1, "{first}");
    let (table, item) = &dependencies[0];
    assert_eq!(table, "dev-dependencies", "{first}");
    let path = item
        .get("path")
        .and_then(|path| path.as_str())
        .unwrap_or_else(|| panic!("habeas has a path: {first}"));
    assert_eq!(
        fs::canonicalize(project.join(path)).unwrap(),
        local_crate,
        "{first}"
    );

    // Repeating setup leaves the manifest exactly as it was.
    run(project, &["setup", "--path", local_crate.to_str().unwrap()]);
    assert_eq!(
        fs::read_to_string(project.join("Cargo.toml")).unwrap(),
        first
    );
}
Input 3: outline tests/setup.rs fn[local_path_setup_keeps_an_existing_habeas_dev_dependency]
  • File: tests/setup.rs
  • Selector: fn[local_path_setup_keeps_an_existing_habeas_dev_dependency]
  • Kind: fn
  • tests/setup.rs:300–313
// {test}`requirements.cli.setup_local_path`
#[test]
fn local_path_setup_keeps_an_existing_habeas_dev_dependency() {
    let temp = fixture();
    let project = temp.path();
    let mut manifest = fs::read_to_string(project.join("Cargo.toml")).unwrap();
    manifest.push_str("\n[dev-dependencies]\nhabeas = \"0.1\"\n");
    fs::write(project.join("Cargo.toml"), &manifest).unwrap();
    run(project, &["setup", "--path", env!("CARGO_MANIFEST_DIR")]);
    assert_eq!(
        fs::read_to_string(project.join("Cargo.toml")).unwrap(),
        manifest
    );
}
Input 4: outline tests/setup.rs fn[local_path_setup_rejects_a_missing_crate_directory]
  • File: tests/setup.rs
  • Selector: fn[local_path_setup_rejects_a_missing_crate_directory]
  • Kind: fn
  • tests/setup.rs:315–333
// {test}`requirements.cli.setup_local_path`
#[test]
fn local_path_setup_rejects_a_missing_crate_directory() {
    let temp = fixture();
    let project = temp.path();
    let manifest = fs::read_to_string(project.join("Cargo.toml")).unwrap();
    let output = Command::new(env!("CARGO_BIN_EXE_habeas"))
        .args(["setup", "--path", "no-such-crate"])
        .current_dir(project)
        .env("CARGO_NET_OFFLINE", "true")
        .output()
        .unwrap();
    assert!(!output.status.success(), "{output:?}");
    assert_eq!(
        fs::read_to_string(project.join("Cargo.toml")).unwrap(),
        manifest
    );
    assert!(habeas_dependencies(project).is_empty());
}
Input 5: outline tests/setup.rs fn[setup_is_repeatable]
// {test}`requirements.cli.setup`
// {test}`requirements.cli.setup_css`
// {test}`requirements.cli.setup_local_path`
// {test}`requirements.cli.mdbook`
// {test}`requirements.index.detail_pages`
#[test]
fn setup_is_repeatable() {
    let temp = fixture();
    let project = temp.path();
    let local_crate = env!("CARGO_MANIFEST_DIR");

    run(project, &["setup", "--path", local_crate]);
    assert!(!project.join(".agents/skills").exists());
    run(project, &["setup", "--path", local_crate]);

    let cargo = fs::read_to_string(project.join("Cargo.toml")).unwrap();
    assert!(cargo.contains("[dev-dependencies]"));
    assert!(cargo.contains("habeas"));
    assert!(cargo.contains("path = "));
    let book = fs::read_to_string(project.join("book.toml")).unwrap();
    assert!(book.contains("command = \"habeas mdbook\""));
    assert!(book.contains("md/habeas.css"));
    assert!(book.contains("md/custom.css"));
    assert_eq!(book.matches("md/habeas.css").count(), 1);
    assert_eq!(
        fs::read_to_string(project.join("tests/habeas.rs")).unwrap(),
        "#[test]\nfn habeas() {\n    habeas::validate();\n}\n"
    );
    assert!(project.join("md/habeas.css").is_file());
    assert!(!project.join(".agents/skills").exists());
    let book: toml_edit::DocumentMut = book.parse().unwrap();
    assert_eq!(
        book["output"]["html"]["fold"]["enable"].as_bool(),
        Some(true)
    );
    assert_eq!(book.to_string().matches("enable").count(), 1, "{book}");
}

Other sources

Input 1: requirements selection requirements.cli.setup_local_path
:::{req} setup_local_path
When given `--path <crate-dir>` and no existing Habeas dev dependency, `habeas setup` MUST add that local crate as the dev dependency. Repeating setup MUST NOT add a duplicate Habeas dependency.
:::
Input 6: outline tests/setup.rs fn[fixture]
// {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
}
Input 7: outline tests/setup.rs fn[run]
// {cite}`habeas:util`
fn run(project: &Path, args: &[&str]) {
    let output = Command::new(env!("CARGO_BIN_EXE_habeas"))
        .args(args)
        .current_dir(project)
        .env("CARGO_NET_OFFLINE", "true")
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "habeas {:?} failed:\n{}",
        args,
        String::from_utf8_lossy(&output.stderr)
    );
}
Input 8: outline tests/setup.rs fn[habeas_dependencies]
// {cite}`habeas:util`
/// Every `habeas` entry in the manifest's dependency tables.
fn habeas_dependencies(project: &Path) -> Vec<(String, toml_edit::Item)> {
    let manifest: toml_edit::DocumentMut = fs::read_to_string(project.join("Cargo.toml"))
        .unwrap()
        .parse()
        .unwrap();
    ["dependencies", "dev-dependencies", "build-dependencies"]
        .into_iter()
        .filter_map(|table| {
            let item = manifest.get(table)?.get("habeas")?.clone();
            Some((table.to_owned(), item))
        })
        .collect()
}