bookyard 0.1.1

Build and locally edit a bookshelf for multiple mdBook projects.
use std::fs;

use assert_cmd::Command;
use bookyard_core::BookyardConfig;
use predicates::prelude::*;

#[test]
fn help_lists_command_descriptions() {
    let mut cmd = Command::cargo_bin("bookyard").unwrap();

    cmd.arg("help").assert().success().stdout(
        predicate::str::contains("new")
            .and(predicate::str::contains(
                "Create a new mdBook and add it to the shelf",
            ))
            .and(predicate::str::contains(
                "Add an existing mdBook to the shelf",
            )),
    );
}

#[test]
fn new_help_describes_arguments() {
    let mut cmd = Command::cargo_bin("bookyard").unwrap();

    cmd.args(["new", "--help"]).assert().success().stdout(
        predicate::str::contains("Path where the new mdBook will be created")
            .and(predicate::str::contains(
                "Logical folder path inside the shelf",
            ))
            .and(predicate::str::contains("Replace generated files if they already exist")),
    );
}

#[test]
fn new_creates_mdbook_skeleton_and_registers_it() {
    let dir = tempfile::tempdir().unwrap();

    Command::cargo_bin("bookyard")
        .unwrap()
        .current_dir(dir.path())
        .args(["init", "--title", "Docs"])
        .assert()
        .success();

    Command::cargo_bin("bookyard")
        .unwrap()
        .current_dir(dir.path())
        .args([
            "new",
            "books/rust-guide",
            "--folder",
            "Guides",
            "--title",
            "Rust Guide",
            "--tag",
            "rust",
        ])
        .assert()
        .success();

    let book_dir = dir.path().join("books/rust-guide");
    assert!(book_dir.join("book.toml").exists());
    assert!(book_dir.join("src/SUMMARY.md").exists());
    assert!(book_dir.join("src/index.md").exists());

    let book_toml = fs::read_to_string(book_dir.join("book.toml")).unwrap();
    assert!(book_toml.contains("title = \"Rust Guide\""));

    let summary = fs::read_to_string(book_dir.join("src/SUMMARY.md")).unwrap();
    assert!(summary.contains("- [Introduction](./index.md)"));

    let config = BookyardConfig::load_from(dir.path().join("bookyard.toml")).unwrap();
    assert_eq!(config.books.len(), 1);
    assert_eq!(config.books[0].id, "rust-guide");
    assert_eq!(config.books[0].title, "Rust Guide");
    assert_eq!(config.books[0].source, "books/rust-guide");
    assert_eq!(config.books[0].folders, vec!["Guides"]);
    assert_eq!(config.books[0].tags, vec!["rust"]);
}