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_issue_add_bug() {
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", "issue", "bug"])
.assert()
.success()
.stdout(predicate::str::contains("Added issue template").or(predicate::str::contains("✓")));
assert_file_exists(&temp_path.join(".github/ISSUE_TEMPLATE/bug.yml"));
assert_file_contains(
&temp_path.join(".github/ISSUE_TEMPLATE/bug.yml"),
"Bug Report",
);
}
#[test]
fn test_issue_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", "issue", "bug", "feature"])
.assert()
.success()
.stdout(predicate::str::contains("Added issue template").or(predicate::str::contains("✓")));
assert_file_exists(&temp_path.join(".github/ISSUE_TEMPLATE/bug.yml"));
assert_file_exists(&temp_path.join(".github/ISSUE_TEMPLATE/feature.yml"));
}
#[test]
fn test_issue_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", "issue", "bug", "--dir", target_dir.to_str().unwrap()])
.assert()
.success()
.stdout(predicate::str::contains("Added issue template").or(predicate::str::contains("✓")));
assert_file_exists(&target_dir.join("bug.yml"));
assert_file_contains(&target_dir.join("bug.yml"), "Bug Report");
}
#[test]
fn test_issue_add_force_overwrite() {
let temp_dir = setup_test_env();
let temp_path = temp_dir.path().to_path_buf();
create_git_repo(&temp_path);
let issue_path = temp_path.join(".github/ISSUE_TEMPLATE/bug.yml");
fs::create_dir_all(issue_path.parent().unwrap()).unwrap();
fs::write(&issue_path, "existing content").unwrap();
let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
cmd.current_dir(&temp_path);
cmd.args(&["add", "issue", "bug"])
.assert()
.failure()
.stderr(predicate::str::contains("already exists"));
let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
cmd.current_dir(&temp_path);
cmd.args(&["add", "issue", "bug", "--force"])
.assert()
.success();
assert_file_contains(&issue_path, "Bug Report");
}
#[test]
fn test_issue_add_invalid_type() {
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", "issue", "invalid-template"])
.assert()
.failure()
.stderr(
predicate::str::contains("Request failed").or(predicate::str::contains("not found")),
);
}
#[test]
fn test_issue_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", "issue"])
.assert()
.failure()
.stderr(predicate::str::contains("No issue template specified"));
}
#[test]
fn test_issue_add_unknown_argument() {
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", "issue", "--unknown"])
.assert()
.failure()
.stderr(predicate::str::contains(
"unexpected argument '--unknown' found",
));
}
#[test]
fn test_issue_add_valid_and_invalid_templates() {
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", "issue", "bug", "not-a-template"])
.assert()
.failure()
.stderr(
predicate::str::contains("Request failed")
.or(predicate::str::contains("not found"))
.or(predicate::str::contains("not-a-template")),
);
assert_file_exists(&temp_path.join(".github/ISSUE_TEMPLATE/bug.yml"));
assert_file_contains(
&temp_path.join(".github/ISSUE_TEMPLATE/bug.yml"),
"Bug Report",
);
}
#[test]
fn test_issue_add_default_with_output_without_ext() {
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", "issue", "feature", "-o", "feat"])
.assert()
.success()
.stdout(predicate::str::contains("Added issue template").or(predicate::str::contains("✓")));
assert_file_exists(&temp_path.join(".github/ISSUE_TEMPLATE/feat.yml"));
assert_file_contains(
&temp_path.join(".github/ISSUE_TEMPLATE/feat.yml"),
"Feature Request",
);
}
#[test]
fn test_issue_add_default_with_output_with_ext() {
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", "issue", "feature", "-o", "feat.yml"])
.assert()
.success()
.stdout(predicate::str::contains("Added issue template").or(predicate::str::contains("✓")));
assert_file_exists(&temp_path.join(".github/ISSUE_TEMPLATE/feat.yml"));
assert_file_contains(
&temp_path.join(".github/ISSUE_TEMPLATE/feat.yml"),
"Feature Request",
);
}
#[test]
fn test_issue_add_uneven_templates_and_outputs() {
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", "issue", "feature", "bug", "-o", "feat"])
.assert()
.failure()
.stderr(predicate::str::contains(
"The number of templates and output file names must match.",
));
}
#[test]
fn test_issue_list() {
let _temp_dir = setup_test_env();
let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
cmd.args(&["list", "issues"])
.assert()
.success()
.stdout(predicate::str::contains("bug"))
.stdout(predicate::str::contains("feature"));
}
#[test]
fn test_issue_preview_bug() {
let _temp_dir = setup_test_env();
let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
cmd.args(&["preview", "issue", "bug"])
.assert()
.success()
.stdout(predicate::str::contains("Bug Report"));
}
#[test]
fn test_issue_preview_multiple() {
let _temp_dir = setup_test_env();
let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
cmd.args(&["preview", "issue", "bug", "feature"])
.assert()
.success()
.stdout(predicate::str::contains("Bug Report"))
.stdout(predicate::str::contains("Feature Request"));
}
#[test]
fn test_issue_preview_invalid_id() {
let _temp_dir = setup_test_env();
let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
cmd.args(&["preview", "issue", "not-a-template"])
.assert()
.failure()
.stderr(
predicate::str::contains("Request failed").or(predicate::str::contains("not found")),
);
}
#[test]
fn test_issue_help_command() {
let _temp_dir = setup_test_env();
let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
cmd.args(&["add", "issue", "--help"])
.assert()
.success()
.stdout(predicate::str::contains("Add an issue template"))
.stdout(predicate::str::contains("Usage: gitcraft add issue-template"))
.stdout(predicate::str::contains("--dir"))
.stdout(predicate::str::contains("--force"))
.stdout(predicate::str::contains("-o, --output"));
let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
cmd.args(&["list", "issue", "--help"])
.assert()
.success()
.stdout(predicate::str::contains("List available issue templates"))
.stdout(predicate::str::contains("Usage: gitcraft list issue"));
let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
cmd.args(&["preview", "issue", "--help"])
.assert()
.success()
.stdout(predicate::str::contains("Preview an issue template"))
.stdout(predicate::str::contains("Usage: gitcraft preview issue"));
}