use assert_cmd::Command as AssertCommand;
use predicates::prelude::*;
use std::fs;
use crate::common::test_utils::{
assert_file_contains, assert_file_exists, create_git_repo, setup_test_env,
};
#[test]
fn test_gitignore_add_rust() {
let temp_dir = setup_test_env();
let temp_path = temp_dir.path().to_path_buf();
create_git_repo(&temp_path);
let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
cmd.current_dir(&temp_path);
cmd.args(&["add", "gitignore", "rust"])
.assert()
.success()
.stdout(
predicate::str::contains("Added gitignore templates").or(predicate::str::contains("✓")),
);
assert_file_exists(&temp_path.join(".gitignore"));
assert_file_contains(&temp_path.join(".gitignore"), "Rust");
}
#[test]
fn test_gitignore_add_rust_gitignore() {
let temp_dir = setup_test_env();
let temp_path = temp_dir.path().to_path_buf();
create_git_repo(&temp_path);
let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
cmd.current_dir(&temp_path);
cmd.args(&["add", "gitignore", "rust.gitignore"])
.assert()
.success()
.stdout(
predicate::str::contains("Added gitignore templates").or(predicate::str::contains("✓")),
);
assert_file_exists(&temp_path.join(".gitignore"));
assert_file_contains(&temp_path.join(".gitignore"), "Rust");
}
#[test]
fn test_gitignore_add_multiple() {
let temp_dir = setup_test_env();
let temp_path = temp_dir.path().to_path_buf();
create_git_repo(&temp_path);
let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
cmd.current_dir(&temp_path);
cmd.args(&["add", "gitignore", "rust", "python"])
.assert()
.success()
.stdout(
predicate::str::contains("Added gitignore templates").or(predicate::str::contains("✓")),
);
let content = fs::read_to_string(temp_path.join(".gitignore")).unwrap();
assert!(content.contains("Rust"));
assert!(content.contains("Python"));
}
#[test]
fn test_gitignore_add_with_dir() {
let temp_dir = setup_test_env();
let temp_path = temp_dir.path().to_path_buf();
let target_dir = temp_path.join("custom_dir");
fs::create_dir_all(&target_dir).unwrap();
let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
cmd.args(&[
"add",
"gitignore",
"rust",
"--dir",
target_dir.to_str().unwrap(),
])
.assert()
.success()
.stdout(
predicate::str::contains("Added gitignore templates").or(predicate::str::contains("✓")),
);
assert_file_exists(&target_dir.join(".gitignore"));
assert_file_contains(&target_dir.join(".gitignore"), "Rust");
}
#[test]
fn test_gitignore_add_append() {
let temp_dir = setup_test_env();
let temp_path = temp_dir.path().to_path_buf();
create_git_repo(&temp_path);
let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
cmd.current_dir(&temp_path);
cmd.args(&["add", "gitignore", "rust"])
.assert()
.success()
.stdout(
predicate::str::contains("Added gitignore templates").or(predicate::str::contains("✓")),
);
assert_file_exists(&temp_path.join(".gitignore"));
assert_file_contains(&temp_path.join(".gitignore"), "Rust");
let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
cmd.current_dir(&temp_path);
cmd.args(&["add", "gitignore", "python", "--append"])
.assert()
.success()
.stdout(
predicate::str::contains("Added gitignore templates: rust")
.or(predicate::str::contains("✓")),
);
let content = fs::read_to_string(temp_path.join(".gitignore")).unwrap();
assert!(content.contains("Rust"));
assert!(content.contains("Python"));
}
#[test]
fn test_gitignore_add_force_overwrite() {
let temp_dir = setup_test_env();
let temp_path = temp_dir.path().to_path_buf();
create_git_repo(&temp_path);
fs::write(temp_path.join(".gitignore"), "existing content").unwrap();
let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
cmd.current_dir(&temp_path);
cmd.args(&["add", "gitignore", "rust"])
.assert()
.failure()
.stderr(predicate::str::contains("already exists"));
let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
cmd.current_dir(&temp_path);
cmd.args(&["add", "gitignore", "rust", "--force"])
.assert()
.success();
assert_file_contains(&temp_path.join(".gitignore"), "Rust");
let content = fs::read_to_string(temp_path.join(".gitignore")).unwrap();
assert!(!content.contains("existing content"));
}
#[test]
#[ignore] fn test_gitignore_add_all() {
let temp_dir = setup_test_env();
let temp_path = temp_dir.path().to_path_buf();
create_git_repo(&temp_path);
let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
cmd.current_dir(&temp_path);
cmd.args(&["add", "gitignore", "--all"])
.assert()
.success()
.stdout(
predicate::str::contains("Downloaded and merged all gitignore templates")
.or(predicate::str::contains("✓")),
);
assert_file_exists(&temp_path.join(".gitignore"));
let content = fs::read_to_string(temp_path.join(".gitignore")).unwrap();
assert!(content.contains(".gitignore"));
}
#[test]
fn test_gitignore_add_invalid_template() {
let temp_dir = setup_test_env();
let temp_path = temp_dir.path().to_path_buf();
create_git_repo(&temp_path);
let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
cmd.current_dir(&temp_path);
cmd.args(&["add", "gitignore", "not-a-template"])
.assert()
.failure()
.stderr(predicate::str::contains("not found").or(predicate::str::contains("Unknown")));
}
#[test]
fn test_gitignore_add_no_template() {
let temp_dir = setup_test_env();
let temp_path = temp_dir.path().to_path_buf();
create_git_repo(&temp_path);
let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
cmd.current_dir(&temp_path);
cmd.args(&["add", "gitignore", "not-a-template"])
.assert()
.failure()
.stderr(predicate::str::contains("not found").or(predicate::str::contains("Unknown")));
}
#[test]
fn test_gitignore_add_update_cache() {
let temp_dir = setup_test_env();
let temp_path = temp_dir.path().to_path_buf();
create_git_repo(&temp_path);
let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
cmd.current_dir(&temp_path);
cmd.args(&["add", "gitignore", "rust", "--update-cache"])
.assert()
.success()
.stdout(
predicate::str::contains("Added gitignore templates").or(predicate::str::contains("✓")),
);
}
#[test]
fn test_gitignore_add_valid_and_invalid_template() {
let temp_dir = setup_test_env();
let temp_path = temp_dir.path().to_path_buf();
create_git_repo(&temp_path);
let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
cmd.current_dir(&temp_path);
cmd.args(&["add", "gitignore", "rust", "not-a-template"])
.assert()
.success()
.stdout(
predicate::str::contains("Added gitignore templates").or(predicate::str::contains("✓")),
)
.stderr(predicate::str::contains("not found").or(predicate::str::contains("Unknown")));
assert_file_exists(&temp_path.join(".gitignore"));
assert_file_contains(&temp_path.join(".gitignore"), "Rust");
let content = fs::read_to_string(temp_path.join(".gitignore")).unwrap();
assert!(!content.contains("not-a-template"));
}
#[test]
fn test_gitignore_add_default_with_output() {
let temp_dir = setup_test_env();
let temp_path = temp_dir.path().to_path_buf();
create_git_repo(&temp_path);
let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
cmd.current_dir(&temp_path);
cmd.args(&["add", "gitignore", "rust", "-o", ".gitignore"])
.assert()
.success()
.stdout(
predicate::str::contains("Added gitignore templates").or(predicate::str::contains("✓")),
);
assert_file_exists(&temp_path.join(".gitignore"));
assert_file_contains(&temp_path.join(".gitignore"), "Rust");
}
#[test]
fn test_gitignore_add_multiple_templates_uneven_output_files() {
let temp_dir = setup_test_env();
let temp_path = temp_dir.path().to_path_buf();
create_git_repo(&temp_path);
let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
cmd.current_dir(&temp_path);
cmd.args(&[
"add",
"gitignore",
"python",
"rust",
"-o",
"Python.gitignore",
"Rust.gitignore",
"Ada.gitignore",
])
.assert()
.failure()
.stderr(predicate::str::contains(
"Number of output files must be either 1 or match the number of templates when not using --use-remote-name",
));
}
#[test]
fn test_gitignore_list_default() {
let temp_dir = setup_test_env();
let temp_path = temp_dir.path().to_path_buf();
let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
cmd.current_dir(&temp_path);
cmd.args(&["list", "gitignores"])
.assert()
.success()
.stdout(predicate::str::contains("Available gitignore templates"))
.stdout(predicate::str::contains("POPULAR"))
.stdout(predicate::str::contains("GLOBAL"))
.stdout(predicate::str::contains("COMMUNITY"));
}
#[test]
fn test_gitignore_list_popular() {
let temp_dir = setup_test_env();
let temp_path = temp_dir.path().to_path_buf();
let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
cmd.current_dir(&temp_path);
cmd.args(&["list", "gitignores", "--popular"])
.assert()
.success()
.stdout(predicate::str::contains("POPULAR"));
}
#[test]
fn test_gitignore_list_global() {
let temp_dir = setup_test_env();
let temp_path = temp_dir.path().to_path_buf();
let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
cmd.current_dir(&temp_path);
cmd.args(&["list", "gitignores", "--global"])
.assert()
.success()
.stdout(predicate::str::contains("global"));
}
#[test]
fn test_gitignore_list_community() {
let temp_dir = setup_test_env();
let temp_path = temp_dir.path().to_path_buf();
let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
cmd.current_dir(&temp_path);
cmd.args(&["list", "gitignores", "--community"])
.assert()
.success()
.stdout(predicate::str::contains("community"));
}
#[test]
fn test_gitignore_list_update_cache() {
let temp_dir = setup_test_env();
let temp_path = temp_dir.path().to_path_buf();
let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
cmd.current_dir(&temp_path);
cmd.args(&["list", "gitignores", "--update-cache"])
.assert()
.success()
.stdout(predicate::str::contains("Available gitignore templates"));
}
#[test]
fn test_gitignore_preview_single_template() {
let temp_dir = setup_test_env();
let temp_path = temp_dir.path().to_path_buf();
let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
cmd.current_dir(&temp_path);
cmd.args(&["preview", "gitignore", "rust"])
.assert()
.success()
.stdout(predicate::str::contains("Rust"));
}
#[test]
fn test_gitignore_preview_single_template_update_cache() {
let temp_dir = setup_test_env();
let temp_path = temp_dir.path().to_path_buf();
let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
cmd.current_dir(&temp_path);
cmd.args(&["preview", "gitignore", "rust", "--update-cache"])
.assert()
.success()
.stdout(predicate::str::contains("Rust"));
}
#[test]
fn test_gitignore_preview_multiple_templates() {
let temp_dir = setup_test_env();
let temp_path = temp_dir.path().to_path_buf();
let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
cmd.current_dir(&temp_path);
cmd.args(&["preview", "gitignore", "rust", "python"])
.assert()
.success()
.stdout(predicate::str::contains("Rust"))
.stdout(predicate::str::contains("Python"));
}
#[test]
fn test_gitignore_preview_update_cache() {
let temp_dir = setup_test_env();
let temp_path = temp_dir.path().to_path_buf();
let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
cmd.current_dir(&temp_path);
cmd.args(&["preview", "gitignore", "rust", "--update-cache"])
.assert()
.success()
.stdout(predicate::str::contains("Rust"));
}
#[test]
fn test_gitignore_preview_invalid_template() {
let temp_dir = setup_test_env();
let temp_path = temp_dir.path().to_path_buf();
let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
cmd.current_dir(&temp_path);
cmd.args(&["preview", "gitignore", "not-a-template"])
.assert()
.failure()
.stderr(predicate::str::contains("not found").or(predicate::str::contains("Unknown")));
}
#[test]
fn test_gitignore_preview_no_template() {
let temp_dir = setup_test_env();
let temp_path = temp_dir.path().to_path_buf();
let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
cmd.current_dir(&temp_path);
cmd.args(&["preview", "gitignore"])
.assert()
.failure()
.stderr(predicate::str::contains("No gitignore template specified"));
}
#[test]
fn test_gitignore_help_command() {
let temp_dir = setup_test_env();
let temp_path = temp_dir.path().to_path_buf();
let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
cmd.current_dir(&temp_path);
cmd.args(&["add", "gitignore", "--help"])
.assert()
.success()
.stdout(predicate::str::contains("Add gitignore templates"))
.stdout(predicate::str::contains("Usage: gitcraft add gitignore"))
.stdout(predicate::str::contains("--dir"))
.stdout(predicate::str::contains("--force"))
.stdout(predicate::str::contains("--update-cache"))
.stdout(predicate::str::contains("--output").or(predicate::str::contains("-o")));
let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
cmd.current_dir(&temp_path);
cmd.args(&["list", "gitignore", "--help"])
.assert()
.success()
.stdout(predicate::str::contains("List available gitignore templates"))
.stdout(predicate::str::contains("Usage: gitcraft list gitignore"))
.stdout(predicate::str::contains("-p").or(predicate::str::contains("--popular")))
.stdout(predicate::str::contains("--update-cache"));
let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
cmd.current_dir(&temp_path);
cmd.args(&["preview", "gitignore", "--help"])
.assert()
.success()
.stdout(predicate::str::contains("Preview a gitignore template"))
.stdout(predicate::str::contains("Usage: gitcraft preview gitignore"))
.stdout(predicate::str::contains("--update-cache"));
}