chronogit 0.9.0

A terminal UI for Git history, diffs, and source code
Documentation
use std::process::Command;

#[test]
fn display_config_uses_xdg_or_explicit_path_and_fails_before_terminal_setup() {
    let directory = tempfile::tempdir().unwrap_or_else(|error| panic!("{error}"));
    let config_dir = directory.path().join("chronogit");
    std::fs::create_dir(&config_dir).unwrap_or_else(|error| panic!("{error}"));
    let path = config_dir.join("config.toml");
    for explicit in [false, true] {
        for source in ["scrolloff = 3", "scrolloff = -1"] {
            std::fs::write(&path, source).unwrap_or_else(|error| panic!("{error}"));
            let mut command = Command::new(env!("CARGO_BIN_EXE_chronogit"));
            command
                .arg(env!("CARGO_MANIFEST_DIR"))
                .env("XDG_CONFIG_HOME", directory.path());
            if explicit {
                command.arg("--config").arg(&path);
            }
            let output = command.output().unwrap_or_else(|error| panic!("{error}"));
            let stderr = String::from_utf8_lossy(&output.stderr);
            if source.ends_with("-1") {
                assert!(stderr.contains("display configuration failed"), "{stderr}");
                assert!(stderr.contains(&path.display().to_string()));
            } else {
                assert!(stderr.contains("interactive TTY"), "{stderr}");
            }
        }
    }
    std::fs::remove_file(&path).unwrap_or_else(|error| panic!("{error}"));
    let output = Command::new(env!("CARGO_BIN_EXE_chronogit"))
        .arg(env!("CARGO_MANIFEST_DIR"))
        .env("XDG_CONFIG_HOME", directory.path())
        .output()
        .unwrap_or_else(|error| panic!("{error}"));
    assert!(String::from_utf8_lossy(&output.stderr).contains("interactive TTY"));
}

#[test]
fn help_and_version_do_not_require_a_tty() {
    for argument in ["--help", "--version"] {
        let output = Command::new(env!("CARGO_BIN_EXE_chronogit"))
            .arg(argument)
            .output()
            .unwrap_or_else(|error| panic!("could not run chronogit {argument}: {error}"));
        assert!(output.status.success());
        assert!(String::from_utf8_lossy(&output.stdout).contains("chronogit"));
    }
}

#[test]
fn non_repository_is_a_contextual_error() {
    let directory = tempfile::tempdir()
        .unwrap_or_else(|error| panic!("could not create temporary directory: {error}"));
    let output = Command::new(env!("CARGO_BIN_EXE_chronogit"))
        .arg(directory.path())
        .output()
        .unwrap_or_else(|error| panic!("could not run chronogit: {error}"));
    assert!(!output.status.success());
    assert!(String::from_utf8_lossy(&output.stderr).contains("Git operation failed"));
}

#[test]
fn repository_without_tty_is_rejected_before_terminal_setup() {
    let output = Command::new(env!("CARGO_BIN_EXE_chronogit"))
        .arg(env!("CARGO_MANIFEST_DIR"))
        .output()
        .unwrap_or_else(|error| panic!("could not run chronogit: {error}"));
    assert!(!output.status.success());
    assert!(String::from_utf8_lossy(&output.stderr).contains("interactive TTY"));
}

#[test]
fn missing_git_executable_has_a_contextual_error_chain() {
    let output = Command::new(env!("CARGO_BIN_EXE_chronogit"))
        .arg(env!("CARGO_MANIFEST_DIR"))
        .env("PATH", "")
        .output()
        .unwrap_or_else(|error| panic!("could not run chronogit: {error}"));
    assert!(!output.status.success());
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(stderr.contains("Git operation failed"));
    assert!(stderr.contains("could not discover repository"));
}

#[cfg(unix)]
#[test]
fn permission_denied_git_executable_has_a_contextual_error_chain() {
    use std::fs;
    use std::os::unix::fs::PermissionsExt;

    let directory = tempfile::tempdir()
        .unwrap_or_else(|error| panic!("could not create temporary directory: {error}"));
    let git = directory.path().join("git");
    fs::write(&git, b"not executable")
        .unwrap_or_else(|error| panic!("could not create fake Git: {error}"));
    fs::set_permissions(&git, fs::Permissions::from_mode(0o600))
        .unwrap_or_else(|error| panic!("could not set fake Git permissions: {error}"));

    let output = Command::new(env!("CARGO_BIN_EXE_chronogit"))
        .arg(env!("CARGO_MANIFEST_DIR"))
        .env("PATH", directory.path())
        .output()
        .unwrap_or_else(|error| panic!("could not run chronogit: {error}"));
    assert!(!output.status.success());
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(stderr.contains("Git operation failed"));
    assert!(stderr.contains("could not discover repository"));
}

#[test]
fn invalid_explicit_keymap_is_reported_before_terminal_setup() {
    let directory = tempfile::tempdir()
        .unwrap_or_else(|error| panic!("could not create temporary directory: {error}"));
    let keymap = directory.path().join("keymap.conf");
    std::fs::write(&keymap, "not_an_action = x\n")
        .unwrap_or_else(|error| panic!("could not write keymap: {error}"));
    let output = Command::new(env!("CARGO_BIN_EXE_chronogit"))
        .arg(env!("CARGO_MANIFEST_DIR"))
        .arg("--keymap")
        .arg(&keymap)
        .output()
        .unwrap_or_else(|error| panic!("could not run chronogit: {error}"));
    assert!(!output.status.success());
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(stderr.contains("keymap configuration failed"));
    assert!(stderr.contains("unknown action"));
}

#[test]
fn invalid_explicit_lsp_config_is_reported_before_terminal_setup() {
    let directory = tempfile::tempdir()
        .unwrap_or_else(|error| panic!("could not create temporary directory: {error}"));
    let config = directory.path().join("lsp.toml");
    std::fs::write(
        &config,
        "[servers.unsafe]\nlanguage_id='x'\nextensions=['x']\ncommand=['server', '--root={workspace_root}']\n",
    )
    .unwrap_or_else(|error| panic!("could not write LSP config: {error}"));
    let output = Command::new(env!("CARGO_BIN_EXE_chronogit"))
        .arg(env!("CARGO_MANIFEST_DIR"))
        .arg("--lsp")
        .arg("unsafe")
        .arg("--lsp-config")
        .arg(&config)
        .output()
        .unwrap_or_else(|error| panic!("could not run chronogit: {error}"));
    assert!(!output.status.success());
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(stderr.contains("LSP configuration failed"));
    assert!(stderr.contains("placeholder"));
}