smirrors 0.1.0

Automatic mirror list updater for Linux distributions
Documentation
use assert_cmd::Command;
use predicates::prelude::*;
use tempfile::TempDir;

#[test]
fn test_cli_version() {
    let mut cmd = Command::cargo_bin("smirrors").unwrap();
    cmd.arg("--version")
        .assert()
        .success()
        .stdout(predicate::str::contains(env!("CARGO_PKG_VERSION")));
}

#[test]
fn test_cli_help() {
    let mut cmd = Command::cargo_bin("smirrors").unwrap();
    cmd.arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains("Automatic mirror list updater"));
}

#[test]
fn test_cli_help_test_command() {
    let mut cmd = Command::cargo_bin("smirrors").unwrap();
    cmd.arg("test")
        .arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains("Test mirrors without updating"));
}

#[test]
fn test_cli_help_update_command() {
    let mut cmd = Command::cargo_bin("smirrors").unwrap();
    cmd.arg("update")
        .arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains("Update mirror list"));
}

#[test]
fn test_cli_help_config_command() {
    let mut cmd = Command::cargo_bin("smirrors").unwrap();
    cmd.arg("config")
        .arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains("Configuration management"));
}

#[test]
fn test_init_command() {
    let temp_dir = TempDir::new().unwrap();
    let config_path = temp_dir.path().join("config.toml");

    let mut cmd = Command::cargo_bin("smirrors").unwrap();
    cmd.arg("--config")
        .arg(&config_path)
        .arg("init")
        .assert()
        .success();

    // Config file should be created
    assert!(config_path.exists());
}

#[test]
fn test_config_show() {
    let temp_dir = TempDir::new().unwrap();
    let config_path = temp_dir.path().join("config.toml");

    // First initialize
    let mut init_cmd = Command::cargo_bin("smirrors").unwrap();
    init_cmd
        .arg("--config")
        .arg(&config_path)
        .arg("init")
        .assert()
        .success();

    // Then show config
    let mut cmd = Command::cargo_bin("smirrors").unwrap();
    cmd.arg("--config")
        .arg(&config_path)
        .arg("config")
        .arg("show")
        .assert()
        .success()
        .stdout(predicate::str::contains("[general]"));
}

#[test]
fn test_config_get() {
    let temp_dir = TempDir::new().unwrap();
    let config_path = temp_dir.path().join("config.toml");

    // Initialize
    let mut init_cmd = Command::cargo_bin("smirrors").unwrap();
    init_cmd
        .arg("--config")
        .arg(&config_path)
        .arg("init")
        .assert()
        .success();

    // Get a value
    let mut cmd = Command::cargo_bin("smirrors").unwrap();
    cmd.arg("--config")
        .arg(&config_path)
        .arg("config")
        .arg("get")
        .arg("general.timeout")
        .assert()
        .success()
        .stdout(predicate::str::contains("10"));
}

#[test]
fn test_config_set() {
    let temp_dir = TempDir::new().unwrap();
    let config_path = temp_dir.path().join("config.toml");

    // Initialize
    let mut init_cmd = Command::cargo_bin("smirrors").unwrap();
    init_cmd
        .arg("--config")
        .arg(&config_path)
        .arg("init")
        .assert()
        .success();

    // Set a value
    let mut cmd = Command::cargo_bin("smirrors").unwrap();
    cmd.arg("--config")
        .arg(&config_path)
        .arg("config")
        .arg("set")
        .arg("general.timeout")
        .arg("20")
        .assert()
        .success();

    // Verify the change
    let mut get_cmd = Command::cargo_bin("smirrors").unwrap();
    get_cmd
        .arg("--config")
        .arg(&config_path)
        .arg("config")
        .arg("get")
        .arg("general.timeout")
        .assert()
        .success()
        .stdout(predicate::str::contains("20"));
}

#[test]
fn test_config_validate_default() {
    let temp_dir = TempDir::new().unwrap();
    let config_path = temp_dir.path().join("config.toml");

    // Initialize
    let mut init_cmd = Command::cargo_bin("smirrors").unwrap();
    init_cmd
        .arg("--config")
        .arg(&config_path)
        .arg("init")
        .assert()
        .success();

    // Validate
    let mut cmd = Command::cargo_bin("smirrors").unwrap();
    cmd.arg("--config")
        .arg(&config_path)
        .arg("config")
        .arg("validate")
        .assert()
        .success()
        .stdout(predicate::str::contains("valid"));
}

#[test]
fn test_list_command_empty() {
    let temp_dir = TempDir::new().unwrap();
    let config_path = temp_dir.path().join("config.toml");

    // Initialize
    let mut init_cmd = Command::cargo_bin("smirrors").unwrap();
    init_cmd
        .arg("--config")
        .arg(&config_path)
        .arg("init")
        .assert()
        .success();

    // List (should succeed even if empty)
    let mut cmd = Command::cargo_bin("smirrors").unwrap();
    cmd.arg("--config")
        .arg(&config_path)
        .arg("list")
        .assert()
        .success();
}

#[test]
fn test_invalid_command() {
    let mut cmd = Command::cargo_bin("smirrors").unwrap();
    cmd.arg("invalid-command")
        .assert()
        .failure();
}

#[test]
fn test_quiet_and_verbose_conflict() {
    let mut cmd = Command::cargo_bin("smirrors").unwrap();
    cmd.arg("--verbose")
        .arg("--quiet")
        .arg("--help")
        .assert()
        .failure();
}