<commit_before>//! Integration tests to make sure alternate backends work.

extern crate mdbook;
extern crate tempfile;

use mdbook::config::Config;
use mdbook::MDBook;
#[cfg(not(windows))]
use std::path::Path;
use tempfile::{Builder as TempFileBuilder, TempDir};

#[test]
fn passing_alternate_backend() {
    let (md, _temp) = dummy_book_with_backend("passing", "true");

    md.build().unwrap();
}

#[test]
fn failing_alternate_backend() {
    let (md, _temp) = dummy_book_with_backend("failing", "false");

    md.build().unwrap_err();
}

#[test]
fn missing_backends_arent_fatal() {
    let (md, _temp) = dummy_book_with_backend("missing", "trduyvbhijnorgevfuhn");

    assert!(md.build().is_ok());
}

#[test]
fn alternate_backend_with_arguments() {
    let (md, _temp) = dummy_book_with_backend("arguments", "echo Hello World!");

    md.build().unwrap();
}

/// Get a command which will pipe `stdin` to the provided file.
#[cfg(not(windows))]
fn tee_command<P: AsRef<Path>>(out_file: P) -> String {
    let out_file = out_file.as_ref();

    if cfg!(windows) {
        format!("cmd.exe /c \"type > {}\"", out_file.display())
    } else {
        format!("tee {}", out_file.display())
    }
}

#[test]
#[cfg(not(windows))]
fn backends_receive_render_context_via_stdin() {
    use mdbook::renderer::RenderContext;
    use std::fs::File;

    let temp = TempFileBuilder::new().prefix("output").tempdir().unwrap();
    let out_file = temp.path().join("out.txt");
    let cmd = tee_command(&out_file);

    let (md, _temp) = dummy_book_with_backend("cat-to-file", &cmd);

    assert!(!out_file.exists());
    md.build().unwrap();
    assert!(out_file.exists());

    let got = RenderContext::from_json(File::open(&out_file).unwrap());
    assert!(got.is_ok());
}

fn dummy_book_with_backend(name: &str, command: &str) -> (MDBook, TempDir) {
    let temp = TempFileBuilder::new().prefix("mdbook").tempdir().unwrap();

    let mut config = Config::default();
    config
        .set(format!("output.{}.command", name), command)
        .unwrap();

    let md = MDBook::init(temp.path())
        .with_config(config)
        .build()
        .unwrap();

    (md, temp)
}<commit_msg>Make failing_alternate_backend test more platform specific<commit_after>//! Integration tests to make sure alternate backends work.

extern crate mdbook;
extern crate tempfile;

use mdbook::config::Config;
use mdbook::MDBook;
#[cfg(not(windows))]
use std::path::Path;
use tempfile::{Builder as TempFileBuilder, TempDir};

#[test]
fn passing_alternate_backend() {
    let (md, _temp) = dummy_book_with_backend("passing", success_cmd());

    md.build().unwrap();
}

#[test]
fn failing_alternate_backend() {
    let (md, _temp) = dummy_book_with_backend("failing", fail_cmd());

    md.build().unwrap_err();
}

#[test]
fn missing_backends_arent_fatal() {
    let (md, _temp) = dummy_book_with_backend("missing", "trduyvbhijnorgevfuhn");

    assert!(md.build().is_ok());
}

#[test]
fn alternate_backend_with_arguments() {
    let (md, _temp) = dummy_book_with_backend("arguments", "echo Hello World!");

    md.build().unwrap();
}

/// Get a command which will pipe `stdin` to the provided file.
#[cfg(not(windows))]
fn tee_command<P: AsRef<Path>>(out_file: P) -> String {
    let out_file = out_file.as_ref();

    if cfg!(windows) {
        format!("cmd.exe /c \"type > {}\"", out_file.display())
    } else {
        format!("tee {}", out_file.display())
    }
}

#[test]
#[cfg(not(windows))]
fn backends_receive_render_context_via_stdin() {
    use mdbook::renderer::RenderContext;
    use std::fs::File;

    let temp = TempFileBuilder::new().prefix("output").tempdir().unwrap();
    let out_file = temp.path().join("out.txt");
    let cmd = tee_command(&out_file);

    let (md, _temp) = dummy_book_with_backend("cat-to-file", &cmd);

    assert!(!out_file.exists());
    md.build().unwrap();
    assert!(out_file.exists());

    let got = RenderContext::from_json(File::open(&out_file).unwrap());
    assert!(got.is_ok());
}

fn dummy_book_with_backend(name: &str, command: &str) -> (MDBook, TempDir) {
    let temp = TempFileBuilder::new().prefix("mdbook").tempdir().unwrap();

    let mut config = Config::default();
    config
        .set(format!("output.{}.command", name), command)
        .unwrap();

    let md = MDBook::init(temp.path())
        .with_config(config)
        .build()
        .unwrap();

    (md, temp)
}

fn fail_cmd() -> &'static str {
    if cfg!(windows) {
        r#"cmd.exe /c "exit 1""#
    } else {
        "false"
    }
}

fn success_cmd() -> &'static str {
    if cfg!(windows) {
        r#"cmd.exe /c "exit 0""#
    } else {
        "true"
    }
}
{'fraction_non_alphanumeric': 0.1812727662309835, 'fraction_numerical': 0.00042854081851296334, 'mean_word_length': 3.8828451882845187, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5367265', 'n_tokens_mistral': 1637, 'n_tokens_neox': 1625, 'n_words': 329}